Completed
Push — master ( aa04f4...ced6ad )
by Vuong
35:09
created

VerifiedData::validateSignature()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 17
cts 17
cp 1
rs 8.5866
c 0
b 0
f 0
cc 7
nc 8
nop 3
crap 7
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
    public function rules()
39
    {
40
        return [
41 5
            [['signature'], 'validateSignature', 'message' => '{attribute} is not valid!', 'on' => [
42
                PaymentGateway::VRC_PURCHASE_SUCCESS, PaymentGateway::VRC_IPN
43
            ], 'skipOnEmpty' => false]
44
        ];
45
    }
46
47
    /**
48
     * Phương thức kiểm tra chữ ký dữ liệu có hợp lệ hay không từ VTCPay gửi sang.
49
     *
50
     * @param string $attribute Attribute có giá trị là chữ ký cần kiểm tra.
51
     * @param array $params Mảng tham trị thiết lập từ rule
52
     * @param \yii\validators\InlineValidator $validator
53
     * @throws \yii\base\InvalidConfigException|\yii\base\NotSupportedException
54
     */
55 5
    public function validateSignature($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...
56
    {
57 5
        $client = $this->getClient();
58 5
        $data = $this->get(false);
59 5
        $expectSignature = ArrayHelper::remove($data, $attribute, false);
60
61 5
        if ($this->command === PaymentGateway::VRC_IPN) {
62 4
            $dataSign = $data['data'] ?? '';
63
        } else {
64 1
            $data = array_filter($data);
65 1
            ksort($data);
66 1
            $dataSign = implode('|', $data);
67
        }
68
69 5
        if (!$expectSignature || !$client->validateSignature($dataSign, $expectSignature)) {
70 3
            $validator->addError($this, $attribute, $validator->message);
71 2
        } elseif ($this->command === PaymentGateway::VRC_IPN) {
72 2
            $attributes = ['amount', 'message', 'payment_type', 'reference_number', 'status', 'trans_ref_no', 'website_id'];
73 2
            $values = explode('|', $dataSign);
74
75 2
            foreach (array_combine($attributes, $values) as $attr => $value) {
76 2
                $this->defineAttribute($attr, $value === '' ? null : $value);
77
            }
78
        }
79 5
    }
80
81
}
82