Gecharl::getInstanceName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
65
            return new GecharlResponse($responseObject->status ? 200 : 422, $responseObject->message);
66
        return new GecharlResponse(504);
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?username={$this->config['username']}", [
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))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
88
            return new GecharlResponse($responseObject->status ? 200 : 422, $responseObject->message);
89
        return new GecharlResponse(504);
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
        if ($response->successful())
105
            return new GecharlResponse(200, json_decode($response->body()));
106
        return new GecharlResponse(504);
107
    }
108
109
    /**
110
     * Buy data bundle
111
     * @param string $network Unique network identification code e.g. mtn, glo, airtel...
112
     * @param string $plan Plan code of data plan to subscribe to
113
     * @param string $recipient Phone number to receive data subscription
114
     * @return GecharlResponse
115
     * @throws GecharlErrorException
116
     */
117 View Code Duplication
    public function purchaseDataBundle(string $network, string $plan, string $recipient): GecharlResponse
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
118
    {
119
        $endpoint = "{$this->baseUrl}/data?network=$network&plan=$plan&recipent=$recipient";
120
        $response = $this->withOAuth2()->get($endpoint);
121
122
        $responseObject = json_decode($response->body());
123
        if ($response->successful())
124
            return new GecharlResponse($responseObject->status_code, $responseObject->message);
125
        return new GecharlResponse(504);
126
    }
127
128
    /**
129
     * You need to verify your meter number before purchasing.
130
     *
131
     * Please note the disco unique codes below:
132
     * Ikaja Electricity = <strong>ie</strong>
133
     * Eko Electricity = <strong>ekedc</strong>
134
     * Enugu Electricity = <strong>eedc</strong>
135
     * Kano Electricity = <strong>kano</strong>
136
     * Port Harcourt Electricity = <strong>phed</strong>
137
     * Abuja Electricity = <strong>abuja</strong>
138
     * Ibadan Electricity = <strong>ibedc</strong>
139
     *
140
     * On successful validation for a prepaid meter number, a product_code key comes with it, which should be passed along other parameters when paying.
141
     *
142
     *
143
     * @param string $disco Unique code of the Electricity distribution company the meter number is for
144
     * @param string $meterNumber Meter Number to verify
145
     * @param string $meterType Meter type i.e. <strong>prepaid</strong> or <strong>postpaid</strong>
146
     * @return GecharlResponse
147
     * @throws GecharlErrorException
148
     */
149 View Code Duplication
    public function verifyMeterNumber(string $disco, string $meterNumber, string $meterType): GecharlResponse
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
150
    {
151
        $endpoint = "{$this->baseUrl}/electricity/validate/$disco/?meter_number=$meterNumber&meter_type=$meterType";
152
        $response = $this->withOAuth2()->get($endpoint);
153
154
        $responseObject = json_decode($response->body());
155
        if ($response->successful())
156
            return new GecharlResponse($responseObject->status_code, $responseObject->message);
157
        return new GecharlResponse(504);
158
    }
159
160
    /**
161
     * Purchase Electricity
162
     *
163
     * Please note the disco unique codes below:
164
     * Ikaja Electricity = <strong>ie</strong>
165
     * Eko Electricity = <strong>ekedc</strong>
166
     * Enugu Electricity = <strong>eedc</strong>
167
     * Kano Electricity = <strong>kano</strong>
168
     * Port Harcourt Electricity = <strong>phed</strong>
169
     * Abuja Electricity = <strong>abuja</strong>
170
     * Ibadan Electricity = <strong>ibedc</strong>
171
     *
172
     *
173
     * @param string $disco Unique code of the Electricity distribution company the meter number is for
174
     * @param string $meterNumber Meter Number to verify
175
     * @param string $meterType Meter type i.e. <strong>prepaid</strong> or <strong>postpaid</strong>
176
     * @param $amount
177
     * @param string|null $productCode
178
     * @return GecharlResponse
179
     * @throws GecharlErrorException
180
     */
181
    public function purchaseElectricity(string $disco, string $meterNumber, string $meterType, $amount, string $productCode = null): GecharlResponse
182
    {
183
        $endpoint = "{$this->baseUrl}/electricity/payment/$disco";
184
        $params = [
185
            "meter_number" => $meterNumber,
186
            "meter_type" => $meterType,
187
            "amount" => $amount,
188
        ];
189
        if ($productCode !== null) $params['product_code'] = $productCode;
190
        $response = $this->withOAuth2()->post($endpoint, $params);
191
192
        $responseObject = json_decode($response->body());
193
        if ($response->successful())
194
            return new GecharlResponse($responseObject->status_code, $responseObject->message);
195
        return new GecharlResponse(504);
196
    }
197
198
    /**
199
     * Multichoice(DSTV and GoTv) Smart Card Number/Decoder verification
200
     * You need to verify your Smart card number before purchasing.
201
     *
202
     * @param string $multichoiceType DSTV|GOTV
203
     * @param string $smartCardNumber Customer unique smart card number to subscribe
204
     * @return GecharlResponse
205
     * @throws GecharlErrorException
206
     */
