22
33namespace ZarulIzham \DuitNowQR ;
44
5+ use Carbon \Carbon ;
6+ use Exception ;
57use Illuminate \Support \Facades \Cache ;
68use Illuminate \Support \Facades \Http ;
79use ZarulIzham \DuitNowQR \Exceptions \BadRequest ;
@@ -146,6 +148,60 @@ public function getStatus($qrString, $transactionDate)
146148 return $ response ->json ();
147149 }
148150
151+ public function getStatusByTransaction (DuitNowQRTransaction $ duitNowQRTransaction , Carbon $ transactionDate , $ transactionTimeStart , $ transactionTimeEnd )
152+ {
153+ $ token = Cache::remember ('duitnow_qr_token ' , config ('duitnowqr.token_expiry ' ), fn () => $ this ->authenticate ());
154+
155+ $ sourceReferenceNumber = $ this ->getSrcRefNo ();
156+ $ url = config ('duitnowqr.url ' )."/api/MerchantQR/v1.0/GetQTNotification/ $ sourceReferenceNumber " ;
157+
158+ $ ambankTimestamp = now ()->format ('dmYHis ' );
159+
160+ $ headers = [
161+ 'Authorization ' => 'Bearer ' .$ token ,
162+ 'Authentication ' => $ token ,
163+ 'AmBank-Timestamp ' => $ ambankTimestamp ,
164+ 'Channel-Token ' => config ('duitnowqr.channel_token ' ),
165+ 'srcRefNo ' => $ sourceReferenceNumber ,
166+ 'Content-Type ' => 'application/json ' ,
167+ 'Accept ' => 'application/json ' ,
168+ 'Channel-APIKey ' => config ('duitnowqr.api_key ' ),
169+ ];
170+
171+ try {
172+ $ transactionTimeStart = $ transactionDate ->clone ()->setTimeFromTimeString ($ transactionTimeStart );
173+ $ transactionTimeEnd = $ transactionDate ->clone ()->setTimeFromTimeString ($ transactionTimeEnd );
174+ } catch (\Throwable $ th ) {
175+ throw new Exception ('Failed to read transaction time. Please use format H:i (Eg: 23:00) ' );
176+ }
177+
178+ $ body = [
179+ 'QRString ' => $ duitNowQRTransaction ->qr_string ,
180+ 'TrxDate ' => $ transactionDate ->format ('d/m/Y ' ),
181+ 'TrxTimeStart ' => $ transactionTimeStart ->format ('H:i ' ),
182+ 'TrxTimeEnd ' => $ transactionTimeEnd ->format ('H:i ' ),
183+ ];
184+
185+ $ bodyEscaped = str_replace ('\\' , '' , json_encode ($ body ));
186+
187+ $ headers ['Ambank-Signature ' ] = $ this ->signature ('/api/MerchantQR/v1.0/GetQTNotification/ ' .$ sourceReferenceNumber , $ ambankTimestamp , $ body );
188+
189+ $ response = null ;
190+ try {
191+ $ response = Http::withHeaders ($ headers )->withOptions ([
192+ 'debug ' => false ,
193+ ])
194+ ->withBody ($ bodyEscaped , 'application/json ' )
195+ ->post ($ url );
196+ } catch (\Throwable $ th ) {
197+ //throw $th;
198+ }
199+
200+ $ this ->storeRequeryStatus ($ duitNowQRTransaction , $ sourceReferenceNumber , $ transactionTimeStart , $ transactionTimeEnd , $ response );
201+
202+ return $ response ->json ();
203+ }
204+
149205 public function signature ($ uri , $ ambankTimestamp , $ body ): string
150206 {
151207 $ header = [
@@ -167,4 +223,16 @@ public function signature($uri, $ambankTimestamp, $body): string
167223
168224 return $ base64 ;
169225 }
226+
227+ public function storeRequeryStatus ($ duitNowQRTransaction , $ sourceReferenceNumber , $ transactionTimeStart , $ transactionTimeEnd , $ response )
228+ {
229+ $ duitNowQRTransaction ->requeries ()->create ([
230+ 'source_reference_number ' => $ sourceReferenceNumber ,
231+ 'status_code ' => $ response ?->status(),
232+ 'response_payload ' => $ response ?->body(),
233+ 'transaction_date ' => $ transactionTimeStart ->format ('Y-m-d ' ),
234+ 'transaction_time_start ' => $ transactionTimeStart ->format ('H:i:s ' ),
235+ 'transaction_time_end ' => $ transactionTimeEnd ->format ('H:i:s ' ),
236+ ]);
237+ }
170238}
0 commit comments