Total Complexity | 41 |
Total Lines | 401 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like VTPass often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use VTPass, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class VTPass |
||
38 | { |
||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $productPurchaseEndpoint; |
||
43 | |||
44 | /** |
||
45 | * base url |
||
46 | * |
||
47 | * @var |
||
48 | */ |
||
49 | private $baseUrl; |
||
50 | |||
51 | /** |
||
52 | * the cart session key |
||
53 | * |
||
54 | * @var |
||
55 | */ |
||
56 | protected $instanceName; |
||
57 | |||
58 | /** |
||
59 | * Flexible handle to the VTPass Configuration |
||
60 | * |
||
61 | * @var |
||
62 | */ |
||
63 | protected $config; |
||
64 | |||
65 | |||
66 | public function __construct($baseUrl, $instanceName, $config) |
||
67 | { |
||
68 | $this->baseUrl = $baseUrl; |
||
69 | $this->instanceName = $instanceName; |
||
70 | $this->config = $config; |
||
71 | $this->productPurchaseEndpoint = "{$this->baseUrl}/pay"; |
||
72 | } |
||
73 | |||
74 | |||
75 | /** |
||
76 | * get instance name of the cart |
||
77 | * |
||
78 | * @return string |
||
79 | */ |
||
80 | public function getInstanceName() |
||
81 | { |
||
82 | return $this->instanceName; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @return PendingRequest |
||
87 | */ |
||
88 | private function withBasicAuth() |
||
89 | { |
||
90 | return Http::withBasicAuth($this->config['username'], $this->config['password'])->asJson(); |
||
91 | } |
||
92 | |||
93 | |||
94 | /** |
||
95 | * Using a GET method, the VTpass service categories can be accessed with the endpoint below: |
||
96 | * @return VTPassResponse |
||
97 | * |
||
98 | * @throws VTPassErrorException |
||
99 | */ |
||
100 | public function getServicesCategories() |
||
101 | { |
||
102 | $endpoint = "{$this->baseUrl}/service-categories"; |
||
103 | |||
104 | $response = $this->withBasicAuth()->get($endpoint); |
||
105 | |||
106 | $responseObject = json_decode($response->body()); |
||
107 | if (isset($responseObject->response_description) && isset($responseObject->content)) |
||
108 | return new VTPassResponse($responseObject->response_description, $responseObject->content); |
||
109 | return new VTPassResponse(-1, null); |
||
110 | } |
||
111 | |||
112 | |||
113 | /** |
||
114 | * This help in getting the available service IDs on VTpass RESTful API. |
||
115 | * Using a GET method, the VTpass service IDs for Data [for instance] can be accessed with the endpoint below: |
||
116 | * @param $serviceCategoryIdentifier |
||
117 | * @return VTPassResponse |
||
118 | * |
||
119 | * @throws VTPassErrorException |
||
120 | */ |
||
121 | public function getServiceID($serviceCategoryIdentifier): VTPassResponse |
||
122 | { |
||
123 | $endpoint = "{$this->baseUrl}/services?identifier=$serviceCategoryIdentifier"; |
||
124 | |||
125 | $response = $this->withBasicAuth()->get($endpoint); |
||
126 | |||
127 | $responseObject = json_decode($response->body()); |
||
128 | if (isset($responseObject->response_description) && isset($responseObject->content)) |
||
129 | return new VTPassResponse($responseObject->response_description, $responseObject->content); |
||
130 | return new VTPassResponse(-1, null); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * This help in getting the available service code from the biller using their respective service ID on VTpass RESTful API. |
||
135 | * @param string $serviceID Service ID as specified by VTpass. For example mtn-data as serviceID to get all MTN Data Plans |
||
136 | * @return VTPassResponse |
||
137 | * |
||
138 | * @throws VTPassErrorException |
||
139 | */ |
||
140 | public function getVariationCodes($serviceID): VTPassResponse |
||
141 | { |
||
142 | $endpoint = "{$this->baseUrl}/service-variations?serviceID=$serviceID"; |
||
143 | |||
144 | $response = $this->withBasicAuth()->get($endpoint); |
||
145 | |||
146 | $responseObject = json_decode($response->body()); |
||
147 | |||
148 | if (isset($responseObject->response_description) && isset($responseObject->content)) |
||
149 | return new VTPassResponse($responseObject->response_description, $responseObject->content); |
||
150 | return new VTPassResponse(-1, null); |
||
151 | } |
||
152 | |||
153 | |||
154 | /** |
||
155 | * This help in getting the available service IDs on VTpass RESTful API. |
||
156 | * Using a GET method, the VTpass variation codes for GOTV bouquets [for instance] can be accessed with the endpoint below: |
||
157 | * @param string $serviceID Service ID as specified by VTpass. In this case, it is aero |
||
158 | * @param string $name This refers to the option name as specified by VTpass. In this case, it is passenger_type. Other option name include trip_type. |
||
159 | * @return VTPassResponse |
||
160 | * |
||
161 | * @throws VTPassErrorException |
||
162 | */ |
||
163 | public function getProductOptions(string $serviceID, string $name): VTPassResponse |
||
164 | { |
||
165 | $endpoint = "{$this->baseUrl}/options?serviceID=$serviceID&name=$name"; |
||
166 | |||
167 | $response = $this->withBasicAuth()->get($endpoint); |
||
168 | |||
169 | $responseObject = json_decode($response->body()); |
||
170 | if (isset($responseObject->response_description) && isset($responseObject->content)) |
||
171 | return new VTPassResponse($responseObject->response_description, $responseObject->content); |
||
172 | return new VTPassResponse(-1, null); |
||
173 | } |
||
174 | |||
175 | |||
176 | /** |
||
177 | * Purchase Airtime with the unique service ID (i.e. mtn, glo, airtel, or etisalat to buy airtime corresponding the provided telco service code) |
||
178 | * @param string $requestId This is a unique reference (otherwise refer to as order number generated from your server to uniquely identify this purchase) with which you can use to identify and query the status of a given transaction after the transaction has been executed. |
||
179 | * @param string $serviceID Service ID as specified by VTpass. For example mtn, use VTPass:: |
||
180 | * @param int $amount The amount you wish to topup |
||
181 | * @param string $phoneNumber The phone number of the recipient of this service |
||
182 | * @return VTPassResponse |
||
183 | * |
||
184 | * @throws VTPassErrorException |
||
185 | */ |
||
186 | public function purchaseAirtime(string $requestId, string $serviceID, int $amount, $phoneNumber): VTPassResponse |
||
187 | { |
||
188 | $response = $this->withBasicAuth()->post($this->productPurchaseEndpoint, [ |
||
189 | 'request_id' => $requestId, |
||
190 | 'serviceID' => $serviceID, |
||
191 | 'amount' => $amount, |
||
192 | 'phone' => $phoneNumber |
||
193 | ]); |
||
194 | |||
195 | $responseObject = json_decode($response->body()); |
||
196 | if (isset($responseObject->code) && isset($responseObject->transactionId)) { |
||
197 | $statusCode = $responseObject->code; |
||
198 | unset($responseObject->code); |
||
199 | return new VTPassResponse($statusCode, $responseObject); |
||
200 | } |
||
201 | return new VTPassResponse(-1, null); |
||
202 | } |
||
203 | |||
204 | |||
205 | /** |
||
206 | * Query Transaction Status |
||
207 | * @param string $requestId This is the reference with which you sent when purchasing a transaction after the transaction has been executed. |
||
208 | * @return VTPassResponse |
||
209 | * |
||
210 | * @throws VTPassErrorException |
||
211 | */ |
||
212 | public function queryTransactionStatus(string $requestId): VTPassResponse |
||
213 | { |
||
214 | $endpoint = "{$this->baseUrl}/requery"; |
||
215 | |||
216 | $response = $this->withBasicAuth()->post($endpoint, [ |
||
217 | 'request_id' => $requestId, |
||
218 | ]); |
||
219 | |||
220 | $responseObject = json_decode($response->body()); |
||
221 | if (isset($responseObject->code) && isset($responseObject->transactionId)) { |
||
222 | $statusCode = $responseObject->code; |
||
223 | unset($responseObject->code); |
||
224 | return new VTPassResponse($statusCode, $responseObject); |
||
225 | } |
||
226 | return new VTPassResponse(-1, null); |
||
227 | } |
||
228 | |||
229 | |||
230 | /** |
||
231 | * @param string $requestId |
||
232 | * @param string $serviceID |
||
233 | * @param $billersCode |
||
234 | * @param $variationCode |
||
235 | * @param $phoneNumber |
||
236 | * @param int $amount |
||
237 | * @return VTPassResponse |
||
238 | * @throws VTPassErrorException |
||
239 | */ |
||
240 | public function purchaseProduct(string $requestId, string $serviceID, $billersCode, $variationCode, $phoneNumber, int $amount = 0): VTPassResponse |
||
241 | { |
||
242 | $params = [ |
||
243 | 'request_id' => $requestId, |
||
244 | 'serviceID' => $serviceID, |
||
245 | 'billersCode' => $billersCode, |
||
246 | 'variation_code' => $variationCode, |
||
247 | 'phone' => $phoneNumber |
||
248 | ]; |
||
249 | if ($amount !== 0) $params['amount'] = $amount; |
||
250 | |||
251 | $response = $this->withBasicAuth()->post($this->productPurchaseEndpoint, $params); |
||
252 | |||
253 | $responseObject = json_decode($response->body()); |
||
254 | if (isset($responseObject->code) && isset($responseObject->transactionId)) { |
||
255 | $statusCode = $responseObject->code; |
||
256 | unset($responseObject->code); |
||
257 | return new VTPassResponse($statusCode, $responseObject); |
||
258 | } |
||
259 | return new VTPassResponse(-1, null); |
||
260 | } |
||
261 | |||
262 | |||
263 | /** |
||
264 | * Purchase Data Bundle with the unique service ID (i.e. mtn-data, glo-data, airtel-data, or etisalat-data to buy data corresponding the provided telco service code) |
||
265 | * <strong>This Exclude Smile Data Bundle and strictly for mtn, glo, airtel, and 9mobile only</strong> |
||
266 | * @param string $requestId This is a unique reference (otherwise refer to as order number generated from your server to uniquely identify this purchase) with which you can use to identify and query the status of a given transaction after the transaction has been executed. |
||
267 | * @param string $serviceID Service ID as specified by VTpass. For example mtn, use VTPass:: |
||
268 | * @param string $phoneNumber The Phone Number your with to subscribe |
||
269 | * @param string $variationCode The variation code of the bundle you wish to subscribe to @see getVariationCodes($serviceID) to get a list of bundle for that telco |
||
270 | * @param int $amount The amount as specified with the provided $variationCode. This amount will be ignored as the variation code determine the price of the data bundle, hence this is an optional parameter. |
||
271 | * @param string $customerPhoneNumber The phone number of the recipient of this service |
||
272 | * @return VTPassResponse |
||
273 | * |
||
274 | * @throws VTPassErrorException |
||
275 | */ |
||
276 | public function purchaseData(string $requestId, string $serviceID, $phoneNumber, $variationCode, $customerPhoneNumber, int $amount = 0): VTPassResponse |
||
277 | { |
||
278 | return $this->purchaseProduct($requestId, $serviceID, $phoneNumber, $variationCode, $customerPhoneNumber, $amount); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * This help in getting the available VTpass variation codes (Bundles) for Smile Network bundles |
||
283 | * @return VTPassResponse |
||
284 | * |
||
285 | * @throws VTPassErrorException |
||
286 | */ |
||
287 | public function getSmileBundles(): VTPassResponse |
||
288 | { |
||
289 | return $this->getVariationCodes('smile-direct'); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Merchant Verify |
||
294 | * @param $billersCode |
||
295 | * @param $serviceID |
||
296 | * @param string|null $type |
||
297 | * @return VTPassResponse |
||
298 | * |
||
299 | * @throws VTPassErrorException |
||
300 | */ |
||
301 | public function verifyMerchant($billersCode, $serviceID, $type = null): VTPassResponse |
||
302 | { |
||
303 | $endpoint = "{$this->baseUrl}/merchant-verify"; |
||
304 | $params = [ |
||
305 | 'billersCode' => $billersCode, |
||
306 | 'serviceID' => $serviceID, |
||
307 | ]; |
||
308 | if ($type !== null) $params['type'] = $type; |
||
309 | $response = $this->withBasicAuth()->post($endpoint, $params); |
||
310 | $responseObject = json_decode($response->body()); |
||
311 | if (isset($responseObject->code) && isset($responseObject->content)) |
||
312 | return new VTPassResponse($responseObject->code, $responseObject->content); |
||
313 | return new VTPassResponse(-1, null); |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * This endpoint allows you to verify customer details before attempting to make payment. |
||
318 | * There are different options for to verify customer details. You can verify the customer�s accounts using either the Account ID, Customer�s registered email (Email registered on Smile account) or customer phone number (Smile Phone number). |
||
319 | * @param string $customerID Smile Customer Account ID |
||
320 | * @return VTPassResponse |
||
321 | * |
||
322 | * @throws VTPassErrorException |
||
323 | */ |
||
324 | public function verifySmileCustomerByID($customerID): VTPassResponse |
||
325 | { |
||
326 | return $this->verifyMerchant($customerID, 'smile-direct'); |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * This endpoint allows you to verify customer details before attempting to make payment. |
||
331 | * There are different options for to verify customer details. You can verify the customer�s accounts using either the Account ID, Customer�s registered email (Email registered on Smile account) or customer phone number (Smile Phone number). |
||
332 | * @param string $customerUniqueDetail Smile Customer Account Unique Identification detail e.g AccountID, Email, or Phone Number |
||
333 | * @param string $detailType Unique detail type corresponding to the e.g AccountID, Email, or Phone Number |
||
334 | * @return VTPassResponse |
||
335 | * |
||
336 | * @throws VTPassErrorException |
||
337 | */ |
||
338 | public function verifySmileCustomer($customerUniqueDetail, $detailType): VTPassResponse |
||
339 | { |
||
340 | $endpoint = "{$this->baseUrl}/merchant-verify/smile/$detailType"; |
||
341 | $response = $this->withBasicAuth()->post($endpoint, [ |
||
342 | 'billersCode' => $customerUniqueDetail, |
||
343 | ]); |
||
344 | $responseObject = json_decode($response->body()); |
||
345 | if (isset($responseObject->code) && isset($responseObject->content)) |
||
346 | return new VTPassResponse($responseObject->code, $responseObject->content); |
||
347 | return new VTPassResponse(-1, null); |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * This endpoint allows you to verify customer details before attempting to make payment. |
||
352 | * There are different options for to verify customer details. You can verify the customer�s accounts using either the Account ID, Customer�s registered email (Email registered on Smile account) or customer phone number (Smile Phone number). |
||
353 | * @param string $customerEmail Smile Customer Account Email |
||
354 | * @return VTPassResponse |
||
355 | * |
||
356 | * @throws VTPassErrorException |
||
357 | */ |
||
358 | public function verifySmileCustomerByEmail($customerEmail): VTPassResponse |
||
359 | { |
||
360 | return $this->verifySmileCustomer($customerEmail, 'email'); |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * This endpoint allows you to verify customer details before attempting to make payment. |
||
365 | * There are different options for to verify customer details. You can verify the customer�s accounts using either the Account ID, Customer�s registered email (Email registered on Smile account) or customer phone number (Smile Phone number). |
||
366 | * @param string $customerPhone Smile Customer Account Phone Number |
||
367 | * @return VTPassResponse |
||
368 | * |
||
369 | * @throws VTPassErrorException |
||
370 | */ |
||
371 | public function verifySmileCustomerByPhone($customerPhone): VTPassResponse |
||
372 | { |
||
373 | return $this->verifySmileCustomer($customerPhone, 'phone'); |
||
374 | } |
||
375 | |||
376 | |||
377 | /** |
||
378 | * Purchase GoTV plan |
||
379 | * <strong>This Exclude Smile Data Bundle and strictly for mtn, glo, airtel, and 9mobile only</strong> |
||
380 | * @param string $requestId This is a unique reference (otherwise refer to as order number generated from your server to uniquely identify this purchase) with which you can use to identify and query the status of a given transaction after the transaction has been executed. |
||
381 | * @param string $smilePhoneNumber Customer Smile Phone Number to Subscribe |
||
382 | * @param string $variationCode The variation code of the bundle you wish to subscribe to @see getVariationCodes($serviceID) to get a list of bundle for that telco |
||
383 | * @param string $phoneNumber The phone number of the recipient of this service |
||
384 | * @param int $amount The amount as specified with the provided $variationCode. This amount will be ignored as the variation code determine the price of the data bundle, hence this is an optional parameter. |
||
385 | * @return VTPassResponse |
||
386 | * |
||
387 | * @throws VTPassErrorException |
||
388 | */ |
||
389 | public function buySmileData(string $requestId, $smilePhoneNumber, $variationCode, $phoneNumber, int $amount = 0): VTPassResponse |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Purchase GoTV plan |
||
396 | * <strong>This Exclude Smile Data Bundle and strictly for mtn, glo, airtel, and 9mobile only</strong> |
||
397 | * @param string $requestId This is a unique reference (otherwise refer to as order number generated from your server to uniquely identify this purchase) with which you can use to identify and query the status of a given transaction after the transaction has been executed. |
||
398 | * @param string $smartCartNumber Customer GoTV |
||
399 | * @param string $variationCode The variation code of the bundle you wish to subscribe to @see getVariationCodes($serviceID) to get a list of bundle for that telco |
||
400 | * @param string $phoneNumber The phone number of the recipient of this service |
||
401 | * @param int $amount The amount as specified with the provided $variationCode. This amount will be ignored as the variation code determine the price of the data bundle, hence this is an optional parameter. |
||
402 | * @return VTPassResponse |
||
403 | * |
||
404 | * @throws VTPassErrorException |
||
405 | */ |
||
406 | public function payGoTV(string $requestId, $smartCartNumber, $variationCode, $phoneNumber, int $amount = 0): VTPassResponse |
||
407 | { |
||
408 | return $this->purchaseProduct($requestId, 'gotv', $smartCartNumber, $variationCode, $phoneNumber, $amount); |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * @param string $customerMeterNumber Customer Meter Number to verify |
||
413 | * @param string $serviceID The DISCO unique serviceID on the VTPass Server i.e. ikeja-electric, eko-electric, portharcourt-electric, e.t.c |
||
414 | * @param string $type This is basically the type of meter you are trying to validate. It can be either prepaid or postpaid |
||
415 | * @return VTPassResponse |
||
416 | * @throws VTPassErrorException |
||
417 | */ |
||
418 | public function verifyElectricityBillMeterNumber($customerMeterNumber, $serviceID, $type): VTPassResponse |
||
419 | { |
||
420 | return $this->verifyMerchant($customerMeterNumber, $serviceID, $type); |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Purchase Electricity plan |
||
425 | * @param string $requestId This is a unique reference (otherwise refer to as order number generated from your server to uniquely identify this purchase) with which you can use to identify and query the status of a given transaction after the transaction has been executed. |
||
426 | * @param string $serviceID The DISCO unique serviceID on the VTPass Server i.e. ikeja-electric, eko-electric, portharcourt-electric, e.t.c |
||
427 | * @param $customerMeterNumber |
||
428 | * @param string $type This is basically the type of meter you are trying to validate. It can be either prepaid or postpaid |
||
429 | * @param string $phoneNumber The phone number of the recipient of this service |
||
430 | * @param int $amount The amount as specified with the provided $variationCode. This amount will be ignored as the variation code determine the price of the data bundle, hence this is an optional parameter. |
||
431 | * @return VTPassResponse |
||
432 | * |
||
433 | * @throws VTPassErrorException |
||
434 | */ |
||
435 | public function buyElectricity(string $requestId, $serviceID, $customerMeterNumber, $type, $phoneNumber, int $amount): VTPassResponse |
||
438 | } |
||
439 | |||
440 | } |
||
441 |