55
66use SoapClient ;
77use SoapFault ;
8+ use PhpMonsters \Larapay \Adapter \Zarinpal \Helper ;
89use PhpMonsters \Larapay \Adapter \Zarinpal \Exception ;
910use PhpMonsters \Log \Facades \XLog ;
1011
1415 */
1516class Zarinpal extends AdapterAbstract implements AdapterInterface
1617{
17- protected $ WSDL = 'https://www.zarinpal.com/pg/services/WebGate/wsdl ' ;
18+ protected $ paymentRequestEndPoint = "https://payment.zarinpal.com/pg/v4/payment/request.json " ;
19+ protected $ paymentVerifyEndPoint = "https://payment.zarinpal.com/pg/v4/payment/verify.json " ;
1820
19- protected $ endPoint = 'https://www.zarinpal.com/pg/StartPay/{authority} ' ;
20- protected $ zarinEndPoint = 'https://www.zarinpal.com/pg/StartPay/{authority}/ZarinGate ' ;
21+
22+ protected $ endPoint = 'https://www.zarinpal.com/pg/StartPay/{authority} ' ;
23+ protected $ zarinEndPoint = 'https://www.zarinpal.com/pg/StartPay/{authority}/ZarinGate ' ;
2124 protected $ mobileEndPoint = 'https://www.zarinpal.com/pg/StartPay/{authority}/MobileGate ' ;
2225
23- protected $ testWSDL = 'https://banktest.ir/gateway/zarinpal/ws?wsdl ' ;
24- protected $ testEndPoint = 'https://banktest.ir/gateway/zarinpal/gate/{authority} ' ;
26+ protected $ testEndPoint = 'https://sandbox.banktest.ir/zarinpal/www.zarinpal.com/pg/StartPay/{authority} ' ;
27+ protected $ testPaymentRequestEndPoint = "https://sandbox.banktest.ir/zarinpal/api.zarinpal.com/pg/v4/payment/request.json " ;
28+ protected $ testPaymentVerifyEndPoint = "https://sandbox.banktest.ir/zarinpal/api.zarinpal.com/pg/v4/payment/verify.json " ;
29+
2530
2631 public $ reverseSupport = false ;
2732
@@ -43,42 +48,39 @@ protected function requestToken(): string
4348 ]);
4449
4550 $ sendParams = [
46- 'MerchantID ' => $ this ->merchant_id ,
47- 'Amount ' => intval ($ this ->amount ),
48- 'Description ' => $ this ->description ? $ this ->description : '' ,
49- 'Email ' => $ this ->email ? $ this ->email : '' ,
50- 'Mobile ' => $ this ->mobile ? $ this ->mobile : '' ,
51- 'CallbackURL ' => $ this ->redirect_url ,
51+ 'merchant_id ' => $ this ->merchant_id ,
52+ 'amount ' => intval ($ this ->amount ),
53+ 'description ' => $ this ->description ? $ this ->description : '' ,
54+ "metadata " => [
55+ 'mobile ' => $ this ->mobile ? $ this ->mobile : '' ,
56+ 'email ' => $ this ->email ? $ this ->email : '' ,
57+ ],
58+ 'callback_url ' => $ this ->redirect_url ,
5259 ];
5360
5461 try {
55- $ soapClient = new SoapClient ($ this ->getWSDL ());
56-
5762 XLog::debug ('PaymentRequest call ' , $ sendParams );
5863
59- $ response = $ soapClient ->PaymentRequest ($ sendParams );
60-
61- XLog::info ('PaymentRequest response ' , $ this ->obj2array ($ response ));
62-
64+ $ response = Helper::post2https ($ sendParams , $ this ->getPaymentRequestEndpPoint ());
65+ $ result = json_decode ($ response );
66+ XLog::info ('reservation result ' , $ result );
6367
64- if (isset ($ response ->Status )) {
68+ if (empty ($ result ->errors )) {
69+ if ($ result ->data ->code == 100 ) {
70+ $ this ->getTransaction ()->setGatewayToken (strval ($ result ->data ->authority )); // update transaction reference id
6571
66- if ($ response ->Status == 100 ) {
67- $ this ->getTransaction ()->setGatewayToken (strval ($ response ->Authority )); // update transaction reference id
68-
69- return $ response ->Authority ;
72+ return $ result ->data ->authority ;
7073 } else {
71- throw new Exception ($ response -> Status );
74+ throw new Exception (' no error provided and not 100 ' );
7275 }
7376 } else {
74- throw new Exception ('larapay::larapay.invalid_response ' );
77+ throw new Exception ('code: ' . $ result -> errors -> code . "\n" . ' message: ' . $ result -> errors -> message );
7578 }
76- } catch (SoapFault $ e ) {
77- throw new Exception (' SoapFault: ' . $ e ->getMessage () . ' # ' . $ e -> getCode (), $ e -> getCode ());
79+ } catch (\ Exception $ e ) {
80+ throw new Exception ($ e ->getMessage ());
7881 }
7982 }
8083
81-
8284 /**
8385 * @return string
8486 * @throws Exception
@@ -89,9 +91,9 @@ protected function generateForm(): string
8991 $ authority = $ this ->requestToken ();
9092
9193 $ form = view ('larapay::zarinpal-form ' , [
92- 'endPoint ' => strtr ($ this ->getEndPoint (), ['{authority} ' => $ authority ]),
94+ 'endPoint ' => strtr ($ this ->getEndPoint (), ['{authority} ' => $ authority ]),
9395 'submitLabel ' => !empty ($ this ->submit_label ) ? $ this ->submit_label : trans ("larapay::larapay.goto_gate " ),
94- 'autoSubmit ' => boolval ($ this ->auto_submit ),
96+ 'autoSubmit ' => boolval ($ this ->auto_submit ),
9597 ]);
9698
9799 return $ form ->__toString ();
@@ -106,8 +108,8 @@ public function formParams(): array
106108 {
107109 $ authority = $ this ->requestToken ();
108110
109- return [
110- 'endPoint ' => strtr ($ this ->getEndPoint (), ['{authority} ' => $ authority ]),
111+ return [
112+ 'endPoint ' => strtr ($ this ->getEndPoint (), ['{authority} ' => $ authority ]),
111113 ];
112114 }
113115
@@ -128,38 +130,29 @@ protected function verifyTransaction(): bool
128130 ]);
129131
130132 $ sendParams = [
131- 'MerchantID ' => $ this ->merchant_id ,
132- 'Authority ' => $ this ->Authority ,
133- 'Amount ' => intval ($ this ->transaction ->amount ),
133+ 'merchant_id ' => $ this ->merchant_id ,
134+ 'authority ' => $ this ->Authority ,
135+ 'amount ' => intval ($ this ->transaction ->amount ),
134136 ];
135137
136- try {
137- $ soapClient = new SoapClient ($ this ->getWSDL ());
138-
139- XLog::debug ('PaymentVerification call ' , $ sendParams );
140-
141- $ response = $ soapClient ->PaymentVerification ($ sendParams );
138+ XLog::debug ('PaymentVerification call ' , $ sendParams );
142139
143- XLog::info ('PaymentVerification response ' , $ this ->obj2array ($ response ));
144-
145-
146- if (isset ($ response ->Status , $ response ->RefID )) {
147-
148- if ($ response ->Status == 100 ) {
149- $ this ->getTransaction ()->setVerified ();
150- $ this ->getTransaction ()->setReferenceId ((string )$ response ->RefID ); // update transaction reference id
151-
152- return true ;
153- } else {
154- throw new Exception ($ response ->Status );
155- }
140+ try {
141+ $ response = Helper::post2https ($ sendParams , $ this ->getPaymentVerifyEndpPoint ());
142+ $ result = json_decode ($ response );
143+ XLog::info ('PaymentVerification response ' , $ this ->obj2array ($ result ));
144+
145+ if ($ result ->data ->code === 100 ) {
146+ $ this ->getTransaction ()->setVerified ();
147+ $ this ->getTransaction ()->setReferenceId ((string ) $ result ->data ->ref_id ); // update transaction reference id
148+ return true ;
149+ } else if ($ result ->data ->code === 101 ) {
150+ return true ;
156151 } else {
157- throw new Exception ('larapay::larapay.invalid_response ' );
152+ throw new Exception ('code: ' . $ result -> errors -> code . "\n" . ' message: ' . $ result -> errors -> message );
158153 }
159-
160- } catch (SoapFault $ e ) {
161-
162- throw new Exception ('SoapFault: ' . $ e ->getMessage () . ' # ' . $ e ->getCode (), $ e ->getCode ());
154+ } catch (\Exception $ e ) {
155+ throw new Exception ("Payment Verify Error: " . $ e ->getMessage ());
163156 }
164157 }
165158
0 commit comments