1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HenryEjemuta\LaravelGecharl; |
4
|
|
|
|
5
|
|
|
use HenryEjemuta\LaravelGecharl\Classes\GecharlResponse; |
6
|
|
|
use HenryEjemuta\LaravelGecharl\Exceptions\GecharlErrorException; |
7
|
|
|
use Illuminate\Support\Facades\Http; |
8
|
|
|
|
9
|
|
|
class Gecharl |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* base url |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $baseUrl; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* the cart session key |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $instanceName; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Flexible handle to the VTPass Configuration |
27
|
|
|
* |
28
|
|
|
* @var |
29
|
|
|
*/ |
30
|
|
|
protected $config; |
31
|
|
|
|
32
|
|
|
public function __construct($baseUrl, $instanceName, $config) |
33
|
|
|
{ |
34
|
|
|
$this->baseUrl = $baseUrl; |
35
|
|
|
$this->instanceName = $instanceName; |
36
|
|
|
$this->config = $config; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* get instance name of the cart |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getInstanceName() |
45
|
|
|
{ |
46
|
|
|
return $this->instanceName; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function withOAuth2() |
50
|
|
|
{ |
51
|
|
|
return Http::withToken($this->config['api_key']); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get Your wallet available balance, Wallet is identified by username set in gecharl config or environmental variable |
56
|
|
|
* @return GecharlResponse |
57
|
|
|
* @throws GecharlErrorException |
58
|
|
|
*/ |
59
|
|
|
public function getWalletBalance(): GecharlResponse |
60
|
|
|
{ |
61
|
|
|
$response = $this->withOAuth2()->get("{$this->baseUrl}/account?username={$this->config['username']}"); |
62
|
|
|
|
63
|
|
|
$responseObject = json_decode($response->body()); |
64
|
|
View Code Duplication |
if (isset($responseObject->status) && isset($responseObject->message)) |
|
|
|
|
65
|
|
|
return new GecharlResponse($responseObject->status ? 200 : 422, $responseObject->message); |
66
|
|
|
return new GecharlResponse(-1, null); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Purchase Airtime with py specifying the network (i.e. mtn, glo, airtel, or 9mobile to buy airtime corresponding the provided telco service code) |
71
|
|
|
* @param string $network Network ID e.g mtn, glo, airtel, or 9mobile |
72
|
|
|
* @param int $amount The amount you wish to topup |
73
|
|
|
* @param string $phoneNumber The phone number of the recipient of this service |
74
|
|
|
* @return GecharlResponse |
75
|
|
|
* |
76
|
|
|
* @throws GecharlErrorException |
77
|
|
|
*/ |
78
|
|
|
public function purchaseAirtime(string $network, int $amount, $phoneNumber): GecharlResponse |
79
|
|
|
{ |
80
|
|
|
$response = $this->withOAuth2()->post("{$this->baseUrl}/account", [ |
81
|
|
|
'network' => $network, |
82
|
|
|
'amount' => $amount, |
83
|
|
|
'phone_number' => $phoneNumber |
84
|
|
|
]); |
85
|
|
|
|
86
|
|
|
$responseObject = json_decode($response->body()); |
87
|
|
View Code Duplication |
if (isset($responseObject->status) && isset($responseObject->message)) |
|
|
|
|
88
|
|
|
return new GecharlResponse($responseObject->status ? 200 : 422, $responseObject->message); |
89
|
|
|
return new GecharlResponse(-1, null); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get all data plan for all networks or for specified network only |
94
|
|
|
* @param string|null $network |
95
|
|
|
* @return GecharlResponse |
96
|
|
|
* @throws GecharlErrorException |
97
|
|
|
*/ |
98
|
|
|
public function getDataPlans(string $network = null): GecharlResponse |
99
|
|
|
{ |
100
|
|
|
$endpoint = "{$this->baseUrl}/data_lookup"; |
101
|
|
|
if ($network !== null) $endpoint .= "?network=$network"; |
102
|
|
|
$response = $this->withOAuth2()->get($endpoint); |
103
|
|
|
|
104
|
|
|
$responseObject = json_decode($response->body()); |
105
|
|
|
if ($response->successful()) |
106
|
|
|
return new GecharlResponse(200, $responseObject); |
107
|
|
|
return new GecharlResponse(-1, null); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Buy data bundle |
112
|
|
|
* @param string $network Unique network identification code e.g. mtn, glo, airtel... |
113
|
|
|
* @param string $plan Plan code of data plan to subscribe to |
114
|
|
|
* @param string $recipient Phone number to receive data subscription |
115
|
|
|
* @return GecharlResponse |
116
|
|
|
* @throws GecharlErrorException |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public function purchaseDataBundle(string $network, string $plan, string $recipient): GecharlResponse |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$endpoint = "{$this->baseUrl}/data?network=$network&plan=$plan&recipent=$recipient"; |
121
|
|
|
$response = $this->withOAuth2()->get($endpoint); |
122
|
|
|
|
123
|
|
|
$responseObject = json_decode($response->body()); |
124
|
|
|
if ($response->successful()) |
125
|
|
|
return new GecharlResponse($responseObject->status_code, $responseObject->message); |
126
|
|
|
return new GecharlResponse(-1, null); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* You need to verify your meter number before purchasing. |
131
|
|
|
* |
132
|
|
|
* Please note the disco unique codes below: |
133
|
|
|
* Ikaja Electricity = <strong>ie</strong> |
134
|
|
|
* Eko Electricity = <strong>ekedc</strong> |
135
|
|
|
* Enugu Electricity = <strong>eedc</strong> |
136
|
|
|
* Kano Electricity = <strong>kano</strong> |
137
|
|
|
* Port Harcourt Electricity = <strong>phed</strong> |
138
|
|
|
* Abuja Electricity = <strong>abuja</strong> |
139
|
|
|
* Ibadan Electricity = <strong>ibedc</strong> |
140
|
|
|
* |
141
|
|
|
* On successful validation for a prepaid meter number, a product_code key comes with it, which should be passed along other parameters when paying. |
142
|
|
|
* |
143
|
|
|
* |
144
|
|
|
* @param string $disco Unique code of the Electricity distribution company the meter number is for |
145
|
|
|
* @param string $meterNumber Meter Number to verify |
146
|
|
|
* @param string $meterType Meter type i.e. <strong>prepaid</strong> or <strong>postpaid</strong> |
147
|
|
|
* @return GecharlResponse |
148
|
|
|
* @throws GecharlErrorException |
149
|
|
|
*/ |
150
|
|
View Code Duplication |
public function verifyMeterNumber(string $disco, string $meterNumber, string $meterType): GecharlResponse |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$endpoint = "{$this->baseUrl}/electricity/validate/$disco/?meter_number=$meterNumber&meter_type=$meterType"; |
153
|
|
|
$response = $this->withOAuth2()->get($endpoint); |
154
|
|
|
|
155
|
|
|
$responseObject = json_decode($response->body()); |
156
|
|
|
if ($response->successful()) |
157
|
|
|
return new GecharlResponse($responseObject->status_code, $responseObject->message); |
158
|
|
|
return new GecharlResponse(-1, null); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Purchase Electricity |
163
|
|
|
* |
164
|
|
|
* Please note the disco unique codes below: |
165
|
|
|
* Ikaja Electricity = <strong>ie</strong> |
166
|
|
|
* Eko Electricity = <strong>ekedc</strong> |
167
|
|
|
* Enugu Electricity = <strong>eedc</strong> |
168
|
|
|
* Kano Electricity = <strong>kano</strong> |
169
|
|
|
* Port Harcourt Electricity = <strong>phed</strong> |
170
|
|
|
* Abuja Electricity = <strong>abuja</strong> |
171
|
|
|
* Ibadan Electricity = <strong>ibedc</strong> |
172
|
|
|
* |
173
|
|
|
* |
174
|
|
|
* @param string $disco Unique code of the Electricity distribution company the meter number is for |
175
|
|
|
* @param string $meterNumber Meter Number to verify |
176
|
|
|
* @param string $meterType Meter type i.e. <strong>prepaid</strong> or <strong>postpaid</strong> |
177
|
|
|
* @param $amount |
178
|
|
|
* @param string|null $productCode |
179
|
|
|
* @return GecharlResponse |
180
|
|
|
* @throws GecharlErrorException |
181
|
|
|
*/ |
182
|
|
|
public function purchaseElectricity(string $disco, string $meterNumber, string $meterType, $amount, string $productCode = null): GecharlResponse |
183
|
|
|
{ |
184
|
|
|
$endpoint = "{$this->baseUrl}/electricity/payment/$disco"; |
185
|
|
|
$params = [ |
186
|
|
|
"meter_number" => $meterNumber, |
187
|
|
|
"meter_type" => $meterType, |
188
|
|
|
"amount" => $amount, |
189
|
|
|
]; |
190
|
|
|
if ($productCode !== null) $params['product_code'] = $productCode; |
191
|
|
|
$response = $this->withOAuth2()->post($endpoint, $params); |
192
|
|
|
|
193
|
|
|
$responseObject = json_decode($response->body()); |
194
|
|
|
if ($response->successful()) |
195
|
|
|
return new GecharlResponse($responseObject->status_code, $responseObject->message); |
196
|
|
|
return new GecharlResponse(-1, null); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Multichoice(DSTV and GoTv) Smart Card Number/Decoder verification |
201
|
|
|
* You need to verify your Smart card number before purchasing. |
202
|
|
|
* |
203
|
|
|
* @param string $multichoiceType DSTV|GOTV |
204
|
|
|
* @param string $smartCardNumber Customer unique smart card number to subscribe |
205
|
|
|
* @return GecharlResponse |
206
|
|
|
* @throws GecharlErrorException |
207
|
|
|
*/ |
208
|
|
View Code Duplication |
public function verifySmartCardNumber(string $multichoiceType, string $smartCardNumber): GecharlResponse |
|
|
|
|
209
|
|
|
{ |
210
|
|
|
$endpoint = "{$this->baseUrl}/validate/multichoice?multichoice_type=$multichoiceType&smart_card_no=$smartCardNumber"; |
211
|
|
|
$response = $this->withOAuth2()->get($endpoint); |
212
|
|
|
|
213
|
|
|
$responseObject = json_decode($response->body()); |
214
|
|
|
if ($response->successful()) |
215
|
|
|
return new GecharlResponse($responseObject->status_code, $responseObject->message); |
216
|
|
|
return new GecharlResponse(-1, null); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Purchase DSTV or GoTv Cable Tv Plan |
221
|
|
|
* @param string $multiChoiceType DSTV|GOTV |
222
|
|
|
* @param string $smartCardNumber Customer unique smart card number to subscribe |
223
|
|
|
* @param $amount |
224
|
|
|
* @param string $productCode productCode as gotten from the verification call |
225
|
|
|
* @param string $plan Unique product_code as gotten from the verification call for available plan for the provided SmartCard Number |
226
|
|
|
* @param string $customerPhoneNumber |
227
|
|
|
* @param string $customerName |
228
|
|
|
* @param string|null $transactionId |
229
|
|
|
* @return GecharlResponse |
230
|
|
|
* @throws GecharlErrorException |
231
|
|
|
*/ |
232
|
|
|
public function purchaseMultiChoice(string $multiChoiceType, string $smartCardNumber, $amount, string $productCode, string $plan, string $customerPhoneNumber = '', string $customerName = '', string $transactionId = null): GecharlResponse |
233
|
|
|
{ |
234
|
|
|
$endpoint = "{$this->baseUrl}/multichoice/payment"; |
235
|
|
|
$params = [ |
236
|
|
|
"multichoice_type" => $multiChoiceType, |
237
|
|
|
"smart_card_no" => $smartCardNumber, |
238
|
|
|
"amount" => $amount, |
239
|
|
|
"product_code" => $plan, |
240
|
|
|
"productCode" => $productCode, |
241
|
|
|
]; |
242
|
|
|
if (empty($customerName)) $params['customer_name'] = $customerName; |
243
|
|
|
if (empty($customerPhoneNumber)) $params['phone_number'] = $customerPhoneNumber; |
244
|
|
|
if ($transactionId !== null) $params['transaction_id'] = $transactionId; |
245
|
|
|
$response = $this->withOAuth2()->post($endpoint, $params); |
246
|
|
|
|
247
|
|
|
$responseObject = json_decode($response->body()); |
248
|
|
|
if ($response->successful()) |
249
|
|
|
return new GecharlResponse($responseObject->status_code, $responseObject->message); |
250
|
|
|
return new GecharlResponse(-1, null); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* StarTimes Smart Card Number/Decoder verification |
256
|
|
|
* You need to verify your Smart card number before purchasing. |
257
|
|
|
* |
258
|
|
|
* @param string $smartCardNumber Customer unique smart card number to subscribe |
259
|
|
|
* @return GecharlResponse |
260
|
|
|
* @throws GecharlErrorException |
261
|
|
|
*/ |
262
|
|
View Code Duplication |
public function verifyStarTimesSmartCardNumber(string $smartCardNumber): GecharlResponse |
|
|
|
|
263
|
|
|
{ |
264
|
|
|
$endpoint = "{$this->baseUrl}/validate/startimes?smart_card_no=$smartCardNumber"; |
265
|
|
|
$response = $this->withOAuth2()->get($endpoint); |
266
|
|
|
|
267
|
|
|
$responseObject = json_decode($response->body()); |
268
|
|
|
if ($response->successful()) |
269
|
|
|
return new GecharlResponse($responseObject->status_code, $responseObject->message); |
270
|
|
|
return new GecharlResponse(-1, null); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Purchase StarTimes Cable Tv Plan |
275
|
|
|
* The product_code is either <strong>NOVA</strong>, <strong>BASIC</strong>, <strong>SMART</strong>, <strong>CLASSIC</strong> or <strong>SUPER</strong> |
276
|
|
|
* |
277
|
|
|
* @param string $smartCardNumber Customer unique smart card number to subscribe |
278
|
|
|
* @param $amount |
279
|
|
|
* @param string $productCode productCode as gotten from the verification call |
280
|
|
|
* @param string $plan Unique product_code as gotten from the verification call for available plan for the provided SmartCard Number |
281
|
|
|
* @param string $customerPhoneNumber |
282
|
|
|
* @param string $customerName |
283
|
|
|
* @param string|null $transactionId |
284
|
|
|
* @return GecharlResponse |
285
|
|
|
* @throws GecharlErrorException |
286
|
|
|
*/ |
287
|
|
|
public function purchaseStarTimes(string $smartCardNumber, $amount, string $productCode, string $plan, string $transactionId, string $customerPhoneNumber = '', string $customerName = ''): GecharlResponse |
288
|
|
|
{ |
289
|
|
|
$endpoint = "{$this->baseUrl}/startimes/payment"; |
290
|
|
|
$params = [ |
291
|
|
|
"smart_card_no" => $smartCardNumber, |
292
|
|
|
"amount" => $amount, |
293
|
|
|
"product_code" => $plan, |
294
|
|
|
"productCode" => $productCode, |
295
|
|
|
"transaction_id" => $transactionId, |
296
|
|
|
]; |
297
|
|
|
if (empty($customerName)) $params['customer_name'] = $customerName; |
298
|
|
|
if (empty($customerPhoneNumber)) $params['phone_number'] = $customerPhoneNumber; |
299
|
|
|
$response = $this->withOAuth2()->post($endpoint, $params); |
300
|
|
|
|
301
|
|
|
$responseObject = json_decode($response->body()); |
302
|
|
|
if ($response->successful()) |
303
|
|
|
return new GecharlResponse($responseObject->status_code, $responseObject->message); |
304
|
|
|
return new GecharlResponse(-1, null); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
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.