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

PaymentGateway::beforeRequest()   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 yii\httpclient\Client as HttpClient;
12
13
use yiiviet\payment\BasePaymentGateway;
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 MOMO.
17
 * 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.
18
 *
19
 * @method ResponseData purchase(array $data, $clientId = null)
20
 * @method ResponseData queryDR(array $data, $clientId = null)
21
 * @method ResponseData refund(array $data, $clientId = null)
22
 * @method ResponseData queryRefund(array $data, $clientId = null)
23
 * @method bool|VerifiedData verifyRequestIPN($clientId = null, \yii\web\Request $request = null)
24
 * @method bool|VerifiedData verifyRequestPurchaseSuccess($clientId = null, \yii\web\Request $request = null)
25
 * @method PaymentClient getClient($id = null)
26
 * @method PaymentClient getDefaultClient()
27
 *
28
 * @property PaymentClient $client
29
 * @property PaymentClient $defaultClient
30
 *
31
 * @author Vuong Minh <[email protected]>
32
 * @since 1.0.3
33
 */
34
class PaymentGateway extends BasePaymentGateway
35
{
36
    /**
37
     * Dùng để khai báo lệnh khi tạo request [[RC_PURCHASE]].
38
     */
39
    const REQUEST_TYPE_PURCHASE = 'captureMoMoWallet';
40
41
    /**
42
     * Dùng để khai báo lệnh khi tạo request [[RC_REFUND]].
43
     */
44
    const REQUEST_TYPE_REFUND = 'refundMoMoWallet';
45
46
    /**
47
     * Dùng để khai báo lệnh khi tạo request [[RC_QUERY_DR]].
48
     */
49
    const REQUEST_TYPE_QUERY_DR = 'transactionStatus';
50
51
    /**
52
     * Dùng để khai báo lệnh khi tạo request [[RC_QUERY_REFUND]].
53
     */
54
    const REQUEST_TYPE_QUERY_REFUND = 'refundStatus';
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public $clientConfig = ['class' => PaymentClient::class];
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public $requestDataConfig = ['class' => RequestData::class];
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public $responseDataConfig = ['class' => ResponseData::class];
70
71
    /**
72
     * @inheritdoc
73
     */
74
    public $verifiedDataConfig = ['class' => VerifiedData::class];
75
76
    /**
77
     * @inheritdoc
78
     */
79 8
    public function getBaseUrl(): string
80
    {
81 8
        return $this->sandbox ? 'https://test-payment.momo.vn/gw_payment/transactionProcessor' : 'https://payment.momo.vn/gw_payment/transactionProcessor';
82
    }
83
84
    /**
85
     * @inheritdoc
86
     * @throws \yii\base\InvalidConfigException
87
     */
88 11
    protected function initSandboxEnvironment()
89
    {
90 11
        $clientConfig = require(__DIR__ . '/sandbox-client.php');
91 11
        $this->setClient($clientConfig);
92 11
    }
93
94
    /**
95
     * @inheritdoc
96
     * @throws \yii\base\InvalidConfigException
97
     * @throws \yii\httpclient\Exception
98
     */
99 8
    protected function requestInternal(\vxm\gatewayclients\RequestData $requestData, HttpClient $httpClient): array
100
    {
101 8
        $data = $requestData->get();
102
103 4
        return $this->getHttpClient()->post('', $data)->setFormat('json')->send()->getData();
104
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109 2
    protected function getVerifyRequestData($command, \yii\web\Request $request): array
110
    {
111
        $params = [
112 2
            'partnerCode', 'accessKey', 'requestId', 'amount', 'orderId', 'orderInfo', 'orderType',
113
            'transId', 'message', 'localMessage', 'responseTime', 'errorCode', 'payType', 'extraData', 'signature'
114
        ];
115 2
        $commandRequestMethods = [self::VRC_PURCHASE_SUCCESS => 'get', self::VRC_IPN => 'post'];
116 2
        $requestMethod = $commandRequestMethods[$command];
117
118 2
        $data = [];
119 2 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 2
            if (($value = call_user_func([$request, $requestMethod], $param)) !== null) {
121 2
                $data[$param] = $value;
122
            }
123
        }
124
125 2
        return $data;
126
    }
127
128
}
129