Skip to content

Add support for version 2.1.0 #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2b59c39
bump to php8
bangnokia Sep 12, 2023
9fb7f98
migrate phpunit.xml.dist to new version
bangnokia Sep 12, 2023
33a0b6d
fix deprecated function signature
bangnokia Sep 12, 2023
a23fe86
bump default version
bangnokia Sep 12, 2023
4447bb3
remove old signature version
bangnokia Sep 12, 2023
5fad361
add vnp_ExpireDate
bangnokia Sep 12, 2023
c2a798e
remove weak hash algorithm
bangnokia Sep 12, 2023
3c977f8
wip
bangnokia Sep 12, 2023
6148ab7
update tests
bangnokia Sep 12, 2023
35e25e0
try to test Signature
bangnokia Sep 12, 2023
02706fa
cleanup
bangnokia Sep 12, 2023
d60733d
cleanup
bangnokia Sep 12, 2023
dff7549
add test action
bangnokia Sep 12, 2023
4c97a55
wip
bangnokia Sep 12, 2023
3aa7fa6
drop php < 7.4
bangnokia Sep 12, 2023
f76000a
rename branch
bangnokia Sep 12, 2023
f0ca18f
wip
bangnokia Sep 12, 2023
7806fa6
revert construct signature
bangnokia Sep 12, 2023
07b796b
wip
bangnokia Sep 12, 2023
fb6f68c
force create
bangnokia Sep 12, 2023
175e1bb
cleanup
bangnokia Sep 12, 2023
630fc7c
refactoring
bangnokia Sep 12, 2023
b2fa5f2
update comment
bangnokia Sep 12, 2023
bea34d5
wip
bangnokia Sep 12, 2023
aa4c508
wip
bangnokia Sep 12, 2023
59178e3
update to publish to packagist myself
bangnokia Sep 14, 2023
474889e
revert
bangnokia Sep 14, 2023
bd06911
change name to publish on composer
bangnokia Sep 18, 2023
752c8a5
rollback name
bangnokia Sep 18, 2023
a434655
remove unused parameter
bangnokia Sep 25, 2023
0be1d86
update packages usage in composer.json
bangnokia Sep 25, 2023
26dc713
update composer.json
bangnokia Sep 25, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: PHP Composer

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php: [7.4, 8.0, 8.1, 8.2]

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none

