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