55use GuzzleHttp \Exception \ClientException ;
66use GuzzleHttp \Exception \ServerException ;
77use Illuminate \Http \JsonResponse ;
8+ use Illuminate \Support \Facades \Storage ;
89use PhpMonsters \LaraWallet \Enums \AsanpardakhtStatusEnum ;
910use PhpMonsters \Log \Facades \XLog ;
1011
@@ -41,15 +42,15 @@ public function checkWalletBalance(): JsonResponse|array
4142 $ hostRequestSign = $ this ->signRequest ($ hostRequest );
4243 $ rawResponse = $ this ->sendInfoToAp ($ hostRequest , $ hostRequestSign , self ::POST_METHOD , $ this ->getUrl ());
4344
44- $ responseJson = json_decode ($ rawResponse ["hresp " ]);
45+ $ responseJson = json_decode ($ rawResponse ["hresp " ], false , 512 , JSON_THROW_ON_ERROR );
4546
4647 $ credit = 0 ;
4748
4849 if (property_exists ($ responseJson , 'wball ' )) {
4950 $ credit = $ responseJson ->wball / 10 ;
5051 }
5152
52- return $ this -> generalResponse (
53+ return self :: generalResponse (
5354 code: AsanpardakhtStatusEnum::SuccessResponse->value ,
5455 value: $ credit ,
5556 );
@@ -60,11 +61,7 @@ public function checkWalletBalance(): JsonResponse|array
6061
6162
6263 /**
63- * sign request
64- *
6564 * @param string $input
66- * @param bool $debug
67- *
6865 * @return string
6966 */
7067 public function signRequest (string $ input ): string
@@ -74,7 +71,7 @@ public function signRequest(string $input): string
7471 openssl_sign (
7572 $ input ,
7673 $ binary_signature ,
77- config ( ' wallet.asanpardakht.private_key ' ),
74+ Storage:: disk ( ' private ' )-> get ( ' private.pem ' ),
7875 OPENSSL_ALGO_SHA256
7976 );
8077
@@ -114,35 +111,35 @@ public function payByWallet(): JsonResponse|array
114111 if ($ responseJson ['st ' ] == AsanpardakhtStatusEnum::SuccessRequest->value ) {
115112 $ this ->getTransaction ()->setCallBackParameters ($ responseJson );
116113
117- return $ this -> generalResponse (
114+ return self :: generalResponse (
118115 code: AsanpardakhtStatusEnum::SuccessResponse->value ,
119116 );
120117 }
121118
122119 if ($ responseJson ->st == AsanpardakhtStatusEnum::AccessDeniedRequest->value || $ responseJson ->st == AsanpardakhtStatusEnum::InsufficientInventory) { // access denied : 1332, low credit : 1330
123- return $ this -> generalResponse (
120+ return self :: generalResponse (
124121 code: AsanpardakhtStatusEnum::AccessDeniedResponse->value ,
125122 value: $ responseJson ->addData ->ipgURL ,
126123 );
127- } else {
128- return $ this ->generalResponse (
129- code: AsanpardakhtStatusEnum::FailedResponse->value ,
130- value: $ responseJson ->stm ,
131- );
132124 }
125+
126+ return self ::generalResponse (
127+ code: AsanpardakhtStatusEnum::FailedResponse->value ,
128+ value: $ responseJson ->stm ,
129+ );
133130 } catch (ServerException $ exception ) {
134131 if ($ responseJson != '' ) {
135132 $ this ->reverseWalletPaymentResult ();
136133 }
137134
138135 $ errorJson = json_decode ($ exception ->getResponse ()->getBody ()->getContents ());
139136
140- return $ this -> generalExceptionResponse (
137+ return self :: generalExceptionResponse (
141138 AsanpardakhtStatusEnum::FailedResponse->value ,
142139 $ errorJson ->description ?? ''
143140 );
144141 } catch (\Exception $ exception ) {
145- return $ this -> generalExceptionResponse (
142+ return self :: generalExceptionResponse (
146143 AsanpardakhtStatusEnum::FailedResponse->value ,
147144 $ exception ->getMessage ()
148145 );
@@ -152,6 +149,7 @@ public function payByWallet(): JsonResponse|array
152149
153150 /**
154151 * @return mixed
152+ * @throws \JsonException
155153 */
156154 public function verifyWalletPaymentResult (): mixed
157155 {
@@ -180,14 +178,14 @@ public function verifyWalletPaymentResult(): mixed
180178
181179//----------------------------------successfully verified-------------------------------------
182180 if ($ responseJson ->st == AsanpardakhtStatusEnum::SuccessRequest->value or $ responseJson ->st == AsanpardakhtStatusEnum::TransactionAlreadyBeenVerified->value ) {
183- $ result = $ this -> generalResponse (
181+ $ result = self :: generalResponse (
184182 code: AsanpardakhtStatusEnum::SuccessResponse->value ,
185183 );
186184 }
187185 } catch (ServerException $ ex ) {
188186 $ errorJson = json_decode ($ ex ->getResponse ()->getBody ()->getContents ());
189187
190- $ result = $ this -> generalExceptionResponse (
188+ $ result = self :: generalExceptionResponse (
191189 AsanpardakhtStatusEnum::FailedResponse->value ,
192190 $ errorJson
193191 );
@@ -199,6 +197,7 @@ public function verifyWalletPaymentResult(): mixed
199197
200198 /**
201199 * @return mixed
200+ * @throws \JsonException
202201 */
203202 public function settleWalletPaymentResult (): mixed
204203 {
@@ -225,14 +224,14 @@ public function settleWalletPaymentResult(): mixed
225224
226225 //---------------------Successfully verified--------------------------
227226 if ($ responseJson ->st == AsanpardakhtStatusEnum::SuccessResponse->value or $ responseJson ->st == AsanpardakhtStatusEnum::TransactionAlreadyBeenSettled->value ) {
228- $ result = $ this -> generalResponse (
227+ $ result = self :: generalResponse (
229228 code: AsanpardakhtStatusEnum::SuccessResponse->value ,
230229 );
231230 }
232231 } catch (ServerException $ ex ) {
233232 $ errorJson = json_decode ($ ex ->getResponse ()->getBody ()->getContents ());
234233
235- $ result = $ this -> generalExceptionResponse (
234+ $ result = self :: generalExceptionResponse (
236235 AsanpardakhtStatusEnum::FailedResponse->value ,
237236 $ errorJson
238237 );
@@ -242,7 +241,11 @@ public function settleWalletPaymentResult(): mixed
242241 }
243242
244243
245- public function getBalanceWalletError (\Exception $ exception )
244+ /**
245+ * @param \Exception $exception
246+ * @return JsonResponse
247+ */
248+ public function getBalanceWalletError (\Exception $ exception ): JsonResponse
246249 {
247250 if (method_exists ($ exception , 'getResponse ' ) && !empty ($ exception ->getResponse ())) {
248251 $ errorJson = json_decode ($ exception ->getResponse ()->getBody ()->getContents ());
@@ -256,7 +259,7 @@ public function getBalanceWalletError(\Exception $exception)
256259
257260 XLog::emergency ('check balance failure ' .' ' .' message: ' .$ errorMsg );
258261
259- return $ this -> generalExceptionResponse (
262+ return self :: generalExceptionResponse (
260263 AsanpardakhtStatusEnum::FailedResponse->value ,
261264 'error in wallet service '
262265 );
@@ -266,19 +269,21 @@ public function getBalanceWalletError(\Exception $exception)
266269 /**
267270 * @param array $data
268271 * @return string|array|false
272+ * @throws \JsonException
269273 */
270274 public function prepareJsonString (array $ data ): string |array |false
271275 {
272- return json_encode ($ data , JSON_UNESCAPED_SLASHES );
276+ return json_encode ($ data , JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES );
273277 }
274278
275279 /**
276280 * @param $hresp
277281 * @return mixed
282+ * @throws \JsonException
278283 */
279284 public function getHresponseData ($ hresp ): mixed
280285 {
281- return json_decode ($ hresp , true );
286+ return json_decode ($ hresp , true , 512 , JSON_THROW_ON_ERROR );
282287 }
283288
284289 /**
@@ -309,7 +314,6 @@ public function reverseWalletPaymentResult(): mixed
309314
310315 try {
311316 $ rawResponse = $ this ->sendInfoToAp ($ hostRequest , $ hostRequestSign , self ::POST_METHOD , $ this ->getUrl ());
312-
313317 $ responseJson = json_decode ($ rawResponse ["hresp " ], true );
314318
315319 $ result = $ responseJson ['st ' ];
@@ -320,7 +324,7 @@ public function reverseWalletPaymentResult(): mixed
320324 $ this ->log ('successfully reversed ' , [], 'info ' );
321325 $ this ->getTransaction ()->setCallBackParameters ($ responseJson );
322326
323- $ result = $ this -> generalResponse (
327+ $ result = self :: generalResponse (
324328 code: AsanpardakhtStatusEnum::SuccessResponse->value ,
325329 );
326330 }
0 commit comments