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

PaymentGateway::authenticate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
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 yii\base\NotSupportedException;
11
12
use yiiviet\payment\BasePaymentGateway;
13
14
use vxm\gatewayclients\RequestEvent;
15
16
/**
17
 * Lớp PaymentGateway hổ trợ việc kết nối đến Ngân Lượng.
18
 * Hiện tại nó hỗ trợ 100% tính năng của Ngân Lượng v3.1
19
 *
20
 * @method ResponseData purchase(array $data, $clientId = null)
21
 * @method ResponseData queryDR(array $data, $clientId = null)
22
 * @method bool|VerifiedData verifyRequestPurchaseSuccess($clientId = null, \yii\web\Request $request = null)
23
 *
24
 * @property PaymentClient $client
25
 * @property PaymentClient $defaultClient
26
 *
27
 * @author Vuong Minh <[email protected]>
28
 * @since 1.0
29
 */
30
class PaymentGateway extends BasePaymentGateway
31
{
32
    /**
33
     * Lệnh `authenticate` yêu cầu Ngân Lượng xác minh OTP của khách có hợp lệ hay không.
34
     * Nó được sử dụng ở version 3.2 phương thức seamless checkout.
35
     */
36
    const RC_AUTHENTICATE = 'authenticate';
37
38
    /**
39
     * @event RequestEvent được gọi trước khi thực hiện yêu cầu Ngân Lượng xác minh OTP của khách.
40
     */
41
    const EVENT_BEFORE_AUTHENTICATE = 'beforeAuthenticate';
42
43
    /**
44
     * @event RequestEvent được gọi sau khi thực hiện yêu cầu Ngân Lượng xác minh OTP của khách.
45
     */
46
    const EVENT_AFTER_AUTHENTICATE = 'afterAuthenticate';
47
48
    /**
49
     * Hằng khai báo giúp Ngân Lượng xác định phương thức thanh toán là Ngân Lượng,
50
     * khi khởi tạo lệnh [[RC_PURCHASE]] tại phương thức [[request()]].
51
     */
52
    const PAYMENT_METHOD_NL = 'NL';
53
54
    /**
55
     * Hằng khai báo giúp Ngân Lượng xác định phương thức thanh toán là QR Code,
56
     * khi khởi tạo lệnh [[RC_PURCHASE]] tại phương thức [[request()]].
57
     */
58
    const PAYMENT_METHOD_QR_CODE = 'QRCODE';
59
60
    /**
61
     * Hằng khai báo giúp Ngân Lượng xác định phương thức thanh toán là tại ngân hàng,
62
     * khi khởi tạo lệnh [[RC_PURCHASE]] tại phương thức [[request()]].
63
     */
64
    const PAYMENT_METHOD_BANK_OFFLINE = 'NH_OFFLINE';
65
66
    /**
67
     * Hằng khai báo giúp Ngân Lượng xác định phương thức thanh toán là thẻ tín dụng trả trước,
68
     * khi khởi tạo lệnh [[RC_PURCHASE]] tại phương thức [[request()]].
69
     */
70
    const PAYMENT_METHOD_CREDIT_CARD_PREPAID = 'CREDIT_CARD_PREPAID';
71
72
    /**
73
     * Hằng khai báo giúp Ngân Lượng xác định phương thức thanh toán là thẻ VISA,
74
     * khi khởi tạo lệnh [[RC_PURCHASE]] tại phương thức [[request()]].
75
     */
76
    const PAYMENT_METHOD_VISA = 'VISA';
77
78
    /**
79
     * Hằng khai báo giúp Ngân Lượng xác định phương thức thanh toán là ATM Online,
80
     * khi khởi tạo lệnh [[RC_PURCHASE]] tại phương thức [[request()]].
81
     */
82
    const PAYMENT_METHOD_ATM_ONLINE = 'ATM_ONLINE';
83
84
    /**
85
     * Hằng khai báo giúp Ngân Lượng xác định phương thức thanh toán là ATM Offline,
86
     * khi khởi tạo lệnh [[RC_PURCHASE]] tại phương thức [[request()]].
87
     */
88
    const PAYMENT_METHOD_ATM_OFFLINE = 'ATM_ONLINE';
89
90
    /**
91
     * Hằng khai báo giúp Ngân Lượng xác định phương thức thanh toán là Internet Banking,
92
     * khi khởi tạo lệnh [[RC_PURCHASE]] tại phương thức [[request()]].
93
     */
94
    const PAYMENT_METHOD_INTERNET_BANKING = 'IB_ONLINE';
95
96
    /**
97
     * Hằng khai báo giúp Ngân Lượng xác định phương thức giao dịch là trực tiếp (trực tiếp nhận tiền),
98
     * khi khởi tạo lệnh [[RC_PURCHASE]] tại phương thức [[request()]].
99
     */
100
    const PAYMENT_TYPE_REDIRECT = 1;
101
    /**
102
     * Hằng khai báo giúp Ngân Lượng xác định phương thức giao dịch là tạm giữ (tạm giữ tiền),
103
     * khi khởi tạo lệnh [[RC_PURCHASE]] tại phương thức [[request()]].
104
     */
105
    const PAYMENT_TYPE_SAFE = 2;
106
107
    /**
108
     * Hằng khai báo giúp bạn xác định trạng thái giao dịch thành công,
109
     * khi khởi tạo lệnh [[VRC_PURCHASE_SUCCESS]] hoặc [[RC_QUERY_DR]] tại phương thức [[request()]] hoặc [[verifyRequest()]].
110
     */
111
    const TRANSACTION_STATUS_SUCCESS = '00';
112
113
    /**
114
     * Hằng khai báo giúp bạn xác định trạng thái giao dịch thàng công nhưng Ngân Lượng tạm giữ,
115
     * khi khởi tạo lệnh [[VRC_PURCHASE_SUCCESS]] hoặc [[RC_QUERY_DR]] tại phương thức [[request()]] hoặc [[verifyRequest()]].
116
     */
117
    const TRANSACTION_STATUS_PENDING = '01';
118
119
    /**
120
     * Hằng khai báo giúp bạn xác định trạng thái giao dịch thất bại khách hàng không thanh toán hoặc lỗi,
121
     * khi khởi tạo lệnh [[VRC_PURCHASE_SUCCESS]] hoặc [[RC_QUERY_DR]] tại phương thức [[request()]] hoặc [[verifyRequest()]].
122
     */
123
    const TRANSACTION_STATUS_ERROR = '02';
124
125
    /**
126
     * Hằng cho biết gateway ở version 3.1
127
     */
128
    const VERSION_3_1 = '3.1';
129
130
    /**
131
     * Hằng cho biết gateway ở version 3.2
132
     */
133
    const VERSION_3_2 = '3.2';
134
135
    /**
136
     * @inheritdoc
137
     */
138
    public $clientConfig = ['class' => PaymentClient::class];
139
140
    /**
141
     * @inheritdoc
142
     */
143
    public $requestDataConfig = ['class' => RequestData::class];
144
145
    /**
146
     * @inheritdoc
147
     */
148
    public $responseDataConfig = ['class' => ResponseData::class];
149
150
    /**
151
     * @inheritdoc
152
     */
153
    public $verifiedDataConfig = ['class' => VerifiedData::class];
154
155
    /**
156
     * @inheritdoc
157
     */
158 2
    public function getBaseUrl(): string
159
    {
160 2
        return ($this->sandbox ? 'https://sandbox.nganluong.vn:8088/nl30' : 'https://www.nganluong.vn') . '/checkout.api.nganluong.post.php';
161
    }
162
163
    /**
164
     * @inheritdoc
165
     */
166
    public function getSupportedVersions(): array
167
    {
168
        return [self::VERSION_3_1, self::VERSION_3_2];
169
    }
170
171
    /**
172
     * Phương thức yêu cầu Ngân Lượng xác minh OTP.
173
     * Đây là phương thức ánh xạ của [[request()]] sử dụng lệnh [[RC_AUTHENTICATE]]. Nó chỉ được hổ trợ ở version 3.2.
174
     *
175
     * @param array $data Dữ liệu yêu cầu xác minh.
176
     * @param null $clientId Client id sử dụng để tạo yêu. Nếu không thiết lập [[getDefaultClient()]] sẽ được gọi để xác định client.
0 ignored issues
show
Bug introduced by
There is no parameter named $clientId. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
177
     * @return ResponseData|\vxm\gatewayclients\DataInterface Trả về [[ResponseData]] là dữ liệu từ Ngân Lượng phản hồi.
178
     * @throws \yii\base\InvalidConfigException|\ReflectionException
179
     */
180
    public function authenticate(array $data, $merchantId = null)
181
    {
182
        return $this->request(self::RC_AUTHENTICATE, $data, $merchantId);
183
    }
184
185
    /**
186
     * @inheritdoc
187
     */
188 2
    protected function defaultVersion(): string
189
    {
190 2
        return self::VERSION_3_1;
191
    }
192
193
    /**
194
     * @inheritdoc
195
     * @throws \yii\base\InvalidConfigException
196
     */
197 4
    protected function initSandboxEnvironment()
198
    {
199 4
        $clientConfig = require(__DIR__ . '/sandbox-client.php');
200 4
        $this->setClient($clientConfig);
201 4
    }
202
203
    /**
204
     * @inheritdoc
205
     */
206 2 View Code Duplication
    protected function getHttpClientConfig(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
207
    {
208
        return [
209 2
            'transport' => 'yii\httpclient\CurlTransport',
210
            'requestConfig' => [
211
                'options' => [
212 2
                    CURLOPT_SSL_VERIFYPEER => false,
213 2
                    CURLOPT_SSL_VERIFYHOST => false
214
                ]
215
            ]
216
        ];
217
    }
218
219
    /**
220
     * @inheritdoc
221
     * @throws NotSupportedException
222
     */
223
    public function verifyRequestIPN($clientId = null, \yii\web\Request $request = null)
224
    {
225
        throw new NotSupportedException(__METHOD__ . " doesn't supported in Ngan Luong gateway");
226
    }
227
228
    /**
229
     * @inheritdoc
230
     * @throws \yii\base\InvalidConfigException|NotSupportedException
231
     */
232 2
    protected function requestInternal(\vxm\gatewayclients\RequestData $requestData, \yii\httpclient\Client $httpClient): array
233
    {
234 2
        if ($requestData->command === self::RC_AUTHENTICATE && $this->getVersion() !== self::VERSION_3_2) {
235
            throw new NotSupportedException('Lệnh authenticate chỉ hổ trợ ở phiên bản ' . self::VERSION_3_2);
236
        }
237
238 2
        $data = $requestData->get();
239
240 2
        return $httpClient->post('', $data)->send()->getData();
241
    }
242
243
    /**
244
     * @inheritdoc
245
     */
246 1
    protected function getVerifyRequestData($command, \yii\web\Request $request): array
247
    {
248
        return [
249 1
            'token' => $request->get('token')
250
        ];
251
    }
252
253
}
254