|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/phpviet/omnipay-vnpay |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright (c) PHP Viet |
|
6
|
|
|
* @license [MIT](https://opensource.org/licenses/MIT) |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Omnipay\VNPay\Message; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Vuong Minh <[email protected]> |
|
13
|
|
|
* @since 1.0.0 |
|
14
|
|
|
*/ |
|
15
|
|
|
class PurchaseRequest extends AbstractSignatureRequest |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* {@inheritdoc} |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $productionEndpoint = 'https://pay.vnpay.vn/vpcpay.html'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* {@inheritdoc} |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $testEndpoint = 'http://sandbox.vnpayment.vn/paymentv2/vpcpay.html'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
|
|
public function initialize(array $parameters = []) |
|
31
|
|
|
{ |
|
32
|
|
|
parent::initialize($parameters); |
|
33
|
|
|
|
|
34
|
|
|
$this->setParameter('vnp_Command', 'pay'); |
|
35
|
|
|
$this->setVnpLocale( |
|
36
|
|
|
$this->getVnpLocale() ?? 'vn' |
|
37
|
|
|
); |
|
38
|
|
|
$this->setVnpCurrCode( |
|
39
|
|
|
$this->getVnpCurrCode() ?? 'VND' |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
public function sendData($data): PurchaseResponse |
|
49
|
|
|
{ |
|
50
|
|
|
$query = http_build_query($data); |
|
51
|
|
|
$redirectUrl = $this->getEndpoint().'?'.$query; |
|
52
|
|
|
|
|
53
|
|
|
return $this->response = new PurchaseResponse($this, $data, $redirectUrl); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Trả về vùng ngôn ngữ hiển thị trên VNPay khi khách thanh toán. |
|
58
|
|
|
* |
|
59
|
|
|
* @return null|string |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getVnpLocale(): ?string |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->getParameter('vnp_Locale'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Thiết lập vùng ngôn ngữ hiển thị trên VNPay khi khách thanh toán. |
|
68
|
|
|
* Mặc định nếu không thiết lập sẽ là `vn`. |
|
69
|
|
|
* |
|
70
|
|
|
* @param null|string $locale |
|
71
|
|
|
* |
|
72
|
|
|
* @return $this |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setVnpLocale(?string $locale) |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->setParameter('vnp_Locale', $locale); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Trả về mã tiền tệ dùng để thanh toán. |
|
81
|
|
|
* Đây là phương thức ánh xạ của [[getCurrency()]]. |
|
82
|
|
|
* |
|
83
|
|
|
* @return null|string |
|
84
|
|
|
* @see getCurrency |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getVnpCurrCode(): ?string |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->getCurrency(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Thiết lập mã tiền tệ dùng để thanh toán. |
|
93
|
|
|
* Đây là phương thức ánh xạ của [[setCurrency()]]. |
|
94
|
|
|
* |
|
95
|
|
|
* @param null|string $code |
|
96
|
|
|
* @return $this |
|
97
|
|
|
* @see setCurrency |
|
98
|
|
|
*/ |
|
99
|
|
|
public function setVnpCurrCode(?string $code) |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->setCurrency($code); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* {@inheritdoc} |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getCurrency(): ?string |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->getParameter('vnp_CurrCode'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* {@inheritdoc} |
|
114
|
|
|
* Mặc định nếu không thiết lập sẽ là `VND`. |
|
115
|
|
|
*/ |
|
116
|
|
|
public function setCurrency($value) |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->setParameter('vnp_CurrCode', $value); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Trả về mã ngân hàng dùng để thanh toán. |
|
123
|
|
|
* |
|
124
|
|
|
* @return null|string |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getVnpBankCode(): ?string |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->getParameter('vnp_BankCode'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Thiết lập mã ngân hàng dùng để thanh toán. |
|
133
|
|
|
* |
|
134
|
|
|
* @param null|string $code |
|
135
|
|
|
* @return $this |
|
136
|
|
|
*/ |
|
137
|
|
|
public function setVnpBankCode(?string $code) |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->setParameter('vnp_BankCode', $code); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Trả về nhóm loại đơn hàng. |
|
144
|
|
|
* |
|
145
|
|
|
* @return null|string |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getVnpOrderType(): ?string |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->getParameter('vnp_OrderType'); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Thiết lập nhóm loại đơn hàng. |
|
154
|
|
|
* |
|
155
|
|
|
* @param null|string $type |
|
156
|
|
|
* @return $this |
|
157
|
|
|
*/ |
|
158
|
|
|
public function setVnpOrderType(?string $type) |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->setParameter('vnp_OrderType', $type); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Trả về số tiền của đơn hàng. |
|
165
|
|
|
* Đây là phương thức ánh xạ của [[getAmount()]]. |
|
166
|
|
|
* |
|
167
|
|
|
* @return null|string |
|
168
|
|
|
* @see getAmount |
|
169
|
|
|
*/ |
|
170
|
|
|
public function getVnpAmount(): ?string |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->getAmount(); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Thiết lập số tiền cần thanh toán. |
|
177
|
|
|
* Đây là phương thức ánh xạ của [[setAmount()]]. |
|
178
|
|
|
* |
|
179
|
|
|
* @param null|string $number |
|
180
|
|
|
* @return $this |
|
181
|
|
|
* @see setAmount |
|
182
|
|
|
*/ |
|
183
|
|
|
public function setVnpAmount(?string $number) |
|
184
|
|
|
{ |
|
185
|
|
|
return $this->setAmount($number); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* {@inheritdoc} |
|
190
|
|
|
*/ |
|
191
|
|
|
public function getAmount(): ?string |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->getParameter('vnp_Amount'); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* {@inheritdoc} |
|
198
|
|
|
*/ |
|
199
|
|
|
public function setAmount($value) |
|
200
|
|
|
{ |
|
201
|
|
|
return $this->setParameter('vnp_Amount', $value); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Trả về url sẽ dẫn khách về sau khi thanh toán xong. |
|
206
|
|
|
* Đây là phương thức ánh xạ của [[getVnpReturnUrl()]]. |
|
207
|
|
|
* |
|
208
|
|
|
* @return null|string |
|
209
|
|
|
* @see getVnpReturnUrl |
|
210
|
|
|
*/ |
|
211
|
|
|
public function getVnpReturnUrl(): ?string |
|
212
|
|
|
{ |
|
213
|
|
|
return $this->getReturnUrl(); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Thiết lập url dẫn khách về sau khi thanh toán xong. |
|
218
|
|
|
* Đây là phương thức ánh xạ của [[setReturnUrl()]]. |
|
219
|
|
|
* |
|
220
|
|
|
* @param null|string $url |
|
221
|
|
|
* @return $this |
|
222
|
|
|
* @see setReturnUrl |
|
223
|
|
|
*/ |
|
224
|
|
|
public function setVnpReturnUrl(?string $url) |
|
225
|
|
|
{ |
|
226
|
|
|
return $this->setReturnUrl($url); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* {@inheritdoc} |
|
231
|
|
|
*/ |
|
232
|
|
|
public function getReturnUrl(): ?string |
|
233
|
|
|
{ |
|
234
|
|
|
return $this->getParameter('vnp_ReturnUrl'); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* {@inheritdoc} |
|
239
|
|
|
*/ |
|
240
|
|
|
public function setReturnUrl($value) |
|
241
|
|
|
{ |
|
242
|
|
|
return $this->setParameter('vnp_ReturnUrl', $value); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* {@inheritdoc} |
|
247
|
|
|
*/ |
|
248
|
|
|
protected function getSignatureParameters(): array |
|
249
|
|
|
{ |
|
250
|
|
|
$parameters = [ |
|
251
|
|
|
'vnp_CreateDate', 'vnp_IpAddr', 'vnp_ReturnUrl', 'vnp_Amount', 'vnp_OrderType', 'vnp_OrderInfo', |
|
252
|
|
|
'vnp_TxnRef', 'vnp_CurrCode', 'vnp_Locale', 'vnp_TmnCode', 'vnp_Command', 'vnp_Version', |
|
253
|
|
|
]; |
|
254
|
|
|
|
|
255
|
|
|
if ($this->getVnpBankCode()) { |
|
|
|
|
|
|
256
|
|
|
$parameters[] = 'vnp_BankCode'; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
return $parameters; |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: