Skip to content

Commit 83bb58d

Browse files
Added test cases
1 parent f4b209b commit 83bb58d

20 files changed

+878
-159
lines changed

src/Message/AbstractRequest.php

Lines changed: 0 additions & 151 deletions
This file was deleted.

src/Message/AbstractSignatureRequest.php

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,40 @@
77

88
namespace Omnipay\VNPay\Message;
99

10+
use Omnipay\VNPay\Concerns\Parameters;
11+
use Omnipay\Common\Message\AbstractRequest;
12+
use Omnipay\VNPay\Concerns\ParametersNormalization;
13+
1014
/**
1115
* @author Vuong Minh <vuongxuongminh@gmail.com>
1216
* @since 1.0.0
1317
*/
1418
abstract class AbstractSignatureRequest extends AbstractRequest
1519
{
20+
use Parameters;
21+
use ParametersNormalization;
1622
use Concerns\RequestEndpoint;
1723
use Concerns\RequestSignature;
1824

25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function initialize(array $parameters = [])
29+
{
30+
parent::initialize(
31+
$this->normalizeParameters($parameters)
32+
);
33+
34+
$this->setVnpIpAddr(
35+
$this->getVnpIpAddr() ?? $this->httpRequest->getClientIp()
36+
);
37+
$this->setVnpCreateDate(
38+
$this->getVnpCreateDate() ?? date('Ymdhis')
39+
);
40+
41+
return $this;
42+
}
43+
1944
/**
2045
* {@inheritdoc}
2146
*/
@@ -35,4 +60,133 @@ public function getData(): array
3560

3661
return $parameters;
3762
}
63+
64+
/**
65+
* Trả về mã đơn hàng cần thực thi tác vụ.
66+
* Đây là phương thức ánh xạ của [[getTransactionId()]].
67+
*
68+
* @return null|string
69+
* @see getTransactionId
70+
*/
71+
public function getVnpTxnRef(): ?string
72+
{
73+
return $this->getTransactionId();
74+
}
75+
76+
/**
77+
* Thiết lập mã đơn hàng cần thực thi tác vụ.
78+
* Đây là phương thức ánh xạ của [[setTransactionId()]].
79+
*
80+
* @param null|string $ref
81+
*
82+
* @return $this
83+
* @see setTransactionId
84+
*/
85+
public function setVnpTxnRef(?string $ref)
86+
{
87+
return $this->setTransactionId($ref);
88+
}
89+
90+
/**
91+
* {@inheritdoc}
92+
*/
93+
public function getTransactionId(): ?string
94+
{
95+
return $this->getParameter('vnp_TxnRef');
96+
}
97+
98+
/**
99+
* {@inheritdoc}
100+
*/
101+
public function setTransactionId($value)
102+
{
103+
return $this->setParameter('vnp_TxnRef', $value);
104+
}
105+
106+
/**
107+
* Trả về thông tin đơn hàng hay lý do truy vấn đến VNPay.
108+
*
109+
* @return null|string
110+
*/
111+
public function getVnpOrderInfo(): ?string
112+
{
113+
return $this->getParameter('vnp_OrderInfo');
114+
}
115+
116+
/**
117+
* Thiết lập thông tin đơn hàng hay lý do truy vấn đến VNPay.
118+
*
119+
* @param null|string $info
120+
* @return $this
121+
*/
122+
public function setVnpOrderInfo(?string $info)
123+
{
124+
return $this->setParameter('vnp_OrderInfo', $info);
125+
}
126+
127+
/**
128+
* Trả về thời gian khởi tạo truy vấn đến VNPay.
129+
*
130+
* @return null|string
131+
* @see getVnpReturnUrl
132+
*/
133+
public function getVnpCreateDate(): ?string
134+
{
135+
return $this->getParameter('vnp_CreateDate');
136+
}
137+
138+
/**
139+
* Thiết lập thời gian khởi tạo truy vấn đến VNPay.
140+
* Mặc định sẽ là thời gian hiện tại.
141+
*
142+
* @param null|string $date
143+
* @return $this
144+
* @see setReturnUrl
145+
*/
146+
public function setVnpCreateDate(?string $date)
147+
{
148+
return $this->setParameter('vnp_CreateDate', $date);
149+
}
150+
151+
/**
152+
* Trả về ip của khách dùng để thanh toán.
153+
* Đây là phương thức ánh xạ của [[getClientIp()]].
154+
*
155+
* @return null|string
156+
* @see getClientIp
157+
*/
158+
public function getVnpIpAddr(): ?string
159+
{
160+
return $this->getClientIp();
161+
}
162+
163+
/**
164+
* Thiết lập ip của khách dùng để thanh toán.
165+
* Đây là phương thức ánh xạ của [[setClientIp()]].
166+
* Mặc định nếu không thiết lập sẽ là IP của khách.
167+
*
168+
* @param null|string $ip
169+
* @return $this
170+
* @see setClientIp
171+
*/
172+
public function setVnpIpAddr(?string $ip)
173+
{
174+
return $this->setClientIp($ip);
175+
}
176+
177+
/**
178+
* {@inheritdoc}
179+
*/
180+
public function getClientIp(): ?string
181+
{
182+
return $this->getParameter('vnp_IpAddr');
183+
}
184+
185+
/**
186+
* {@inheritdoc}
187+
*/
188+
public function setClientIp($value)
189+
{
190+
return $this->setParameter('vnp_IpAddr', $value);
191+
}
38192
}

