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
|
|
|
namespace yiiviet\payment\baokim; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
|
12
|
|
|
use yii\base\InvalidConfigException; |
13
|
|
|
|
14
|
|
|
use yiiviet\payment\BasePaymentClient; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Lớp PaymentClient chứa các thuộc tính dùng để hổ trợ [[PaymentGateway]] kết nối đến Bảo Kim. |
18
|
|
|
* |
19
|
|
|
* @method PaymentGateway getGateway() |
20
|
|
|
* @property PaymentGateway $gateway |
21
|
|
|
* |
22
|
|
|
* @author Vuong Minh <[email protected]> |
23
|
|
|
* @since 1.0 |
24
|
|
|
*/ |
25
|
|
|
class PaymentClient extends BasePaymentClient |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Hằng khai báo kiểu chữ ký `RSA` được dùng khi gọi phương thức [[signature()]] và [[validateSignature()]]. |
29
|
|
|
*/ |
30
|
|
|
const SIGNATURE_RSA = 'RSA'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Hằng khai báo kiểu chữ ký `HMAC` được dùng khi gọi phương thức [[signature()]] và [[validateSignature()]]. |
34
|
|
|
*/ |
35
|
|
|
const SIGNATURE_HMAC = 'HMAC'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Thuộc tính dùng để khai báo `merchant id` kết nối đến Bảo Kim khi tạo [[request()]] ở [[PaymentGateway]]. |
39
|
|
|
* Nó do Bảo Kim cấp khi đăng ký tích hợp website. |
40
|
|
|
* |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
public $merchantId; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Thuộc tính thường được dùng để bổ sung email tài khoản nhận tiền hoặc cần lấy thông tin khi `end-user` không cung cấp thông tin. |
47
|
|
|
* Nó chính là email tài khoản Bảo Kim của bạn. |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
public $merchantEmail; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Thuộc tính được dùng để tạo và kiểm tra chữ ký dữ liệu khi truy vấn thông tin giao dịch, |
55
|
|
|
* tạo thanh toán theo phương thức Bảo Kim, xác minh BPN (Bao Kim Payment Notification), xác minh success url. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
public $securePassword; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Thuộc tính dùng để khai báo `user` kết nối đến Bảo Kim khi tạo [[request()]] ở [[PaymentGateway]] khi thanh toán theo phương thức PRO. |
63
|
|
|
* Nó do Bảo Kim cấp khi đăng ký tích hợp website. |
64
|
|
|
* |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
public $apiUser; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Thuộc tính dùng để khai báo `password` kết nối đến Bảo Kim khi tạo [[request()]] ở [[PaymentGateway]] khi thanh toán theo phương thức PRO. |
71
|
|
|
* Nó do Bảo Kim cấp khi đăng ký tích hợp website. |
72
|
|
|
* |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
public $apiPassword; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Thuộc tính được dùng để tạo chữ ký dữ liệu khi thanh toán theo phương thức PRO, lấy thông tin merchant. |
79
|
|
|
* |
80
|
|
|
* @var string |
81
|
|
|
*/ |
82
|
|
|
public $privateCertificate; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Thuộc tính được dùng để kiểm tra chữ ký dữ liệu. Hiện nó chưa được dùng. |
86
|
|
|
* |
87
|
|
|
* @var string |
88
|
|
|
*/ |
89
|
|
|
public $publicCertificate; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
* @throws InvalidConfigException |
94
|
|
|
* @since 1.0.3 |
95
|
|
|
*/ |
96
|
15 |
|
public function init() |
97
|
|
|
{ |
98
|
15 |
|
if ($this->merchantId === null) { |
99
|
|
|
throw new InvalidConfigException('Property `merchantId` must be set!'); |
100
|
|
|
} |
101
|
|
|
|
102
|
15 |
|
if ($this->merchantEmail === null) { |
103
|
|
|
throw new InvalidConfigException('Property `merchantEmail` must be set!'); |
104
|
|
|
} |
105
|
|
|
|
106
|
15 |
|
if ($this->securePassword === null) { |
107
|
|
|
throw new InvalidConfigException('Property `securePassword` must be set!'); |
108
|
|
|
} |
109
|
|
|
|
110
|
15 |
|
if ($this->getGateway()->pro) { |
111
|
|
|
if ($this->apiUser === null) { |
112
|
|
|
throw new InvalidConfigException('Property `apiUser` must be set on pro mode!'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($this->apiPassword === null) { |
116
|
|
|
throw new InvalidConfigException('Property `apiPassword` must be set on pro mode!'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ($this->privateCertificate === null) { |
120
|
|
|
throw new InvalidConfigException('Property `privateCertificate` must be set on pro mode!'); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
15 |
|
parent::init(); |
125
|
15 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param $file |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function setPublicCertificateFile($file): bool |
132
|
|
|
{ |
133
|
|
|
$file = Yii::getAlias($file); |
134
|
|
|
$this->publicCertificate = file_get_contents($file); |
135
|
|
|
|
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param $file |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
public function setPrivateCertificateFile($file): bool |
144
|
|
|
{ |
145
|
|
|
$file = Yii::getAlias($file); |
146
|
|
|
$this->privateCertificate = file_get_contents($file); |
147
|
|
|
|
148
|
|
|
return true; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @inheritdoc |
153
|
|
|
* @return object|\yiiviet\payment\DataSignature |
154
|
|
|
* @throws \yii\base\InvalidConfigException |
155
|
|
|
*/ |
156
|
10 |
|
protected function initDataSignature(string $data, string $type = null): ?\yiiviet\payment\DataSignature |
157
|
|
|
{ |
158
|
10 |
|
if ($type === self::SIGNATURE_RSA) { |
159
|
|
|
$config = [ |
160
|
7 |
|
'class' => 'yiiviet\payment\RsaDataSignature', |
161
|
7 |
|
'publicCertificate' => $this->publicCertificate ?? false, |
162
|
7 |
|
'privateCertificate' => $this->privateCertificate, |
163
|
7 |
|
'openSSLAlgo' => OPENSSL_ALGO_SHA1 |
164
|
|
|
]; |
165
|
3 |
|
} elseif ($type === self::SIGNATURE_HMAC) { |
166
|
|
|
$config = [ |
167
|
3 |
|
'class' => 'yiiviet\payment\HmacDataSignature', |
168
|
3 |
|
'key' => $this->securePassword, |
169
|
3 |
|
'hmacAlgo' => 'SHA1', |
170
|
|
|
'caseSensitive' => false |
171
|
|
|
]; |
172
|
|
|
} else { |
173
|
|
|
return null; |
174
|
|
|
} |
175
|
|
|
|
176
|
10 |
|
return Yii::createObject($config, [$data]); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|