207 View Code Duplication
    public function verifySmartCardNumber(string $multichoiceType, string $smartCardNumber): GecharlResponse
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
208
    {
209
        $endpoint = "{$this->baseUrl}/validate/multichoice?multichoice_type=$multichoiceType&smart_card_no=$smartCardNumber";
210
        $response = $this->withOAuth2()->get($endpoint);
211
212
        $responseObject = json_decode($response->body());
213
        if ($response->successful())
214
            return new GecharlResponse($responseObject->status_code, $responseObject->message);
215
        return new GecharlResponse(504);
216
    }
217
218
    /**
219
     * Purchase DSTV or GoTv Cable Tv Plan
220
     * @param string $multiChoiceType DSTV|GOTV
221
     * @param string $smartCardNumber Customer unique smart card number to subscribe
222
     * @param $amount
223
     * @param string $productCode productCode as gotten from the verification call
224
     * @param string $plan Unique product_code as gotten from the verification call for available plan for the provided SmartCard Number
225
     * @param string $customerPhoneNumber
226
     * @param string $customerName
227
     * @param string|null $transactionId
228
     * @return GecharlResponse
229
     * @throws GecharlErrorException
230
     */
231
    public function purchaseMultiChoice(string $multiChoiceType, string $smartCardNumber, $amount, string $productCode, string $plan, string $customerPhoneNumber = '', string $customerName = '', string $transactionId = null): GecharlResponse
232
    {
233
        $endpoint = "{$this->baseUrl}/multichoice/payment";
234
        $params = [
235
            "multichoice_type" => $multiChoiceType,
236
            "smart_card_no" => $smartCardNumber,
237
            "amount" => $amount,
238
            "product_code" => $plan,
239
            "productCode" => $productCode,
240
        ];
241
        if (empty($customerName)) $params['customer_name'] = $customerName;
242
        if (empty($customerPhoneNumber)) $params['phone_number'] = $customerPhoneNumber;
243
        if ($transactionId !== null) $params['transaction_id'] = $transactionId;
244
        $response = $this->withOAuth2()->post($endpoint, $params);
245
246
        $responseObject = json_decode($response->body());
247
        if ($response->successful())
248
            return new GecharlResponse($responseObject->status_code, $responseObject->message);
249
        return new GecharlResponse(504);
250
    }
251
252
253
    /**
254
     * StarTimes Smart Card Number/Decoder verification
255
     * You need to verify your Smart card number before purchasing.
256
     *
257
     * @param string $smartCardNumber Customer unique smart card number to subscribe
258
     * @return GecharlResponse
259
     * @throws GecharlErrorException
260
     */
261 View Code Duplication
    public function verifyStarTimesSmartCardNumber(string $smartCardNumber): GecharlResponse
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
262
    {
263
        $endpoint = "{$this->baseUrl}/validate/startimes?smart_card_no=$smartCardNumber";
264
        $response = $this->withOAuth2()->get($endpoint);
265
266
        $responseObject = json_decode($response->body());
267
        if ($response->successful())
268
            return new GecharlResponse($responseObject->status_code, $responseObject->message);
269
        return new GecharlResponse(504);
270
    }
271
272
    /**
273
     * Purchase StarTimes Cable Tv Plan
274
     * The product_code is either <strong>NOVA</strong>, <strong>BASIC</strong>, <strong>SMART</strong>, <strong>CLASSIC</strong> or <strong>SUPER</strong>
275
     *
276
     * @param string $smartCardNumber Customer unique smart card number to subscribe
277
     * @param $amount
278
     * @param string $productCode productCode as gotten from the verification call
279
     * @param string $plan Unique product_code as gotten from the verification call for available plan for the provided SmartCard Number
280
     * @param string $customerPhoneNumber
281
     * @param string $customerName
282
     * @param string|null $transactionId
283
     * @return GecharlResponse
284
     * @throws GecharlErrorException
285
     */
286
    public function purchaseStarTimes(string $smartCardNumber, $amount, string $productCode, string $plan, string $transactionId, string $customerPhoneNumber = '', string $customerName = ''): GecharlResponse
287
    {
288
        $endpoint = "{$this->baseUrl}/startimes/payment";
289
        $params = [
290
            "smart_card_no" => $smartCardNumber,
291
            "amount" => $amount,
292
            "product_code" => $plan,
293
            "productCode" => $productCode,
294
            "transaction_id" => $transactionId,
295
        ];
296
        if (empty($customerName)) $params['customer_name'] = $customerName;
297
        if (empty($customerPhoneNumber)) $params['phone_number'] = $customerPhoneNumber;
298
        $response = $this->withOAuth2()->post($endpoint, $params);
299
300
        $responseObject = json_decode($response->body());
301
        if ($response->successful())
302
            return new GecharlResponse($responseObject->status_code, $responseObject->message);
303
        return new GecharlResponse(504);
304
    }
305
}
306