Completed
Push — master ( ac749f...8181ea )
by Vuong
01:54
created

PaymentGateway::verifyRequestIPN()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 4

Importance

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