Completed
Push — master ( 1be858...1c2c45 )
by Vuong
02:00
created

PaymentGateway::initSandboxEnvironment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
9
namespace yiiviet\payment\vtcpay;
10
11
use GatewayClients\DataInterface;
12
13
use yii\base\NotSupportedException;
14
use yii\httpclient\Client as HttpClient;
15
16
use yiiviet\payment\BasePaymentGateway;
17
18
use vxm\gatewayclients\RequestData;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, yiiviet\payment\vtcpay\RequestData.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
19
20
/**
21
 * 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 VTCPay.
22
 * Hiện tại nó hổ trợ 100% các tính năng từ cổng thanh toán VTCPay.
23
 *
24
 * @method ResponseData purchase(array $data, $clientId = null)
25
 * @method VerifiedData verifyRequestIPN(\yii\web\Request $request = null, $clientId = null)
26
 * @method VerifiedData verifyRequestPurchaseSuccess(\yii\web\Request $request = null, $clientId = null)
27
 * @method PaymentClient getClient($id = null)
28
 * @method PaymentClient getDefaultClient()
29
 *
30
 * @author Vuong Minh <[email protected]>
31
 * @since 1.0.2
32
 */
33
class PaymentGateway extends BasePaymentGateway
34
{
35
36
    /**
37
     * Đường dẫn API để yêu cầu tạo giao dịch thanh toán.
38
     */
39
    const PURCHASE_URL = '/checkout.html';
40
41
    /**
42
     * Phương thức thanh toán bằng ví điện tử VTCPay.
43
     */
44
    const PAYMENT_METHOD_VTCPAY = 'VTCPay';
45
46
    /**
47
     * Phương thức thanh toán bằng ngân hàng trong nước.
48
     */
49
    const PAYMENT_METHOD_DOMESTIC_BANK = 'DomesticBank';
50
51
    /**
52
     * Phương thức thanh toán bằng thẻ quốc tế (visa/master).
53
     */
54
    const PAYMENT_METHOD_INTERNATIONAL_CARD = 'InternationalCard';
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function getBaseUrl(): string
60
    {
61
        return $this->sandbox ? 'http://alpha1.vtcpay.vn/portalgateway' : 'https://vtcpay.vn/bank-gateway';
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67
    public function requestCommands(): array
68
    {
69
        return [self::RC_PURCHASE];
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75
    protected function initSandboxEnvironment()
76
    {
77
        $clientConfig = require(__DIR__ . '/sandbox-client.php');
78
        $this->setClient($clientConfig);
79
    }
80
81
    /**
82
     * @inheritdoc
83
     * @throws NotSupportedException
84
     */
85
    public function queryDR(array $data, $clientId = null): DataInterface
86
    {
87
        throw new NotSupportedException('queryDR doesnt\'t supported in VTCPay!');
88
    }
89
90
    /**
91
     * @inheritdoc
92
     * @throws \yii\base\InvalidConfigException
93
     */
94
    protected function requestInternal(RequestData $requestData, HttpClient $httpClient): array
95
    {
96
        $data = $requestData->get();
97
        $data[0] = self::PURCHASE_URL;
98
99
        return ['redirect_url' => $httpClient->createRequest()->setUrl($data)->getFullUrl()];
100
    }
101
102
    /**
103
     * @param int|string $command
104
     * @param \yii\web\Request $request
105
     * @return array
106
     */
107
    protected function getVerifyRequestData($command, \yii\web\Request $request): array
108
    {
109
        if ($command === self::VRC_IPN) {
110
            $params = ['data', 'signature'];
111
            $requestMethod = 'post';
112
        } else {
113
            $params = ['amount', 'message', 'payment_type', 'reference_number', 'status', 'trans_ref_no', 'website_id', 'signature'];
114
            $requestMethod = 'get';
115
        }
116
117
        $data = [];
118
119 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...
120
            if (($value = call_user_func([$request, $requestMethod], $param)) !== null) {
121
                $data[$param] = $value;
122
            }
123
        }
124
125
        return $data;
126
    }
127
128
}
129