Completed
Pull Request — master (#15)
by
unknown
02:55
created

RequestData   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 62
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 22 2
A ensureAttributes() 0 28 4
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 nhuluc\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
 * @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 Nhu Luc <[email protected]>
20
 * @since 1.0
21
 */
22
class RequestData extends BaseRequestData
23
{
24
25
    /**
26
     * @inheritdoc
27
     */
28 4
    public function rules()
29
    {
30
        $rules = [
31 4
            [['merchant_id', 'merchant_password', 'version', 'function'], 'required', 'on' => [
32
                PaymentGateway::RC_PURCHASE, PaymentGateway::RC_QUERY_DR, PaymentGateway::RC_AUTHENTICATE
33
            ]],
34
            [[
35
                'bank_code', 'buyer_fullname', 'buyer_email', 'buyer_mobile', 'return_url',
36
                'total_amount', 'order_code', 'receiver_email', 'payment_method'
37
            ], 'required', 'on' => PaymentGateway::RC_PURCHASE],
38
            [['otp', 'auth_url'], 'required', 'on' => PaymentGateway::RC_AUTHENTICATE],
39
            [['token'], 'required', 'on' => [PaymentGateway::RC_QUERY_DR, PaymentGateway::RC_AUTHENTICATE]]
40
        ];
41
42 4
        if ($this->getClient()->getGateway()->getSeamless()) {
43 2
            return array_merge($rules, [
44 2
                [['card_number', 'card_fullname', 'card_month', 'card_year'], 'required', 'on' => PaymentGateway::RC_PURCHASE]
45
            ]);
46
        } else {
47 2
            return $rules;
48
        }
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54 4
    protected function ensureAttributes(array &$attributes)
55
    {
56 4
        $client = $this->getClient();
57 4
        $command = $this->getCommand();
58
59 4
        $attributes = array_merge($attributes, [
60 4
            'merchant_id' => $client->merchantId,
61 4
            'merchant_password' => md5($client->merchantPassword),
62 4
            'version' => $client->getGateway()->getVersion()
63
        ]);
64
65 4
        if ($command === PaymentGateway::RC_PURCHASE) {
66 2
            $attributes['function'] = 'SetExpressCheckout';
67 2
            $attributes['receiver_email'] = $attributes['receiver_email'] ?? $client->email;
68
69 2
            if ($attributes['version'] === PaymentGateway::VERSION_3_2) {
70 1
                $attributes['payment_method'] = $attributes['payment_method'] ?? PaymentGateway::PAYMENT_METHOD_ATM_ONLINE;
71
            } else {
72 2
                $attributes['payment_method'] = $attributes['payment_method'] ?? PaymentGateway::PAYMENT_METHOD_NL;
73
            }
74 2
        } elseif ($command === PaymentGateway::RC_AUTHENTICATE) {
75 1
            $attributes['function'] = 'AuthenTransaction';
76
        } else {
77 1
            $attributes['function'] = 'GetTransactionDetail';
78
        }
79
80 4
        parent::ensureAttributes($attributes);
81 4
    }
82
83
}
84