|
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\RedirectionForm; |
|
14
|
|
|
|
|
15
|
|
|
class SnappPay extends Driver |
|
16
|
|
|
{ |
|
17
|
|
|
const VERSION = '1.8'; |
|
18
|
|
|
const RELEASE_DATE = '2023-01-08'; |
|
19
|
|
|
|
|
20
|
|
|
const OAUTH_URL = '/api/online/v1/oauth/token'; |
|
21
|
|
|
const ELIGIBLE_URL = '/api/online/offer/v1/eligible'; |
|
22
|
|
|
const TOKEN_URL = '/api/online/payment/v1/token'; |
|
23
|
|
|
const VERIFY_URL = '/api/online/payment/v1/verify'; |
|
24
|
|
|
const SETTLE_URL = '/api/online/payment/v1/settle'; |
|
25
|
|
|
const REVERT_URL = '/api/online/payment/v1/revert'; |
|
26
|
|
|
const STATUS_URL = '/api/online/payment/v1/status'; |
|
27
|
|
|
const CANCEL_URL = '/api/online/payment/v1/cancel'; |
|
28
|
|
|
const UPDATE_URL = '/api/online/payment/v1/update'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* SnappPay Client. |
|
32
|
|
|
* |
|
33
|
|
|
* @var Client |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $client; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Invoice |
|
39
|
|
|
* |
|
40
|
|
|
* @var Invoice |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $invoice; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Driver settings |
|
46
|
|
|
* |
|
47
|
|
|
* @var object |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $settings; |
|
50
|
|
|
/** |
|
51
|
|
|
* SnappPay Oauth Data |
|
52
|
|
|
* |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $oauthToken; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* SnappPay payment url |
|
59
|
|
|
* |
|
60
|
|
|
* @var string |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $paymentUrl; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* SnappPay constructor. |
|
66
|
|
|
* Construct the class with the relevant settings. |
|
67
|
|
|
*/ |
|
68
|
|
|
public function __construct(Invoice $invoice, $settings) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->invoice($invoice); |
|
71
|
|
|
$this->settings = (object) $settings; |
|
72
|
|
|
$this->client = new Client(); |
|
73
|
|
|
$this->oauthToken = $this->oauth(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @throws PurchaseFailedException |
|
78
|
|
|
*/ |
|
79
|
|
|
public function purchase(): string |
|
80
|
|
|
{ |
|
81
|
|
|
$phone = $this->invoice->getDetail('phone') |
|
82
|
|
|
?? $this->invoice->getDetail('cellphone') |
|
83
|
|
|
?? $this->invoice->getDetail('mobile'); |
|
84
|
|
|
|
|
85
|
|
|
$data = [ |
|
86
|
|
|
'amount' => $this->normalizerAmount($this->invoice->getAmount()), |
|
|
|
|
|
|
87
|
|
|
'mobile' => $phone, |
|
88
|
|
|
'paymentMethodTypeDto' => 'INSTALLMENT', |
|
89
|
|
|
'transactionId' => $this->invoice->getUuid(), |
|
90
|
|
|
'returnURL' => $this->settings->callbackUrl, |
|
91
|
|
|
]; |
|
92
|
|
|
|
|
93
|
|
|
if (!is_null($discountAmount = $this->invoice->getDetail('discountAmount'))) { |
|
94
|
|
|
$data['discountAmount'] = $this->normalizerAmount($discountAmount); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if (!is_null($externalSourceAmount = $this->invoice->getDetail('externalSourceAmount'))) { |
|
98
|
|
|
$data['externalSourceAmount'] = $externalSourceAmount; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if (!is_null($cartList = $this->invoice->getDetail('cartList'))) { |
|
102
|
|
|
$data['cartList'] = $cartList; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$this->normalizerCartList($data); |
|
106
|
|
|
|
|
107
|
|
|
$response = $this |
|
108
|
|
|
->client |
|
109
|
|
|
->post( |
|
110
|
|
|
$this->settings->apiPaymentUrl.self::TOKEN_URL, |
|
111
|
|
|
[ |
|
112
|
|
|
RequestOptions::FORM_PARAMS => $data, |
|
113
|
|
|
RequestOptions::HEADERS => [ |
|
114
|
|
|
'Content-Type' => 'application/json', |
|
115
|
|
|
'Authorization' => 'Bearer '.$this->oauthToken, |
|
116
|
|
|
], |
|
117
|
|
|
RequestOptions::HTTP_ERRORS => false, |
|
118
|
|
|
] |
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
|
|
$body = json_decode($response->getBody()->getContents(), true); |
|
122
|
|
|
|
|
123
|
|
|
if ($response->getStatusCode() != 200 || $body['successful'] === false) { |
|
124
|
|
|
// error has happened |
|
125
|
|
|
$message = $body['errorData']['message'] ?? 'خطا در هنگام درخواست برای پرداخت رخ داده است.'; |
|
126
|
|
|
throw new PurchaseFailedException($message); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$this->invoice->transactionId($body['response']['paymentToken']); |
|
130
|
|
|
$this->setPaymentUrl($body['response']['paymentPageUrl']); |
|
131
|
|
|
|
|
132
|
|
|
// return the transaction's id |
|
133
|
|
|
return $this->invoice->getTransactionId(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function pay(): RedirectionForm |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->redirectWithForm($this->getPaymentUrl(), [], 'GET'); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @throws InvalidPaymentException |
|
143
|
|
|
*/ |
|
144
|
|
|
public function verify(): ReceiptInterface |
|
145
|
|
|
{ |
|
146
|
|
|
$paymentToken = $this->invoice->getTransactionId(); |
|
147
|
|
|
|
|
148
|
|
|
$response = $this |
|
|
|
|
|
|
149
|
|
|
->client |
|
150
|
|
|
->post( |
|
151
|
|
|
$this->settings->apiPaymentUrl.self::TOKEN_URL, |
|
152
|
|
|
[ |
|
153
|
|
|
RequestOptions::BODY => [ |
|
154
|
|
|
'paymentToken' => $paymentToken, |
|
155
|
|
|
], |
|
156
|
|
|
RequestOptions::HEADERS => [ |
|
157
|
|
|
'Authorization' => 'Bearer '.$this->oauthToken, |
|
158
|
|
|
], |
|
159
|
|
|
RequestOptions::HTTP_ERRORS => false, |
|
160
|
|
|
] |
|
161
|
|
|
); |
|
|
|
|
|
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @throws PurchaseFailedException |
|
166
|
|
|
*/ |
|
167
|
|
|
protected function oauth() |
|
168
|
|
|
{ |
|
169
|
|
|
$response = $this |
|
170
|
|
|
->client |
|
171
|
|
|
->post( |
|
172
|
|
|
$this->settings->apiPaymentUrl.self::OAUTH_URL, |
|
173
|
|
|
[ |
|
174
|
|
|
RequestOptions::HEADERS => [ |
|
175
|
|
|
'Authorization' => 'Basic '.base64_encode("{$this->settings->client_id}:{$this->settings->client_secret}"), |
|
176
|
|
|
], |
|
177
|
|
|
RequestOptions::FORM_PARAMS => [ |
|
178
|
|
|
'grant_type' => 'password', |
|
179
|
|
|
'scope' => 'online-merchant', |
|
180
|
|
|
'username' => $this->settings->username, |
|
181
|
|
|
'password' => $this->settings->password, |
|
182
|
|
|
], |
|
183
|
|
|
RequestOptions::HTTP_ERRORS => false, |
|
184
|
|
|
] |
|
185
|
|
|
); |
|
186
|
|
|
|
|
187
|
|
|
if ($response->getStatusCode() != 200) { |
|
188
|
|
|
throw new PurchaseFailedException('خطا در هنگام احراز هویت.'); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
$body = json_decode($response->getBody()->getContents(), true); |
|
193
|
|
|
|
|
194
|
|
|
return $body['access_token']; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @throws PurchaseFailedException |
|
199
|
|
|
*/ |
|
200
|
|
|
public function eligible() |
|
201
|
|
|
{ |
|
202
|
|
|
if (is_null($amount = $this->invoice->getAmount())) { |
|
|
|
|
|
|
203
|
|
|
throw new PurchaseFailedException('"amount" is required for this method.'); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
$response = $this->client->get($this->settings->apiPaymentUrl.self::ELIGIBLE_URL, [ |
|
207
|
|
|
RequestOptions::HEADERS => [ |
|
208
|
|
|
'Authorization' => 'Bearer '.$this->oauthToken, |
|
209
|
|
|
], |
|
210
|
|
|
RequestOptions::QUERY => [ |
|
211
|
|
|
'amount' => $this->normalizerAmount($amount), |
|
|
|
|
|
|
212
|
|
|
], |
|
213
|
|
|
]); |
|
214
|
|
|
|
|
215
|
|
|
$body = json_decode($response->getBody()->getContents(), true); |
|
216
|
|
|
|
|
217
|
|
|
if ($response->getStatusCode() != 200) { |
|
218
|
|
|
throw new InvalidPaymentException('', (int) $response->getStatusCode()); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
return $body; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
private function normalizerAmount(int $amount): int |
|
225
|
|
|
{ |
|
226
|
|
|
return $amount * ($this->settings->currency == 'T' ? 10 : 1); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
private function normalizerCartList(array &$data): void |
|
230
|
|
|
{ |
|
231
|
|
|
if (isset($data['cartList']['shippingAmount'])) { |
|
232
|
|
|
$data['cartList']['shippingAmount'] = $this->normalizerAmount($data['cartList']['shippingAmount']); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
if (isset($data['cartList']['taxAmount'])) { |
|
236
|
|
|
$data['cartList']['taxAmount'] = $this->normalizerAmount($data['cartList']['taxAmount']); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
if (isset($data['cartList']['totalAmount'])) { |
|
240
|
|
|
$data['cartList']['totalAmount'] = $this->normalizerAmount($data['cartList']['totalAmount']); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
foreach ($data['cartList']['cartItems'] as &$cartItem) { |
|
244
|
|
|
$cartItem['amount'] = $this->normalizerAmount($cartItem['amount']); |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
public function settle() |
|
249
|
|
|
{ |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
public function revert() |
|
253
|
|
|
{ |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
public function status() |
|
257
|
|
|
{ |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
public function cancel() |
|
261
|
|
|
{ |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
public function update() |
|
265
|
|
|
{ |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
public function getPaymentUrl(): string |
|
269
|
|
|
{ |
|
270
|
|
|
return $this->paymentUrl; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
public function setPaymentUrl(string $paymentUrl): void |
|
274
|
|
|
{ |
|
275
|
|
|
$this->paymentUrl = $paymentUrl; |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
|