Skip to content

Commit cd5c98d

Browse files
Added test cases
1 parent c6c077e commit cd5c98d

12 files changed

+464
-31
lines changed

src/Concerns/Parameters.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,6 @@
1414
*/
1515
trait Parameters
1616
{
17-
/**
18-
* Trả về tài khoản nhận tiền.
19-
*
20-
* @return null|string
21-
*/
22-
public function getReceiverAccount(): ?string
23-
{
24-
return $this->getParameter('receiver_account');
25-
}
26-
27-
/**
28-
* Thiết lập tài khoản nhận tiền.
29-
*
30-
* @param null|string $account
31-
*
32-
* @return $this
33-
*/
34-
public function setReceiverAccount(?string $account)
35-
{
36-
return $this->setParameter('receiver_account', $account);
37-
}
38-
3917
/**
4018
* Trả về mã website.
4119
*

src/Message/Concerns/RequestSignature.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ trait RequestSignature
2222
*/
2323
protected function generateSignature(): string
2424
{
25+
$data = [];
2526
$signature = new Signature(
2627
$this->getSecurityCode()
2728
);
2829

29-
return $signature->generate(
30-
$this->getParameters()
31-
);
30+
foreach ($this->getSignatureParameters() as $parameter) {
31+
$data[$parameter] = $this->getParameter($parameter);
32+
}
33+
34+
return $signature->generate($data);
3235
}
36+
37+
abstract protected function getSignatureParameters(): array;
3338
}

src/Message/NotificationRequest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ public function getData(): array
3131
public function sendData($data): IncomingResponse
3232
{
3333
$signature = $data['signature'];
34-
$data = $data['data'];
34+
$dataNormalized = explode('|', $data['data']);
35+
[$amount, $message, $payment_type, $reference_number, $status, $trans_ref_no, $website_id] = $dataNormalized;
36+
$data = compact(
37+
'amount', 'message', 'payment_type', 'reference_number', 'status', 'trans_ref_no', 'website_id'
38+
);
3539
$data['signature'] = $signature;
3640

3741
return parent::sendData($data);

src/Message/PurchaseRequest.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ public function initialize(array $parameters = [])
3737
*/
3838
public function getData(): array
3939
{
40-
$data = $this->getParameters();
4140
$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);
4245
$data['signature'] = $this->generateSignature();
4346

4447
return $data;
@@ -54,6 +57,28 @@ public function sendData($data): PurchaseResponse
5457
return $this->response = new PurchaseResponse($this, $data, $redirectUrl);
5558
}
5659

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+
5782
/**
5883
* Trả về mã đơn hàng.
5984
* Ánh xạ của [[getTransactionId()]]
@@ -260,7 +285,6 @@ public function getBillToAddressCity(): ?string
260285
* Thiết lập tỉnh, thành phố giao hàng.
261286
*
262287
* @param null|string $city
263-
*
264288
* @return $this
265289
*/
266290
public function setBillToAddressCity(?string $city)
@@ -303,11 +327,21 @@ public function getBillToPhone(): ?string
303327
* Thiết lập số điện thoại người mua hàng.
304328
*
305329
* @param null|string $number
306-
*
307330
* @return $this
308331
*/
309332
public function setBillToPhone(?string $number)
310333
{
311334
return $this->setParameter('bill_to_phone', $number);
312335
}
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+
}
313347
}

src/Message/PurchaseResponse.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ public function isSuccessful(): bool
4444
return false;
4545
}
4646

47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function isRedirect(): bool
51+
{
52+
return true;
53+
}
54+
4755
/**
4856
* {@inheritdoc}
4957
*/

src/Support/Signature.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public function __construct(string $securityCode)
3939
public function generate(array $data): string
4040
{
4141
ksort($data);
42-
$data[] = $this->securityCode;
43-
$dataSign = implode('|', $data);
42+
$dataSign = implode('|', $data).'|'.$this->securityCode;
4443

4544
return strtoupper(hash('sha256', $dataSign));
4645
}

