RequestData::ensureAttributes()   B
last analyzed

Complexity

Conditions 6
Paths 18

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 28
cts 28
cp 1
rs 8.5937
c 0
b 0
f 0
cc 6
nc 18
nop 1
crap 6
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;
11
12
use yii\helpers\ArrayHelper;
13
14
use vxm\gatewayclients\RequestData as BaseRequestData;
15
16
/**
17
 * Lớp RequestData cung cấp dữ liệu để truy vấn đến VnPayment tạo lệnh thanh toán,
18
 * kiểm tra giao dịch, hoàn trả hóa đơn....
19
 *
20
 * @method PaymentClient getClient() đối tượng client đã dùng để thực thi request.
21
 *
22
 * @property PaymentClient $client đối tượng client đã dùng để thực thi request.
23
 *
24
 * @author Vuong Minh <[email protected]>
25
 * @since 1.0
26
 */
27
class RequestData extends BaseRequestData
28
{
29
30
    /**
31
     * @inheritdoc
32
     */
33 3
    public function rules()
34
    {
35
        return [
36 3
            [['vnp_Version', 'vnp_Command', 'vnp_TmnCode', 'vnp_TxnRef', 'vnp_OrderInfo', 'vnp_IpAddr', 'vnp_CreateDate', 'vnp_SecureHash'], 'required', 'on' => [
37
                PaymentGateway::RC_QUERY_DR, PaymentGateway::RC_REFUND, PaymentGateway::RC_PURCHASE
38
            ]],
39
            [['vnp_Amount'], 'required', 'on' => [PaymentGateway::RC_REFUND, PaymentGateway::RC_PURCHASE]],
40
            [['vnp_TransDate'], 'required', 'on' => [PaymentGateway::RC_REFUND, PaymentGateway::RC_QUERY_DR]],
41
            [['vnp_Locale', 'vnp_CurrCode', 'vnp_OrderType', 'vnp_ReturnUrl'], 'required', 'on' => PaymentGateway::RC_PURCHASE],
42
            [['vnp_TransactionNo'], 'required', 'on' => PaymentGateway::RC_QUERY_DR]
43
        ];
44
    }
45
46
    /**
47
     * @inheritdoc
48
     * @throws \yii\base\NotSupportedException
49
     */
50 3
    protected function ensureAttributes(array &$attributes)
51
    {
52 3
        parent::ensureAttributes($attributes);
53
54 3
        $attributesEnsured = [];
55
56 3
        foreach ($attributes as $attribute => $value) {
57 3
            if (substr($attribute, 0, 4) !== 'vnp_') {
58 3
                $attributesEnsured['vnp_' . $attribute] = $value;
59
            } else {
60 3
                $attributesEnsured[$attribute] = $value;
61
            }
62
        }
63
64 3
        $client = $this->getClient();
65 3
        $command = $this->getCommand();
66 3
        $hashType = ArrayHelper::remove($attributesEnsured, 'vnp_SecureHashType', 'MD5');
67
68 3
        if (Yii::$app instanceof \yii\web\Application) {
69 3
            $attributesEnsured['vnp_IpAddr'] = $attributesEnsured['vnp_IpAddr'] ?? Yii::$app->getRequest()->getUserIP();
70
        }
71
72 3
        $attributesEnsured['vnp_CreateDate'] = $attributesEnsured['vnp_CreateDate'] ?? date('Ymdhis');
73 3
        $attributesEnsured['vnp_Version'] = $client->getGateway()->getVersion();
74 3
        $attributesEnsured['vnp_TmnCode'] = $client->tmnCode;
75
76 3
        if ($command === PaymentGateway::RC_PURCHASE) {
77 1
            $attributesEnsured['vnp_OrderType'] = $attributesEnsured['vnp_OrderType'] ?? $client->defaultOrderType;
78 1
            $attributesEnsured['vnp_Locale'] = $attributesEnsured['vnp_Locale'] ?? 'vn';
79 1
            $attributesEnsured['vnp_Command'] = 'pay';
80 1
            $attributesEnsured['vnp_CurrCode'] = 'VND';
81
        } else {
82 2
            $attributesEnsured['vnp_Command'] = $command === PaymentGateway::RC_REFUND ? 'refund' : 'querydr';
83
        }
84
85 3
        ksort($attributesEnsured);
86
87 3
        unset($attributesEnsured['vnp_SecureHash']);
88 3
        $dataSign = urldecode(http_build_query($attributesEnsured));
89 3
        $attributesEnsured['vnp_SecureHash'] = $client->signature($dataSign, $hashType);
90 3
        $attributesEnsured['vnp_SecureHashType'] = $hashType;
91
92 3
        $attributes = $attributesEnsured;
93 3
    }
94
95
}
96