RequestData::rules()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 26

Duplication

Lines 14
Ratio 53.85 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 0
Metric Value
dl 14
loc 26
ccs 15
cts 15
cp 1
rs 9.504
c 0
b 0
f 0
cc 3
nc 1
nop 0
crap 3
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 vxm\gatewayclients\RequestData as BaseRequestData;
11
12
/**
13
 * Lớp RequestData cung cấp dữ liệu đã được kiểm tra tính trọn vẹn khi tạo [[request()]] ở [[PaymentGateway]].
14
 *
15
 * @method PaymentClient getClient() đối tượng client đã dùng để thực thi request.
16
 *
17
 * @property PaymentClient $client đối tượng client đã dùng để thực thi request.
18
 *
19
 * @author Vuong Minh <[email protected]>
20
 * @since 1.0
21
 */
22
class RequestData extends BaseRequestData
23
{
24
25
    /**
26
     * @inheritdoc
27
     */
28 9
    public function rules(): array
29
    {
30
        return [
31 9
            [['business'], 'required', 'on' => [PaymentGateway::RC_PURCHASE, PaymentGateway::RC_GET_MERCHANT_DATA]],
32
            [['merchant_id', 'transaction_id'], 'required', 'on' => PaymentGateway::RC_QUERY_DR],
33 9 View Code Duplication
            [['checksum'], 'required', 'on' => [PaymentGateway::RC_QUERY_DR, PaymentGateway::RC_PURCHASE], 'when' => function () {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
34 3
                if ($this->command === PaymentGateway::RC_PURCHASE) {
35 2
                    return !$this->getClient()->getGateway()->pro;
36
                } else {
37 1
                    return true;
38
                }
39 9
            }],
40 9 View Code Duplication
            [['signature'], 'required', 'on' => [PaymentGateway::RC_GET_MERCHANT_DATA, PaymentGateway::RC_PURCHASE], 'when' => function () {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
41 8
                if ($this->command === PaymentGateway::RC_PURCHASE) {
42 2
                    return $this->getClient()->getGateway()->pro;
43
                } else {
44 6
                    return true;
45
                }
46 9
            }],
47
            [['order_id', 'total_amount', 'url_success'], 'required', 'on' => [PaymentGateway::RC_PURCHASE]],
48 9
            [['payer_name', 'payer_email', 'payer_phone_no', 'bank_payment_method_id'], 'required', 'on' => PaymentGateway::RC_PURCHASE, 'when' => function () {
49 2
                return $this->getClient()->getGateway()->pro;
50 9
            }]
51
        ];
52
53
    }
54
55
    /**
56
     * @inheritdoc
57
     * @throws \yii\base\NotSupportedException
58
     */
59 9
    protected function ensureAttributes(array &$attributes)
60
    {
61 9
        $client = $this->getClient();
62 9
        $command = $this->getCommand();
63
64 9
        if (in_array($command, [PaymentGateway::RC_PURCHASE, PaymentGateway::RC_GET_MERCHANT_DATA], true)) {
65 8
            $attributes['business'] = $attributes['business'] ?? $client->merchantEmail;
66
        }
67
68 9
        if ($command === PaymentGateway::RC_GET_MERCHANT_DATA || ($command === PaymentGateway::RC_PURCHASE && $client->getGateway()->pro)) {
69 7
            unset($attributes['signature']);
70 7
            ksort($attributes);
71
72 7
            if ($command === PaymentGateway::RC_PURCHASE) {
73 1
                $strSign = 'POST' . '&' . urlencode(PaymentGateway::PURCHASE_PRO_URL) . '&&' . urlencode(http_build_query($attributes));
74
            } else {
75 6
                $strSign = 'GET' . '&' . urlencode(PaymentGateway::PRO_SELLER_INFO_URL) . '&' . urlencode(http_build_query($attributes)) . '&';
76
            }
77
78 7
            $signature = $client->signature($strSign, PaymentClient::SIGNATURE_RSA);
79 7
            $attributes['signature'] = base64_encode($signature);
80 2
        } elseif (in_array($command, [PaymentGateway::RC_PURCHASE, PaymentGateway::RC_QUERY_DR], true)) {
81 2
            $attributes['merchant_id'] = $client->merchantId;
82 2
            unset($attributes['checksum']);
83 2
            ksort($attributes);
84 2
            $strSign = implode("", $attributes);
85 2
            $attributes['checksum'] = $client->signature($strSign, PaymentClient::SIGNATURE_HMAC);
86
        }
87
88 9
        parent::ensureAttributes($attributes);
89 9
    }
90
91
}
92