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\Traits; |
14
|
|
|
|
15
|
|
|
trait SmartLoad |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Authenticate and retrieves the dealer balance in Rands. |
19
|
|
|
* |
20
|
|
|
* @param string $dealerMsisdn |
21
|
|
|
* |
22
|
|
|
* @throws Exception |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
4 |
View Code Duplication |
public function balance($dealerMsisdn) |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
try { |
29
|
4 |
|
$response = $this->get( |
|
|
|
|
30
|
4 |
|
sprintf( |
31
|
4 |
|
'/webservice/smartload/balance/%s', |
32
|
4 |
|
$dealerMsisdn |
33
|
|
|
), |
34
|
|
|
[ |
35
|
|
|
'headers' => [ |
36
|
4 |
|
'Authorization' => $this->bearerOrBasic(), |
|
|
|
|
37
|
|
|
], |
38
|
|
|
] |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
return [ |
42
|
2 |
|
'status' => 'ok', |
43
|
2 |
|
'http_code' => $response->getStatusCode(), |
44
|
2 |
|
'body' => (string) $response->getBody(), |
45
|
|
|
]; |
46
|
2 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
47
|
2 |
|
return $this->clientError($e); |
|
|
|
|
48
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
49
|
|
|
return $this->parseError($e); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Authenticate and request to cancel a previous recharge request. Will only |
55
|
|
|
* succeed if the recharge has not been successfully submitted to the network. |
56
|
|
|
* |
57
|
|
|
* @param string $dealerMsisdn |
58
|
|
|
* @param string $clientReference |
59
|
|
|
* |
60
|
|
|
* @throws Exception |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function cancelRecharge($dealerMsisdn, $clientReference) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
try { |
67
|
|
|
$response = $this->delete( |
|
|
|
|
68
|
|
|
sprintf( |
69
|
|
|
'/webservice/smartload/recharges/%s/%s', |
70
|
|
|
$dealerMsisdn, |
71
|
|
|
$clientReference |
72
|
|
|
), |
73
|
|
|
[ |
74
|
|
|
'headers' => [ |
75
|
|
|
'Authorization' => $this->bearerOrBasic(), |
|
|
|
|
76
|
|
|
], |
77
|
|
|
] |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
return [ |
81
|
|
|
'status' => 'ok', |
82
|
|
|
'http_code' => $response->getStatusCode(), |
83
|
|
|
'body' => (string) $response->getBody(), |
84
|
|
|
]; |
85
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
86
|
|
|
return $this->clientError($e); |
|
|
|
|
87
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
88
|
|
|
return $this->parseError($e); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Authenticate and request period based cashup reports. |
94
|
|
|
* |
95
|
|
|
* @param string $dealerMsisdn |
96
|
|
|
* @param string $start |
97
|
|
|
* @param string $end |
98
|
|
|
* |
99
|
|
|
* @throws Exception |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function cashup($dealerMsisdn, $start, $end) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
try { |
106
|
|
|
$response = $this->post( |
|
|
|
|
107
|
|
|
'/webservice/smartload/cashup', |
108
|
|
|
[ |
109
|
|
|
'headers' => [ |
110
|
|
|
'Authorization' => $this->bearerOrBasic(), |
|
|
|
|
111
|
|
|
], |
112
|
|
|
'json' => [ |
113
|
|
|
'smartloadId' => $dealerMsisdn, |
114
|
|
|
'startDate' => $start, |
115
|
|
|
'endDate' => $end, |
116
|
|
|
], |
117
|
|
|
] |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
return [ |
121
|
|
|
'status' => 'ok', |
122
|
|
|
'http_code' => $response->getStatusCode(), |
123
|
|
|
'body' => (string) $response->getBody(), |
124
|
|
|
]; |
125
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
126
|
|
|
return $this->clientError($e); |
|
|
|
|
127
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
128
|
|
|
return $this->parseError($e); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Authenticate and request current day cashup report. |
134
|
|
|
* |
135
|
|
|
* @param string $dealerMsisdn |
136
|
|
|
* |
137
|
|
|
* @throws Exception |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
View Code Duplication |
public function cashupToday($dealerMsisdn) |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
try { |
144
|
|
|
$response = $this->get( |
|
|
|
|
145
|
|
|
sprintf( |
146
|
|
|
'/webservice/smartload/cashup/%s', |
147
|
|
|
$dealerMsisdn |
148
|
|
|
), |
149
|
|
|
[ |
150
|
|
|
'headers' => [ |
151
|
|
|
'Authorization' => $this->bearerOrBasic(), |
|
|
|
|
152
|
|
|
], |
153
|
|
|
] |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
return [ |
157
|
|
|
'status' => 'ok', |
158
|
|
|
'http_code' => $response->getStatusCode(), |
159
|
|
|
'body' => (string) $response->getBody(), |
160
|
|
|
]; |
161
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
162
|
|
|
return $this->clientError($e); |
|
|
|
|
163
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
164
|
|
|
return $this->parseError($e); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Authenticate and request to transfer funds from one Smartload account to another. |
170
|
|
|
* |
171
|
|
|
* @param string $fromDealerMsisdn |
172
|
|
|
* @param string $toDealerMsisdn |
173
|
|
|
* @param string $amount |
174
|
|
|
* @param string $sendSms |
175
|
|
|
* |
176
|
|
|
* @throws Exception |
177
|
|
|
* |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
3 |
View Code Duplication |
public function fundstransfer($fromDealerMsisdn, $toDealerMsisdn, $amount, $sendSms) |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
try { |
183
|
3 |
|
$response = $this->post( |
|
|
|
|
184
|
3 |
|
'/webservice/smartload/fundstransfer', |
185
|
|
|
[ |
186
|
|
|
'headers' => [ |
187
|
3 |
|
'Authorization' => $this->bearerOrBasic(), |
|
|
|
|
188
|
|
|
], |
189
|
|
|
'json' => [ |
190
|
3 |
|
'sourceSmartloadId' => $fromDealerMsisdn, |
191
|
3 |
|
'recipientSmartloadId' => $toDealerMsisdn, |
192
|
3 |
|
'amount' => $amount, |
193
|
3 |
|
'sendSms' => $sendSms, |
194
|
|
|
], |
195
|
|
|
] |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
return [ |
199
|
2 |
|
'status' => 'ok', |
200
|
2 |
|
'http_code' => $response->getStatusCode(), |
201
|
2 |
|
'body' => (string) $response->getBody(), |
202
|
|
|
]; |
203
|
1 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
204
|
1 |
|
return $this->clientError($e); |
|
|
|
|
205
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
206
|
|
|
return $this->parseError($e); |
|
|
|
|
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Authenticate and retrieves a list of all available networks. |
212
|
|
|
* |
213
|
|
|
* @throws Exception |
214
|
|
|
* |
215
|
|
|
* @return array |
216
|
|
|
*/ |
217
|
2 |
View Code Duplication |
public function network($id) |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
try { |
220
|
2 |
|
$response = $this->get( |
|
|
|
|
221
|
2 |
|
sprintf( |
222
|
2 |
|
'/webservice/smartload/networks/%d', |
223
|
2 |
|
$id |
224
|
|
|
), |
225
|
|
|
[ |
226
|
|
|
'headers' => [ |
227
|
2 |
|
'Authorization' => $this->bearerOrBasic(), |
|
|
|
|
228
|
|
|
], |
229
|
|
|
] |
230
|
|
|
); |
231
|
|
|
|
232
|
|
|
return [ |
233
|
1 |
|
'status' => 'ok', |
234
|
1 |
|
'http_code' => $response->getStatusCode(), |
235
|
1 |
|
'body' => (string) $response->getBody(), |
236
|
|
|
]; |
237
|
1 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
238
|
1 |
|
return $this->clientError($e); |
|
|
|
|
239
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
240
|
|
|
return $this->parseError($e); |
|
|
|
|
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Authenticate and retrieves a list of all available networks. |
246
|
|
|
* |
247
|
|
|
* @throws Exception |
248
|
|
|
* |
249
|
|
|
* @return array |
250
|
|
|
*/ |
251
|
2 |
View Code Duplication |
public function networks() |
|
|
|
|
252
|
|
|
{ |
253
|
|
|
try { |
254
|
2 |
|
$response = $this->get( |
|
|
|
|
255
|
2 |
|
'/webservice/smartload/networks', |
256
|
|
|
[ |
257
|
|
|
'headers' => [ |
258
|
2 |
|
'Authorization' => $this->bearerOrBasic(), |
|
|
|
|
259
|
|
|
], |
260
|
|
|
] |
261
|
|
|
); |
262
|
|
|
|
263
|
|
|
return [ |
264
|
1 |
|
'status' => 'ok', |
265
|
1 |
|
'http_code' => $response->getStatusCode(), |
266
|
1 |
|
'body' => (string) $response->getBody(), |
267
|
|
|
]; |
268
|
1 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
269
|
1 |
|
return $this->clientError($e); |
|
|
|
|
270
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
271
|
|
|
return $this->parseError($e); |
|
|
|
|
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Authenticate and recharge prevend request. |
277
|
|
|
* |
278
|
|
|
* @param string $dealerMsisdn |
|
|
|
|
279
|
|
|
* @param string $clientReference |
|
|
|
|
280
|
|
|
* @param string $smsRecipientMsisdn |
|
|
|
|
281
|
|
|
* @param string $deviceId |
|
|
|
|
282
|
|
|
* @param int $productId |
|
|
|
|
283
|
|
|
* @param int $amount |
|
|
|
|
284
|
|
|
* @param bool $pinless |
|
|
|
|
285
|
|
|
* @param bool $sendSms |
|
|
|
|
286
|
|
|
* |
287
|
|
|
* |
288
|
|
|
* { |
289
|
|
|
* "smartloadId": "27821234567", |
290
|
|
|
* "clientReference": "abc123", |
291
|
|
|
* "smsRecipientMsisdn": "27821234567", |
292
|
|
|
* "deviceId": "27821234567", |
293
|
|
|
* "productId": 62, |
294
|
|
|
* "amount": 12, |
295
|
|
|
* "pinless": true, |
296
|
|
|
* "sendSms": true |
297
|
|
|
* } |
298
|
|
|
*/ |
299
|
|
|
public function prevend() |
300
|
|
|
{ |
301
|
|
|
try { |
302
|
|
|
$response = $this->post( |
|
|
|
|
303
|
|
|
sprintf( |
304
|
|
|
'/webservice/smartload/products/%d', |
305
|
|
|
$id |
|
|
|
|
306
|
|
|
), |
307
|
|
|
[ |
308
|
|
|
'headers' => [ |
309
|
|
|
'Authorization' => $this->bearerOrBasic(), |
|
|
|
|
310
|
|
|
], |
311
|
|
|
'json' => [ |
312
|
|
|
'smartloadId' => '', |
313
|
|
|
'clientReference' => '', |
314
|
|
|
'smsRecipientMsisdn' => '', |
315
|
|
|
'deviceId' => '', |
316
|
|
|
'productId' => '', |
317
|
|
|
'amount' => '', |
318
|
|
|
'pinless' => '', |
319
|
|
|
'sendSms' => '', |
320
|
|
|
], |
321
|
|
|
] |
322
|
|
|
); |
323
|
|
|
|
324
|
|
|
return [ |
325
|
|
|
'status' => 'ok', |
326
|
|
|
'http_code' => $response->getStatusCode(), |
327
|
|
|
'body' => (string) $response->getBody(), |
328
|
|
|
]; |
329
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
330
|
|
|
return $this->clientError($e); |
|
|
|
|
331
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
332
|
|
|
return $this->parseError($e); |
|
|
|
|
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Authenticate and retrieves a list of all available networks. |
338
|
|
|
* |
339
|
|
|
* @param int $id |
340
|
|
|
* |
341
|
|
|
* @throws Exception |
342
|
|
|
* |
343
|
|
|
* @return array |
344
|
|
|
*/ |
345
|
2 |
View Code Duplication |
public function products($id) |
|
|
|
|
346
|
|
|
{ |
347
|
|
|
try { |
348
|
2 |
|
$response = $this->get( |
|
|
|
|
349
|
2 |
|
sprintf( |
350
|
2 |
|
'/webservice/smartload/products/%d', |
351
|
2 |
|
$id |
352
|
|
|
), |
353
|
|
|
[ |
354
|
|
|
'headers' => [ |
355
|
2 |
|
'Authorization' => $this->bearerOrBasic(), |
|
|
|
|
356
|
|
|
], |
357
|
|
|
] |
358
|
|
|
); |
359
|
|
|
|
360
|
|
|
return [ |
361
|
1 |
|
'status' => 'ok', |
362
|
1 |
|
'http_code' => $response->getStatusCode(), |
363
|
1 |
|
'body' => (string) $response->getBody(), |
364
|
|
|
]; |
365
|
1 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
366
|
1 |
|
return $this->clientError($e); |
|
|
|
|
367
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
368
|
|
|
return $this->parseError($e); |
|
|
|
|
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Authenticate and recharge request. |
374
|
|
|
* |
375
|
|
|
* @param string $dealerMsisdn |
376
|
|
|
* @param string $clientReference |
377
|
|
|
* @param string $smsRecipientMsisdn |
378
|
|
|
* @param string $deviceId |
379
|
|
|
* @param int $productId |
380
|
|
|
* @param int $amount |
381
|
|
|
* @param bool $pinless |
382
|
|
|
* @param bool $sendSms |
383
|
|
|
*/ |
384
|
2 |
|
public function recharge($dealerMsisdn, $clientReference, $smsRecipientMsisdn, $deviceId, $productId, $amount, $pinless, $sendSms) |
385
|
|
|
{ |
386
|
|
|
try { |
387
|
2 |
|
$response = $this->post( |
|
|
|
|
388
|
2 |
|
'/webservice/smartload/recharges', |
389
|
|
|
[ |
390
|
|
|
'headers' => [ |
391
|
2 |
|
'Authorization' => $this->bearerOrBasic(), |
|
|
|
|
392
|
|
|
], |
393
|
|
|
'json' => [ |
394
|
2 |
|
'smartloadId' => $dealerMsisdn, |
395
|
2 |
|
'clientReference' => $clientReference, |
396
|
2 |
|
'smsRecipientMsisdn' => $smsRecipientMsisdn, |
397
|
2 |
|
'deviceId' => $deviceId, |
398
|
2 |
|
'productId' => $productId, |
399
|
2 |
|
'amount' => $amount, |
400
|
2 |
|
'pinless' => $pinless, |
401
|
2 |
|
'sendSms' => $sendSms, |
402
|
|
|
], |
403
|
|
|
] |
404
|
|
|
); |
405
|
|
|
|
406
|
|
|
return [ |
407
|
1 |
|
'status' => 'ok', |
408
|
1 |
|
'http_code' => $response->getStatusCode(), |
409
|
1 |
|
'body' => (string) $response->getBody(), |
410
|
|
|
]; |
411
|
1 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
412
|
1 |
|
return $this->clientError($e); |
|
|
|
|
413
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
414
|
|
|
return $this->parseError($e); |
|
|
|
|
415
|
|
|
} |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Authenticate and checks if the provided ID (MSISDN) is registered with Smartload. |
420
|
|
|
* |
421
|
|
|
* @param string $dealerMsisdn |
422
|
|
|
* |
423
|
|
|
* @throws Exception |
424
|
|
|
* |
425
|
|
|
* @return array |
426
|
|
|
*/ |
427
|
3 |
View Code Duplication |
public function registered($dealerMsisdn) |
|
|
|
|
428
|
|
|
{ |
429
|
|
|
try { |
430
|
3 |
|
$response = $this->get( |
|
|
|
|
431
|
3 |
|
sprintf( |
432
|
3 |
|
'/webservice/smartload/registered/%s', |
433
|
3 |
|
$dealerMsisdn |
434
|
|
|
), |
435
|
|
|
[ |
436
|
|
|
'headers' => [ |
437
|
3 |
|
'Authorization' => $this->bearerOrBasic(), |
|
|
|
|
438
|
|
|
], |
439
|
|
|
] |
440
|
|
|
); |
441
|
|
|
|
442
|
|
|
return [ |
443
|
2 |
|
'status' => 'ok', |
444
|
2 |
|
'http_code' => $response->getStatusCode(), |
445
|
2 |
|
'body' => (string) $response->getBody(), |
446
|
|
|
]; |
447
|
1 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
448
|
1 |
|
return $this->clientError($e); |
|
|
|
|
449
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
450
|
|
|
return $this->parseError($e); |
|
|
|
|
451
|
|
|
} |
452
|
|
|
} |
453
|
|
|
} |
454
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.