|
| 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 | +} |
0 commit comments