1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/phpviet/omnipay-vtcpay |
4
|
|
|
* |
5
|
|
|
* @copyright (c) PHP Viet |
6
|
|
|
* @license [MIT](https://opensource.org/licenses/MIT) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Omnipay\VTCPay\Message; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Vuong Minh <[email protected]> |
13
|
|
|
* @since 1.0.0 |
14
|
|
|
*/ |
15
|
|
|
class PurchaseRequest extends AbstractRequest |
16
|
|
|
{ |
17
|
|
|
use Concerns\RequestEndpoint; |
18
|
|
|
use Concerns\RequestSignature; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public function initialize(array $parameters = []) |
24
|
|
|
{ |
25
|
|
|
parent::initialize($parameters); |
26
|
|
|
|
27
|
|
|
$this->setCurrency( |
28
|
|
|
$this->getCurrency() ?? 'VND' |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
* @throws \Omnipay\Common\Exception\InvalidRequestException |
37
|
|
|
*/ |
38
|
|
|
public function getData(): array |
39
|
|
|
{ |
40
|
|
|
$this->validate('currency', 'receiver_account', 'reference_number', 'website_id', 'amount'); |
41
|
|
|
$validParameters = $this->getSignatureParameters(); |
42
|
|
|
$data = array_filter($this->getParameters(), function ($parameter) use ($validParameters) { |
43
|
|
|
return in_array($parameter, $validParameters, true); |
44
|
|
|
}, ARRAY_FILTER_USE_KEY); |
45
|
|
|
$data['signature'] = $this->generateSignature(); |
46
|
|
|
|
47
|
|
|
return $data; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function sendData($data): PurchaseResponse |
54
|
|
|
{ |
55
|
|
|
$redirectUrl = $this->getEndpoint().'/checkout.html?'.http_build_query($data); |
56
|
|
|
|
57
|
|
|
return $this->response = new PurchaseResponse($this, $data, $redirectUrl); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Trả về tài khoản nhận tiền. |
62
|
|
|
* |
63
|
|
|
* @return null|string |
64
|
|
|
*/ |
65
|
|
|
public function getReceiverAccount(): ?string |
66
|
|
|
{ |
67
|
|
|
return $this->getParameter('receiver_account'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Thiết lập tài khoản nhận tiền. |
72
|
|
|
* |
73
|
|
|
* @param null|string $account |
74
|
|
|
* |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
|
|
public function setReceiverAccount(?string $account) |
78
|
|
|
{ |
79
|
|
|
return $this->setParameter('receiver_account', $account); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Trả về mã đơn hàng. |
84
|
|
|
* Ánh xạ của [[getTransactionId()]]. |
85
|
|
|
* |
86
|
|
|
* @return null|string |
87
|
|
|
* @see getTransactionId |
88
|
|
|
*/ |
89
|
|
|
public function getReferenceNumber(): ?string |
90
|
|
|
{ |
91
|
|
|
return $this->getTransactionId(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Thiết lập mã đơn hàng. |
96
|
|
|
* Ánh xạ của [[setTransactionId()]]. |
97
|
|
|
* |
98
|
|
|
* @param null|string $number |
99
|
|
|
* @return $this |
100
|
|
|
* @see setTransactionId |
101
|
|
|
*/ |
102
|
|
|
public function setReferenceNumber(?string $number) |
103
|
|
|
{ |
104
|
|
|
return $this->setTransactionId($number); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function getTransactionId(): ?string |
111
|
|
|
{ |
112
|
|
|
return $this->getParameter('reference_number'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function setTransactionId($value) |
119
|
|
|
{ |
120
|
|
|
return $this->setParameter('reference_number', $value); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get value of the url_return parameter |
125
|
|
|
* Ánh xạ của [[getReturnUrl()]]. |
126
|
|
|
* |
127
|
|
|
* @return null|string |
128
|
|
|
* @see getReturnUrl |
129
|
|
|
*/ |
130
|
|
|
public function getUrlReturn(): ?string |
131
|
|
|
{ |
132
|
|
|
return $this->getReturnUrl(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set value of the url_return parameter |
137
|
|
|
* Ánh xạ của [[setReturnUrl()]]. |
138
|
|
|
* |
139
|
|
|
* @param string $value |
140
|
|
|
* @return $this |
141
|
|
|
* @see setReturnUrl |
142
|
|
|
*/ |
143
|
|
|
public function setUrlReturn($value) |
144
|
|
|
{ |
145
|
|
|
return $this->setReturnUrl($value); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get the request return URL. |
150
|
|
|
* |
151
|
|
|
* @return null|string |
152
|
|
|
*/ |
153
|
|
|
public function getReturnUrl(): ?string |
154
|
|
|
{ |
155
|
|
|
return $this->getParameter('url_return'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Sets the request return URL. |
160
|
|
|
* |
161
|
|
|
* @param string $value |
162
|
|
|
* @return $this |
163
|
|
|
*/ |
164
|
|
|
public function setReturnUrl($value) |
165
|
|
|
{ |
166
|
|
|
return $this->setParameter('url_return', $value); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Trả về ngôn ngữ của giao diện thanh toán. |
171
|
|
|
* |
172
|
|
|
* @return null|string |
173
|
|
|
*/ |
174
|
|
|
public function getLanguage(): ?string |
175
|
|
|
{ |
176
|
|
|
return $this->getParameter('language'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Thiết lập ngôn ngữ của giao diện thanh toán. |
181
|
|
|
* |
182
|
|
|
* @param null|string $language |
183
|
|
|
* @return $this |
184
|
|
|
*/ |
185
|
|
|
public function setLanguage(?string $language) |
186
|
|
|
{ |
187
|
|
|
return $this->setParameter('language', $language); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Trả về hình thức thanh toán. |
192
|
|
|
* |
193
|
|
|
* @return null|string |
194
|
|
|
*/ |
195
|
|
|
public function getPaymentType(): ?string |
196
|
|
|
{ |
197
|
|
|
return $this->getParameter('payment_type'); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Thiết lập hình thức thanh toán. |
202
|
|
|
* |
203
|
|
|
* @param null|string $type |
204
|
|
|
* @return $this |
205
|
|
|
*/ |
206
|
|
|
public function setPaymentType(?string $type) |
207
|
|
|
{ |
208
|
|
|
return $this->setParameter('payment_type', $type); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Trả về tên chủ đơn hàng. |
213
|
|
|
* |
214
|
|
|
* @return null|string |
215
|
|
|
*/ |
216
|
|
|
public function getBillToForename(): ?string |
217
|
|
|
{ |
218
|
|
|
return $this->getParameter('bill_to_forename'); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Thiết lập tên chủ đơn hàng. |
223
|
|
|
* |
224
|
|
|
* @param null|string $forename |
225
|
|
|
* @return $this |
226
|
|
|
*/ |
227
|
|
|
public function setBillToForename(?string $forename) |
228
|
|
|
{ |
229
|
|
|
return $this->setParameter('bill_to_forename', $forename); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Trả về tên chủ đơn hàng. |
234
|
|
|
* |
235
|
|
|
* @return null|string |
236
|
|
|
*/ |
237
|
|
|
public function getBillToSurname(): ?string |
238
|
|
|
{ |
239
|
|
|
return $this->getParameter('bill_to_surname'); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Thiết lập tên chủ đơn hàng. |
244
|
|
|
* |
245
|
|
|
* @param null|string $surname |
246
|
|
|
* @return $this |
247
|
|
|
*/ |
248
|
|
|
public function setBillToSurname(?string $surname) |
249
|
|
|
{ |
250
|
|
|
return $this->setParameter('bill_to_surname', $surname); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Trả về địa chỉ giao hàng. |
255
|
|
|
* |
256
|
|
|
* @return null|string |
257
|
|
|
*/ |
258
|
|
|
public function getBillToAddress(): ?string |
259
|
|
|
{ |
260
|
|
|
return $this->getParameter('bill_to_address'); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Thiết lập địa chỉ giao hàng. |
265
|
|
|
* |
266
|
|
|
* @param null|string $address |
267
|
|
|
* @return $this |
268
|
|
|
*/ |
269
|
|
|
public function setBillToAddress($address) |
270
|
|
|
{ |
271
|
|
|
return $this->setParameter('bill_to_address', $address); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Trả về tỉnh, thành phố giao hàng. |
276
|
|
|
* |
277
|
|
|
* @return null|string |
278
|
|
|
*/ |
279
|
|
|
public function getBillToAddressCity(): ?string |
280
|
|
|
{ |
281
|
|
|
return $this->getParameter('bill_to_address_city'); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Thiết lập tỉnh, thành phố giao hàng. |
286
|
|
|
* |
287
|
|
|
* @param null|string $city |
288
|
|
|
* @return $this |
289
|
|
|
*/ |
290
|
|
|
public function setBillToAddressCity(?string $city) |
291
|
|
|
{ |
292
|
|
|
return $this->setParameter('bill_to_address_city', $city); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Trả về email người mua hàng. |
297
|
|
|
* |
298
|
|
|
* @return null|string |
299
|
|
|
*/ |
300
|
|
|
public function getBillToEmail(): ?string |
301
|
|
|
{ |
302
|
|
|
return $this->getParameter('bill_to_email'); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Thiết lập email người mua hàng. |
307
|
|
|
* |
308
|
|
|
* @param null|string $email |
309
|
|
|
* @return $this |
310
|
|
|
*/ |
311
|
|
|
public function setBillToEmail($email) |
312
|
|
|
{ |
313
|
|
|
return $this->setParameter('bill_to_email', $email); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Trả về số điện thoại người mua hàng. |
318
|
|
|
* |
319
|
|
|
* @return null|string |
320
|
|
|
*/ |
321
|
|
|
public function getBillToPhone(): ?string |
322
|
|
|
{ |
323
|
|
|
return $this->getParameter('bill_to_phone'); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Thiết lập số điện thoại người mua hàng. |
328
|
|
|
* |
329
|
|
|
* @param null|string $number |
330
|
|
|
* @return $this |
331
|
|
|
*/ |
332
|
|
|
public function setBillToPhone(?string $number) |
333
|
|
|
{ |
334
|
|
|
return $this->setParameter('bill_to_phone', $number); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* {@inheritdoc} |
339
|
|
|
*/ |
340
|
|
|
protected function getSignatureParameters(): array |
341
|
|
|
{ |
342
|
|
|
$parameters = $this->getParameters(); |
343
|
|
|
unset($parameters['testMode'], $parameters['security_code']); |
344
|
|
|
|
345
|
|
|
return array_keys($parameters); |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|