Completed
Push — master ( 1be858...1c2c45 )
by Vuong
02:00
created

VerifiedData   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 44
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 8 1
A validateSecureHash() 0 19 4
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()
19
 *
20
 * @property PaymentClient $client
21
 *
22
 * @author Vuong Minh <[email protected]>
23
 * @since 1.0.2
24
 */
25
class VerifiedData extends BaseVerifiedData
26
{
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function rules()
32
    {
33
        return [
34
            [['signature'], 'validateSignature', 'message' => '{attribute} is not valid!', 'on' => [
35
                PaymentGateway::VRC_PURCHASE_SUCCESS, PaymentGateway::VRC_IPN
36
            ], 'skipOnEmpty' => false]
37
        ];
38
    }
39
40
    /**
41
     * Phương thức kiểm tra chữ ký dữ liệu có hợp lệ hay không từ VTCPay gửi sang.
42
     *
43
     * @param string $attribute Attribute có giá trị là chữ ký cần kiểm tra.
44
     * @param array $params Mảng tham trị thiết lập từ rule
45
     * @param \yii\validators\InlineValidator $validator
46
     * @throws \yii\base\InvalidConfigException|\yii\base\NotSupportedException
47
     */
48
    public function validateSecureHash($attribute, $params, \yii\validators\InlineValidator $validator)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50
        $client = $this->getClient();
51
        $data = $this->get(false);
52
        $expectSignature = ArrayHelper::remove($data, $attribute, false);
53
54
        if ($this->command === PaymentGateway::VRC_IPN) {
55
            $dataSign = $data['data'] ?? '';
56
        } else {
57
            ksort($data);
58
            $dataSign = implode('|', $data);
59
        }
60
61
        $dataSign .= '|' . $client->secureCode;
62
63
        if (!$expectSignature || !$client->validateSignature($dataSign, $expectSignature)) {
64
            $validator->addError($this, $attribute, $validator->message);
65
        }
66
    }
67
68
}
69