- name: Validate composer.json and composer.lock
run: composer validate

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run test suite
run: composer run-script test
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
}
],
"require": {
"php": "^7.1",
"php": "^7.4|^8",
"omnipay/common": "^3.0"
},
"require-dev": {
"omnipay/tests": "^3.0",
"scrutinizer/ocular": "^1.5"
"http-interop/http-factory-guzzle": "^1.2",
"omnipay/tests": "^4.0",
"scrutinizer/ocular": "^1.5",
"php-http/guzzle7-adapter": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
31 changes: 11 additions & 20 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="PHP Viet Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="PHP Viet Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
4 changes: 2 additions & 2 deletions src/Concerns/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
trait Parameters
{
/**
* Trả về mã Tmn do VNPay cấp.
* Trả về mã Version
*
* @return null|string
*/
Expand All @@ -25,7 +25,7 @@ public function getVnpVersion(): ?string
}

/**
* Thiết lập mã Tmn.
* Thiết lập mã Version
*
* @param null|string $code
* @return $this
Expand Down
2 changes: 1 addition & 1 deletion src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function initialize(array $parameters = [])
public function getDefaultParameters()
{
return [
'vnp_Version' => '2.0.0',
'vnp_Version' => '2.1.0',
];
}

Expand Down
34 changes: 22 additions & 12 deletions src/Message/AbstractSignatureRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Omnipay\VNPay\Message;

use Omnipay\Common\Exception\InvalidRequestException;
use Omnipay\Common\Message\AbstractRequest;
use Omnipay\VNPay\Concerns\Parameters;
use Omnipay\VNPay\Concerns\ParametersNormalization;
Expand Down Expand Up @@ -37,24 +38,23 @@ public function initialize(array $parameters = [])
$this->setVnpCreateDate(
$this->getVnpCreateDate() ?? date('Ymdhis')
);
$this->setVnpExpireDate(
$this->getVnpExpireDate() ?? date('Ymdhis', strtotime('+15 minutes'))
);

return $this;
}

/**
* {@inheritdoc}
* @throws InvalidRequestException
*/
public function getData(): array
{
call_user_func_array(
[$this, 'validate'],
$this->getSignatureParameters()
);
$this->validate(...$this->getSignatureParameters());

$parameters = $this->getParameters();
$parameters['vnp_SecureHash'] = $this->generateSignature(
$parameters['vnp_SecureHashType'] = $this->getSecureHashType() ?? 'sha256'
);
$parameters['vnp_SecureHash'] = $this->generateSignature();

unset($parameters['vnp_HashSecret'], $parameters['testMode']);

Expand All @@ -77,7 +77,7 @@ public function getVnpTxnRef(): ?string
* Thiết lập mã đơn hàng cần thực thi tác vụ.
* Đây là phương thức ánh xạ của [[setTransactionId()]].
*
* @param null|string $ref
* @param null|string $ref
*
* @return $this
* @see setTransactionId
Expand Down Expand Up @@ -116,7 +116,7 @@ public function getVnpOrderInfo(): ?string
/**
* Thiết lập thông tin đơn hàng hay lý do truy vấn đến VNPay.
*
* @param null|string $info
* @param null|string $info
* @return $this
*/
public function setVnpOrderInfo(?string $info)
Expand All @@ -139,7 +139,7 @@ public function getVnpCreateDate(): ?string
* Thiết lập thời gian khởi tạo truy vấn đến VNPay.
* Mặc định sẽ là thời gian hiện tại.
*
* @param null|string $date
* @param null|string $date
* @return $this
* @see setReturnUrl
*/
Expand All @@ -148,6 +148,16 @@ public function setVnpCreateDate(?string $date)
return $this->setParameter('vnp_CreateDate', $date);
}

public function getVnpExpireDate(): ?string
{
return $this->getParameter('vnp_ExpireDate');
}

public function setVnpExpireDate(string $date)
{
$this->setParameter('vnp_ExpireDate', $date);
}

/**
* Trả về ip của khách dùng để thanh toán.
* Đây là phương thức ánh xạ của [[getClientIp()]].
Expand All @@ -165,7 +175,7 @@ public function getVnpIpAddr(): ?string
* Đây là phương thức ánh xạ của [[setClientIp()]].
* Mặc định nếu không thiết lập sẽ là IP của khách.
*
* @param null|string $ip
* @param null|string $ip
* @return $this
* @see setClientIp
*/
Expand Down Expand Up @@ -204,7 +214,7 @@ public function getSecureHashType(): ?string
/**
* Thiết lập phương thức mã hóa dùng để tạo chữ ký dự liệu.
*
* @param null|string $secureHashType
* @param null|string $secureHashType
*
* @return $this
* @since 1.0.1
Expand Down
9 changes: 3 additions & 6 deletions src/Message/Concerns/RequestSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,18 @@ trait RequestSignature
/**
* Trả về chữ ký điện tử gửi đến VNPay dựa theo [[getSignatureParameters()]].
*
* @param string $hashType
* @return string
*/
protected function generateSignature(string $hashType = 'sha256'): string
protected function generateSignature(): string
{
$data = [];
$signature = new Signature(
$this->getVnpHashSecret(),
$hashType
);

foreach ($this->getSignatureParameters() as $parameter) {
$data[$parameter] = $this->getParameter($parameter);
}

$signature = new Signature($this->getVnpHashSecret());

return $signature->generate($data);
}

Expand Down
11 changes: 3 additions & 8 deletions src/Message/Concerns/ResponseSignatureValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,13 @@ protected function validateSignature(): void
}

$dataSignature = array_filter($this->getData(), function ($parameter) {
return 0 === strpos($parameter, 'vnp_')
&& 'vnp_SecureHash' !== $parameter
&& 'vnp_SecureHashType' !== $parameter;
return 0 === strpos($parameter, 'vnp_') && $parameter !== 'vnp_SecureHash';
}, ARRAY_FILTER_USE_KEY);

