1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Client for Smartcall's Restful Proxy. |
4
|
|
|
* |
5
|
|
|
* @author Jacques Marneweck <[email protected]> |
6
|
|
|
* @copyright 2016 Jacques Marneweck. All rights strictly reserved. |
7
|
|
|
* @license MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Jacques\SmartCallProxy; |
11
|
|
|
|
12
|
|
|
class Client extends \GuzzleHttp\Client |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @const string Version number |
16
|
|
|
*/ |
17
|
|
|
const VERSION = '0.0.1'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Defaults to expecting that Apache Tomcat runs on port 8080 on localhost |
21
|
|
|
* (127.0.0.1). |
22
|
|
|
* |
23
|
|
|
* @var array[] |
24
|
|
|
*/ |
25
|
|
|
protected $options = [ |
26
|
|
|
'scheme' => 'http', |
27
|
|
|
'hostname' => 'localhost', |
28
|
|
|
'port' => '8080', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param $options array |
33
|
|
|
* |
34
|
|
|
* @return void |
|
|
|
|
35
|
|
|
*/ |
36
|
2 |
|
public function __construct($options = []) |
37
|
|
|
{ |
38
|
|
|
/* |
39
|
|
|
* Allow on instantiation to overwrite the defaults |
40
|
|
|
*/ |
41
|
2 |
|
$this->options = array_merge( |
|
|
|
|
42
|
2 |
|
$this->options, |
43
|
|
|
$options |
44
|
2 |
|
); |
45
|
|
|
|
46
|
|
|
$config = [ |
47
|
2 |
|
'base_uri' => sprintf( |
48
|
2 |
|
'%s://%s:%s/', |
49
|
2 |
|
$this->options['scheme'], |
50
|
2 |
|
$this->options['hostname'], |
51
|
2 |
|
$this->options['port'] |
52
|
2 |
|
), |
53
|
|
|
'headers' => [ |
54
|
2 |
|
'User-Agent' => 'SmartcallRestfulProxyClient-PHP/'.self::VERSION.' '.\GuzzleHttp\default_user_agent(), |
55
|
2 |
|
], |
56
|
2 |
|
]; |
57
|
|
|
|
58
|
2 |
|
parent::__construct($config); |
59
|
2 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Fetches the Dealer Balance from SmartCall. |
63
|
|
|
* |
64
|
|
|
* @throws Exception |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
1 |
View Code Duplication |
public function getDealerBalance() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
try { |
71
|
1 |
|
$response = $this->get('/SmartcallRestfulProxy/balance'); |
72
|
|
|
|
73
|
|
|
return [ |
74
|
1 |
|
'status' => 'ok', |
75
|
1 |
|
'http_code' => $response->getStatusCode(), |
76
|
1 |
|
'body' => (string) $response->getBody(), |
77
|
1 |
|
]; |
78
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
79
|
|
|
return $this->parseError($e); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Fetches the Dealer Balance from SmartCall. |
85
|
|
|
* |
86
|
|
|
* @throws Exception |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
1 |
View Code Duplication |
public function isDealerRegistered($msisdn) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
try { |
93
|
1 |
|
$response = $this->get( |
94
|
1 |
|
sprintf( |
95
|
1 |
|
'/SmartcallRestfulProxy/registered/%s', |
96
|
|
|
$msisdn |
97
|
1 |
|
) |
98
|
1 |
|
); |
99
|
|
|
|
100
|
|
|
return [ |
101
|
1 |
|
'status' => 'ok', |
102
|
1 |
|
'http_code' => $response->getStatusCode(), |
103
|
1 |
|
'body' => (string) $response->getBody(), |
104
|
1 |
|
]; |
105
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
106
|
|
|
return $this->parseError($e); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Fetches the Product from SmartCall. |
112
|
|
|
* |
113
|
|
|
* @param int $productId Product Identifier |
114
|
|
|
* |
115
|
|
|
* @throws Exception |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
2 |
View Code Duplication |
public function getProduct($productId) |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
try { |
122
|
2 |
|
$response = $this->get( |
123
|
2 |
|
sprintf( |
124
|
2 |
|
'/SmartcallRestfulProxy/product_js/%d', |
125
|
|
|
$productId |
126
|
2 |
|
) |
127
|
2 |
|
); |
128
|
|
|
|
129
|
|
|
return [ |
130
|
1 |
|
'status' => 'ok', |
131
|
1 |
|
'http_code' => $response->getStatusCode(), |
132
|
1 |
|
'body' => (string) $response->getBody(), |
133
|
1 |
|
]; |
134
|
1 |
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
135
|
1 |
|
return $this->parseError($e); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Fetches the Product List from SmartCall. |
141
|
|
|
* |
142
|
|
|
* @throws Exception |
143
|
|
|
* |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
1 |
View Code Duplication |
public function getProducts() |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
try { |
149
|
1 |
|
$response = $this->get('/SmartcallRestfulProxy/all_networks_js'); |
150
|
|
|
|
151
|
|
|
return [ |
152
|
1 |
|
'status' => 'ok', |
153
|
1 |
|
'http_code' => $response->getStatusCode(), |
154
|
1 |
|
'body' => (string) $response->getBody(), |
155
|
1 |
|
]; |
156
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
157
|
|
|
return $this->parseError($e); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Fetches the details of the last transaction processed from SmartCall. |
163
|
|
|
* |
164
|
|
|
* @throws Exception |
165
|
|
|
* |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
1 |
View Code Duplication |
public function getLastTransaction() |
|
|
|
|
169
|
|
|
{ |
170
|
|
|
try { |
171
|
1 |
|
$response = $this->get('/SmartcallRestfulProxy/last_transaction_js'); |
172
|
|
|
|
173
|
|
|
return [ |
174
|
1 |
|
'status' => 'ok', |
175
|
1 |
|
'http_code' => $response->getStatusCode(), |
176
|
1 |
|
'body' => (string) $response->getBody(), |
177
|
1 |
|
]; |
178
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
179
|
|
|
return $this->parseError($e); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Fetches the Product List by the specified network identifier from SmartCall. |
185
|
|
|
* |
186
|
|
|
* @param int $networkId identifier for the network |
187
|
|
|
* |
188
|
|
|
* @throws Exception |
189
|
|
|
* |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
1 |
View Code Duplication |
public function getProductsByNetwork($networkId) |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
try { |
195
|
1 |
|
$response = $this->get( |
196
|
1 |
|
sprintf( |
197
|
1 |
|
'/SmartcallRestfulProxy/network_js/%d', |
198
|
|
|
$networkId |
199
|
1 |
|
) |
200
|
1 |
|
); |
201
|
|
|
|
202
|
|
|
return [ |
203
|
1 |
|
'status' => 'ok', |
204
|
1 |
|
'http_code' => $response->getStatusCode(), |
205
|
1 |
|
'body' => (string) $response->getBody(), |
206
|
1 |
|
]; |
207
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
208
|
|
|
return $this->parseError($e); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Purchase a voucher or do a pinless recharge on SmartCall. |
214
|
|
|
* |
215
|
|
|
* @param int $productId identifier for the product |
216
|
|
|
* @param int $amount amount in rands of the product |
217
|
|
|
* @param int $msisdn mobile number of the recipient of the product |
218
|
|
|
* @param int $deviceId mobile number or meter number of the device being recharged |
219
|
|
|
* @param string $clientRef Client Reference (use a UUID) |
220
|
|
|
* @param bool $pinless true = device will be recharged via the network's IN platform | false = pinbased virtual voucher |
221
|
|
|
* @param bool $sendSms true = SmartCall will send the voucher via SMS | false don't send the voucher via SMS |
222
|
|
|
* |
223
|
|
|
* @throws Exception |
224
|
|
|
* |
225
|
|
|
* @return array |
226
|
|
|
*/ |
227
|
1 |
|
public function purchaseProduct($productId, $amount, $msisdn, $deviceId, $clientRef, $pinless, $sendSms) |
228
|
|
|
{ |
229
|
|
|
try { |
230
|
1 |
|
$response = $this->post( |
231
|
1 |
|
sprintf( |
232
|
1 |
|
'/SmartcallRestfulProxy/recharge_js/%s', |
233
|
|
|
$clientRef |
234
|
1 |
|
), |
235
|
|
|
[ |
236
|
|
|
'json' => [ |
237
|
1 |
|
'amount' => $amount, |
238
|
1 |
|
'deviceId' => $deviceId, |
239
|
1 |
|
'pinless' => $pinless, |
240
|
1 |
|
'productId' => $productId, |
241
|
1 |
|
'sendSms' => $sendSms, |
242
|
1 |
|
'smsRecipientMsisdn' => $msisdn, |
243
|
1 |
|
], |
244
|
|
|
] |
245
|
1 |
|
); |
246
|
|
|
|
247
|
|
|
return [ |
248
|
1 |
|
'status' => 'ok', |
249
|
1 |
|
'http_code' => $response->getStatusCode(), |
250
|
1 |
|
'body' => (string) $response->getBody(), |
251
|
1 |
|
]; |
252
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
253
|
|
|
return $this->parseError($e); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Searches SmartCall for a specified transaction using a specified key and string to search |
259
|
|
|
* against at SmartCall. |
260
|
|
|
* |
261
|
|
|
* @param string $field client_ref | msisdn | order_reference |
262
|
|
|
* @param string $query_string Client Reference when client_ref or a users MSISDN when msisdn |
|
|
|
|
263
|
|
|
* |
264
|
|
|
* @throws Exception |
265
|
|
|
* |
266
|
|
|
* @return array |
267
|
|
|
*/ |
268
|
3 |
|
public function searchTransaction($field, $search) |
269
|
|
|
{ |
270
|
|
|
/** |
271
|
|
|
* Map Smartcall's longer version of the url to shorter param to pass in. |
272
|
|
|
*/ |
273
|
|
|
$fields = [ |
274
|
3 |
|
'client_ref' => 'client_reference', |
275
|
3 |
|
'msisdn' => 'msisdn', |
276
|
3 |
|
'order_ref' => 'order_reference', |
277
|
3 |
|
]; |
278
|
|
|
|
279
|
|
|
try { |
280
|
3 |
|
$response = $this->get( |
281
|
3 |
|
sprintf( |
282
|
3 |
|
'/SmartcallRestfulProxy/last_transaction_%s_js/%s', |
283
|
3 |
|
(string) $fields[$field], |
284
|
|
|
(string) $search |
285
|
3 |
|
) |
286
|
3 |
|
); |
287
|
|
|
|
288
|
|
|
return [ |
289
|
3 |
|
'status' => 'ok', |
290
|
3 |
|
'http_code' => $response->getStatusCode(), |
291
|
3 |
|
'body' => (string) $response->getBody(), |
292
|
3 |
|
]; |
293
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
294
|
|
|
return $this->parseError($e); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
298
|
1 |
|
private function parseError(\GuzzleHttp\Exception\ServerException $e) |
299
|
|
|
{ |
300
|
1 |
|
$body = (string) $e->getResponse()->getBody(); |
301
|
|
|
|
302
|
1 |
|
preg_match('!<p><b>type</b> Exception report</p><p><b>message</b> <u>(.*[^</u>])</u></p><p><b>description</b>!', $body, $matches); |
303
|
|
|
|
304
|
|
|
return [ |
305
|
1 |
|
'status' => 'error', |
306
|
1 |
|
'http_code' => $e->getResponse()->getStatusCode(), |
307
|
1 |
|
'body' => $matches['1'], |
308
|
1 |
|
]; |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.