Completed
Push — master ( 075950...974b7a )
by Vuong
02:25
created

PaymentGateway::afterRequest()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 15
loc 15
ccs 0
cts 14
cp 0
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 1
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
9
namespace yiiviet\payment\momo;
10
11
use GatewayClients\DataInterface;
12
13
use yii\httpclient\Client as HttpClient;
14
15
use vxm\gatewayclients\RequestData;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, yiiviet\payment\momo\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...
16
use vxm\gatewayclients\RequestEvent;
17
18
use yiiviet\payment\BasePaymentGateway;
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 MOMO.
22
 * Hiện tại nó hổ trợ 100% các tính năng từ cổng thanh toán MOMO All In One Payment.
23
 *
24
 * @method ResponseData purchase(array $data, $clientId = null)
25
 * @method ResponseData queryDR(array $data, $clientId = null)
26
 * @method bool|VerifiedData verifyRequestIPN($clientId = null, \yii\web\Request $request = null)
27
 * @method bool|VerifiedData verifyRequestPurchaseSuccess($clientId = null, \yii\web\Request $request = null)
28
 * @method PaymentClient getClient($id = null)
29
 * @method PaymentClient getDefaultClient()
30
 *
31
 * @property PaymentClient $client
32
 * @property PaymentClient $defaultClient
33
 *
34
 * @author Vuong Minh <[email protected]>
35
 * @since 1.0.3
36
 */
37
class PaymentGateway extends BasePaymentGateway
38
{
39
    /**
40
     * Lệnh `refund` sử dụng cho việc tạo [[request()]] yêu cầu hoàn trả tiền.
41
     */
42
    const RC_REFUND = 'refund';
43
44
    /**
45
     * Lệnh `queryRefund` sử dụng cho việc tạo [[request()]] để kiểm tra trang thái của lệnh `refund` đã tạo.
46
     */
47
    const RC_QUERY_REFUND = 'queryRefund';
48
49
    /**
50
     * @event RequestEvent được gọi trước khi khởi tạo lệnh [[RC_REFUND]] ở phương thức [[request()]].
51
     */
52
    const EVENT_BEFORE_REFUND = 'beforeRefund';
53
54
    /**
55
     * @event RequestEvent được gọi sau khi khởi tạo lệnh [[RC_REFUND]] ở phương thức [[request()]].
56
     */
57
    const EVENT_AFTER_REFUND = 'afterRefund';
58
59
    /**
60
     * @event RequestEvent được gọi trước khi khởi tạo lệnh [[RC_QUERY_REFUND]] ở phương thức [[request()]].
61
     */
62
    const EVENT_BEFORE_QUERY_REFUND = 'beforeQueryRefund';
63
64
    /**
65
     * @event RequestEvent được gọi sau khi khởi tạo lệnh [[RC_QUERY_REFUND]] ở phương thức [[request()]].
66
     */
67
    const EVENT_AFTER_QUERY_REFUND = 'afterQueryRefund';
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
    public function getBaseUrl(): string
93
    {
94
        return $this->sandbox ? 'https://test-payment.momo.vn/gw_payment/transactionProcessor' : 'https://payment.momo.vn/gw_payment/transactionProcessor';
95
    }
96
97
    /**
98
     * Phương thức yêu cầu MOMO hoàn trả tiền cho đơn hàng chỉ định.
99
     * Đây là phương thức ánh xạ của [[request()]] sử dụng lệnh [[RC_REFUND]].
100
     *
101
     * @param array $data Dữ liệu yêu cầu hoàn trả.
102
     * @param null $clientId Client id sử dụng để tạo yêu cầu.
103
     * Nếu không thiết lập [[getDefaultClient()]] sẽ được gọi để xác định client.
104
     * @return ResponseData|DataInterface Trả về [[DataInterface]] là dữ liệu tổng hợp từ MOMO phản hồi.
105
     * @throws \ReflectionException|\yii\base\InvalidConfigException|\yii\base\InvalidArgumentException
106
     */
107
    public function refund(array $data, $clientId = null): DataInterface
108
    {
109
        return $this->request(self::RC_REFUND, $data, $clientId);
110
    }
111
112
    /**
113
     * Phương thức truy vấn thông tin của lệnh hoàn tiền tại MOMO.
114
     * Đây là phương thức ánh xạ của [[request()]] sử dụng lệnh [[RC_QUERY_REFUND]].
115
     *
116
     * @param array $data Dữ liệu trạng thái hoàn tiền.
117
     * @param null $clientId Client id sử dụng để tạo yêu cầu truy vấn trạng thái.
118
     * Nếu không thiết lập [[getDefaultClient()]] sẽ được gọi để xác định client.
119
     * @return ResponseData|DataInterface Trả về [[DataInterface]] là dữ liệu tổng hợp từ MOMO phản hồi.
120
     * @throws \ReflectionException|\yii\base\InvalidConfigException|\yii\base\InvalidArgumentException
121
     */
122
    public function queryRefund(array $data, $clientId = null): DataInterface
123
    {
124
        return $this->request(self::RC_QUERY_REFUND, $data, $clientId);
125
    }
126
127
    /**
128
     * @inheritdoc
129
     */
130 View Code Duplication
    public function beforeRequest(RequestEvent $event)
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...
131
    {
132
        switch ($event->command) {
133
            case self::RC_REFUND:
134
                $this->trigger(self::EVENT_BEFORE_REFUND, $event);
135
                break;
136
            case self::RC_QUERY_REFUND:
137
                $this->trigger(self::EVENT_BEFORE_QUERY_REFUND, $event);
138
                break;
139
            default:
140
                break;
141
        }
142
143
        parent::beforeRequest($event);
144
    }
145
146
    /**
147
     * @inheritdoc
148
     */
149 View Code Duplication
    public function afterRequest(RequestEvent $event)
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...
150
    {
151
        switch ($event->command) {
152
            case self::RC_REFUND:
153
                $this->trigger(self::EVENT_AFTER_REFUND, $event);
154
                break;
155
            case self::RC_QUERY_REFUND:
156
                $this->trigger(self::EVENT_AFTER_QUERY_REFUND, $event);
157
                break;
158
            default:
159
                break;
160
        }
161
162
        parent::afterRequest($event);
163
    }
164
165
    /**
166
     * @inheritdoc
167
     * @throws \yii\base\InvalidConfigException
168
     */
169
    protected function initSandboxEnvironment()
170
    {
171
        $clientConfig = require(__DIR__ . '/sandbox-client.php');
172
        $this->setClient($clientConfig);
173
    }
174
175
    /**
176
     * @inheritdoc
177
     * @throws \yii\base\InvalidConfigException
178
     * @throws \yii\httpclient\Exception
179
     */
180
    protected function requestInternal(RequestData $requestData, HttpClient $httpClient): array
181
    {
182
        $data = $requestData->get();
183
184
        return $this->getHttpClient()->post('', $data)->send()->getData();
185
    }
186
187
    protected function getVerifyRequestData($command, \yii\web\Request $request): array
188
    {
189
        // TODO: Implement getVerifyRequestData() method.
190
    }
191
192
}
193