1
|
|
|
<?php namespace Arcanedev\Stripe; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Stripe\Contracts\Stripe as StripeContract; |
4
|
|
|
use Arcanedev\Stripe\Exceptions\ApiException; |
5
|
|
|
use Arcanedev\Stripe\Exceptions\ApiKeyNotSetException; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Stripe |
10
|
|
|
* |
11
|
|
|
* @package Arcanedev\Stripe |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
abstract class Stripe implements StripeContract |
15
|
|
|
{ |
16
|
|
|
/* ----------------------------------------------------------------- |
17
|
|
|
| Constants |
18
|
|
|
| ----------------------------------------------------------------- |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Library Version. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
const VERSION = '5.2.0'; |
27
|
|
|
|
28
|
|
|
/* ----------------------------------------------------------------- |
29
|
|
|
| Properties |
30
|
|
|
| ----------------------------------------------------------------- |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The Stripe API key to be used for requests. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private static $apiKey; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The Stripe client_id to be used for Connect requests. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
public static $clientId; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The base URL for the Stripe API. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
public static $apiBaseUrl = 'https://api.stripe.com'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The base URL for the OAuth API. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
public static $connectBase = 'https://connect.stripe.com'; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The base URL for the Stripe API uploads endpoint. |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
public static $uploadBaseUrl = 'https://uploads.stripe.com'; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The version of the Stripe API to use for requests. |
70
|
|
|
* |
71
|
|
|
* @var string|null |
72
|
|
|
*/ |
73
|
|
|
public static $apiVersion = null; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* The account ID for connected accounts requests. |
77
|
|
|
* |
78
|
|
|
* @var string|null |
79
|
|
|
*/ |
80
|
|
|
public static $accountId = null; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Verify SSL Certs. |
84
|
|
|
* |
85
|
|
|
* @var bool |
86
|
|
|
*/ |
87
|
|
|
public static $verifySslCerts = true; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* The application's information (name, version, URL). |
91
|
|
|
* |
92
|
|
|
* @var array |
93
|
|
|
*/ |
94
|
|
|
public static $appInfo = []; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* The logger instance. |
98
|
|
|
* |
99
|
|
|
* @var \Psr\Log\LoggerInterface |
100
|
|
|
*/ |
101
|
|
|
public static $logger = null; |
102
|
|
|
|
103
|
|
|
/* ----------------------------------------------------------------- |
104
|
|
|
| Getters & Setters |
105
|
|
|
| ----------------------------------------------------------------- |
106
|
|
|
*/ |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the API key used for requests. |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
414 |
|
public static function getApiKey() |
114
|
|
|
{ |
115
|
414 |
|
return self::$apiKey; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Sets the API key to be used for requests. |
120
|
|
|
* |
121
|
|
|
* @param string $apiKey |
122
|
|
|
*/ |
123
|
690 |
|
public static function setApiKey($apiKey) |
124
|
|
|
{ |
125
|
690 |
|
self::checkApiKey($apiKey); |
126
|
|
|
|
127
|
690 |
|
self::$apiKey = $apiKey; |
128
|
690 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get API Base URL. |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
408 |
|
public static function getApiBaseUrl() |
136
|
|
|
{ |
137
|
408 |
|
return self::$apiBaseUrl; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Set API Base URL. |
142
|
|
|
* |
143
|
|
|
* @param string $apiBaseUrl |
144
|
|
|
*/ |
145
|
6 |
|
public static function setApiBaseUrl($apiBaseUrl) |
146
|
|
|
{ |
147
|
6 |
|
self::checkApiBaseUrl($apiBaseUrl); |
148
|
|
|
|
149
|
2 |
|
self::$apiBaseUrl = $apiBaseUrl; |
150
|
2 |
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* The client_id used for Connect requests. |
154
|
|
|
* |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
6 |
|
public static function getClientId() |
158
|
|
|
{ |
159
|
6 |
|
return self::$clientId; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Sets the client_id to be used for Connect requests. |
164
|
|
|
* |
165
|
|
|
* @param string $clientId |
166
|
|
|
*/ |
167
|
12 |
|
public static function setClientId($clientId) |
168
|
|
|
{ |
169
|
12 |
|
self::$clientId = $clientId; |
170
|
12 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get Upload Base URL. |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
8 |
|
public static function getUploadBaseUrl() |
178
|
|
|
{ |
179
|
8 |
|
return self::$uploadBaseUrl; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Set Upload Base URL. |
184
|
|
|
* |
185
|
|
|
* @param string $uploadBaseUrl |
186
|
|
|
*/ |
187
|
6 |
|
public static function setUploadBaseUrl($uploadBaseUrl) |
188
|
|
|
{ |
189
|
6 |
|
self::checkUploadBaseUrl($uploadBaseUrl); |
190
|
|
|
|
191
|
2 |
|
self::$uploadBaseUrl = $uploadBaseUrl; |
192
|
2 |
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* The API version used for requests. null if we're using the latest version. |
196
|
|
|
* |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
324 |
|
public static function getApiVersion() |
200
|
|
|
{ |
201
|
324 |
|
return self::$apiVersion; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* The API version used for requests. null if we're using the latest version. |
206
|
|
|
* |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
2 |
|
public static function version() |
210
|
|
|
{ |
211
|
2 |
|
return self::getApiVersion(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Sets the API version to use for requests. |
216
|
|
|
* |
217
|
|
|
* @param string $apiVersion |
218
|
|
|
*/ |
219
|
690 |
|
public static function setApiVersion($apiVersion) |
220
|
|
|
{ |
221
|
690 |
|
self::checkApiVersion($apiVersion); |
222
|
|
|
|
223
|
690 |
|
self::$apiVersion = $apiVersion; |
224
|
690 |
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Get the Stripe account ID for connected accounts requests. |
228
|
|
|
* |
229
|
|
|
* @return string|null |
230
|
|
|
*/ |
231
|
2 |
|
public static function getAccountId() |
232
|
|
|
{ |
233
|
2 |
|
return self::$accountId; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Set the Stripe account ID to set for connected accounts requests. |
238
|
|
|
* |
239
|
|
|
* @param string $accountId |
240
|
|
|
*/ |
241
|
2 |
|
public static function setAccountId($accountId) |
242
|
|
|
{ |
243
|
2 |
|
self::$accountId = $accountId; |
244
|
2 |
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Get Verify SSL Certs. |
248
|
|
|
* |
249
|
|
|
* @return bool |
250
|
|
|
*/ |
251
|
2 |
|
public static function getVerifySslCerts() |
252
|
|
|
{ |
253
|
2 |
|
return self::$verifySslCerts; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Sets Verify SSL Certs. |
258
|
|
|
* |
259
|
|
|
* @param bool $verify |
260
|
|
|
*/ |
261
|
18 |
|
public static function setVerifySslCerts($verify) |
262
|
|
|
{ |
263
|
18 |
|
self::$verifySslCerts = validate_bool($verify); |
264
|
18 |
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Get the Application's information. |
268
|
|
|
* |
269
|
|
|
* @return array |
270
|
|
|
*/ |
271
|
314 |
|
public static function getAppInfo() |
272
|
|
|
{ |
273
|
314 |
|
return self::$appInfo; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Set the Application's information. |
278
|
|
|
* |
279
|
|
|
* @param string $name The application's name |
280
|
|
|
* @param string $version The application's version |
281
|
|
|
* @param string $url The application's URL |
282
|
|
|
*/ |
283
|
4 |
|
public static function setAppInfo($name, $version = null, $url = null) |
284
|
|
|
{ |
285
|
4 |
|
self::$appInfo = compact('name', 'version', 'url'); |
286
|
4 |
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Get the logger instance. |
290
|
|
|
* |
291
|
|
|
* @return \Psr\Log\LoggerInterface |
292
|
|
|
*/ |
293
|
|
|
public static function getLogger() |
294
|
|
|
{ |
295
|
|
|
if (is_null(self::$logger)) |
296
|
|
|
return new Utilities\DefaultLogger; |
297
|
|
|
|
298
|
|
|
return self::$logger; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Set the logger instance. |
303
|
|
|
* |
304
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
305
|
|
|
*/ |
306
|
|
|
public static function setLogger(LoggerInterface $logger) |
307
|
|
|
{ |
308
|
|
|
self::$logger = $logger; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/* ----------------------------------------------------------------- |
312
|
|
|
| Main Methods |
313
|
|
|
| ----------------------------------------------------------------- |
314
|
|
|
*/ |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Init Stripe. |
318
|
|
|
* |
319
|
|
|
* @param string $apiKey |
320
|
|
|
*/ |
321
|
2 |
|
public static function init($apiKey) |
322
|
|
|
{ |
323
|
2 |
|
self::setApiKey($apiKey); |
324
|
2 |
|
} |
325
|
|
|
|
326
|
|
|
/* ----------------------------------------------------------------- |
327
|
|
|
| Check Methods |
328
|
|
|
| ----------------------------------------------------------------- |
329
|
|
|
*/ |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Check API Key. |
333
|
|
|
* |
334
|
|
|
* @param string $apiKey |
335
|
|
|
* |
336
|
|
|
* @throws \Arcanedev\Stripe\Exceptions\ApiException |
337
|
|
|
* @throws \Arcanedev\Stripe\Exceptions\ApiKeyNotSetException |
338
|
|
|
*/ |
339
|
690 |
|
private static function checkApiKey(&$apiKey) |
340
|
|
|
{ |
341
|
690 |
|
if ( ! is_string($apiKey)) |
342
|
2 |
|
throw new ApiException('The API KEY must be a string value.', 500); |
343
|
|
|
|
344
|
690 |
|
$apiKey = trim($apiKey); |
345
|
|
|
|
346
|
690 |
|
if (empty($apiKey)) |
347
|
2 |
|
throw new ApiKeyNotSetException('You must specify your api key to use stripe.'); |
348
|
690 |
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Check API Base URL. |
352
|
|
|
* |
353
|
|
|
* @param string $apiBaseUrl |
354
|
|
|
* |
355
|
|
|
* @throws \Arcanedev\Stripe\Exceptions\ApiException |
356
|
|
|
*/ |
357
|
6 |
|
private static function checkApiBaseUrl($apiBaseUrl) |
358
|
|
|
{ |
359
|
6 |
|
if ( ! is_string($apiBaseUrl)) |
360
|
2 |
|
throw new ApiException( |
361
|
2 |
|
'The API base URL be string value. '.gettype($apiBaseUrl).' is given.', 500 |
362
|
|
|
); |
363
|
|
|
|
364
|
4 |
|
if ( ! validate_url($apiBaseUrl)) |
365
|
2 |
|
throw new ApiException('The API base URL is not a valid URL.', 500); |
366
|
2 |
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Check Upload Base URL. |
370
|
|
|
* |
371
|
|
|
* @param string $uploadBaseUrl |
372
|
|
|
* |
373
|
|
|
* @throws \Arcanedev\Stripe\Exceptions\ApiException |
374
|
|
|
*/ |
375
|
6 |
|
private static function checkUploadBaseUrl($uploadBaseUrl) |
376
|
|
|
{ |
377
|
6 |
|
if ( ! is_string($uploadBaseUrl)) { |
378
|
2 |
|
throw new ApiException( |
379
|
2 |
|
'The Upload base URL be string value. '.gettype($uploadBaseUrl).' is given.', 500 |
380
|
|
|
); |
381
|
|
|
} |
382
|
|
|
|
383
|
4 |
|
if ( ! validate_url($uploadBaseUrl)) |
384
|
2 |
|
throw new ApiException('The Upload base URL is not a valid URL.', 500); |
385
|
2 |
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Check API Version. |
389
|
|
|
* |
390
|
|
|
* @param string|null $apiVersion |
391
|
|
|
* |
392
|
|
|
* @throws \Arcanedev\Stripe\Exceptions\ApiException |
393
|
|
|
*/ |
394
|
690 |
|
private static function checkApiVersion(&$apiVersion) |
395
|
|
|
{ |
396
|
|
|
if ( |
397
|
690 |
|
! is_null($apiVersion) && |
398
|
690 |
|
! is_string($apiVersion) |
399
|
|
|
) |
400
|
2 |
|
throw new ApiException( |
401
|
2 |
|
'The API version must be a null or string value. '.gettype($apiVersion).' is given.', 500 |
402
|
|
|
); |
403
|
|
|
|
404
|
690 |
|
if (is_null($apiVersion)) return; |
405
|
|
|
|
406
|
690 |
|
$apiVersion = trim($apiVersion); |
407
|
|
|
|
408
|
690 |
|
if ( ! validate_version($apiVersion)) |
409
|
2 |
|
throw new ApiException('The API version must valid a semantic version [x.x.x].', 500); |
410
|
690 |
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Check if API version exists. |
414
|
|
|
* |
415
|
|
|
* @return bool |
416
|
|
|
*/ |
417
|
314 |
|
public static function hasApiVersion() |
418
|
|
|
{ |
419
|
314 |
|
return ! empty(self::$apiVersion); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Check if the Stripe has account ID for connected accounts requests. |
424
|
|
|
* |
425
|
|
|
* @return bool |
426
|
|
|
*/ |
427
|
314 |
|
public static function hasAccountId() |
428
|
|
|
{ |
429
|
314 |
|
return ! empty(self::$accountId); |
430
|
|
|
} |
431
|
|
|
} |
432
|
|
|
|