CollectCvsApi::getApiEndpoint()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace PayumTW\Collect;
4
5
use Carbon\Carbon;
6
7
class CollectCvsApi extends Api
8
{
9
    /**
10
     * @return string
11
     */
12 2
    public function getApiEndpoint($type = '')
13
    {
14 2
        return 'https://www.ccat.com.tw/cvs/ap_interface.php';
15
    }
16
17
    /**
18
     * createTransaction.
19
     *
20
     * @param array $params
21
     * @return array
22
     */
23 2
    public function createTransaction(array $params, $returnType = 'xml')
24
    {
25 2
        $cmd = 'cvs_order_regiater';
26
        $supportedParams = [
27 2
            'cust_order_number' => null,
28 2
            'order_amount' => null,
29 2
            'expire_date' => Carbon::now(static::TIMEZONE)->endOfDay()->addDays(7)->toDateTimeString(),
30 2
            'payer_name' => null,
31 2
            'payer_postcode' => null,
32 2
            'payer_address' => null,
33 2
            'payer_mobile' => null,
34 2
            'payer_email' => null,
35 2
        ];
36
37 2
        $params = array_filter(array_replace(
38 2
            $supportedParams,
39 2
            array_intersect_key($params, $supportedParams)
40 2
        ));
41
42 2
        if (empty($params['expire_date']) === false) {
43 2
            $params['expire_date'] = $this->toIso8601String($params['expire_date']);
44 2
        }
45
46 2
        return $this->options['submit_type'] === 'redirect'
47 2
            ? array_merge([
48 1
                'cmd' => $cmd,
49 1
                'cust_id' => $this->options['cust_id'],
50 1
                'cust_password' => $this->options['cust_password'],
51 1
            ], $params)
52 2
            : $this->parseResponseXML(
53 1
                $this->doRequest(
54 1
                    'POST',
55 1
                    $this->createRequestXML($params, $cmd),
56 1
                    '',
57
                    false
58 1
                )
59 2
            );
60
    }
61
62
    /**
63
     * getTransactionData.
64
     *
65
     * @param array $params
66
     * @return string
67
     */
68 1
    public function getTransactionData(array $params)
69
    {
70
        $supportedParams = [
71 1
            'process_code_update_time_begin' => Carbon::now(static::TIMEZONE)->toDateTimeString(),
72 1
            'process_code_update_time_end' => Carbon::now(static::TIMEZONE)->addDays(1)->toDateTimeString(),
73 1
        ];
74
75 1
        $params = array_filter(array_replace(
76 1
            $supportedParams,
77 1
            array_intersect_key($params, $supportedParams)
78 1
        ));
79
80 1
        $params['process_code_update_time_begin'] = $this->toIso8601String($params['process_code_update_time_begin']);
81 1
        $params['process_code_update_time_end'] = $this->toIso8601String($params['process_code_update_time_end']);
82
83 1
        return $this->parseResponseXML(
84 1
            $this->doRequest(
85 1
                'POST',
86 1
                $this->createRequestXML($params, 'cvs_order_query'),
87 1
                '',
88
                false
89 1
            )
90 1
        );
91
    }
92
93
    /**
94
     * toIso8601String.
95
     *
96
     * @param string $string
97
     * @return string
98
     */
99 3
    protected function toIso8601String($string)
100
    {
101 3
        return Carbon::parse($string)->toIso8601String();
102
    }
103
104
    /**
105
     * createQueryRequestXML.
106
     *
107
     * @param array $params
108
     * @return string
109
     */
110 2
    protected function createRequestXML($params, $cmd = 'cvs_order_regiater')
111
    {
112 2
        $key = $cmd === 'cvs_order_regiater' ? 'order' : 'query';
113
        $params = [
114
            'header' => [
115 2
                'cmd' => $cmd,
116 2
                'cust_id' => $this->options['cust_id'],
117 2
                'cust_password' => $this->options['cust_password'],
118 2
            ],
119 2
            $key => $params,
120 2
        ];
121
122
        $xml = [
123 2
            '<?xml version="1.0" encoding="UTF-8"?>',
124 2
            '<request>',
125 2
        ];
126
127 2
        foreach ($params as $key => $values) {
128 2
            $xml[] = '<'.$key.'>';
129 2
            foreach ($values as $key2 => $value) {
130 2
                $xml[] = '<'.$key2.'>';
131 2
                $xml[] = $value;
132 2
                $xml[] = '</'.$key2.'>';
133 2
            }
134 2
            $xml[] = '</'.$key.'>';
135 2
        }
136
137 2
        $xml[] = '</request>';
138
139 2
        return implode('', $xml);
140
    }
141
142
    /**
143
     * parseResponseXML.
144
     *
145
     * @param string $xml
146
     * @return array
147
     */
148 2
    protected function parseResponseXML($xml)
149
    {
150
        $response = [
151 2
            'status' => 'ERROR',
152 2
            'orders' => [],
153 2
        ];
154
155 2
        if (preg_match('/<status>(.*)<\/status>/', $xml, $matches) !== false) {
156 2
            $response['status'] = $matches[1];
157 2
        }
158
159 2
        if (preg_match_all('/<order>(.*)<\/order>/sU', $xml, $matches) !== false) {
160 2
            $orders = $matches[1];
161
            $tags = [
162 2
                'cust_order_number',
163 2
                'order_amount',
164 2
                'expire_date',
165 2
                'st_barcode1',
166 2
                'st_barcode2',
167 2
                'st_barcode3',
168 2
                'post_barcode1',
169 2
                'post_barcode2',
170 2
                'post_barcode3',
171 2
                'virtual_account',
172 2
                'cs_fee',
173 2
                'ibon_code',
174 2
                'bill_amount',
175 2
                'ibon_shopid',
176 2
                'create_time',
177 2
                'process_code',
178 2
                'pay_date',
179 2
                'grant_amount',
180 2
                'grant_date',
181 2
            ];
182
183 2
            $regexp = '/<(?<key>'.implode('|', $tags).')>(?<value>[^<]*)<\/('.implode('|', $tags).')>/';
184 2
            foreach ($orders as $order) {
185 2
                $temp = [];
186 2
                $matches = [];
187 2
                if (preg_match_all($regexp, $order, $matches, PREG_SET_ORDER) !== false) {
188 2
                    foreach ($matches as $match) {
189 2
                        $temp[$match['key']] = $match['value'];
190 2
                    }
191 2
                }
192 2
                $response['orders'][] = $temp;
193 2
            }
194 2
        }
195
196 2
        return $response;
197
    }
198
}
199