|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/yiiviet/yii2-payment |
|
4
|
|
|
* @copyright Copyright (c) 2017 Yii Viet |
|
5
|
|
|
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php) |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
namespace yiiviet\payment\vtcpay; |
|
10
|
|
|
|
|
11
|
|
|
use GatewayClients\DataInterface; |
|
12
|
|
|
|
|
13
|
|
|
use yii\base\NotSupportedException; |
|
14
|
|
|
use yii\httpclient\Client as HttpClient; |
|
15
|
|
|
|
|
16
|
|
|
use yiiviet\payment\BasePaymentGateway; |
|
17
|
|
|
|
|
18
|
|
|
use vxm\gatewayclients\RequestData; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Lớp PaymentGateway thực thi các phương thức trừu tượng dùng hổ trợ kết nối đến VTCPay. |
|
22
|
|
|
* Hiện tại nó hổ trợ 100% các tính năng từ cổng thanh toán VTCPay. |
|
23
|
|
|
* |
|
24
|
|
|
* @method ResponseData purchase(array $data, $clientId = null) |
|
25
|
|
|
* @method VerifiedData verifyRequestIPN(\yii\web\Request $request = null, $clientId = null) |
|
26
|
|
|
* @method VerifiedData verifyRequestPurchaseSuccess(\yii\web\Request $request = null, $clientId = null) |
|
27
|
|
|
* @method PaymentClient getClient($id = null) |
|
28
|
|
|
* @method PaymentClient getDefaultClient() |
|
29
|
|
|
* |
|
30
|
|
|
* @author Vuong Minh <[email protected]> |
|
31
|
|
|
* @since 1.0.2 |
|
32
|
|
|
*/ |
|
33
|
|
|
class PaymentGateway extends BasePaymentGateway |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Đường dẫn API để yêu cầu tạo giao dịch thanh toán. |
|
38
|
|
|
*/ |
|
39
|
|
|
const PURCHASE_URL = '/checkout.html'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Phương thức thanh toán bằng ví điện tử VTCPay. |
|
43
|
|
|
*/ |
|
44
|
|
|
const PAYMENT_METHOD_VTCPAY = 'VTCPay'; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Phương thức thanh toán bằng ngân hàng trong nước. |
|
48
|
|
|
*/ |
|
49
|
|
|
const PAYMENT_METHOD_DOMESTIC_BANK = 'DomesticBank'; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Phương thức thanh toán bằng thẻ quốc tế (visa/master). |
|
53
|
|
|
*/ |
|
54
|
|
|
const PAYMENT_METHOD_INTERNATIONAL_CARD = 'InternationalCard'; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @inheritdoc |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getBaseUrl(): string |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->sandbox ? 'http://alpha1.vtcpay.vn/portalgateway' : 'https://vtcpay.vn/bank-gateway'; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @inheritdoc |
|
66
|
|
|
*/ |
|
67
|
|
|
public function requestCommands(): array |
|
68
|
|
|
{ |
|
69
|
|
|
return [self::RC_PURCHASE]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @inheritdoc |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function initSandboxEnvironment() |
|
76
|
|
|
{ |
|
77
|
|
|
$clientConfig = require(__DIR__ . '/sandbox-client.php'); |
|
78
|
|
|
$this->setClient($clientConfig); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @inheritdoc |
|
83
|
|
|
* @throws NotSupportedException |
|
84
|
|
|
*/ |
|
85
|
|
|
public function queryDR(array $data, $clientId = null): DataInterface |
|
86
|
|
|
{ |
|
87
|
|
|
throw new NotSupportedException('queryDR doesnt\'t supported in VTCPay!'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @inheritdoc |
|
92
|
|
|
* @throws \yii\base\InvalidConfigException |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function requestInternal(RequestData $requestData, HttpClient $httpClient): array |
|
95
|
|
|
{ |
|
96
|
|
|
$data = $requestData->get(); |
|
97
|
|
|
$data[0] = self::PURCHASE_URL; |
|
98
|
|
|
|
|
99
|
|
|
return ['redirect_url' => $httpClient->createRequest()->setUrl($data)->getFullUrl()]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param int|string $command |
|
104
|
|
|
* @param \yii\web\Request $request |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function getVerifyRequestData($command, \yii\web\Request $request): array |
|
108
|
|
|
{ |
|
109
|
|
|
if ($command === self::VRC_IPN) { |
|
110
|
|
|
$params = ['data', 'signature']; |
|
111
|
|
|
$requestMethod = 'post'; |
|
112
|
|
|
} else { |
|
113
|
|
|
$params = ['amount', 'message', 'payment_type', 'reference_number', 'status', 'trans_ref_no', 'website_id', 'signature']; |
|
114
|
|
|
$requestMethod = 'get'; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$data = []; |
|
118
|
|
|
|
|
119
|
|
View Code Duplication |
foreach ($params as $param) { |
|
|
|
|
|
|
120
|
|
|
if (($value = call_user_func([$request, $requestMethod], $param)) !== null) { |
|
121
|
|
|
$data[$param] = $value; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $data; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: