Completed
Push — master ( 4f7c7c...fdaf8f )
by Vuong
02:10
created

PaymentGateway::getVerifyRequestData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 5
Ratio 31.25 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 5
loc 16
ccs 0
cts 7
cp 0
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
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
/**
13
 * 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.
14
 * Hiện tại nó hổ trợ 100% các tính năng từ cổng thanh toán VnPayment v2.
15
 *
16
 * @method ResponseData purchase(array $data, $clientId = null)
17
 * @method ResponseData queryDR(array $data, $clientId = null)
18
 * @method ResponseData refund(array $data, $clientId = null)
19
 * @method VerifiedData verifyRequestIPN(\yii\web\Request $request = null, $clientId = null)
20
 * @method VerifiedData verifyRequestPurchaseSuccess(\yii\web\Request $request = null, $clientId = null)
21
 * @method PaymentClient getClient($id = null)
22
 * @method PaymentClient getDefaultClient()
23
 *
24
 * @property PaymentClient $client
25
 * @property PaymentClient $defaultClient
26
 *
27
 * @author Vuong Minh <[email protected]>
28
 * @since 1.0
29
 */
30
class PaymentGateway extends BasePaymentGateway
31
{
32
33
    /**
34
     * Đường dẫn API để yêu cầu tạo giao dịch thanh toán.
35
     */
36
    const PURCHASE_URL = '/paymentv2/vpcpay.html';
37
38
    /**
39
     * Đường dẫn API để truy vấn thông tin giao dịch.
40
     */
41
    const QUERY_DR_URL = '/merchant_webapi/merchant.html';
42
43
    /**
44
     * Đường dẫn API để yêu cầu hoàn trả tiền.
45
     */
46
    const REFUND_URL = '/merchant_webapi/merchant.html';
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public $clientConfig = ['class' => PaymentClient::class];
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public $requestDataConfig = ['class' => RequestData::class];
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public $responseDataConfig = ['class' => ResponseData::class];
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public $verifiedDataConfig = ['class' => VerifiedData::class];
67
68
    /**
69
     * @inheritdoc
70
     */
71 3
    public function getBaseUrl(): string
72
    {
73 3
        return $this->sandbox ? 'http://sandbox.vnpayment.vn' : 'http://vnpayment.vn';
74
    }
75
76
    /**
77
     * @inheritdoc
78
     * @since 1.0.3
79
     */
80 3
    public function requestCommands(): array
81
    {
82 3
        return [self::RC_PURCHASE, self::RC_QUERY_DR, self::RC_REFUND];
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88 3
    protected function defaultVersion(): string
89
    {
90 3
        return '2.0.0';
91
    }
92
93
    /**
94
     * @inheritdoc
95
     * @throws \yii\base\InvalidConfigException
96
     */
97 7
    protected function initSandboxEnvironment()
98
    {
99 7
        $clientConfig = require(__DIR__ . '/sandbox-client.php');
100
101 7
        $this->setClient($clientConfig);
102 7
    }
103
104
    /**
105
     * @inheritdoc
106
     * @throws \yii\base\InvalidConfigException|\yii\httpclient\Exception
107
     */
108 3
    protected function requestInternal(\vxm\gatewayclients\RequestData $requestData, \yii\httpclient\Client $httpClient): array
109
    {
110 3
        $command = $requestData->getCommand();
111
        $commandUrls = [
112 3
            self::RC_PURCHASE => self::PURCHASE_URL,
113 3
            self::RC_REFUND => self::REFUND_URL,
114 3
            self::RC_QUERY_DR => self::QUERY_DR_URL
115
        ];
116 3
        $data = $requestData->get();
117 3
        $data[0] = $commandUrls[$command];
118
119 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...
120 1
            return ['redirect_url' => $httpClient->createRequest()->setUrl($data)->getFullUrl()];
121
        } else {
122 2
            return $httpClient->get($data)->send()->getData();
123
        }
124
    }
125
126
    /**
127
     * @inheritdoc
128
     */
129
    protected function getVerifyRequestData($command, \yii\web\Request $request): array
130
    {
131
        $params = [
132
            'vnp_TmnCode', 'vnp_Amount', 'vnp_BankCode', 'vnp_BankTranNo', 'vnp_CardType', 'vnp_PayDate', 'vnp_CurrCode',
133
            'vnp_OrderInfo', 'vnp_TransactionNo', 'vnp_ResponseCode', 'vnp_TxnRef', 'vnp_SecureHashType', 'vnp_SecureHash'
134
        ];
135
136
        $data = [];
137 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...
138
            if (($value = $request->get($param)) !== null) {
139
                $data[$param] = $value;
140
            }
141
        }
142
143
        return $data;
144
    }
145
146
}
147