1 | <?php |
||
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 = []) |
||
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) |
||
111 | |||
112 | /** |
||
113 | * Gets a sale |
||
114 | * @param string $paymentId |
||
115 | * @return mixed |
||
116 | */ |
||
117 | public function get($paymentId) |
||
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() |
||
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.