$signature = new Signature(
$this->getRequest()->getVnpHashSecret(),
$data['vnp_SecureHashType'] ?? 'md5'
);
$signature = new Signature($this->getRequest()->getVnpHashSecret());

if (! $signature->validate($dataSignature, $data['vnp_SecureHash'])) {
throw new InvalidResponseException(sprintf('Data signature response from VNPay is invalid!'));
throw new InvalidResponseException('Data signature response from VNPay is invalid!');
}
}
}
2 changes: 1 addition & 1 deletion src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public function setReturnUrl($value)
protected function getSignatureParameters(): array
{
$parameters = [
'vnp_CreateDate', 'vnp_IpAddr', 'vnp_ReturnUrl', 'vnp_Amount', 'vnp_OrderType', 'vnp_OrderInfo',
'vnp_CreateDate', 'vnp_ExpireDate', 'vnp_IpAddr', 'vnp_ReturnUrl', 'vnp_Amount', 'vnp_OrderType', 'vnp_OrderInfo',
'vnp_TxnRef', 'vnp_CurrCode', 'vnp_Locale', 'vnp_TmnCode', 'vnp_Command', 'vnp_Version',
];

Expand Down
2 changes: 1 addition & 1 deletion src/Message/QueryTransactionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function initialize(array $parameters = [])
*/
public function sendData($data): SignatureResponse
{
$query = http_build_query($data, null, '&', PHP_QUERY_RFC3986);
$query = http_build_query($data, '', '&', PHP_QUERY_RFC3986);
$requestUrl = $this->getEndpoint().'?'.$query;
$response = $this->httpClient->request('GET', $requestUrl);
$responseRawData = $response->getBody()->getContents();
Expand Down
2 changes: 1 addition & 1 deletion src/Message/RefundRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function initialize(array $parameters = [])
*/
public function sendData($data): SignatureResponse
{
$query = http_build_query($data, null, '&', PHP_QUERY_RFC3986);
$query = http_build_query($data, '', '&', PHP_QUERY_RFC3986);
$requestUrl = $this->getEndpoint().'?'.$query;
$response = $this->httpClient->request('GET', $requestUrl);
$responseRawData = $response->getBody()->getContents();
Expand Down
33 changes: 3 additions & 30 deletions src/Support/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,17 @@ class Signature
{
/**
* Khóa bí mật dùng để tạo và kiểm tra chữ ký dữ liệu.
*
* @var string
*/
protected $hashSecret;

/**
* Loại thuật toán mã hóa sẽ sử dụng.
*
* @var string
*/
protected $hashType;
protected string $hashSecret;

/**
* Khởi tạo đối tượng DataSignature.
*
* @param string $hashSecret
* @param string $hashType
* @throws InvalidArgumentException
*/
public function __construct(string $hashSecret, string $hashType = 'sha256')
public function __construct(string $hashSecret)
{
if (! $this->isSupportHashType($hashType)) {
throw new InvalidArgumentException(sprintf('Hash type: `%s` is not supported by VNPay', $hashType));
}

$this->hashType = $hashType;
$this->hashSecret = $hashSecret;
}

Expand All @@ -55,9 +40,8 @@ public function __construct(string $hashSecret, string $hashType = 'sha256')
public function generate(array $data): string
{
ksort($data);
$dataSign = $this->hashSecret.urldecode(http_build_query($data));

return hash($this->hashType, $dataSign);
return hash_hmac('sha512', http_build_query($data), $this->hashSecret);
}

/**
Expand All @@ -73,15 +57,4 @@ public function validate(array $data, string $expect): bool

return 0 === strcasecmp($expect, $actual);
}

/**
* Phương thức cho biết loại mã hóa truyền vào có được VNPay hổ trợ hay không.
*
* @param string $type
* @return bool
*/
protected function isSupportHashType(string $type): bool
{
return 0 === strcasecmp($type, 'md5') || 0 === strcasecmp($type, 'sha256');
}
}
Loading