1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/yii2-vn/payment |
4
|
|
|
* @copyright Copyright (c) 2017 Yii2VN |
5
|
|
|
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php) |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yiiviet\payment\vnpayment; |
9
|
|
|
|
10
|
|
|
use yii\helpers\ArrayHelper; |
11
|
|
|
|
12
|
|
|
use yiiviet\payment\VerifiedData as BaseVerifiedData; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Lớp VerifiedData tổng hợp dữ liệu đã được xác minh từ VnPayment. |
16
|
|
|
* |
17
|
|
|
* @method PaymentClient getClient() đối tượng client đã dùng để thực thi request. |
18
|
|
|
* |
19
|
|
|
* @property PaymentClient $client đối tượng client đã dùng để thực thi request. |
20
|
|
|
* @property string $OrderInfo mô tả đơn hàng. |
21
|
|
|
* @property mixed $TxnRef mã đơn hàng. |
22
|
|
|
* @property double $Amount số tiền. |
23
|
|
|
* @property string $TmnCode của client. |
24
|
|
|
* @property string $TransactionNo mã giao dịch tại VNPayment, chỉ tồn tại khi `ResponseCode` khác `00`. |
25
|
|
|
* @property string $Message thông báo lỗi, chỉ tồn tại khi `ResponseCode` khác `00`. |
26
|
|
|
* @property string $BankCode mã ngân hàng đã thực thi giao dịch, chỉ tồn tại khi `ResponseCode` khác `00`. |
27
|
|
|
* @property string $BankTranNo mã giao dịch tại ngân hàng, chỉ tồn tại khi `ResponseCode` khác `00`. |
28
|
|
|
* @property string $PayDate thời gian giao dịch, chỉ tồn tại khi `ResponseCode` khác `00`. |
29
|
|
|
* @property string $ResponseCode mã phản hồi. |
30
|
|
|
* |
31
|
|
|
* @author Vuong Minh <[email protected]> |
32
|
|
|
* @since 1.0 |
33
|
|
|
*/ |
34
|
|
|
class VerifiedData extends BaseVerifiedData |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
use MagicPropertiesTrait; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
public function rules() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
|
|
[['vnp_SecureHash'], 'required', 'on' => [ |
46
|
|
|
PaymentGateway::VRC_PURCHASE_SUCCESS, PaymentGateway::VRC_IPN |
47
|
|
|
]], |
48
|
|
|
[['vnp_SecureHash'], 'validateSecureHash', 'message' => '{attribute} is not valid!', 'on' => [ |
49
|
|
|
PaymentGateway::VRC_PURCHASE_SUCCESS, PaymentGateway::VRC_IPN |
50
|
|
|
]] |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Phương thức kiểm tra chữ ký dữ liệu có hợp lệ hay không từ VnPayment gửi sang. |
56
|
|
|
* |
57
|
|
|
* @param string $attribute Attribute có giá trị là chữ ký cần kiểm tra. |
58
|
|
|
* @param array $params Mảng tham trị thiết lập từ rule |
59
|
|
|
* @param \yii\validators\InlineValidator $validator |
60
|
|
|
* @throws \yii\base\InvalidConfigException|\yii\base\NotSupportedException |
61
|
|
|
*/ |
62
|
|
|
public function validateSecureHash($attribute, $params, \yii\validators\InlineValidator $validator) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$data = $this->get(false); |
65
|
|
|
$expectSignature = ArrayHelper::remove($data, $attribute, false); |
66
|
|
|
$hashType = ArrayHelper::remove($data, 'vnp_SecureHashType', 'MD5'); |
67
|
|
|
ksort($data); |
68
|
|
|
|
69
|
|
|
$client = $this->getClient(); |
70
|
|
|
$dataSign = urldecode(http_build_query($data)); |
71
|
|
|
|
72
|
|
View Code Duplication |
if (!$expectSignature || !$client->validateSignature($dataSign, $expectSignature, $hashType)) { |
|
|
|
|
73
|
|
|
$validator->addError($this, $attribute, $validator->message); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
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.