PaymentGateway::getBaseUrl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
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
namespace yiiviet\payment\momo;
9
10
use yii\httpclient\Client as HttpClient;
11
12
use yiiviet\payment\BasePaymentGateway;
13
14
/**
15
 * 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.
16
 * 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.
17
 *
18
 * @method ResponseData purchase(array $data, $clientId = null)
19
 * @method ResponseData queryDR(array $data, $clientId = null)
20
 * @method ResponseData refund(array $data, $clientId = null)
21
 * @method ResponseData queryRefund(array $data, $clientId = null)
22
 * @method bool|VerifiedData verifyRequestIPN($clientId = null, \yii\web\Request $request = null)
23
 * @method bool|VerifiedData verifyRequestPurchaseSuccess($clientId = null, \yii\web\Request $request = 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.3
32
 */
33
class PaymentGateway extends BasePaymentGateway
34
{
35
    /**
36
     * @inheritdoc
37
     */
38
    public $clientConfig = ['class' => PaymentClient::class];
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public $requestDataConfig = ['class' => RequestData::class];
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public $responseDataConfig = ['class' => ResponseData::class];
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public $verifiedDataConfig = ['class' => VerifiedData::class];
54
55
    /**
56
     * @inheritdoc
57
     */
58 8
    public function getBaseUrl(): string
59
    {
60 8
        return $this->sandbox ? 'https://test-payment.momo.vn/gw_payment/transactionProcessor' : 'https://payment.momo.vn/gw_payment/transactionProcessor';
61
    }
62
63
    /**
64
     * @inheritdoc
65
     * @throws \yii\base\InvalidConfigException
66
     */
67 11
    protected function initSandboxEnvironment()
68
    {
69 11
        $clientConfig = require(__DIR__ . '/sandbox-client.php');
70 11
        $this->setClient($clientConfig);
71 11
    }
72
73
    /**
74
     * @inheritdoc
75
     * @throws \yii\base\InvalidConfigException
76
     * @throws \yii\httpclient\Exception
77
     */
78 8
    protected function requestInternal(\vxm\gatewayclients\RequestData $requestData, HttpClient $httpClient): array
79
    {
80 8
        $data = $requestData->get();
81
82 4
        return $httpClient->post('', $data)->setFormat('json')->send()->getData();
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88 2
    protected function getVerifyRequestData($command, \yii\web\Request $request): array
89
    {
90
        $params = [
91 2
            'partnerCode', 'accessKey', 'requestId', 'amount', 'orderId', 'orderInfo', 'orderType',
92
            'transId', 'message', 'localMessage', 'responseTime', 'errorCode', 'payType', 'extraData', 'signature'
93
        ];
94 2
        $commandRequestMethods = [self::VRC_PURCHASE_SUCCESS => 'get', self::VRC_IPN => 'post'];
95 2
        $requestMethod = $commandRequestMethods[$command];
96
97 2
        $data = [];
98 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...
99 2
            if (($value = call_user_func([$request, $requestMethod], $param)) !== null) {
100 2
                $data[$param] = $value;
101
            }
102
        }
103
104 2
        return $data;
105
    }
106
107
}
108