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

PaymentGateway::getVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\vnpayment;
9
10
use yiiviet\payment\BasePaymentGateway;
11
12
use vxm\gatewayclients\DataInterface;
13
use vxm\gatewayclients\RequestEvent;
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 VnPayment v2.
18
 *
19
 * @method ResponseData purchase(array $data, $clientId = null)
20
 * @method ResponseData queryDR(array $data, $clientId = null)
21
 * @method VerifiedData verifyRequestIPN(\yii\web\Request $request = null, $clientId = null)
22
 * @method VerifiedData verifyRequestPurchaseSuccess(\yii\web\Request $request = null, $clientId = 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
     * Lệnh `refund` sử dụng cho việc tạo [[request()]] yêu cầu hoàn trả tiền.
36
     */
37
    const RC_REFUND = 'refund';
38
39
    /**
40
     * @event RequestEvent được gọi trước khi khởi tạo lệnh [[RC_REFUND]] ở phương thức [[request()]].
41
     */
42
    const EVENT_BEFORE_REFUND = 'beforeRefund';
43
44
    /**
45
     * @event RequestEvent được gọi sau khi khởi tạo lệnh [[RC_REFUND]] ở phương thức [[request()]].
46
     */
47
    const EVENT_AFTER_REFUND = 'afterRefund';
48
49
    /**
50
     * Đường dẫn API để yêu cầu tạo giao dịch thanh toán.
51
     */
52
    const PURCHASE_URL = '/paymentv2/vpcpay.html';
53
54
    /**
55
     * Đường dẫn API để truy vấn thông tin giao dịch.
56
     */
57
    const QUERY_DR_URL = '/merchant_webapi/merchant.html';
58
59
    /**
60
     * Đường dẫn API để yêu cầu hoàn trả tiền.
61
     */
62
    const REFUND_URL = '/merchant_webapi/merchant.html';
63
64
    /**
65
     * @inheritdoc
66
     */
67
    public $clientConfig = ['class' => PaymentClient::class];
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public $requestDataConfig = ['class' => RequestData::class];
73
74
    /**
75
     * @inheritdoc
76
     */
77
    public $responseDataConfig = ['class' => ResponseData::class];
78
79
    /**
80
     * @inheritdoc
81
     */
82
    public $verifiedDataConfig = ['class' => VerifiedData::class];
83
84
    /**
85
     * @inheritdoc
86
     */
87 3
    public function getBaseUrl(): string
88
    {
89 3
        return $this->sandbox ? 'http://sandbox.vnpayment.vn' : 'http://vnpayment.vn';
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 3
    protected function defaultVersion(): string
96
    {
97 3
        return '2.0.0';
98
    }
99
100
    /**
101
     * Phương thức yêu cầu VnPayment hoàn trả tiền cho đơn hàng chỉ định.
102
     * Đây là phương thức ánh xạ của [[request()]] sử dụng lệnh [[RC_REFUND]].
103
     *
104
     * @param array $data Dữ liệu yêu cầu hoàn trả.
105
     * @param null $clientId Client id sử dụng để tạo yêu cầu.
106
     * Nếu không thiết lập [[getDefaultClient()]] sẽ được gọi để xác định client.
107
     * @return ResponseData|DataInterface Trả về [[DataInterface]] là dữ liệu tổng hợp từ VnPayment phản hồi.
108
     * @throws \ReflectionException|\yii\base\InvalidConfigException|\yii\base\InvalidArgumentException
109
     */
110 1
    public function refund(array $data, $clientId = null): DataInterface
111
    {
112 1
        return $this->request(self::RC_REFUND, $data, $clientId);
113
    }
114
115
    /**
116
     * @inheritdoc
117
     */
118 3
    public function beforeRequest(RequestEvent $event)
119
    {
120 3
        if ($event->command === self::RC_REFUND) {
121 1
            $this->trigger(self::EVENT_BEFORE_REFUND, $event);
122
        }
123
124 3
        parent::beforeRequest($event);
125 3
    }
126
127
    /**
128
     * @inheritdoc
129
     */
130 3
    public function afterRequest(RequestEvent $event)
131
    {
132 3
        if ($event->command === self::RC_REFUND) {
133 1
            $this->trigger(self::EVENT_AFTER_REFUND, $event);
134
        }
135
136 3
        parent::afterRequest($event);
137 3
    }
138
139
    /**
140
     * @inheritdoc
141
     * @throws \yii\base\InvalidConfigException
142
     */
143 4
    protected function initSandboxEnvironment()
144
    {
145 4
        $clientConfig = require(__DIR__ . '/sandbox-client.php');
146
147 4
        $this->setClient($clientConfig);
148 4
    }
149
150
    /**
151
     * @inheritdoc
152
     * @throws \yii\base\InvalidConfigException
153
     */
154 3
    protected function requestInternal(\vxm\gatewayclients\RequestData $requestData, \yii\httpclient\Client $httpClient): array
155
    {
156 3
        $command = $requestData->getCommand();
157
        $commandUrls = [
158 3
            self::RC_PURCHASE => self::PURCHASE_URL,
159 3
            self::RC_REFUND => self::REFUND_URL,
160 3
            self::RC_QUERY_DR => self::QUERY_DR_URL
161
        ];
162 3
        $data = $requestData->get();
163 3
        $data[0] = $commandUrls[$command];
164
165 3 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...
166 1
            return ['redirect_url' => $httpClient->createRequest()->setUrl($data)->getFullUrl()];
167
        } else {
168 2
            return $httpClient->get($data)->send()->getData();
169
        }
170
    }
171
172
    /**
173
     * @inheritdoc
174
     */
175
    protected function getVerifyRequestData($command, \yii\web\Request $request): array
176
    {
177
        $params = [
178
            'vnp_TmnCode', 'vnp_Amount', 'vnp_BankCode', 'vnp_BankTranNo', 'vnp_CardType', 'vnp_PayDate', 'vnp_CurrCode',
179
            'vnp_OrderInfo', 'vnp_TransactionNo', 'vnp_ResponseCode', 'vnp_TxnRef', 'vnp_SecureHashType', 'vnp_SecureHash'
180
        ];
181
182
        $data = [];
183 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...
184
            if (($value = $request->get($param)) !== null) {
185
                $data[$param] = $value;
186
            }
187
        }
188
189
        return $data;
190
    }
191
192
    /**
193
     * @inheritdoc
194
     */
195 3 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...
196
    {
197
        return [
198 3
            'transport' => 'yii\httpclient\CurlTransport',
199
            'requestConfig' => [
200
                'options' => [
201 3
                    CURLOPT_SSL_VERIFYHOST => false,
202 3
                    CURLOPT_SSL_VERIFYPEER => false
203
                ]
204
            ]
205
        ];
206
    }
207
}
208