Completed
Push — master ( 2a27d8...59535f )
by recca
01:40
created

Api::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 4
crap 2
1
<?php
2
3
namespace PayumTW\Allpay;
4
5
use Exception;
6
use DeviceType;
7
use InvoiceState;
8
use Detection\MobileDetect;
9
use Http\Message\MessageFactory;
10
use PayumTW\Allpay\Sdk\AllInOne;
11
use Payum\Core\HttpClientInterface;
12
13
class Api
14
{
15
    /**
16
     * $client.
17
     *
18
     * @var \Payum\Core\HttpClientInterface
19
     */
20
    protected $client;
21
22
    /**
23
     * MessageFactory.
24
     *
25
     * @var \Http\Message\MessageFactory
26
     */
27
    protected $messageFactory;
28
29
    /**
30
     * $options.
31
     *
32
     * @var array
33
     */
34
    protected $options = [];
35
36
    /**
37
     * $sdk.
38
     *
39
     * @var \PayumTW\Allpay\Sdk\AllInOne
40
     */
41
    protected $sdk;
42
43
    /**
44
     * @param array $options
45
     * @param \Payum\Core\HttpClientInterface $client
46
     * @param \Http\Message\MessageFactory $messageFactory
47
     * @param \PayumTW\Allpay\Sdk\AllInOne $sdk
48
     * @throws \Payum\Core\Exception\InvalidArgumentException if an option is invalid
49
     */
50 5
    public function __construct(array $options, HttpClientInterface $client, MessageFactory $messageFactory, AllInOne $sdk = null)
51
    {
52 5
        $this->options = $options;
53 5
        $this->client = $client;
54 5
        $this->messageFactory = $messageFactory;
55 5
        $this->sdk = $sdk ?: new AllInOne();
56 5
        $this->sdk->HashKey = $this->options['HashKey'];
57 5
        $this->sdk->HashIV = $this->options['HashIV'];
58 5
        $this->sdk->MerchantID = $this->options['MerchantID'];
59 5
    }
60
61
    /**
62
     * getApiEndpoint.
63
     *
64
     * @return string
65
     */
66 4
    public function getApiEndpoint($name = 'AioCheckOut')
67
    {
68
        $map = [
69 4
            'AioCheckOut' => 'https://payment.allpay.com.tw/Cashier/AioCheckOut/V2',
70
            'QueryTradeInfo' => 'https://payment.allpay.com.tw/Cashier/QueryTradeInfo/V2',
71
            'QueryPeriodCreditCardTradeInfo' => 'https://payment.allpay.com.tw/Cashier/QueryCreditCardPeriodInfo',
72
            'DoAction' => 'https://payment.allpay.com.tw/CreditDetail/DoAction',
73
            'AioChargeback' => 'https://payment.allpay.com.tw/Cashier/AioChargeback',
74
        ];
75
76 4
        if ($this->options['sandbox'] === true) {
77
            $map = [
78
                'AioCheckOut' => 'https://payment-stage.allpay.com.tw/Cashier/AioCheckOut/V2',
79
                'QueryTradeInfo' => 'https://payment-stage.allpay.com.tw/Cashier/QueryTradeInfo/V2',
80
                'QueryPeriodCreditCardTradeInfo' => 'https://payment-stage.allpay.com.tw/Cashier/QueryCreditCardPeriodInfo',
81
                'DoAction' => null,
82
                'AioChargeback' => 'https://payment-stage.allpay.com.tw/Cashier/AioChargeback',
83
            ];
84
        }
85
86 4
        return $map[$name];
87
    }
88
89
    /**
90
     * createTransaction.
91
     *
92
     * @param array $params
93
     * @return array
94
     */
95 1
    public function createTransaction(array $params)
96
    {
97 1
        $this->sdk->ServiceURL = $this->getApiEndpoint('AioCheckOut');
98 1
        $this->sdk->Send['MerchantTradeDate'] = date('Y/m/d H:i:s');
99 1
        $this->sdk->Send['DeviceSource'] = $this->isMobile() ? DeviceType::Mobile : DeviceType::PC;
100 1
        $this->sdk->Send = array_replace(
101 1
            $this->sdk->Send,
102 1
            array_intersect_key($params, $this->sdk->Send)
103
        );
104
105
        /*
106
         * 電子發票參數
107
         * $this->sdk->Send['InvoiceMark'] = InvoiceState::Yes;
108
         * $this->sdk->SendExtend['RelateNumber'] = $MerchantTradeNo;
109
         * $this->sdk->SendExtend['CustomerEmail'] = '[email protected]';
110
         * $this->sdk->SendExtend['CustomerPhone'] = '0911222333';
111
         * $this->sdk->SendExtend['TaxType'] = TaxType::Dutiable;
112
         * $this->sdk->SendExtend['CustomerAddr'] = '台北市南港區三重路19-2號5樓D棟';
113
         * $this->sdk->SendExtend['InvoiceItems'] = array();
114
         *  將商品加入電子發票商品列表陣列
115
         * foreach ($this->sdk->Send['Items'] as $info) {
116
         *      array_push($this->sdk->SendExtend['InvoiceItems'],array('Name' => $info['Name'],'Count' =>
117
         *          $info['Quantity'],'Word' => '個','Price' => $info['Price'],'TaxType' => TaxType::Dutiable));
118
         * }
119
         * $this->sdk->SendExtend['InvoiceRemark'] = '測試發票備註';
120
         * $this->sdk->SendExtend['DelayDay'] = '0';
121
         * $this->sdk->SendExtend['InvType'] = InvType::General;
122
         */
123
124 1
        return $this->sdk->formToArray(
125 1
            $this->sdk->CheckOutString()
126
        );
127
    }
128
129
    /**
130
     * cancelTransaction.
131
     *
132
     * @param array $params
133
     * @return array
134
     */
135 1 View Code Duplication
    public function cancelTransaction($params)
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...
136
    {
137 1
        $this->sdk->ServiceURL = $this->getApiEndpoint('DoAction');
138 1
        $this->sdk->Action = array_replace(
139 1
            $this->sdk->Action,
140 1
            array_intersect_key($params, $this->sdk->Action)
141
        );
142
143 1
        return $this->sdk->DoAction();
144
    }
145
146
    /**
147
     * refundTransaction.
148
     *
149
     * @param array $params
150
     * @return array
151
     */
152 1 View Code Duplication
    public function refundTransaction($params)
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...
153
    {
154 1
        $this->sdk->ServiceURL = $this->getApiEndpoint('AioChargeback');
155 1
        $this->sdk->ChargeBack = array_replace(
156 1
            $this->sdk->ChargeBack,
157 1
            array_intersect_key($params, $this->sdk->ChargeBack)
158
        );
159
160 1
        return $this->sdk->AioChargeback();
161
    }
162
163
    /**
164
     * getTransactionData.
165
     *
166
     * @param mixed $params
167
     * @return array
168
     */
169 1
    public function getTransactionData($params)
170
    {
171 1
        $this->sdk->ServiceURL = $this->getApiEndpoint('QueryTradeInfo');
172 1
        $this->sdk->Query['MerchantTradeNo'] = $params['MerchantTradeNo'];
173 1
        $details = $this->sdk->QueryTradeInfo();
174 1
        $details['RtnCode'] = $details['TradeStatus'] === '1' ? '1' : '2';
175
176 1
        return $details;
177
    }
178
179
    /**
180
     * Verify if the hash of the given parameter is correct.
181
     *
182
     * @param array $params
183
     * @return bool
184
     */
185
    public function verifyHash(array $params)
186
    {
187
        $result = false;
188
        try {
189
            $this->sdk->CheckOutFeedback($params);
190
            $result = true;
191
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
192
        }
193
194
        return $result;
195
    }
196
197
    /**
198
     * isMobile.
199
     *
200
     * @return bool
201
     */
202 1
    protected function isMobile()
203
    {
204 1
        $detect = new MobileDetect();
205
206 1
        return ($detect->isMobile() === false && $detect->isTablet() === false) ? false : true;
207
    }
208
}
209