1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Postpay; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Postpay\Exceptions\PostpayException; |
8
|
|
|
use Postpay\Http\Request; |
9
|
|
|
use Postpay\HttpClients\Client; |
10
|
|
|
use Postpay\HttpClients\ClientInterface; |
11
|
|
|
use Postpay\HttpClients\CurlClient; |
12
|
|
|
use Postpay\HttpClients\GuzzleClient; |
13
|
|
|
|
14
|
|
|
class Postpay |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @const string Version number of the Postpay SDK. |
18
|
|
|
*/ |
19
|
|
|
const VERSION = '0.1.1'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @const string Default API version for requests. |
23
|
|
|
*/ |
24
|
|
|
const DEFAULT_API_VERSION = 'v1'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @const string GraphQL path. |
28
|
|
|
*/ |
29
|
|
|
const GRAPHQL_PATH = '/graphql'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @const string GraphQL version for requests. |
33
|
|
|
*/ |
34
|
|
|
const GRAPHQL_VERSION = 'graphql'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @const string The name of the environment variable that contains the merchant ID. |
38
|
|
|
*/ |
39
|
|
|
const MERCHANT_ID_ENV_NAME = 'POSTPAY_MERCHANT_ID'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @const string The name of the environment variable that contains the secret key. |
43
|
|
|
*/ |
44
|
|
|
const SECRET_KEY_ENV_NAME = 'POSTPAY_SECRET_KEY'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Client The Postpay client service. |
48
|
|
|
*/ |
49
|
|
|
protected $client; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array The basic auth credentials. |
53
|
|
|
*/ |
54
|
|
|
protected $auth; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string|null The default API version. |
58
|
|
|
*/ |
59
|
|
|
protected $apiVersion; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var bool Set to true for sandbox requests. |
63
|
|
|
*/ |
64
|
|
|
protected $sandbox; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var \Postpay\Http\Response|null Stores the last request. |
68
|
|
|
*/ |
69
|
|
|
protected $lastResponse; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Instantiates a new Postpay super-class object. |
73
|
|
|
* |
74
|
|
|
* @param array $config |
75
|
|
|
* |
76
|
|
|
* @throws PostpayException |
77
|
|
|
*/ |
78
|
10 |
|
public function __construct(array $config = []) |
79
|
|
|
{ |
80
|
10 |
|
$config = array_merge([ |
81
|
10 |
|
'client_handler' => null, |
82
|
10 |
|
'merchant_id' => getenv(static::MERCHANT_ID_ENV_NAME), |
83
|
10 |
|
'secret_key' => getenv(static::SECRET_KEY_ENV_NAME), |
84
|
10 |
|
'api_version' => static::DEFAULT_API_VERSION, |
85
|
|
|
'sandbox' => false, |
86
|
|
|
], $config); |
87
|
|
|
|
88
|
10 |
|
if ((!$config['merchant_id']) || (!$config['secret_key'])) { |
89
|
1 |
|
throw new PostpayException('Basic credentials required.'); |
90
|
|
|
} |
91
|
10 |
|
$this->client = new Client( |
92
|
10 |
|
self::createClientHandler($config['client_handler']) |
93
|
|
|
); |
94
|
10 |
|
$this->auth = [$config['merchant_id'], $config['secret_key']]; |
95
|
10 |
|
$this->apiVersion = $config['api_version']; |
96
|
10 |
|
$this->sandbox = $config['sandbox']; |
97
|
10 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns the client service. |
101
|
|
|
* |
102
|
|
|
* @return Client |
103
|
|
|
*/ |
104
|
1 |
|
public function getClient() |
105
|
|
|
{ |
106
|
1 |
|
return $this->client; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Creates a client handler. |
111
|
|
|
* |
112
|
|
|
* @param ClientInterface|string|null $handler |
113
|
|
|
* |
114
|
|
|
* @return ClientInterface |
115
|
|
|
* |
116
|
|
|
* @throws \Exception If extensions aren't available. |
117
|
|
|
* @throws InvalidArgumentException If client handler is invalid. |
118
|
|
|
*/ |
119
|
11 |
|
public static function createClientHandler($handler = null) |
120
|
|
|
{ |
121
|
11 |
|
if ($handler instanceof ClientInterface) { |
122
|
10 |
|
return $handler; |
123
|
|
|
} |
124
|
11 |
|
$handler = $handler ?: 'curl'; |
125
|
|
|
|
126
|
11 |
|
if ('curl' === $handler) { |
127
|
11 |
|
if (!extension_loaded('curl')) { |
128
|
|
|
throw new Exception('cURL extension must be loaded.'); |
129
|
|
|
} |
130
|
11 |
|
return new CurlClient(); |
131
|
|
|
} |
132
|
1 |
|
if ('guzzle' === $handler) { |
133
|
1 |
|
if (!class_exists('GuzzleHttp\Client')) { |
134
|
|
|
throw new Exception('GuzzleHttp\Client must be included.'); |
135
|
|
|
} |
136
|
1 |
|
return new GuzzleClient(); |
137
|
|
|
} |
138
|
1 |
|
throw new InvalidArgumentException('Invalid client handler.'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Sets the HTTP client handler. |
143
|
|
|
* |
144
|
|
|
* @param ClientInterface $clientHandler |
145
|
|
|
*/ |
146
|
10 |
|
public function setClientHandler($clientHandler) |
147
|
|
|
{ |
148
|
10 |
|
$this->client->setClientHandler( |
149
|
10 |
|
self::createClientHandler($clientHandler) |
150
|
|
|
); |
151
|
10 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Returns the last response returned from API. |
155
|
|
|
* |
156
|
|
|
* @return \Postpay\Http\Response|null |
157
|
|
|
*/ |
158
|
1 |
|
public function getLastResponse() |
159
|
|
|
{ |
160
|
1 |
|
return $this->lastResponse; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Sends a GET request to API and returns the response. |
165
|
|
|
* |
166
|
|
|
* @param string $path |
167
|
|
|
* @param array $params |
168
|
|
|
* |
169
|
|
|
* @return \Postpay\Http\Response |
170
|
|
|
* |
171
|
|
|
* @throws PostpayException |
172
|
|
|
*/ |
173
|
2 |
|
public function get($path, array $params = []) |
174
|
|
|
{ |
175
|
2 |
|
return $this->request('GET', $path, $params); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Sends a POST request to API and returns the response. |
180
|
|
|
* |
181
|
|
|
* @param string $path |
182
|
|
|
* @param array $params |
183
|
|
|
* |
184
|
|
|
* @return \Postpay\Http\Response |
185
|
|
|
* |
186
|
|
|
* @throws PostpayException |
187
|
|
|
*/ |
188
|
1 |
|
public function post($path, array $params = []) |
189
|
|
|
{ |
190
|
1 |
|
return $this->request('POST', $path, $params); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Sends a PUT request to API and returns the response. |
195
|
|
|
* |
196
|
|
|
* @param string $path |
197
|
|
|
* @param array $params |
198
|
|
|
* |
199
|
|
|
* @return \Postpay\Http\Response |
200
|
|
|
* |
201
|
|
|
* @throws PostpayException |
202
|
|
|
*/ |
203
|
1 |
|
public function put($path, array $params = []) |
204
|
|
|
{ |
205
|
1 |
|
return $this->request('PUT', $path, $params); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Sends a PATCH request to API and returns the response. |
210
|
|
|
* |
211
|
|
|
* @param string $path |
212
|
|
|
* @param array $params |
213
|
|
|
* |
214
|
|
|
* @return \Postpay\Http\Response |
215
|
|
|
* |
216
|
|
|
* @throws PostpayException |
217
|
|
|
*/ |
218
|
1 |
|
public function patch($path, array $params = []) |
219
|
|
|
{ |
220
|
1 |
|
return $this->request('PATCH', $path, $params); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Sends a DELETE request to API and returns the response. |
225
|
|
|
* |
226
|
|
|
* @param string $path |
227
|
|
|
* @param array $params |
228
|
|
|
* |
229
|
|
|
* @return \Postpay\Http\Response |
230
|
|
|
* |
231
|
|
|
* @throws PostpayException |
232
|
|
|
*/ |
233
|
1 |
|
public function delete($path, array $params = []) |
234
|
|
|
{ |
235
|
1 |
|
return $this->request('DELETE', $path, $params); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Sends a query to GraphQL API and returns the response. |
240
|
|
|
* |
241
|
|
|
* @param string $query |
242
|
|
|
* @param array $variables |
243
|
|
|
* |
244
|
|
|
* @return \Postpay\Http\Response |
245
|
|
|
* |
246
|
|
|
* @throws PostpayException |
247
|
|
|
*/ |
248
|
1 |
|
public function query($query, array $variables = []) |
249
|
|
|
{ |
250
|
|
|
$params = [ |
251
|
1 |
|
'query' => $query, |
252
|
1 |
|
'variables' => $variables, |
253
|
|
|
]; |
254
|
1 |
|
return $this->request( |
255
|
1 |
|
'POST', |
256
|
1 |
|
self::GRAPHQL_PATH, |
257
|
|
|
$params, |
258
|
1 |
|
self::GRAPHQL_VERSION |
259
|
|
|
); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Sends a request to API and returns the response. |
264
|
|
|
* |
265
|
|
|
* @param string $method |
266
|
|
|
* @param string $path |
267
|
|
|
* @param array $params |
268
|
|
|
* @param string|null $apiVersion |
269
|
|
|
* |
270
|
|
|
* @return \Postpay\Http\Response |
271
|
|
|
* |
272
|
|
|
* @throws PostpayException |
273
|
|
|
*/ |
274
|
7 |
|
public function request( |
275
|
|
|
$method, |
276
|
|
|
$path, |
277
|
|
|
array $params = [], |
278
|
|
|
$apiVersion = null |
279
|
|
|
) { |
280
|
7 |
|
$request = new Request( |
281
|
7 |
|
$method, |
282
|
|
|
$path, |
283
|
|
|
$params, |
284
|
7 |
|
$this->auth, |
285
|
7 |
|
$apiVersion ?: $this->apiVersion, |
286
|
7 |
|
$this->sandbox |
287
|
|
|
); |
288
|
7 |
|
return $this->lastResponse = $this->client->request($request); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|