MobileAirtime::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Djunehor\Vtu\Concrete;
5
6
7
use Djunehor\Vtu\Contracts\VtuServiceInterface;
8
use GuzzleHttp\Exception\ClientException;
9
use GuzzleHttp\Psr7\Request;
10
use Illuminate\Support\Str;
11
12
class MobileAirtime extends Vtu
13
{
14
    private $baseUrl = 'https://mobileairtimeng.com/';
15
    private $orderId = 0;
16
17
18 1
    public function __construct($username = null, $password = null)
19
    {
20 1
        $this->username = $username ?? config('laravel-vtu.mobile_airtime.username');
21 1
        $this->password = $password ?? config('laravel-vtu.mobile_airtime.password');
22
23 1
        $this->client = $this->getInstance();
24 1
    }
25
26
    /**
27
     * @inheritDoc
28
     */
29 1
    public function buyAirtime($amount, $mobileNumber, $mobileNetwork, $callBackUrl): bool
30
    {
31
32 1
        $this->request = new Request('GET', $this->baseUrl . "/httpapi");
33
34
        try {
35 1
            $response = $this->client->send($this->request, [
36
                'query_params' => [
37 1
                    'userid' => $this->username,
38 1
                    'pass' => $this->password,
39 1
                    'amt' => $amount,
40 1
                    'network' => $mobileNetwork,
41 1
                    'phone' => $mobileNumber,
42
                ],
43
            ]);
44
45 1
            $response = json_decode($response->getBody()->getContents(), true);
46 1
            $this->response = $response;
47
48 1
            return $this->response == '100' ? true : false;
49
        } catch (ClientException $e) {
50
            $this->httpError = $e;
51
52
            return false;
53
        } catch (\Exception $e) {
54
            $this->httpError = $e;
55
56
            return false;
57
58
        }
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function buyData($mobileNumber, $mobileNetwork, $dataPlan, $callBackUrl): bool
65
    {
66
        $this->request = new Request('GET', $this->baseUrl . "httpapi/datashare");
67
68
        try {
69
            $response = $this->client->send($this->request, [
70
                'query_params' => [
71
                    'userid' => $this->username,
72
                    'pass' => $this->password,
73
                    'datasize' => $dataPlan,
74
                    'network' => $mobileNetwork,
75
                    'phone' => $mobileNumber,
76
                    'user_ref' => Str::random(16),
77
                ],
78
            ]);
79
80
            $response = json_decode($response->getBody()->getContents(), true);
81
            $this->response = $response;
82
            $this->orderId = $response['batchno'] ?? 0;
83
84
            return $this->response['code']  == '100' ? true : false;
85
        } catch (ClientException $e) {
86
            $this->httpError = $e;
87
88
            return false;
89
        } catch (\Exception $e) {
90
            $this->httpError = $e;
91
92
            return false;
93
        }
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99
    public function payUtility($smartCardNumber, $cableTv, $package, $callBackUrl): bool
100
    {
101
        //Todo: Implement utility bill payment
102
        return false;
103
    }
104
105
    public function getOrderId()
106
    {
107
        return $this->orderId;
108
    }
109
110
    public function queryOrder($orderId)
111
    {
112
        $this->request = new Request('GET', $this->baseUrl . "/client/status");
113
114
        try {
115
            $response = $this->client->send($this->request, [
116
                'query_params' => [
117
                    'userid' => $this->username,
118
                    'pass' => $this->password,
119
                    'tid' => $orderId,
120
121
                ],
122
            ]);
123
124
            $response = json_decode($response->getBody()->getContents(), true);
125
            $this->response = explode('|', $response);
126
127
            return $this->response ? true : false;
128
        } catch (ClientException $e) {
129
            $this->httpError = $e;
130
131
            return false;
132
        } catch (\Exception $e) {
133
            $this->httpError = $e;
134
135
            return false;
136
137
        }
138
    }
139
}