|
1
|
|
|
<?php |
|
2
|
|
|
namespace Culqi; |
|
3
|
|
|
|
|
4
|
|
|
use Culqi\Error as Errors; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class Client |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* La versión de API usada |
|
11
|
|
|
*/ |
|
12
|
|
|
const API_VERSION = "v1.2"; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* La URL Base por defecto |
|
16
|
|
|
*/ |
|
17
|
|
|
const BASE_URL = "https://integ-pago.culqi.com/api/v1"; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
public function request($method, $url, $api_key, $data = NULL, $headers= array("Content-Type" => "application/json", "Accept" => "application/json") ) { |
|
21
|
|
|
try { |
|
22
|
|
|
$options = array( |
|
23
|
|
|
'auth' => new AuthBearer($api_key), |
|
24
|
|
|
'timeout' => 120 |
|
25
|
|
|
); |
|
26
|
|
|
if($method == "GET") { |
|
27
|
|
|
$url_params = is_array($data) ? '?' . http_build_query($data) : ''; |
|
28
|
|
|
$response = \Requests::get(Culqi::$api_base . $url . $url_params, $headers, $options); |
|
29
|
|
|
} else if($method == "POST") { |
|
30
|
|
|
$response = \Requests::post(Culqi::$api_base . $url, $headers, json_encode($data), $options); |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
} else if($method == "PATCH") { |
|
34
|
|
|
$response = \Requests::patch(Culqi::$api_base . $url, $headers, json_encode($data), $options); |
|
35
|
|
|
} else if($method == "DELETE") { |
|
36
|
|
|
$response = \Requests::delete(Culqi::$api_base, $options); |
|
37
|
|
|
} |
|
38
|
|
|
} catch (\Exception $e) { |
|
39
|
|
|
throw new Errors\UnableToConnect(); |
|
40
|
|
|
} |
|
41
|
|
|
if ($response->status_code >= 200 && $response->status_code <= 206) { |
|
42
|
|
|
if ($method == "DELETE") { |
|
43
|
|
|
return $response->status_code == 204 || $response->status_code == 200; |
|
44
|
|
|
} |
|
45
|
|
|
return json_decode($response->body); |
|
46
|
|
|
} |
|
47
|
|
|
if ($response->status_code == 400) { |
|
48
|
|
|
$code = 0; |
|
49
|
|
|
$message = ""; |
|
50
|
|
|
|
|
51
|
|
|
throw new Errors\UnhandledError($response->body, $response->status_code); |
|
52
|
|
|
} |
|
53
|
|
|
if ($response->status_code == 401) { |
|
54
|
|
|
throw new Errors\AuthenticationError(); |
|
55
|
|
|
} |
|
56
|
|
|
if ($response->status_code == 404) { |
|
57
|
|
|
throw new Errors\NotFound(); |
|
58
|
|
|
} |
|
59
|
|
|
if ($response->status_code == 403) { |
|
60
|
|
|
throw new Errors\InvalidApiKey(); |
|
61
|
|
|
} |
|
62
|
|
|
if ($response->status_code == 405) { |
|
63
|
|
|
throw new Errors\MethodNotAllowed(); |
|
64
|
|
|
} |
|
65
|
|
|
throw new Errors\UnhandledError($response->body, $response->status_code); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|