1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SmartCall Restful API (v3) HTTP Client. |
4
|
|
|
* |
5
|
|
|
* PLEASE NOTE: The interface is very fluid while the intial integration |
6
|
|
|
* is taking place. It will be refactored in the near future. |
7
|
|
|
* |
8
|
|
|
* @author Jacques Marneweck <[email protected]> |
9
|
|
|
* @copyright 2017-2018 Jacques Marneweck. All rights strictly reserved. |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Jacques\Smartcall\HttpClient; |
14
|
|
|
|
15
|
|
|
class Client extends \GuzzleHttp\Client |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @const string Version number |
19
|
|
|
*/ |
20
|
|
|
const VERSION = '0.0.1'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Defaults to expecting that Apache Tomcat runs on port 8080 on localhost |
24
|
|
|
* (127.0.0.1). |
25
|
|
|
* |
26
|
|
|
* @var array[] |
27
|
|
|
*/ |
28
|
|
|
protected $options = [ |
29
|
|
|
'scheme' => 'https', |
30
|
|
|
'hostname' => 'localhost', |
31
|
|
|
'port' => '8080', |
32
|
|
|
'token' => null, |
33
|
|
|
'username' => null, |
34
|
|
|
'password' => null, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $options array |
39
|
|
|
*/ |
40
|
17 |
|
public function __construct($options = []) |
41
|
|
|
{ |
42
|
|
|
/* |
43
|
|
|
* Allow on instantiation to overwrite the defaults |
44
|
|
|
*/ |
45
|
17 |
|
$this->options = array_merge( |
|
|
|
|
46
|
17 |
|
$this->options, |
47
|
17 |
|
$options |
48
|
|
|
); |
49
|
|
|
$config = [ |
50
|
17 |
|
'base_uri' => sprintf( |
51
|
17 |
|
'%s://%s:%s/', |
52
|
17 |
|
$this->options['scheme'], |
53
|
17 |
|
$this->options['hostname'], |
54
|
17 |
|
$this->options['port'] |
55
|
|
|
), |
56
|
|
|
'verify' => false, |
57
|
|
|
'headers' => [ |
58
|
17 |
|
'User-Agent' => 'SmartcallRestfulAPIClient-PHP/'.self::VERSION.' '.\GuzzleHttp\default_user_agent(), |
59
|
|
|
], |
60
|
|
|
]; |
61
|
17 |
|
parent::__construct($config); |
62
|
17 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set the bearer token. |
66
|
|
|
* |
67
|
|
|
* @param string $token Bearer Token from Auth request |
68
|
|
|
*/ |
69
|
9 |
|
public function setBearerToken($token) |
70
|
|
|
{ |
71
|
9 |
|
$this->options['token'] = $token; |
72
|
9 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Authenticate and get Bearer token from SmartCall. |
76
|
|
|
* |
77
|
|
|
* @param string $username |
78
|
|
|
* @param string $password |
79
|
|
|
* |
80
|
|
|
* @throws Exception |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
3 |
View Code Duplication |
public function auth($username, $password) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
try { |
87
|
3 |
|
$response = $this->post( |
88
|
3 |
|
'/webservice/auth', |
89
|
|
|
[ |
90
|
|
|
'headers' => [ |
91
|
3 |
|
'Authorization' => sprintf( |
92
|
3 |
|
'Basic %s', |
93
|
3 |
|
base64_encode( |
94
|
3 |
|
sprintf( |
95
|
3 |
|
'%s:%s', |
96
|
3 |
|
$username, |
97
|
3 |
|
$password |
98
|
|
|
) |
99
|
|
|
) |
100
|
|
|
), |
101
|
|
|
], |
102
|
|
|
] |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
return [ |
106
|
1 |
|
'status' => 'ok', |
107
|
1 |
|
'http_code' => $response->getStatusCode(), |
108
|
1 |
|
'body' => (string) $response->getBody(), |
109
|
|
|
]; |
110
|
2 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
111
|
2 |
|
return $this->clientError($e); |
112
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
113
|
|
|
return $this->parseError($e); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Authenticate and invalidates all the user allocated tokens. |
119
|
|
|
* |
120
|
|
|
* @throws Exception |
121
|
|
|
* |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
2 |
View Code Duplication |
public function authDelete() |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
try { |
127
|
2 |
|
$response = $this->delete( |
128
|
2 |
|
'/webservice/auth', |
129
|
|
|
[ |
130
|
|
|
'headers' => [ |
131
|
2 |
|
'Authorization' => sprintf( |
132
|
2 |
|
'Bearer %s', |
133
|
2 |
|
$this->options['token'] |
134
|
|
|
), |
135
|
|
|
], |
136
|
|
|
] |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
return [ |
140
|
1 |
|
'status' => 'ok', |
141
|
1 |
|
'http_code' => $response->getStatusCode(), |
142
|
1 |
|
'body' => (string) $response->getBody(), |
143
|
|
|
]; |
144
|
1 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
145
|
1 |
|
return $this->clientError($e); |
146
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
147
|
|
|
return $this->parseError($e); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Authenticate and invalidates all the user allocated tokens. |
153
|
|
|
* |
154
|
|
|
* @param string $username |
155
|
|
|
* @param string $password |
156
|
|
|
* |
157
|
|
|
* @throws Exception |
158
|
|
|
* |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
|
View Code Duplication |
public function authFlush($username, $password) |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
try { |
164
|
|
|
$response = $this->delete( |
165
|
|
|
'/webservice/auth/token', |
166
|
|
|
[ |
167
|
|
|
'headers' => [ |
168
|
|
|
'Authorization' => sprintf( |
169
|
|
|
'Basic %s', |
170
|
|
|
base64_encode( |
171
|
|
|
sprintf( |
172
|
|
|
'%s:%s', |
173
|
|
|
$username, |
174
|
|
|
$password |
175
|
|
|
) |
176
|
|
|
) |
177
|
|
|
), |
178
|
|
|
], |
179
|
|
|
] |
180
|
|
|
); |
181
|
|
|
|
182
|
|
|
return [ |
183
|
|
|
'status' => 'ok', |
184
|
|
|
'http_code' => $response->getStatusCode(), |
185
|
|
|
'body' => (string) $response->getBody(), |
186
|
|
|
]; |
187
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
188
|
|
|
return $this->clientError($e); |
189
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
190
|
|
|
return $this->parseError($e); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Authenticate and gets the number of available session tokens. |
196
|
|
|
* |
197
|
|
|
* @param string $username |
198
|
|
|
* @param string $password |
199
|
|
|
* |
200
|
|
|
* @throws Exception |
201
|
|
|
* |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
3 |
View Code Duplication |
public function authToken($username, $password) |
|
|
|
|
205
|
|
|
{ |
206
|
|
|
try { |
207
|
3 |
|
$response = $this->get( |
208
|
3 |
|
'/webservice/auth/token', |
209
|
|
|
[ |
210
|
|
|
'headers' => [ |
211
|
3 |
|
'Authorization' => sprintf( |
212
|
3 |
|
'Basic %s', |
213
|
3 |
|
base64_encode( |
214
|
3 |
|
sprintf( |
215
|
3 |
|
'%s:%s', |
216
|
3 |
|
$username, |
217
|
3 |
|
$password |
218
|
|
|
) |
219
|
|
|
) |
220
|
|
|
), |
221
|
|
|
], |
222
|
|
|
] |
223
|
|
|
); |
224
|
|
|
|
225
|
|
|
return [ |
226
|
1 |
|
'status' => 'ok', |
227
|
1 |
|
'http_code' => $response->getStatusCode(), |
228
|
1 |
|
'body' => (string) $response->getBody(), |
229
|
|
|
]; |
230
|
2 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
231
|
2 |
|
return $this->clientError($e); |
232
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
233
|
|
|
return $this->parseError($e); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Authenticate and request period based cashup reports. |
239
|
|
|
* |
240
|
|
|
* @param string $dealerMsisdn |
241
|
|
|
* @param string $start |
242
|
|
|
* @param string $end |
243
|
|
|
* |
244
|
|
|
* @throws Exception |
245
|
|
|
* |
246
|
|
|
* @return array |
247
|
|
|
*/ |
248
|
|
|
public function cashup($dealerMsisdn, $start, $end) |
249
|
|
|
{ |
250
|
|
|
try { |
251
|
|
|
$response = $this->post( |
252
|
|
|
'/webservice/smartload/cashup', |
253
|
|
|
[ |
254
|
|
|
'headers' => [ |
255
|
|
|
'Authorization' => sprintf( |
256
|
|
|
'Bearer %s', |
257
|
|
|
$this->options['token'] |
258
|
|
|
), |
259
|
|
|
], |
260
|
|
|
'json' => [ |
261
|
|
|
'smartloadId' => $dealerMsisdn, |
262
|
|
|
'startDate' => $start, |
263
|
|
|
'endDate' => $end, |
264
|
|
|
], |
265
|
|
|
] |
266
|
|
|
); |
267
|
|
|
|
268
|
|
|
return [ |
269
|
|
|
'status' => 'ok', |
270
|
|
|
'http_code' => $response->getStatusCode(), |
271
|
|
|
'body' => (string) $response->getBody(), |
272
|
|
|
]; |
273
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
274
|
|
|
return $this->clientError($e); |
275
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
276
|
|
|
return $this->parseError($e); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Authenticate and request current day cashup report. |
282
|
|
|
* |
283
|
|
|
* @param string $dealerMsisdn |
284
|
|
|
* |
285
|
|
|
* @throws Exception |
286
|
|
|
* |
287
|
|
|
* @return array |
288
|
|
|
*/ |
289
|
|
View Code Duplication |
public function cashupToday($dealerMsisdn) |
|
|
|
|
290
|
|
|
{ |
291
|
|
|
try { |
292
|
|
|
$response = $this->get( |
293
|
|
|
sprintf( |
294
|
|
|
'/webservice/smartload/cashup/%s', |
295
|
|
|
$dealerMsisdn |
296
|
|
|
), |
297
|
|
|
[ |
298
|
|
|
'headers' => [ |
299
|
|
|
'Authorization' => sprintf( |
300
|
|
|
'Bearer %s', |
301
|
|
|
$this->options['token'] |
302
|
|
|
), |
303
|
|
|
], |
304
|
|
|
] |
305
|
|
|
); |
306
|
|
|
|
307
|
|
|
return [ |
308
|
|
|
'status' => 'ok', |
309
|
|
|
'http_code' => $response->getStatusCode(), |
310
|
|
|
'body' => (string) $response->getBody(), |
311
|
|
|
]; |
312
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
313
|
|
|
return $this->clientError($e); |
314
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
315
|
|
|
return $this->parseError($e); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Authenticate and retrieves a list of all available networks. |
321
|
|
|
* |
322
|
|
|
* @throws Exception |
323
|
|
|
* |
324
|
|
|
* @return array |
325
|
|
|
*/ |
326
|
2 |
View Code Duplication |
public function network($id) |
|
|
|
|
327
|
|
|
{ |
328
|
|
|
try { |
329
|
2 |
|
$response = $this->get( |
330
|
2 |
|
sprintf( |
331
|
2 |
|
'/webservice/smartload/networks/%d', |
332
|
2 |
|
$id |
333
|
|
|
), |
334
|
|
|
[ |
335
|
|
|
'headers' => [ |
336
|
2 |
|
'Authorization' => sprintf( |
337
|
2 |
|
'Bearer %s', |
338
|
2 |
|
$this->options['token'] |
339
|
|
|
), |
340
|
|
|
], |
341
|
|
|
] |
342
|
|
|
); |
343
|
|
|
|
344
|
|
|
return [ |
345
|
1 |
|
'status' => 'ok', |
346
|
1 |
|
'http_code' => $response->getStatusCode(), |
347
|
1 |
|
'body' => (string) $response->getBody(), |
348
|
|
|
]; |
349
|
1 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
350
|
1 |
|
return $this->clientError($e); |
351
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
352
|
|
|
return $this->parseError($e); |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Authenticate and retrieves a list of all available networks. |
358
|
|
|
* |
359
|
|
|
* @throws Exception |
360
|
|
|
* |
361
|
|
|
* @return array |
362
|
|
|
*/ |
363
|
2 |
View Code Duplication |
public function networks() |
|
|
|
|
364
|
|
|
{ |
365
|
|
|
try { |
366
|
2 |
|
$response = $this->get( |
367
|
2 |
|
'/webservice/smartload/networks', |
368
|
|
|
[ |
369
|
|
|
'headers' => [ |
370
|
2 |
|
'Authorization' => sprintf( |
371
|
2 |
|
'Bearer %s', |
372
|
2 |
|
$this->options['token'] |
373
|
|
|
), |
374
|
|
|
], |
375
|
|
|
] |
376
|
|
|
); |
377
|
|
|
|
378
|
|
|
return [ |
379
|
1 |
|
'status' => 'ok', |
380
|
1 |
|
'http_code' => $response->getStatusCode(), |
381
|
1 |
|
'body' => (string) $response->getBody(), |
382
|
|
|
]; |
383
|
1 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
384
|
1 |
|
return $this->clientError($e); |
385
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
386
|
|
|
return $this->parseError($e); |
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Test SmartCall is responding. |
392
|
|
|
* |
393
|
|
|
* @throws Exception |
394
|
|
|
* |
395
|
|
|
* @return array |
396
|
|
|
*/ |
397
|
1 |
|
public function ping() |
398
|
|
|
{ |
399
|
|
|
try { |
400
|
1 |
|
$response = $this->get( |
401
|
1 |
|
'/webservice/test/ping' |
402
|
|
|
); |
403
|
|
|
|
404
|
|
|
return [ |
405
|
1 |
|
'status' => 'ok', |
406
|
1 |
|
'http_code' => $response->getStatusCode(), |
407
|
1 |
|
'body' => (string) $response->getBody(), |
408
|
|
|
]; |
409
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
410
|
|
|
return $this->clientError($e); |
411
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
412
|
|
|
return $this->parseError($e); |
413
|
|
|
} |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Authenticate and retrieves a list of all available networks. |
418
|
|
|
* |
419
|
|
|
* @throws Exception |
420
|
|
|
* |
421
|
|
|
* @return array |
422
|
|
|
*/ |
423
|
2 |
View Code Duplication |
public function products($id) |
|
|
|
|
424
|
|
|
{ |
425
|
|
|
try { |
426
|
2 |
|
$response = $this->get( |
427
|
2 |
|
sprintf( |
428
|
2 |
|
'/webservice/smartload/products/%d', |
429
|
2 |
|
$id |
430
|
|
|
), |
431
|
|
|
[ |
432
|
|
|
'headers' => [ |
433
|
2 |
|
'Authorization' => sprintf( |
434
|
2 |
|
'Bearer %s', |
435
|
2 |
|
$this->options['token'] |
436
|
|
|
), |
437
|
|
|
], |
438
|
|
|
] |
439
|
|
|
); |
440
|
|
|
|
441
|
|
|
return [ |
442
|
1 |
|
'status' => 'ok', |
443
|
1 |
|
'http_code' => $response->getStatusCode(), |
444
|
1 |
|
'body' => (string) $response->getBody(), |
445
|
|
|
]; |
446
|
1 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
447
|
1 |
|
return $this->clientError($e); |
448
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
449
|
|
|
return $this->parseError($e); |
450
|
|
|
} |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Parse the java exception that we receive from Smartcall's Tomcat's. |
455
|
|
|
* |
456
|
|
|
* @param \GuzzleHttp\Exception\ServerException $exception |
457
|
|
|
* |
458
|
|
|
* @return array |
459
|
|
|
*/ |
460
|
8 |
View Code Duplication |
private function clientError(\GuzzleHttp\Exception\ClientException $exception) |
|
|
|
|
461
|
|
|
{ |
462
|
8 |
|
$body = (string) $exception->getResponse()->getBody(); |
463
|
|
|
|
464
|
|
|
return [ |
465
|
8 |
|
'status' => 'error', |
466
|
8 |
|
'http_code' => $exception->getResponse()->getStatusCode(), |
467
|
8 |
|
'body' => json_decode($body), |
468
|
|
|
]; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* Parse the java exception that we receive from Smartcall's Tomcat's. |
473
|
|
|
* |
474
|
|
|
* @param \GuzzleHttp\Exception\ServerException $exception |
475
|
|
|
* |
476
|
|
|
* @return array |
477
|
|
|
*/ |
478
|
|
View Code Duplication |
private function parseError(\GuzzleHttp\Exception\ServerException $exception) |
|
|
|
|
479
|
|
|
{ |
480
|
|
|
$body = (string) $exception->getResponse()->getBody(); |
481
|
|
|
preg_match('!<p><b>type</b> Exception report</p><p><b>message</b> <u>(.*[^</u>])</u></p><p><b>description</b>!', $body, $matches); |
482
|
|
|
return [ |
483
|
|
|
'status' => 'error', |
484
|
|
|
'http_code' => $exception->getResponse()->getStatusCode(), |
485
|
|
|
'body' => $matches['1'], |
486
|
|
|
]; |
487
|
|
|
} |
488
|
|
|
} |
489
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..