Completed
Push — master ( eccc2c...9c0ef3 )
by Vuong
01:43
created

RequestData::rules()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 7
Ratio 41.18 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 7
loc 17
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 2
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()
16
 *
17
 * @property PaymentClient $client
18
 *
19
 * @author Vuong Minh <[email protected]>
20
 * @since 1.0
21
 */
22
class RequestData extends BaseRequestData
23
{
24
25
    /**
26
     * @inheritdoc
27
     */
28 4
    public function rules(): array
29
    {
30
        $rules = [
31 4
            [['business'], 'required', 'on' => [PaymentGateway::RC_PURCHASE, PaymentGateway::RC_GET_MERCHANT_DATA]],
32
            [['merchant_id', 'transaction_id'], 'required', 'on' => PaymentGateway::RC_QUERY_DR],
33
            [['checksum'], 'required', 'on' => [PaymentGateway::RC_QUERY_DR, PaymentGateway::RC_PURCHASE]],
34
            [['order_id', 'total_amount', 'url_success'], 'required', 'on' => [PaymentGateway::RC_PURCHASE]]
35
        ];
36
37 4 View Code Duplication
        if ($this->getClient()->getGateway()->pro && $this->getCommand() === PaymentGateway::RC_PURCHASE) {
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...
38 1
            return array_merge($rules, [
39 1
                [['payer_name', 'payer_email', 'payer_phone_no', 'bank_payment_method_id'], 'required', 'on' => PaymentGateway::RC_PURCHASE]
40
            ]);
41
        } else {
42 3
            return $rules;
43
        }
44
    }
45
46
    /**
47
     * @inheritdoc
48
     * @throws \yii\base\NotSupportedException
49
     */
50 4
    protected function ensureAttributes(array &$attributes)
51
    {
52 4
        $client = $this->getClient();
53 4
        $command = $this->getCommand();
54
55 4
        if (in_array($command, [PaymentGateway::RC_PURCHASE, PaymentGateway::RC_GET_MERCHANT_DATA], true)) {
56 3
            $attributes['business'] = $attributes['business'] ?? $client->merchantEmail;
57
        }
58
59 4
        if ($command === PaymentGateway::RC_GET_MERCHANT_DATA || ($command === PaymentGateway::RC_PURCHASE && $client->getGateway()->pro)) {
60 2
            ksort($attributes);
61 2
            if ($command === PaymentGateway::RC_PURCHASE) {
62 1
                $strSign = 'POST' . '&' . urlencode(PaymentGateway::PURCHASE_PRO_URL) . '&&' . urlencode(http_build_query($attributes));
63
            } else {
64 1
                $strSign = 'GET' . '&' . urlencode(PaymentGateway::PRO_SELLER_INFO_URL) . '&' . urlencode(http_build_query($attributes)) . '&';
65
            }
66 2
            $signature = $client->signature($strSign, PaymentClient::SIGNATURE_RSA);
67 2
            $attributes['signature'] = base64_encode($signature);
68 2
        } elseif (in_array($command, [PaymentGateway::RC_PURCHASE, PaymentGateway::RC_QUERY_DR], true)) {
69 2
            $attributes['merchant_id'] = $client->merchantId;
70 2
            ksort($attributes);
71 2
            $strSign = implode("", $attributes);
72 2
            $attributes['checksum'] = $client->signature($strSign, PaymentClient::SIGNATURE_HMAC);
73
        }
74
75 4
        parent::ensureAttributes($attributes);
76 4
    }
77
78
}
79