1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright © 2015 Mozg. All rights reserved. |
5
|
|
|
* See LICENSE.txt for license details. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Mozg\Cielo; |
9
|
|
|
|
10
|
|
|
use GuzzleHttp\Exception\RequestException; |
11
|
|
|
use GuzzleHttp\Client as HttpClient; |
12
|
|
|
|
13
|
|
|
class HttpStatus |
14
|
|
|
{ |
15
|
|
|
const Ok = 200; |
16
|
|
|
const Created = 201; |
17
|
|
|
const BadRequest = 400; |
18
|
|
|
const NotFound = 404; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
class ApiService |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $config; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $headers; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var HttpClient |
36
|
|
|
*/ |
37
|
|
|
protected $http; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $debugData = array(); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* ApiService constructor. |
46
|
|
|
* @param array $options |
47
|
|
|
*/ |
48
|
|
|
public function __construct($options = []) |
49
|
|
|
{ |
50
|
|
|
|
51
|
|
|
$this->debugData[] = __METHOD__; |
52
|
|
|
|
53
|
|
|
$this->config = $options; |
54
|
|
|
|
55
|
|
|
$this->headers = $this->config['headers']; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function __destruct() |
59
|
|
|
{ |
60
|
|
|
|
61
|
|
|
$this->debugData[] = __METHOD__; |
62
|
|
|
|
63
|
|
|
$_array = $this->debugData; |
|
|
|
|
64
|
|
|
|
65
|
|
|
//\Zend\Debug\Debug::dump($_array); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param - |
70
|
|
|
* @return - |
|
|
|
|
71
|
|
|
* @throws \Exception |
72
|
|
|
*/ |
73
|
|
|
public function authorize($parameters) |
74
|
|
|
{ |
75
|
|
|
|
76
|
|
|
$this->debugData[] = __METHOD__; |
77
|
|
|
|
78
|
|
|
$method = 'POST'; |
79
|
|
|
$uri = $this->config['apiUri'] . '/sales/'; |
80
|
|
|
$options = [ |
81
|
|
|
'body' => \json_encode($parameters), |
82
|
|
|
'headers' => $this->headers |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
$this->debugData[][__LINE__]['method'] = $method; |
86
|
|
|
$this->debugData[][__LINE__]['uri'] = $uri; |
87
|
|
|
$this->debugData[][__LINE__]['options'] = $uri; |
88
|
|
|
|
89
|
|
|
try { |
90
|
|
|
|
91
|
|
|
$response = $this->http()->request($method, $uri, $options); |
92
|
|
|
|
93
|
|
|
$this->debugData[][__LINE__]['response'] = $response; |
94
|
|
|
|
95
|
|
|
$result = \json_decode($response->getBody()->getContents(), true); |
96
|
|
|
|
97
|
|
|
$this->debugData[][__LINE__]['result'] = $result; |
98
|
|
|
|
99
|
|
|
} catch (RequestException $e) { |
100
|
|
|
|
101
|
|
|
$result = array( |
102
|
|
|
'code'=>$e->getCode(), |
103
|
|
|
'message'=>$e->getMessage() |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$this->debugData[][__LINE__]['exception'] = $e->getMessage(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $result; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Gets a sale |
114
|
|
|
* @param string $paymentId |
115
|
|
|
* @return mixed |
116
|
|
|
*/ |
117
|
|
|
public function get($paymentId) |
118
|
|
|
{ |
119
|
|
|
|
120
|
|
|
$this->debugData[] = __METHOD__; |
121
|
|
|
|
122
|
|
|
$method = 'GET'; |
123
|
|
|
$uri = $this->config['apiQueryUri'] . \sprintf('/sales/%s', $paymentId); |
124
|
|
|
$options = [ |
125
|
|
|
'headers' => $this->headers |
126
|
|
|
]; |
127
|
|
|
|
128
|
|
|
$this->debugData[][__LINE__]['method'] = $method; |
129
|
|
|
$this->debugData[][__LINE__]['uri'] = $uri; |
130
|
|
|
$this->debugData[][__LINE__]['options'] = $uri; |
131
|
|
|
|
132
|
|
|
try { |
133
|
|
|
|
134
|
|
|
$response = $this->http()->request($method, $uri, $options); |
135
|
|
|
|
136
|
|
|
$this->debugData[][__LINE__]['response'] = $response; |
137
|
|
|
|
138
|
|
|
$result = \json_decode($response->getBody()->getContents(), true); |
139
|
|
|
|
140
|
|
|
if ($response->getStatusCode() === HttpStatus::Ok) { |
141
|
|
|
return $result; |
142
|
|
|
} elseif ($response->getStatusCode() == HttpStatus::BadRequest) { |
143
|
|
|
return 'status_codex'; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
} catch (\Exception $e) { |
147
|
|
|
|
148
|
|
|
$result = array( |
149
|
|
|
'code'=>$e->getCode(), |
150
|
|
|
'message'=>$e->getMessage() |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$this->debugData[][__LINE__]['exception'] = $e->getMessage(); |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $result; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Captures a pre-authorized payment |
162
|
|
|
* @param string $paymentId |
163
|
|
|
* @param array $captureRequest |
164
|
|
|
* @return mixed |
165
|
|
|
*/ |
166
|
|
|
public function capture($paymentId, $captureRequest) |
167
|
|
|
{ |
168
|
|
|
|
169
|
|
|
$this->debugData[] = __METHOD__; |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
if (!$paymentId) { |
173
|
|
|
throw new \InvalidArgumentException('$paymentId é obrigatório'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$method = 'PUT'; |
177
|
|
|
$uri = $this->config['apiUri'] . \sprintf('/sales/%s/capture', $paymentId); |
178
|
|
|
$options = [ |
179
|
|
|
'headers' => $this->headers |
180
|
|
|
]; |
181
|
|
|
|
182
|
|
|
if (!empty($captureRequest)) { |
183
|
|
|
$uri .= '?' . \http_build_query($captureRequest); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$this->debugData[][__LINE__]['method'] = $method; |
187
|
|
|
$this->debugData[][__LINE__]['uri'] = $uri; |
188
|
|
|
$this->debugData[][__LINE__]['options'] = $uri; |
189
|
|
|
|
190
|
|
|
try { |
191
|
|
|
|
192
|
|
|
$response = $this->http()->request($method, $uri, $options); |
193
|
|
|
|
194
|
|
|
$this->debugData[][__LINE__]['response'] = $response; |
195
|
|
|
|
196
|
|
|
$result = \json_decode($response->getBody()->getContents(), true); |
197
|
|
|
|
198
|
|
|
$this->debugData[][__LINE__]['result'] = $result; |
199
|
|
|
|
200
|
|
|
} catch (RequestException $e) { |
201
|
|
|
|
202
|
|
|
$result = array( |
203
|
|
|
'code'=>$e->getCode(), |
204
|
|
|
'message'=>$e->getMessage() |
205
|
|
|
); |
206
|
|
|
|
207
|
|
|
$this->debugData[][__LINE__]['exception'] = $e->getMessage(); |
208
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $result; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Void a payment |
216
|
|
|
* @param string $paymentId |
217
|
|
|
* @param int $amount |
218
|
|
|
* @return mixed |
219
|
|
|
*/ |
220
|
|
|
public function void($paymentId, $amount) |
221
|
|
|
{ |
222
|
|
|
|
223
|
|
|
$this->debugData[] = __METHOD__; |
224
|
|
|
|
225
|
|
|
$method = 'PUT'; |
226
|
|
|
$uri = $this->config['apiUri'] . \sprintf('/sales/%s/void', $paymentId); |
227
|
|
|
$options = [ |
228
|
|
|
'headers' => $this->headers |
229
|
|
|
]; |
230
|
|
|
|
231
|
|
|
if ($amount) { |
232
|
|
|
$uri .= sprintf('?amount=%s', (float)$amount); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$this->debugData[][__LINE__]['method'] = $method; |
236
|
|
|
$this->debugData[][__LINE__]['uri'] = $uri; |
237
|
|
|
$this->debugData[][__LINE__]['options'] = $uri; |
238
|
|
|
|
239
|
|
|
try { |
240
|
|
|
|
241
|
|
|
$response = $this->http()->request($method, $uri, $options); |
242
|
|
|
|
243
|
|
|
$this->debugData[][__LINE__]['response'] = $response; |
244
|
|
|
|
245
|
|
|
$result = \json_decode($response->getBody()->getContents(), true); |
246
|
|
|
|
247
|
|
|
$this->debugData[][__LINE__]['result'] = $result; |
248
|
|
|
|
249
|
|
|
} catch (RequestException $e) { |
250
|
|
|
|
251
|
|
|
$result = array( |
252
|
|
|
'code'=>$e->getCode(), |
253
|
|
|
'message'=>$e->getMessage() |
254
|
|
|
); |
255
|
|
|
|
256
|
|
|
$this->debugData[][__LINE__]['exception'] = $e->getMessage(); |
257
|
|
|
|
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return $result; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @return HttpClient |
265
|
|
|
*/ |
266
|
|
|
protected function http() |
267
|
|
|
{ |
268
|
|
|
|
269
|
|
|
$this->debugData[] = __METHOD__; |
270
|
|
|
|
271
|
|
|
if (!$this->http) { |
272
|
|
|
$this->http = new HttpClient(); |
273
|
|
|
} |
274
|
|
|
return $this->http; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
|
278
|
|
|
} |
279
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.