|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Shetabit\Multipay\Drivers\SnappPay; |
|
5
|
|
|
|
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
use GuzzleHttp\RequestOptions; |
|
8
|
|
|
use Shetabit\Multipay\Abstracts\Driver; |
|
9
|
|
|
use Shetabit\Multipay\Contracts\ReceiptInterface; |
|
10
|
|
|
use Shetabit\Multipay\Exceptions\InvalidPaymentException; |
|
11
|
|
|
use Shetabit\Multipay\Exceptions\PurchaseFailedException; |
|
12
|
|
|
use Shetabit\Multipay\Invoice; |
|
13
|
|
|
use Shetabit\Multipay\Receipt; |
|
14
|
|
|
use Shetabit\Multipay\RedirectionForm; |
|
15
|
|
|
|
|
16
|
|
|
class SnappPay extends Driver |
|
17
|
|
|
{ |
|
18
|
|
|
const VERSION = '1.8'; |
|
19
|
|
|
const RELEASE_DATE = '2023-01-08'; |
|
20
|
|
|
|
|
21
|
|
|
const OAUTH_URL = '/api/online/v1/oauth/token'; |
|
22
|
|
|
const ELIGIBLE_URL = '/api/online/offer/v1/eligible'; |
|
23
|
|
|
const TOKEN_URL = '/api/online/payment/v1/token'; |
|
24
|
|
|
const VERIFY_URL = '/api/online/payment/v1/verify'; |
|
25
|
|
|
const SETTLE_URL = '/api/online/payment/v1/settle'; |
|
26
|
|
|
const REVERT_URL = '/api/online/payment/v1/revert'; |
|
27
|
|
|
const STATUS_URL = '/api/online/payment/v1/status'; |
|
28
|
|
|
const CANCEL_URL = '/api/online/payment/v1/cancel'; |
|
29
|
|
|
const UPDATE_URL = '/api/online/payment/v1/update'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* SnappPay Client. |
|
33
|
|
|
* |
|
34
|
|
|
* @var Client |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $client; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Invoice |
|
40
|
|
|
* |
|
41
|
|
|
* @var Invoice |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $invoice; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Driver settings |
|
47
|
|
|
* |
|
48
|
|
|
* @var object |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $settings; |
|
51
|
|
|
/** |
|
52
|
|
|
* SnappPay Oauth Data |
|
53
|
|
|
* |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $oauthToken; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* SnappPay payment url |
|
60
|
|
|
* |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $paymentUrl; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* SnappPay constructor. |
|
67
|
|
|
* Construct the class with the relevant settings. |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __construct(Invoice $invoice, $settings) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->invoice($invoice); |
|
72
|
|
|
$this->settings = (object) $settings; |
|
73
|
|
|
$this->client = new Client(); |
|
74
|
|
|
$this->oauthToken = $this->oauth(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @throws PurchaseFailedException |
|
79
|
|
|
*/ |
|
80
|
|
|
public function purchase(): string |
|
81
|
|
|
{ |
|
82
|
|
|
$phone = $this->invoice->getDetail('phone') |
|
83
|
|
|
?? $this->invoice->getDetail('cellphone') |
|
84
|
|
|
?? $this->invoice->getDetail('mobile'); |
|
85
|
|
|
|
|
86
|
|
|
// convert to format +98 901 XXX XXXX |
|
87
|
|
|
$phone = preg_replace('/^0/', '+98', $phone); |
|
88
|
|
|
|
|
89
|
|
|
$data = [ |
|
90
|
|
|
'amount' => $this->normalizerAmount($this->invoice->getAmount()), |
|
|
|
|
|
|
91
|
|
|
'mobile' => $phone, |
|
92
|
|
|
'paymentMethodTypeDto' => 'INSTALLMENT', |
|
93
|
|
|
'transactionId' => $this->invoice->getUuid(), |
|
94
|
|
|
'returnURL' => $this->settings->callbackUrl, |
|
95
|
|
|
]; |
|
96
|
|
|
|
|
97
|
|
|
if (!is_null($discountAmount = $this->invoice->getDetail('discountAmount'))) { |
|
98
|
|
|
$data['discountAmount'] = $this->normalizerAmount($discountAmount); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if (!is_null($externalSourceAmount = $this->invoice->getDetail('externalSourceAmount'))) { |
|
102
|
|
|
$data['externalSourceAmount'] = $externalSourceAmount; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if (is_null($this->invoice->getDetail('cartList'))) { |
|
106
|
|
|
throw new PurchaseFailedException('"cartList" is required for this driver'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$data['cartList'] = $this->invoice->getDetail('cartList'); |
|
110
|
|
|
|
|
111
|
|
|
$this->normalizerCartList($data); |
|
112
|
|
|
|
|
113
|
|
|
$response = $this |
|
114
|
|
|
->client |
|
115
|
|
|
->post( |
|
116
|
|
|
$this->settings->apiPaymentUrl.self::TOKEN_URL, |
|
117
|
|
|
[ |
|
118
|
|
|
RequestOptions::BODY => json_encode($data), |
|
119
|
|
|
RequestOptions::HEADERS => [ |
|
120
|
|
|
'Content-Type' => 'application/json', |
|
121
|
|
|
'Authorization' => 'Bearer '.$this->oauthToken, |
|
122
|
|
|
], |
|
123
|
|
|
RequestOptions::HTTP_ERRORS => false, |
|
124
|
|
|
] |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
|
|
$body = json_decode($response->getBody()->getContents(), true); |
|
128
|
|
|
|
|
129
|
|
|
if ($response->getStatusCode() != 200 || $body['successful'] === false) { |
|
130
|
|
|
// error has happened |
|
131
|
|
|
$message = $body['errorData']['message'] ?? 'خطا در هنگام درخواست برای پرداخت رخ داده است.'; |
|
132
|
|
|
throw new PurchaseFailedException($message); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$this->invoice->transactionId($body['response']['paymentToken']); |
|
136
|
|
|
$this->setPaymentUrl($body['response']['paymentPageUrl']); |
|
137
|
|
|
|
|
138
|
|
|
// return the transaction's id |
|
139
|
|
|
return $this->invoice->getTransactionId(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function pay(): RedirectionForm |
|
143
|
|
|
{ |
|
144
|
|
|
parse_str(parse_url($this->getPaymentUrl(), PHP_URL_QUERY), $formData); |
|
145
|
|
|
|
|
146
|
|
|
return $this->redirectWithForm($this->getPaymentUrl(), $formData, 'GET'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @throws InvalidPaymentException |
|
151
|
|
|
*/ |
|
152
|
|
|
public function verify(): ReceiptInterface |
|
153
|
|
|
{ |
|
154
|
|
|
$data = [ |
|
155
|
|
|
'paymentToken' => $this->invoice->getTransactionId(), |
|
156
|
|
|
]; |
|
157
|
|
|
|
|
158
|
|
|
$response = $this |
|
159
|
|
|
->client |
|
160
|
|
|
->post( |
|
161
|
|
|
$this->settings->apiPaymentUrl.self::VERIFY_URL, |
|
162
|
|
|
[ |
|
163
|
|
|
RequestOptions::BODY => json_encode($data), |
|
164
|
|
|
RequestOptions::HEADERS => [ |
|
165
|
|
|
'Authorization' => 'Bearer '.$this->oauthToken, |
|
166
|
|
|
], |
|
167
|
|
|
RequestOptions::HTTP_ERRORS => false, |
|
168
|
|
|
] |
|
169
|
|
|
); |
|
170
|
|
|
|
|
171
|
|
|
$body = json_decode($response->getBody()->getContents(), true); |
|
172
|
|
|
|
|
173
|
|
|
if ($response->getStatusCode() != 200 || $body['successful'] === false) { |
|
174
|
|
|
// error has happened |
|
175
|
|
|
$message = $body['errorData']['message'] ?? 'خطا در هنگام تایید تراکنش'; |
|
176
|
|
|
throw new PurchaseFailedException($message); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return (new Receipt('digipay', $body['response']['transactionId']))->detail($body['response']); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @throws PurchaseFailedException |
|
184
|
|
|
*/ |
|
185
|
|
|
protected function oauth() |
|
186
|
|
|
{ |
|
187
|
|
|
$response = $this |
|
188
|
|
|
->client |
|
189
|
|
|
->post( |
|
190
|
|
|
$this->settings->apiPaymentUrl.self::OAUTH_URL, |
|
191
|
|
|
[ |
|
192
|
|
|
RequestOptions::HEADERS => [ |
|
193
|
|
|
'Authorization' => 'Basic '.base64_encode("{$this->settings->client_id}:{$this->settings->client_secret}"), |
|
194
|
|
|
], |
|
195
|
|
|
RequestOptions::FORM_PARAMS => [ |
|
196
|
|
|
'grant_type' => 'password', |
|
197
|
|
|
'scope' => 'online-merchant', |
|
198
|
|
|
'username' => $this->settings->username, |
|
199
|
|
|
'password' => $this->settings->password, |
|
200
|
|
|
], |
|
201
|
|
|
RequestOptions::HTTP_ERRORS => false, |
|
202
|
|
|
] |
|
203
|
|
|
); |
|
204
|
|
|
|
|
205
|
|
|
if ($response->getStatusCode() != 200) { |
|
206
|
|
|
throw new PurchaseFailedException('خطا در هنگام احراز هویت.'); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
$body = json_decode($response->getBody()->getContents(), true); |
|
210
|
|
|
|
|
211
|
|
|
return $body['access_token']; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @throws PurchaseFailedException |
|
216
|
|
|
*/ |
|
217
|
|
|
public function eligible() |
|
218
|
|
|
{ |
|
219
|
|
|
if (is_null($amount = $this->invoice->getAmount())) { |
|
|
|
|
|
|
220
|
|
|
throw new PurchaseFailedException('"amount" is required for this method.'); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
$response = $this->client->get($this->settings->apiPaymentUrl.self::ELIGIBLE_URL, [ |
|
224
|
|
|
RequestOptions::HEADERS => [ |
|
225
|
|
|
'Authorization' => 'Bearer '.$this->oauthToken, |
|
226
|
|
|
], |
|
227
|
|
|
RequestOptions::QUERY => [ |
|
228
|
|
|
'amount' => $this->normalizerAmount($amount), |
|
|
|
|
|
|
229
|
|
|
], |
|
230
|
|
|
]); |
|
231
|
|
|
|
|
232
|
|
|
$body = json_decode($response->getBody()->getContents(), true); |
|
233
|
|
|
|
|
234
|
|
|
if ($response->getStatusCode() != 200) { |
|
235
|
|
|
throw new InvalidPaymentException('', (int) $response->getStatusCode()); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
return $body; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
private function normalizerAmount(int $amount): int |
|
242
|
|
|
{ |
|
243
|
|
|
return $amount * ($this->settings->currency == 'T' ? 10 : 1); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
private function normalizerCartList(array &$data): void |
|
247
|
|
|
{ |
|
248
|
|
|
if (isset($data['cartList']['shippingAmount'])) { |
|
249
|
|
|
$data['cartList'] = [$data['cartList']]; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
foreach ($data['cartList'] as &$item) { |
|
253
|
|
|
if (isset($item['shippingAmount'])) { |
|
254
|
|
|
$item['shippingAmount'] = $this->normalizerAmount($item['shippingAmount']); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
if (isset($item['taxAmount'])) { |
|
258
|
|
|
$item['taxAmount'] = $this->normalizerAmount($item['taxAmount']); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
if (isset($item['totalAmount'])) { |
|
262
|
|
|
$item['totalAmount'] = $this->normalizerAmount($item['totalAmount']); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
foreach ($item['cartItems'] as &$cartItem) { |
|
266
|
|
|
$cartItem['amount'] = $this->normalizerAmount($cartItem['amount']); |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
public function settle(): array |
|
273
|
|
|
{ |
|
274
|
|
|
$data = [ |
|
275
|
|
|
'paymentToken' => $this->invoice->getTransactionId(), |
|
276
|
|
|
]; |
|
277
|
|
|
|
|
278
|
|
|
$response = $this |
|
279
|
|
|
->client |
|
280
|
|
|
->post( |
|
281
|
|
|
$this->settings->apiPaymentUrl.self::SETTLE_URL, |
|
282
|
|
|
[ |
|
283
|
|
|
RequestOptions::BODY => json_encode($data), |
|
284
|
|
|
RequestOptions::HEADERS => [ |
|
285
|
|
|
'Authorization' => 'Bearer '.$this->oauthToken, |
|
286
|
|
|
], |
|
287
|
|
|
RequestOptions::HTTP_ERRORS => false, |
|
288
|
|
|
] |
|
289
|
|
|
); |
|
290
|
|
|
|
|
291
|
|
|
$body = json_decode($response->getBody()->getContents(), true); |
|
292
|
|
|
|
|
293
|
|
|
if ($response->getStatusCode() != 200 || $body['successful'] === false) { |
|
294
|
|
|
// error has happened |
|
295
|
|
|
$message = $body['errorData']['message'] ?? 'خطا در Settle تراکنش'; |
|
296
|
|
|
throw new PurchaseFailedException($message); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
return $body['response']; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
public function revert() |
|
303
|
|
|
{ |
|
304
|
|
|
$data = [ |
|
305
|
|
|
'paymentToken' => $this->invoice->getTransactionId(), |
|
306
|
|
|
]; |
|
307
|
|
|
|
|
308
|
|
|
$response = $this |
|
309
|
|
|
->client |
|
310
|
|
|
->post( |
|
311
|
|
|
$this->settings->apiPaymentUrl.self::REVERT_URL, |
|
312
|
|
|
[ |
|
313
|
|
|
RequestOptions::BODY => json_encode($data), |
|
314
|
|
|
RequestOptions::HEADERS => [ |
|
315
|
|
|
'Authorization' => 'Bearer '.$this->oauthToken, |
|
316
|
|
|
], |
|
317
|
|
|
RequestOptions::HTTP_ERRORS => false, |
|
318
|
|
|
] |
|
319
|
|
|
); |
|
320
|
|
|
|
|
321
|
|
|
$body = json_decode($response->getBody()->getContents(), true); |
|
322
|
|
|
|
|
323
|
|
|
if ($response->getStatusCode() != 200 || $body['successful'] === false) { |
|
324
|
|
|
// error has happened |
|
325
|
|
|
$message = $body['errorData']['message'] ?? 'خطا در Revert تراکنش'; |
|
326
|
|
|
throw new PurchaseFailedException($message); |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
return $body['response']; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
public function status() |
|
333
|
|
|
{ |
|
334
|
|
|
$data = [ |
|
335
|
|
|
'paymentToken' => $this->invoice->getTransactionId(), |
|
336
|
|
|
]; |
|
337
|
|
|
|
|
338
|
|
|
$response = $this |
|
339
|
|
|
->client |
|
340
|
|
|
->get( |
|
341
|
|
|
$this->settings->apiPaymentUrl.self::STATUS_URL, |
|
342
|
|
|
[ |
|
343
|
|
|
RequestOptions::BODY => json_encode($data), |
|
344
|
|
|
RequestOptions::HEADERS => [ |
|
345
|
|
|
'Authorization' => 'Bearer '.$this->oauthToken, |
|
346
|
|
|
], |
|
347
|
|
|
RequestOptions::HTTP_ERRORS => false, |
|
348
|
|
|
] |
|
349
|
|
|
); |
|
350
|
|
|
|
|
351
|
|
|
$body = json_decode($response->getBody()->getContents(), true); |
|
352
|
|
|
|
|
353
|
|
|
if ($response->getStatusCode() != 200 || $body['successful'] === false) { |
|
354
|
|
|
// error has happened |
|
355
|
|
|
$message = $body['errorData']['message'] ?? 'خطا در status تراکنش'; |
|
356
|
|
|
throw new PurchaseFailedException($message); |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
return $body['response']; |
|
360
|
|
|
|
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
public function cancel() |
|
364
|
|
|
{ |
|
365
|
|
|
$data = [ |
|
366
|
|
|
'paymentToken' => $this->invoice->getTransactionId(), |
|
367
|
|
|
]; |
|
368
|
|
|
|
|
369
|
|
|
$response = $this |
|
370
|
|
|
->client |
|
371
|
|
|
->post( |
|
372
|
|
|
$this->settings->apiPaymentUrl.self::CANCEL_URL, |
|
373
|
|
|
[ |
|
374
|
|
|
RequestOptions::BODY => json_encode($data), |
|
375
|
|
|
RequestOptions::HEADERS => [ |
|
376
|
|
|
'Authorization' => 'Bearer '.$this->oauthToken, |
|
377
|
|
|
], |
|
378
|
|
|
RequestOptions::HTTP_ERRORS => false, |
|
379
|
|
|
] |
|
380
|
|
|
); |
|
381
|
|
|
|
|
382
|
|
|
$body = json_decode($response->getBody()->getContents(), true); |
|
383
|
|
|
|
|
384
|
|
|
if ($response->getStatusCode() != 200 || $body['successful'] === false) { |
|
385
|
|
|
// error has happened |
|
386
|
|
|
$message = $body['errorData']['message'] ?? 'خطا در Cancel تراکنش'; |
|
387
|
|
|
throw new PurchaseFailedException($message); |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
return $body['response']; |
|
391
|
|
|
|
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
public function update() |
|
395
|
|
|
{ |
|
396
|
|
|
$data = [ |
|
397
|
|
|
'amount' => $this->normalizerAmount($this->invoice->getAmount()), |
|
|
|
|
|
|
398
|
|
|
'paymentMethodTypeDto' => 'INSTALLMENT', |
|
399
|
|
|
'paymentToken' => $this->invoice->getTransactionId(), |
|
400
|
|
|
]; |
|
401
|
|
|
|
|
402
|
|
|
if (!is_null($discountAmount = $this->invoice->getDetail('discountAmount'))) { |
|
403
|
|
|
$data['discountAmount'] = $this->normalizerAmount($discountAmount); |
|
|
|
|
|
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
if (!is_null($externalSourceAmount = $this->invoice->getDetail('externalSourceAmount'))) { |
|
407
|
|
|
$data['externalSourceAmount'] = $externalSourceAmount; |
|
408
|
|
|
} |
|
409
|
|
|
|
|
410
|
|
|
if (is_null($this->invoice->getDetail('cartList'))) { |
|
411
|
|
|
throw new PurchaseFailedException('"cartList" is required for this driver'); |
|
412
|
|
|
} |
|
413
|
|
|
|
|
414
|
|
|
$data['cartList'] = $this->invoice->getDetail('cartList'); |
|
415
|
|
|
|
|
416
|
|
|
$this->normalizerCartList($data); |
|
417
|
|
|
|
|
418
|
|
|
$response = $this |
|
419
|
|
|
->client |
|
420
|
|
|
->post( |
|
421
|
|
|
$this->settings->apiPaymentUrl.self::TOKEN_URL, |
|
422
|
|
|
[ |
|
423
|
|
|
RequestOptions::BODY => json_encode($data), |
|
424
|
|
|
RequestOptions::HEADERS => [ |
|
425
|
|
|
'Content-Type' => 'application/json', |
|
426
|
|
|
'Authorization' => 'Bearer '.$this->oauthToken, |
|
427
|
|
|
], |
|
428
|
|
|
RequestOptions::HTTP_ERRORS => false, |
|
429
|
|
|
] |
|
430
|
|
|
); |
|
431
|
|
|
|
|
432
|
|
|
$body = json_decode($response->getBody()->getContents(), true); |
|
433
|
|
|
|
|
434
|
|
|
if ($response->getStatusCode() != 200 || $body['successful'] === false) { |
|
435
|
|
|
// error has happened |
|
436
|
|
|
$message = $body['errorData']['message'] ?? 'خطا در بروزرسانی تراکنش رخ داده است.'; |
|
437
|
|
|
throw new PurchaseFailedException($message); |
|
438
|
|
|
} |
|
439
|
|
|
|
|
440
|
|
|
return $body['response']; |
|
441
|
|
|
} |
|
442
|
|
|
|
|
443
|
|
|
public function getPaymentUrl(): string |
|
444
|
|
|
{ |
|
445
|
|
|
return $this->paymentUrl; |
|
446
|
|
|
} |
|
447
|
|
|
|
|
448
|
|
|
public function setPaymentUrl(string $paymentUrl): void |
|
449
|
|
|
{ |
|
450
|
|
|
$this->paymentUrl = $paymentUrl; |
|
451
|
|
|
} |
|
452
|
|
|
} |
|
453
|
|
|
|