Completed
Push — master ( eccc2c...9c0ef3 )
by Vuong
01:43
created

PaymentGateway::getHttpClientConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 13
loc 13
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
crap 1
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\onepay;
9
10
use Yii;
11
12
use yii\httpclient\Client as HttpClient;
13
14
use yiiviet\payment\BasePaymentGateway;
15
use yiiviet\payment\VerifiedRequestEvent;
16
17
use vxm\gatewayclients\DataInterface;
18
use vxm\gatewayclients\RequestEvent;
19
20
21
/**
22
 * Lớp PaymentGateway thực thi các phương thức trừu tượng dùng hổ trợ kết nối đến OnePay.
23
 * Hiện tại nó hổ trợ 100% các tính năng từ cổng thanh toán OnePay v2.
24
 *
25
 * @method PaymentClient getClient($id = null)
26
 * @method PaymentClient getDefaultClient()
27
 *
28
 * @property PaymentClient $client
29
 * @property PaymentClient $defaultClient
30
 *
31
 * @author Vuong Minh <[email protected]>
32
 * @since 1.0
33
 */
34
class PaymentGateway extends BasePaymentGateway
35
{
36
    /**
37
     * Đường dẫn API của thanh toán nội địa.
38
     */
39
    const PURCHASE_DOMESTIC_URL = '/onecomm-pay/vpc.op';
40
41
    /**
42
     * Đường dẫn API để truy vấn thông tin giao dịch nội địa.
43
     */
44
    const QUERY_DR_DOMESTIC_URL = '/onecomm-pay/Vpcdps.op';
45
46
    /**
47
     * Đường dẫn API của thanh toán quốc tế.
48
     */
49
    const PURCHASE_INTERNATIONAL_URL = '/vpcpay/vpcpay.op';
50
51
    /**
52
     * Đường dẫn API để truy vấn thông tin giao dịch quốc tế.
53
     */
54
    const QUERY_DR_INTERNATIONAL_URL = '/vpcpay/Vpcdps.op';
55
56
    /**
57
     * Id của client trong môi trường thử nghiệm dùng để giao tiếp với OnePay ở cổng quốc tế.
58
     */
59
    const ID_CLIENT_SANDBOX_INTERNATIONAL = '__sandboxInternational';
60
61
    /**
62
     * Id của client trong môi trường thử nghiệm dùng để giao tiếp với OnePay ở cổng nội địa.
63
     */
64
    const ID_CLIENT_SANDBOX_DOMESTIC = '__sandboxDomestic';
65
66
    /**
67
     * @var bool Optional to use international gateway. Set to TRUE if you want use methods (requests, verifies) with international mode.
68
     */
69
    public $international = false;
70
71
    /**
72
     * @inheritdoc
73
     */
74
    public $clientConfig = ['class' => PaymentClient::class];
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public $requestDataConfig = ['class' => RequestData::class];
80
81
    /**
82
     * @inheritdoc
83
     */
84
    public $responseDataConfig = ['class' => ResponseData::class];
85
86
    /**
87
     * @inheritdoc
88
     */
89
    public $verifiedDataConfig = ['class' => VerifiedData::class];
90
91
    /**
92
     * @inheritdoc
93
     */
94 4
    public function getBaseUrl(): string
95
    {
96 4
        return $this->sandbox ? 'https://mtf.onepay.vn' : 'https://onepay.vn';
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102 4
    protected function defaultVersion(): string
103
    {
104 4
        return '2';
105
    }
106
107
    /**
108
     * @inheritdoc
109
     * @throws \yii\base\InvalidConfigException
110
     */
111 9
    protected function initSandboxEnvironment()
112
    {
113 9
        $clientDomesticConfig = require(__DIR__ . '/sandbox-client-domestic.php');
114 9
        $clientInternationalConfig = require(__DIR__ . '/sandbox-client-international.php');
115
116 9
        $this->setClient(static::ID_CLIENT_SANDBOX_DOMESTIC, $clientDomesticConfig);
117 9
        $this->setClient(static::ID_CLIENT_SANDBOX_INTERNATIONAL, $clientInternationalConfig);
118 9
    }
119
120
    /**
121
     * @inheritdoc
122
     */
123 4 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...
124
    {
125
        return [
126 4
            'class' => HttpClient::class,
127 4
            'transport' => 'yii\httpclient\CurlTransport',
128
            'requestConfig' => [
129
                'options' => [
130 4
                    CURLOPT_SSL_VERIFYHOST => false,
131 4
                    CURLOPT_SSL_VERIFYPEER => false
132
                ]
133
            ]
134
        ];
135
    }
136
137
    /**
138
     * @return ResponseData|DataInterface
139
     * @inheritdoc
140
     */
141 2 View Code Duplication
    public function purchase(array $data, $clientId = null): DataInterface
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...
142
    {
143 2
        if ($this->sandbox && $clientId === null) {
144 2
            $clientId = $this->international ? self::ID_CLIENT_SANDBOX_INTERNATIONAL : self::ID_CLIENT_SANDBOX_DOMESTIC;
145
        }
146
147 2
        return parent::purchase($data, $clientId);
148
    }
149
150
    /**
151
     * @return ResponseData|DataInterface
152
     * @inheritdoc
153
     */
154 2 View Code Duplication
    public function queryDR(array $data, $clientId = null): DataInterface
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...
155
    {
156 2
        if ($this->sandbox && $clientId === null) {
157 2
            $clientId = $this->international ? self::ID_CLIENT_SANDBOX_INTERNATIONAL : self::ID_CLIENT_SANDBOX_DOMESTIC;
158
        }
159
160 2
        return parent::queryDR($data, $clientId);
161
    }
162
163
    /**
164
     * @return bool|VerifiedData
165
     * @inheritdoc
166
     */
167 2 View Code Duplication
    public function verifyRequestIPN($clientId = null, \yii\web\Request $request = null)
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...
168
    {
169 2
        if ($this->sandbox && $clientId === null) {
170 2
            $clientId = $this->international ? self::ID_CLIENT_SANDBOX_INTERNATIONAL : self::ID_CLIENT_SANDBOX_DOMESTIC;
171
        }
172
173 2
        return parent::verifyRequestIPN($clientId, $request);
174
    }
175
176
    /**
177
     * @return bool|VerifiedData
178
     * @inheritdoc
179
     */
180 2 View Code Duplication
    public function verifyRequestPurchaseSuccess($clientId = null, \yii\web\Request $request = null)
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...
181
    {
182 2
        if ($this->sandbox && $clientId === null) {
183 2
            $clientId = $this->international ? self::ID_CLIENT_SANDBOX_INTERNATIONAL : self::ID_CLIENT_SANDBOX_DOMESTIC;
184
        }
185
186 2
        return parent::verifyRequestPurchaseSuccess($clientId, $request);
187
    }
188
189
190
    /**
191
     * @inheritdoc
192
     * @throws \yii\base\InvalidConfigException|\yii\base\NotSupportedException
193
     */
194 4
    protected function requestInternal(\vxm\gatewayclients\RequestData $requestData, \yii\httpclient\Client $httpClient): array
195
    {
196 4
        $command = $requestData->getCommand();
197
        $commandUrls = [
198 4
            self::RC_PURCHASE => $this->international ? self::PURCHASE_INTERNATIONAL_URL : self::PURCHASE_DOMESTIC_URL,
199 4
            self::RC_QUERY_DR => $this->international ? self::QUERY_DR_INTERNATIONAL_URL : self::QUERY_DR_DOMESTIC_URL,
200
        ];
201
202 4
        $data = $requestData->get();
203 4
        $data[0] = $commandUrls[$command];
204
205 4
        if ($command === self::RC_PURCHASE) {
206 2
            return ['redirect_url' => $httpClient->createRequest()->setUrl($data)->getFullUrl()];
207
        } else {
208 2
            return $httpClient->get($data)->send()->getData();
209
        }
210
    }
211
212
    /**
213
     * @inheritdoc
214
     */
215 4
    protected function getVerifyRequestData($command, \yii\web\Request $request): array
216
    {
217
        $params = [
218 4
            'vpc_Command', 'vpc_Locale', 'vpc_MerchTxnRef', 'vpc_Merchant', 'vpc_OrderInfo', 'vpc_Amount',
219
            'vpc_TxnResponseCode', 'vpc_TransactionNo', 'vcp_Message', 'vpc_SecureHash', 'vpc_AcqResponseCode',
220
            'vpc_Authorizeld', 'vpc_Card', 'vpc_3DSECI', 'vpc_3Dsenrolled', 'vpc_3Dsstatus', 'vpc_CommercialCard'
221
        ];
222
223 4
        $data = [];
224
225 4 View Code Duplication
        foreach ($params as $param) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
226 4
            if (($value = $request->get($param)) !== null) {
227 4
                $data[$param] = $value;
228
            }
229
        }
230
231 4
        return $data;
232
    }
233
234
}
235