Completed
Push — master ( 02e31a...ea780f )
by Vuong
06:21
created

PaymentGateway::getHttpClientConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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