tests/GatewayTest.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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\Tests;
10+
11+
use Omnipay\Omnipay;
12+
use Omnipay\Tests\GatewayTestCase;
13+
use Omnipay\VTCPay\Message\IncomingResponse;
14+
use Omnipay\VTCPay\Message\PurchaseResponse;
15+
use Omnipay\Common\Exception\InvalidRequestException;
16+
use Omnipay\Common\Message\RedirectResponseInterface;
17+
use Omnipay\Common\Exception\InvalidResponseException;
18+
19+
/**
20+
* @author Vuong Minh <vuongxuongminh@gmail.com>
21+
* @since 1.0.0
22+
*/
23+
class GatewayTest extends GatewayTestCase
24+
{
25+
/**
26+
* @var \Omnipay\VTCPay\Gateway
27+
*/
28+
protected $gateway;
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
protected function setUp()
34+
{
35+
$this->gateway = Omnipay::create('VTCPay', $this->getHttpClient(), $this->getHttpRequest());
36+
$this->gateway->setWebsiteId(7022);
37+
$this->gateway->setSecurityCode('TichhopcongthanhtoanVTC2.0');
38+
$this->gateway->setTestMode(true);
39+
}
40+
41+
public function testPurchaseSuccess()
42+
{
43+
$response = $this->gateway->purchase([
44+
'receiver_account' => '0963465816',
45+
'reference_number' => ($id = microtime(false)),
46+
'amount' => 50000,
47+
])->send();
48+
49+
$this->assertInstanceOf(RedirectResponseInterface::class, $response);
50+
$this->assertInstanceOf(PurchaseResponse::class, $response);
51+
$this->assertTrue($response->isRedirect());
52+
$this->assertFalse($response->isSuccessful());
53+
$this->assertNotEmpty($response->getRedirectUrl());
54+
$this->assertEquals($id, $response->getTransactionId());
55+
}
56+
57+
public function testPurchaseFailure()
58+
{
59+
$this->expectException(InvalidRequestException::class);
60+
$this->gateway->purchase([
61+
'receiver_account' => '0963465816',
62+
'reference_number' => ($id = microtime(false)),
63+
])->send();
64+
}
65+
66+
public function testCompletePurchaseSuccess()
67+
{
68+
$this->getHttpRequest()->query->replace([
69+
'amount' => 50000,
70+
'message' => 'Thành công',
71+
'reference_number' => 123,
72+
'status' => 1,
73+
'trans_ref_no' => 789,
74+
'website_id' => 321,
75+
'signature' => '247b4714ac85fb2114cb996fa7abfe0414576d9f9602cbcc5221af586d953166',
76+
]);
77+
$response = $this->gateway->completePurchase()->send();
78+
79+
$this->assertInstanceOf(IncomingResponse::class, $response);
80+
$this->assertTrue($response->isSuccessful());
81+
$this->assertEquals('789', $response->getTransactionReference());
82+
$this->assertEquals('123', $response->getTransactionId());
83+
$this->assertEquals('321', $response->website_id);
84+
}
85+
86+
public function testCompletePurchaseFailure()
87+
{
88+
$this->expectException(InvalidResponseException::class);
89+
$this->getHttpRequest()->query->replace([
90+
'amount' => 50000,
91+
'message' => 'Thành công',
92+
'reference_number' => 123,
93+
'status' => 1,
94+
'trans_ref_no' => 789,
95+
'website_id' => 321,
96+
'signature' => 'abc',
97+
]);
98+
$this->gateway->completePurchase()->send();
99+
}
100+
101+
public function testNotificationSuccess()
102+
{
103+
$this->getHttpRequest()->request->replace([
104+
'data' => '50000|Thành công|website|123|1|789|321',
105+
'signature' => '909481220416ea789d424b33503500fe94ed8193c089a805e73768f2d6a0c95a',
106+
]);
107+
$response = $this->gateway->notification()->send();
108+
109+
$this->assertInstanceOf(IncomingResponse::class, $response);
110+
$this->assertTrue($response->isSuccessful());
111+
$this->assertEquals('789', $response->getTransactionReference());
112+
$this->assertEquals('123', $response->getTransactionId());
113+
$this->assertEquals('website', $response->payment_type);
114+
}
115+
116+
public function testNotificationFailure()
117+
{
118+
$this->expectException(InvalidRequestException::class);
119+
$this->getHttpRequest()->request->replace([
120+
'data' => '50000|Thành công|website|123|1|789|321',
121+
]);
122+
$this->gateway->notification()->send();
123+
}
124+
125+
/**
126+
* @doesNotPerformAssertions
127+
*/
128+
public function testDefaultParametersHaveMatchingMethods()
129+
{
130+
parent::testDefaultParametersHaveMatchingMethods();
131+
}
132+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\Tests\Message;
10+
11+
use Omnipay\Tests\TestCase;
12+
use Omnipay\VTCPay\Message\CompletePurchaseRequest;
13+
14+
/**
15+
* @author Vuong Minh <vuongxuongminh@gmail.com>
16+
* @since 1.0.0
17+
*/
18+
class CompletePurchaseRequestTest extends TestCase
19+
{
20+
/**
21+
* @var CompletePurchaseRequest
22+
*/
23+
private $request;
24+
25+
public function setUp()
26+
{
27+
$client = $this->getHttpClient();
28+
$request = $this->getHttpRequest();
29+
$request->query->replace([
30+
'amount' => 50000,
31+
'message' => 'Thành công',
32+
'reference_number' => 123,
33+
'status' => 1,
34+
'trans_ref_no' => 789,
35+
'website_id' => 321,
36+
'signature' => '247b4714ac85fb2114cb996fa7abfe0414576d9f9602cbcc5221af586d953166',
37+
]);
38+
$this->request = new CompletePurchaseRequest($client, $request);
39+
}
40+
41+
public function testGetData()
42+
{
43+
$data = $this->request->getData();
44+
$this->assertEquals(7, count($data));
45+
$this->assertEquals(50000, $data['amount']);
46+
$this->assertEquals('Thành công', $data['message']);
47+
$this->assertEquals(123, $data['reference_number']);
48+
$this->assertEquals(1, $data['status']);
49+
$this->assertEquals(789, $data['trans_ref_no']);
50+
$this->assertEquals(321, $data['website_id']);
51+
$this->assertEquals('247b4714ac85fb2114cb996fa7abfe0414576d9f9602cbcc5221af586d953166', $data['signature']);
52+
}
53+
}

0 commit comments

Comments
 (0)