Skip to content

Commit 5208d6f

Browse files
#1 - Support set secure hash type via parameter.
1 parent f4d0ebe commit 5208d6f

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
# Changelog
22

33
Tất cả lịch sử tiến trình phát triển thư viện
4+
5+
## 1.0.1
6+
7+
- Hổ trợ thiết lập kiểu mã hóa khi tạo chữ ký dự liệu thông qua parameter `vnp_SecureHashType`.

src/Message/AbstractSignatureRequest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function getData(): array
5353

5454
$parameters = $this->getParameters();
5555
$parameters['vnp_SecureHash'] = $this->generateSignature(
56-
$parameters['vnp_SecureHashType'] = 'md5'
56+
$parameters['vnp_SecureHashType'] = $this->getSecureHashType() ?? 'sha256'
5757
);
5858

5959
unset($parameters['vnp_HashSecret'], $parameters['testMode']);
@@ -189,4 +189,28 @@ public function setClientIp($value)
189189
{
190190
return $this->setParameter('vnp_IpAddr', $value);
191191
}
192+
193+
/**
194+
* Trả về phương thức mã hóa dùng để tạo chữ ký dự liệu (md5, sha256).
195+
*
196+
* @return null|string
197+
* @since 1.0.1
198+
*/
199+
public function getSecureHashType(): ?string
200+
{
201+
return $this->getParameter('vnp_SecureHashType');
202+
}
203+
204+
/**
205+
* Thiết lập phương thức mã hóa dùng để tạo chữ ký dự liệu.
206+
*
207+
* @param null|string $secureHashType
208+
*
209+
* @return $this
210+
* @since 1.0.1
211+
*/
212+
public function setSecureHashType(?string $secureHashType)
213+
{
214+
return $this->setParameter('vnp_SecureHashType', $secureHashType);
215+
}
192216
}

src/Message/Concerns/RequestSignature.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ trait RequestSignature
2121
* @param string $hashType
2222
* @return string
2323
*/
24-
protected function generateSignature($hashType = 'md5'): string
24+
protected function generateSignature(string $hashType = 'sha256'): string
2525
{
2626
$data = [];
2727
$signature = new Signature(

src/Support/Signature.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Signature
3636
* @param string $hashType
3737
* @throws InvalidArgumentException
3838
*/
39-
public function __construct(string $hashSecret, string $hashType = 'md5')
39+
public function __construct(string $hashSecret, string $hashType = 'sha256')
4040
{
4141
if (! $this->isSupportHashType($hashType)) {
4242
throw new InvalidArgumentException(sprintf('Hash type: `%s` is not supported by VNPay', $hashType));

tests/Message/PurchaseRequestTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function testGetData()
4444
$request->setVnpReturnUrl(10);
4545
$request->setVnpTxnRef(11);
4646
$request->setVnpVersion(12);
47+
$request->setSecureHashType('sha256');
4748
$request->setTestMode(true);
4849
$data = $request->getData();
4950
$this->assertEquals(14, count($data));
@@ -70,6 +71,7 @@ public function testGetData()
7071
$this->assertEquals(10, $data['vnp_ReturnUrl']);
7172
$this->assertEquals(11, $data['vnp_TxnRef']);
7273
$this->assertEquals(12, $data['vnp_Version']);
74+
$this->assertEquals('sha256', $data['vnp_SecureHashType']);
7375
$this->assertTrue(isset($data['vnp_SecureHash']));
7476
$this->assertFalse(isset($data['vnp_HashSecret']));
7577
}

0 commit comments

Comments
 (0)