Completed
Push — master ( 8cff60...3bb957 )
by recca
02:48
created

EcpayLogisticsApi::createTransaction()   C

Complexity

Conditions 7
Paths 27

Size

Total Lines 78
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 7.9149

Importance

Changes 0
Metric Value
dl 0
loc 78
ccs 36
cts 49
cp 0.7347
rs 6.5702
c 0
b 0
f 0
cc 7
eloc 59
nc 27
nop 1
crap 7.9149

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PayumTW\Ecpay;
4
5
use Http\Message\MessageFactory;
6
use Payum\Core\HttpClientInterface;
7
use PayumTW\Ecpay\Bridge\Ecpay\Device;
8
use PayumTW\Ecpay\Bridge\Ecpay\Distance;
9
use PayumTW\Ecpay\Bridge\Ecpay\Temperature;
10
use PayumTW\Ecpay\Bridge\Ecpay\IsCollection;
11
use PayumTW\Ecpay\Bridge\Ecpay\LogisticsType;
12
use PayumTW\Ecpay\Bridge\Ecpay\Specification;
13
use PayumTW\Ecpay\Bridge\Ecpay\EcpayLogistics;
14
use PayumTW\Ecpay\Bridge\Ecpay\LogisticsSubType;
15
use PayumTW\Ecpay\Bridge\Ecpay\ScheduledPickupTime;
16
17
class EcpayLogisticsApi extends Api
18
{
19
    /**
20
     * $client.
21
     *
22
     * @var \Payum\Core\HttpClientInterface
23
     */
24
    protected $client;
25
26
    /**
27
     * MessageFactory.
28
     *
29
     * @var MessageFactory
30
     */
31
    protected $messageFactory;
32
33
    /**
34
     * $options.
35
     *
36
     * @var array
37
     */
38
    protected $options = [];
39
40
    /**
41
     * $sdk.
42
     *
43
     * @var \PayumTW\Ecpay\Bridge\Ecpay\EcpayLogistics
44
     */
45
    protected $sdk;
46
47
    /**
48
     * @var array
49
     */
50
    protected $code = [];
51
52
    /**
53
     * @param array $options
54
     * @param \Payum\Core\HttpClientInterface $client
55
     * @param MessageFactory $messageFactory
56
     * @param \PayumTW\Ecpay\Bridge\Ecpay\EcpayLogistics $sdk
57
     *
58
     * @throws \Payum\Core\Exception\InvalidArgumentException if an option is invalid
59
     */
60 3 View Code Duplication
    public function __construct(array $options, HttpClientInterface $client, MessageFactory $messageFactory, EcpayLogistics $sdk = null)
61
    {
62 3
        $this->options = $options;
63 3
        $this->client = $client;
64 3
        $this->messageFactory = $messageFactory;
65 3
        $this->sdk = $sdk ?: new EcpayLogistics();
66 3
        $this->sdk->HashKey = $this->options['HashKey'];
67 3
        $this->sdk->HashIV = $this->options['HashIV'];
68 3
        $this->sdk->Send['MerchantID'] = $this->options['MerchantID'];
69 3
    }
70
71
    /**
72
     * getApiEndpoint.
73
     *
74
     * @return string
75
     */
76
    public function getApiEndpoint()
77
    {
78
        return $this->sdk->ServiceURL;
79
    }
80
81
    /**
82
     * createTransaction.
83
     *
84
     * @param array $params
85
     * @return array
86
     */
87 1
    public function createCvsMapTransaction(array $params)
88
    {
89 1
        $this->sdk->Send = array_merge($this->sdk->Send, [
90 1
            'MerchantTradeNo' => '',
91
            'LogisticsSubType' => LogisticsSubType::UNIMART,
92
            'IsCollection' => IsCollection::NO,
93 1
            'ServerReplyURL' => '',
94 1
            'ExtraData' => '',
95 1
            'Device' => $this->isMobile() ? Device::MOBILE : Device::PC,
96
        ]);
97
98 1
        $this->sdk->Send = array_replace(
99 1
            $this->sdk->Send,
100 1
            array_intersect_key($params, $this->sdk->Send)
101
        );
102
103 1
        return $this->sdk->formToArray(
104 1
            $this->sdk->CvsMap()
105
        );
106
    }
107
108
    /**
109
     * 產生托運單(宅配)/一段標(超商取貨).
110
     *
111
     * @param array $params
112
     * @return array
113
     */
114
    public function createPrintTradeTransaction(array $params)
115
    {
116
        // 參數初始化
117
        $this->sdk->Send = array_merge($this->sdk->Send, [
118
            'AllPayLogisticsID' => '',
119
            'PlatformID' => '',
120
        ]);
121
122
        $this->sdk->Send = array_replace(
123
            $this->sdk->Send,
124
            array_intersect_key($params, $this->sdk->Send)
125
        );
126
127
        return $this->sdk->formToArray(
128
            $this->sdk->PrintTradeDoc()
129
        );
130
    }
131
132
    /**
133
     * 列印繳款單(統一超商C2C).
134
     *
135
     * @param array $params
136
     * @return array
137
     */
138 View Code Duplication
    public function createPrintUnimartC2CBillTransaction(array $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...
139
    {
140
        // 參數初始化
141
        $this->sdk->Send = array_merge($this->sdk->Send, [
142
            'AllPayLogisticsID' => '',
143
            'CVSPaymentNo' => '',
144
            'CVSValidationNo' => '',
145
            'PlatformID' => '',
146
        ]);
147
148
        $this->sdk->Send = array_replace(
149
            $this->sdk->Send,
150
            array_intersect_key($params, $this->sdk->Send)
151
        );
152
153
        return $this->sdk->formToArray(
154
            $this->sdk->PrintUnimartC2CBill()
155
        );
156
    }
157
158
    /**
159
     * 全家列印小白單(全家超商C2C).
160
     *
161
     * @param array $params
162
     * @return array
163
     */
164 View Code Duplication
    public function createPrintFamilyC2CBillTransaction(array $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...
165
    {
166
        // 參數初始化
167
        $this->sdk->Send = array_merge($this->sdk->Send, [
168
            'AllPayLogisticsID' => '',
169
            'CVSPaymentNo' => '',
170
            'PlatformID' => '',
171
        ]);
172
173
        $this->sdk->Send = array_replace(
174
            $this->sdk->Send,
175
            array_intersect_key($params, $this->sdk->Send)
176
        );
177
178
        return $this->sdk->formToArray(
179
            $this->sdk->PrintUnimartC2CBill()
180
        );
181
    }
182
183
    /**
184
     * createTransaction.
185
     *
186
     * @param array $params
187
     * @return array
188
     */
189 1
    public function createTransaction(array $params)
190
    {
191 1
        $this->sdk->Send = array_merge($this->sdk->Send, [
192 1
            'MerchantTradeNo' => '',
193 1
            'MerchantTradeDate' => date('Y/m/d H:i:s'),
194 1
            'LogisticsType' => '',
195
            'LogisticsSubType' => LogisticsSubType::UNIMART,
196 1
            'GoodsAmount' => 0,
197 1
            'CollectionAmount' => 0,
198
            'IsCollection' => IsCollection::NO,
199 1
            'GoodsName' => '',
200 1
            'SenderName' => '',
201 1
            'SenderPhone' => '',
202 1
            'SenderCellPhone' => '',
203 1
            'ReceiverName' => '',
204 1
            'ReceiverPhone' => '',
205 1
            'ReceiverCellPhone' => '',
206 1
            'ReceiverEmail' => '',
207 1
            'TradeDesc' => '',
208 1
            'ServerReplyURL' => '',
209 1
            'LogisticsC2CReplyURL' => '',
210 1
            'Remark' => '',
211 1
            'PlatformID' => '',
212
        ]);
213
214 1
        $this->sdk->SendExtend = [];
215
216 1
        $this->sdk->Send = array_replace(
217 1
            $this->sdk->Send,
218 1
            array_intersect_key($params, $this->sdk->Send)
219
        );
220
221 1
        $this->sdk->Send['GoodsAmount'] = (int) $this->sdk->Send['GoodsAmount'];
222 1
        $this->sdk->Send['CollectionAmount'] = (int) $this->sdk->Send['CollectionAmount'];
223
224 1
        if (empty($this->sdk->Send['LogisticsType']) === true) {
225
            $this->sdk->Send['LogisticsType'] = LogisticsType::CVS;
226
            switch ($this->sdk->Send['LogisticsSubType']) {
227
                case LogisticsSubType::TCAT:
228
                    $this->sdk->Send['LogisticsType'] = LogisticsType::HOME;
229
                    break;
230
            }
231
        }
232
233 1
        if ($this->sdk->Send['IsCollection'] === IsCollection::NO) {
234
            $this->sdk->Send['CollectionAmount'] = 0;
235 1
        } elseif (isset($this->sdk->Send['CollectionAmount']) === false) {
236
            $this->sdk->Send['CollectionAmount'] = (int) $this->sdk->Send['GoodsAmount'];
237
        }
238
239 1
        switch ($this->sdk->Send['LogisticsType']) {
240 1
            case LogisticsType::HOME:
241
                $this->sdk->SendExtend = array_merge($this->sdk->SendExtend, [
242
                    'SenderZipCode' => '',
243
                    'SenderAddress' => '',
244
                    'ReceiverZipCode' => '',
245
                    'ReceiverAddress' => '',
246
                    'Temperature' => '',
247
                    'Distance' => '',
248
                    'Specification' => '',
249
                    'ScheduledDeliveryTime' => '',
250
                ]);
251
                break;
252 1
            case LogisticsType::CVS:
253
                $this->sdk->SendExtend = array_merge($this->sdk->SendExtend, [
254
                    'ReceiverStoreID' => '',
255
                    'ReturnStoreID' => '',
256
                ]);
257
                break;
258
        }
259
260 1
        $this->sdk->SendExtend = array_replace(
261 1
            $this->sdk->SendExtend,
262 1
            array_intersect_key($params, $this->sdk->SendExtend)
263
        );
264
265 1
        return $this->sdk->BGCreateShippingOrder();
266
    }
267
268
    /**
269
     * refundTransaction.
270
     *
271
     * @param array $params
272
     * @return array
273
     */
274
    public function refundTransaction($params)
275
    {
276
        if ($params['LogisticsSubType'] === LogisticsSubType::TCAT) {
277
            // 宅配逆物流訂單產生
278
            $method = 'CreateHomeReturnOrder';
279
            $supportedParams = [
280
                'AllPayLogisticsID' => '',
281
                'LogisticsSubType' => '',
282
                'ServerReplyURL' => '',
283
                'SenderName' => '',
284
                'SenderPhone' => '',
285
                'SenderCellPhone' => '',
286
                'SenderZipCode' => '',
287
                'SenderAddress' => '',
288
                'ReceiverName' => '',
289
                'ReceiverPhone' => '',
290
                'ReceiverCellPhone' => '',
291
                'ReceiverZipCode' => '',
292
                'ReceiverAddress' => '',
293
                'GoodsAmount' => '',
294
                'GoodsName' => '',
295
                'Temperature' => Temperature::ROOM,
296
                'Distance' => Distance::SAME,
297
                'Specification' => Specification::CM_60,
298
                'ScheduledPickupTime' => ScheduledPickupTime::UNLIMITED,
299
                'ScheduledDeliveryTime' => '',
300
                'Remark' => '',
301
                'PlatformID' => '',
302
            ];
303
        } elseif (isset($params['LogisticsSubType']) === true) {
304
            // LogisticsSubType::FAMILY = 'FAMI'; // 全家
305
            // LogisticsSubType::UNIMART = 'UNIMART'; // 統一超商
306
            // LogisticsSubType::FAMILY_C2C = 'FAMIC2C'; // 全家店到店
307
            // LogisticsSubType::UNIMART_C2C = 'UNIMARTC2C'; // 統一超商寄貨便
308
309
            // 超商取貨逆物流訂單(全家超商B2C).
310
            $method = 'CreateFamilyB2CReturnOrder';
311
            $supportedParams = [
312
                'AllPayLogisticsID' => '',
313
                'ServerReplyURL' => '',
314
                'GoodsName' => '',
315
                'GoodsAmount' => 0,
316
                'SenderName' => '',
317
                'SenderPhone' => '',
318
                'Remark' => '',
319
                'Quantity' => '',
320
                'Cost' => '',
321
                'PlatformID' => '',
322
            ];
323
        } else {
324
            // 全家逆物流核帳(全家超商B2C).
325
            $method = 'CheckFamilyB2CLogistics';
326
            $supportedParams = [
327
                'RtnMerchantTradeNo' => '',
328
                'PlatformID' => '',
329
            ];
330
        }
331
332
        $this->sdk->Send = array_merge($this->sdk->Send, $supportedParams);
333
334
        $this->sdk->Send = array_replace(
335
            $this->sdk->Send,
336
            array_intersect_key($params, $this->sdk->Send)
337
        );
338
339
        return call_user_func_array([$this->sdk, $method]);
340
    }
341
342
    /**
343
     * cancelTransaction.
344
     *
345
     * @param array $params
346
     * @return array
347
     */
348
    public function cancelTransaction($params)
349
    {
350
        $supportedParams = [];
351
        $method = '';
352
        if (isset($params['LogisticsSubType']) === true) {
353
            // LogisticsSubType::FAMILY = 'FAMI'; // 全家
354
            // LogisticsSubType::UNIMART = 'UNIMART'; // 統一超商
355
            // LogisticsSubType::FAMILY_C2C = 'FAMIC2C'; // 全家店到店
356
            // LogisticsSubType::UNIMART_C2C = 'UNIMARTC2C'; // 統一超商寄貨便
357
358
            // 取消訂單(統一超商C2C).
359
            $method = 'CancelUnimartLogisticsOrder';
360
            $supportedParams = [
361
                'AllPayLogisticsID' => '',
362
                'CVSPaymentNo' => '',
363
                'CVSValidationNo' => '',
364
                'PlatformID' => '',
365
            ];
366
        }
367
368
        $this->sdk->Send = array_merge($this->sdk->Send, $supportedParams);
369
370
        $this->sdk->Send = array_replace(
371
            $this->sdk->Send,
372
            array_intersect_key($params, $this->sdk->Send)
373
        );
374
375
        return empty($method) === true ?
376
            [] : call_user_func_array([$this->sdk, $method]);
377
    }
378
379
    /**
380
     * getTransactionData.
381
     *
382
     * @param mixed $params
383
     * @return array
384
     */
385
    public function getTransactionData($params)
386
    {
387
        $supportedParams = [
388
            'AllPayLogisticsID' => '',
389
            'PlatformID' => '',
390
        ];
391
392
        $this->sdk->Send = array_merge($this->sdk->Send, $supportedParams);
393
394
        $this->sdk->Send = array_replace(
395
            $this->sdk->Send,
396
            array_intersect_key($params, $this->sdk->Send)
397
        );
398
399
        return $this->sdk->QueryLogisticsInfo();
400
    }
401
}
402