This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathClient.php
292 lines (246 loc) · 6.76 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
namespace Tuurbo\Spreedly;
use GuzzleHttp\ClientInterface as GuzzleInterface;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
use GuzzleHttp\Exception\ConnectException as GuzzleConnectException;
class Client
{
const BASE_URL = 'https://core.spreedly.com/';
const TIMEOUT = 64;
const CONNECT_TIMEOUT = 10;
protected $client;
protected $config;
protected $response;
protected $status;
protected $key;
/**
* Set config.
*
* @param \GuzzleHttp\ClientInterface $client
* @param array $config
*/
public function __construct(GuzzleInterface $client, $config)
{
// allow overriding BASE_URL
if (!isset($config['baseUrl'])) {
$config['baseUrl'] = self::BASE_URL;
}
$this->client = $client;
$this->config = $config;
}
public function get($url, array $data = null)
{
return $this->request($url, 'get', $data);
}
public function post($url, array $data = null)
{
return $this->request($url, 'post', $data);
}
public function put($url, array $data = null)
{
return $this->request($url, 'put', $data);
}
/**
* Create the CURL request.
*
* @param string $url
* @param string $method optional
* @param array $data optional
*
* @return Tuurbo\Spreedly\Client
*/
protected function request($url, $method, array $data = null)
{
try {
$baseUrl = $this->config['baseUrl'];
$response = $this->client->{$method}($baseUrl.$url, $this->buildData($data));
if (!in_array($response->getStatusCode(), [200, 201])) {
$contentType = $response->getHeader('Content-Type');
$notJson = array_shift($contentType) !== 'application/json; charset=utf-8';
if ($response->getStatusCode() == 404 && $notJson) {
throw new Exceptions\NotFoundHttpException();
}
if ($response->getStatusCode() == 408) {
throw new Exceptions\TimeoutException();
}
$this->setResponse($response);
$this->status = 'error';
} else {
$this->setResponse($response);
}
} catch (GuzzleConnectException $e) {
throw new Exceptions\TimeoutException();
}
return $this;
}
/**
* Set the response from Guzzle.
*
* @param mixed $response
*
* @return Tuurbo\Spreedly\Client
*/
public function setResponse($response)
{
if ($response instanceof GuzzleResponse) {
$contentType = $response->getHeader('Content-Type');
if (array_shift($contentType) === 'application/json; charset=utf-8') {
$response = $response->getBody();
$response = json_decode($response, true);
} else {
$response = ['raw' => (string) $response->getBody()];
}
}
$this->response = $response;
if ($this->response('error') ||
$this->response('errors') ||
$this->response('succeeded') === false) {
$this->status = 'error';
} else {
$this->status = 'success';
}
return $this;
}
/**
* Get the response from Guzzle as an array.
*
* @param string $key
*
* @return array
*/
public function response($key = null)
{
$array = $this->response;
if (!$array) {
return;
}
$this->key = key($array);
$array = $array[$this->key];
if (is_null($key)) {
return $array;
}
if (isset($array[$key])) {
return $array[$key];
}
foreach (explode('.', $key) as $segment) {
if (!is_array($array) || !array_key_exists($segment, $array)) {
return;
}
$array = $array[$segment];
}
return $array;
}
/**
* Get the transaction message.
*
* @return string
*/
public function message()
{
return $this->response('message');
}
/**
* Check if payment purchase has declined.
*
* @return bool
*/
public function hasDeclined()
{
return $this->response('succeeded') !== null && $this->response('succeeded') == false;
}
/**
* Get the transaction token.
*
* @return string
*/
public function transactionToken()
{
if ($this->key == 'payment_method') {
return;
}
return $this->response('token');
}
/**
* Get the payment token.
*
* @return string
*/
public function paymentToken()
{
if ($this->key == 'payment_method') {
return $this->response('token');
}
return $this->response('payment_method.token');
}
/**
* Get an array or string of errors.
*
* @return array|string
*/
public function errors($string = false)
{
if (!isset($this->response['errors'])) {
return $string ? '' : [];
}
$errors = is_array($this->response['errors']) ? $this->response['errors'] : [$this->response['errors']];
if ($string == true) {
$errors = array_map(function ($error) {
return $error['message'];
}, $errors);
return implode(' ', $errors);
}
return $errors;
}
/**
* Check if call returned errors.
*
* @return bool
*/
public function hasErrors()
{
return isset($this->response['errors']);
}
/**
* Check if call was successfull.
*
* @return bool
*/
public function success()
{
return $this->status == 'success';
}
/**
* Check if call failed or purchase declined.
*
* @return bool
*/
public function fails()
{
return $this->status == 'error';
}
/**
* Alias for fails method.
*
* @return bool
*/
public function failed()
{
return $this->fails();
}
protected function buildData($data)
{
return [
'auth' => [
$this->config['key'],
$this->config['secret'],
],
'timeout' => isset($this->config['timeout']) ? $this->config['timeout'] : self::TIMEOUT,
'connect_timeout' => isset($this->config['connect_timeout']) ? $this->config['connect_timeout'] : self::CONNECT_TIMEOUT,
'exceptions' => false,
'headers' => [
'Content-type' => 'application/json',
],
'body' => $data ? json_encode($data) : null,
];
}
}