Completed
Push — master ( 075950...974b7a )
by Vuong
02:25
created

RequestData::getRequestType()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 15
cp 0
rs 9.4555
c 0
b 0
f 0
cc 5
nc 5
nop 0
crap 30
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\momo;
9
10
use yii\base\NotSupportedException;
11
12
use vxm\gatewayclients\RequestData as BaseRequestData;
13
14
/**
15
 * Lớp RequestData cung cấp dữ liệu đã được kiểm tra tính trọn vẹn khi tạo [[request()]] ở [[PaymentGateway]].
16
 *
17
 * @method PaymentClient getClient() đối tượng client đã dùng để thực thi request.
18
 *
19
 * @property PaymentClient $client đối tượng client đã dùng để thực thi request.
20
 *
21
 * @author Vuong Minh <[email protected]>
22
 * @since 1.0.3
23
 */
24
class RequestData extends BaseRequestData
25
{
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function rules()
31
    {
32
        return [
33
            [['amount'], 'required', 'on' => [PaymentGateway::RC_REFUND, PaymentGateway::RC_PURCHASE]],
34
            [['returnUrl'], 'required', 'on' => PaymentGateway::RC_PURCHASE],
35
            [['transId'], 'required', 'on' => PaymentGateway::RC_REFUND],
36
            [['partnerCode', 'accessKey', 'requestId', 'orderId', 'signature', 'requestType'], 'required', 'on' => [
37
                PaymentGateway::RC_PURCHASE, PaymentGateway::RC_QUERY_DR, PaymentGateway::RC_REFUND, PaymentGateway::RC_QUERY_REFUND
38
            ]],
39
        ];
40
    }
41
42
    /**
43
     * @inheritdoc
44
     * @throws NotSupportedException
45
     */
46
    protected function ensureAttributes(array &$attributes)
47
    {
48
        parent::ensureAttributes($attributes);
49
        $client = $this->getClient();
50
        $command = $this->getCommand();
51
        $attributes['partnerCode'] = $client->partnerCode;
52
        $attributes['accessKey'] = $client->accessKey;
53
54
        if ($command === PaymentGateway::RC_QUERY_DR) {
55
            $attributes['orderInfo'] = $attributes['orderInfo'] ?? '';
56
            $attributes['extraData'] = $attributes['extraData'] ?? '';
57
            $attributes['notifyUrl'] = $attributes['notifyUrl'] ?? '';
58
        }
59
60
        $attributes['signature'] = $this->signature($attributes);
0 ignored issues
show
Bug introduced by
The method signature() does not exist on yiiviet\payment\momo\RequestData. Did you maybe mean getSignature()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
61
        $attributes['requestType'] = $this->getRequestType();
62
    }
63
64
    /**
65
     * Phương thức hổ trợ lấy `requestType` tương ứng với [[getCommand()]] khi gửi dữ liệu đến MOMO.
66
     *
67
     * @return string `requestType` gửi đến MOMO
68
     * @throws NotSupportedException
69
     */
70
    protected function getRequestType(): string
71
    {
72
        switch ($command = $this->getCommand()) {
73
            case PaymentGateway::RC_PURCHASE:
74
                return 'captureMoMoWallet';
75
            case PaymentGateway::RC_QUERY_DR:
76
                return 'transactionStatus';
77
            case PaymentGateway::RC_REFUND:
78
                return 'refundMoMoWallet';
79
            case PaymentGateway::RC_QUERY_REFUND:
80
                return 'refundStatus';
81
            default:
82
                throw new NotSupportedException("Not supported command: `$command`");
83
        }
84
    }
85
86
    /**
87
     * Phương thức hồ trợ ký dữ liệu gửi đến MOMO.
88
     *
89
     * @param array $attributes mảng chứa các thông tin dùng để tạo chữ ký.
90
     * @return string chữ ký dữ liệu.
91
     * @throws NotSupportedException
92
     */
93
    protected function getSignature(array $attributes): string
94
    {
95
        switch ($command = $this->getCommand()) {
96
            case PaymentGateway::RC_PURCHASE:
97
                $dataSign = [
98
                    'partnerCode' => $attributes['partnerCode'],
99
                    'accessKey' => $attributes['accessKey'],
100
                    'requestId' => $attributes['requestId'],
101
                    'amount' => $attributes['amount'],
102
                    'orderId' => $attributes['orderId'],
103
                    'orderInfo' => $attributes['orderInfo'],
104
                    'returnUrl' => $attributes['returnUrl'],
105
                    'notifyUrl' => $attributes['notifyUrl'],
106
                    'extraData' => $attributes['extraData'],
107
                ];
108
                break;
109
            case PaymentGateway::RC_QUERY_DR:
110
            case PaymentGateway::RC_QUERY_REFUND:
111
                $dataSign = [
112
                    'partnerCode' => $attributes['partnerCode'],
113
                    'accessKey' => $attributes['accessKey'],
114
                    'requestId' => $attributes['requestId'],
115
                    'orderId' => $attributes['orderId'],
116
                    'requestType' => $this->getRequestType()
117
                ];
118
                break;
119
            case PaymentGateway::RC_REFUND:
120
                $dataSign = [
121
                    'partnerCode' => $attributes['partnerCode'],
122
                    'accessKey' => $attributes['accessKey'],
123
                    'requestId' => $attributes['requestId'],
124
                    'amount' => $attributes['amount'],
125
                    'orderId' => $attributes['orderId'],
126
                    'transId' => $attributes['transId'],
127
                    'requestType' => $this->getRequestType()
128
                ];
129
                break;
130
            default:
131
                throw new NotSupportedException("Not supported command: `$command`");
132
        }
133
134
        $dataSign = http_build_query($dataSign);
135
        return $this->getClient()->signature($dataSign);
136
    }
137
}
138