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 yii\helpers\ArrayHelper; |
12
|
|
|
|
13
|
|
|
use yiiviet\payment\VerifiedData as BaseVerifiedData; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Lớp VerifiedData tổng hợp dữ liệu đã được xác minh từ VTCPay. |
17
|
|
|
* |
18
|
|
|
* @method PaymentClient getClient() đối tượng client đã dùng để thực thi request. |
19
|
|
|
* |
20
|
|
|
* @property PaymentClient $client đối tượng client đã dùng để thực thi request. |
21
|
|
|
* @property double $amount số tiền đơn hàng. |
22
|
|
|
* @property string $message thông tin bổ sung. |
23
|
|
|
* @property string $payment_type hình thức thanh toán. |
24
|
|
|
* @property mixed $reference_number mã đơn hàng. |
25
|
|
|
* @property int $status trạng thái. |
26
|
|
|
* @property mixed $trans_ref_no mã giao dịch tại VTCPay. |
27
|
|
|
* @property string $website_id merchant id của client. |
28
|
|
|
* |
29
|
|
|
* @author Vuong Minh <[email protected]> |
30
|
|
|
* @since 1.0.2 |
31
|
|
|
*/ |
32
|
|
|
class VerifiedData extends BaseVerifiedData |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
*/ |
38
|
5 |
View Code Duplication |
public function rules() |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
return [ |
41
|
5 |
|
[['signature'], 'required', 'on' => [ |
42
|
|
|
PaymentGateway::VRC_PURCHASE_SUCCESS, PaymentGateway::VRC_IPN |
43
|
|
|
]], |
44
|
|
|
[['signature'], 'validateSignature', 'message' => '{attribute} is not valid!', 'on' => [ |
45
|
|
|
PaymentGateway::VRC_PURCHASE_SUCCESS, PaymentGateway::VRC_IPN |
46
|
|
|
]] |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Phương thức kiểm tra chữ ký dữ liệu có hợp lệ hay không từ VTCPay gửi sang. |
52
|
|
|
* |
53
|
|
|
* @param string $attribute Attribute có giá trị là chữ ký cần kiểm tra. |
54
|
|
|
* @param array $params Mảng tham trị thiết lập từ rule |
55
|
|
|
* @param \yii\validators\InlineValidator $validator |
56
|
|
|
* @throws \yii\base\InvalidConfigException|\yii\base\NotSupportedException |
57
|
|
|
*/ |
58
|
5 |
|
public function validateSignature($attribute, $params, \yii\validators\InlineValidator $validator) |
|
|
|
|
59
|
|
|
{ |
60
|
5 |
|
$client = $this->getClient(); |
61
|
5 |
|
$data = $this->get(false); |
62
|
5 |
|
$expectSignature = ArrayHelper::remove($data, $attribute, false); |
63
|
|
|
|
64
|
5 |
|
if ($this->command === PaymentGateway::VRC_IPN) { |
65
|
4 |
|
$dataSign = $data['data'] ?? ''; |
66
|
|
|
} else { |
67
|
1 |
|
ksort($data); |
68
|
1 |
|
$dataSign = implode('|', $data); |
69
|
|
|
} |
70
|
|
|
|
71
|
5 |
|
if (!$expectSignature || !$client->validateSignature($dataSign, $expectSignature)) { |
72
|
3 |
|
$validator->addError($this, $attribute, $validator->message); |
73
|
2 |
|
} elseif ($this->command === PaymentGateway::VRC_IPN) { |
74
|
2 |
|
$attributes = ['amount', 'message', 'payment_type', 'reference_number', 'status', 'trans_ref_no', 'website_id']; |
75
|
2 |
|
$values = explode('|', $dataSign); |
76
|
|
|
|
77
|
2 |
|
foreach (array_combine($attributes, $values) as $attr => $value) { |
78
|
2 |
|
$this->defineAttribute($attr, $value === '' ? null : $value); |
79
|
|
|
} |
80
|
|
|
} |
81
|
5 |
|
} |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.