src/Message/Concerns/ResponseSignatureValidation.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,19 @@ protected function validateSignature(): void
2525
{
2626
$data = $this->getData();
2727

28-
if (! isset($data['vnp_SecureHash'], $data['vnp_SecureHashType'])) {
28+
if (! isset($data['vnp_SecureHash'])) {
2929
throw new InvalidResponseException('Response from VNPay is invalid!');
3030
}
3131

3232
$dataSignature = array_filter($this->getData(), function ($parameter) {
3333
return 0 === strpos($parameter, 'vnp_')
34-
&& ! in_array($parameter, ['vnp_SecureHash', 'vnp_SecureHashType']);
34+
&& 'vnp_SecureHash' !== $parameter
35+
&& 'vnp_SecureHashType' !== $parameter;
3536
}, ARRAY_FILTER_USE_KEY);
3637

3738
$signature = new Signature(
3839
$this->getRequest()->getVnpHashSecret(),
39-
$data['vnp_SecureHashType']
40+
$data['vnp_SecureHashType'] ?? 'md5'
4041
);
4142

4243
if (! $signature->validate($dataSignature, $data['vnp_SecureHash'])) {

src/Message/IncomingRequest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@
88

99
namespace Omnipay\VNPay\Message;
1010

11+
use Omnipay\VNPay\Concerns\Parameters;
12+
use Omnipay\Common\Message\AbstractRequest;
13+
use Omnipay\VNPay\Concerns\ParametersNormalization;
14+
1115
/**
1216
* @author Vuong Minh <vuongxuongminh@gmail.com>
1317
* @since 1.0.0
1418
*/
1519
class IncomingRequest extends AbstractRequest
1620
{
21+
use Parameters;
22+
use ParametersNormalization;
23+
1724
/**
1825
* {@inheritdoc}
1926
*/
@@ -41,7 +48,9 @@ public function sendData($data): SignatureResponse
4148
*/
4249
public function initialize(array $parameters = [])
4350
{
44-
parent::initialize($parameters);
51+
parent::initialize(
52+
$this->normalizeParameters($parameters)
53+
);
4554

4655
foreach ($this->getIncomingParameters() as $parameter => $value) {
4756
$this->setParameter($parameter, $value);

src/Message/PurchaseRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function initialize(array $parameters = [])
5050
*/
5151
public function sendData($data): PurchaseResponse
5252
{
53-
$query = http_build_query($data, null, '&', PHP_QUERY_RFC3986);
53+
$query = http_build_query($data);
5454
$redirectUrl = $this->getEndpoint().'?'.$query;
5555

5656
return $this->response = new PurchaseResponse($this, $data, $redirectUrl);

0 commit comments

Comments
 (0)