Completed
Push — master ( 80c2ce...052674 )
by Vuong
04:43
created

RequestData::ensureAttributes()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4.128

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 16
cts 20
cp 0.8
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 21
nc 4
nop 1
crap 4.128
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\nganluong;
9
10
use vxm\gatewayclients\RequestData as BaseRequestData;
11
12
/**
13
 * Lớp RequestData cung cấp dữ liệu cho phương thức [[request()]] theo lệnh, để truy vấn với Ngân Lượng.
14
 *
15
 * @property PaymentClient $client
16
 *
17
 * @author Vuong Minh <[email protected]>
18
 * @since 1.0
19
 */
20
class RequestData extends BaseRequestData
21
{
22
23
    /**
24
     * @inheritdoc
25
     */
26 2
    public function rules()
27
    {
28
        return [
29 2
            [['merchant_id', 'merchant_password', 'version', 'function'], 'required'],
30
            [[
31
                'bank_code', 'buyer_fullname', 'buyer_email', 'buyer_mobile',
32
                'total_amount', 'order_code', 'receiver_email', 'payment_method'
33
            ], 'required', 'on' => PaymentGateway::RC_PURCHASE],
34
            [['otp', 'auth_url'], 'required', 'on' => PaymentGateway::RC_AUTHENTICATE],
35
            [['token'], 'required', 'on' => [PaymentGateway::RC_QUERY_DR, PaymentGateway::RC_AUTHENTICATE]]
36
        ];
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42 2
    protected function ensureAttributes(array &$attributes)
43
    {
44 2
        parent::ensureAttributes($attributes);
45
        /** @var PaymentClient $client */
46 2
        $client = $this->getClient();
47 2
        $command = $this->getCommand();
48
49 2
        $attributes = array_merge($attributes, [
50 2
            'merchant_id' => $client->merchantId,
51 2
            'merchant_password' => md5($client->merchantPassword),
52 2
            'version' => $attributes['version'] ?? $client->getGateway()->getVersion()
53
        ]);
54
55 2
        if ($command === PaymentGateway::RC_PURCHASE) {
56 1
            $attributes['function'] = 'SetExpressCheckout';
57 1
            $attributes['receiver_email'] = $attributes['receiver_email'] ?? $client->email;
58
59 1
            if ($attributes['version'] === PaymentGateway::VERSION_3_2) {
60
                $attributes['payment_method'] = $attributes['payment_method'] ?? PaymentGateway::PAYMENT_METHOD_ATM_ONLINE;
61
                $this->addRule(['card_number', 'card_fullname', 'card_month', 'card_year'], 'required', [
62
                    'on' => $command
63
                ]);
64
            } else {
65 1
                $attributes['payment_method'] = $attributes['payment_method'] ?? PaymentGateway::PAYMENT_METHOD_NL;
66
            }
67 1
        } elseif ($command === PaymentGateway::RC_AUTHENTICATE) {
68
            $attributes['function'] = 'AuthenTransaction';
69
        } else {
70 1
            $attributes['function'] = 'GetTransactionDetail';
71
        }
72 2
    }
73
74
}
75