VoiceAndText   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 239
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 17.97%

Importance

Changes 0
Metric Value
dl 0
loc 239
ccs 16
cts 89
cp 0.1797
rs 10
c 0
b 0
f 0
wmc 26
lcom 1
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A buyAirtime() 0 28 4
A buyData() 0 28 4
A payUtility() 0 29 5
A getOrderId() 0 4 1
A queryOrder() 0 26 5
A cancelOrder() 0 26 5
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Djunehor
5
 * Date: 1/22/2019
6
 * Time: 9:36 AM.
7
 */
8
9
namespace Djunehor\Vtu\Concrete;
10
11
use GuzzleHttp\Exception\ClientException;
12
use GuzzleHttp\Psr7\Request;
13
14
class VoiceAndText extends Vtu
15
{
16
    private $baseUrl = 'https://www.voiceandtext.com/portal/vtu_api.php';
17
    private $orderId;
18
    private const BUY_AIRTIME = 'APIBuyAirTime';
19
    private const BUY_DATA = 'APIBuyDataPlan';
20
    private const PAY_UTILITY = 'APIBuyCableTv';
21
    private const QUERY_ORDER = 'APIQueryTransaction';
22
    private const CANCEL_ORDER = 'APICancelTransaction';
23
24
    private $statusCodes = [
25
        'MISSING_MOBILENETWORK' => 'Mobile network is empty',
26
        'MISSING_AMOUNT' => 'Amount is empty',
27
28
        'INVALID_ AMOUNT' => 'Amount is not valid',
29
30
        'INVALID_AIRTIME_AMOUNT' =>
31
            'Minimum amount is 100 and Minimum amount is 50,000',
32
33
34
        'INVALID_RECIPIENT' =>
35
            'An invalid mobile phone number was entered',
36
37
        'INSUFICINET_FUND' =>
38
            'Insufficient user wallet balance',
39
40
41
        'INVALID_SERVER_RESPONSE' =>
42
            'Unable to process request',
43
        'SERVER_ERROR' =>
44
            'Server downtime',
45
46
47
        'INVALID_COMMAND' =>
48
            'The command parameter is not valid',
49
50
        'INVALID_API_ KEY' =>
51
            'API Key is wrong',
52
        'ORDER_RECEIVED' =>
53
'Your order has been received',
54
        'MISSING_DATAPLAN'=>
55
'Data plan is empty',
56
57
'INVALID_DATAPLAN'=>
58
'Data plan is not valid',
59
60
61
'INVALID_SMARTCARDNO'=>
62
'An invalid smartcard number was entered',
63
        'MISSING_CABLETV'=>
64
'The URL format is not valid.',
65
66
'MISSING_PACKAGE'=>
67
'Package field is empty',
68
69
70
'PACKAGE_NOT_AVAILABLE'=>
71
'Selected package is not currently available',
72
73
    ];
74
75
    /**
76
     * Class Constructor.
77
     * @param null $token
78
     */
79 5
    public function __construct($token = null)
80
    {
81
82 5
        $this->username = isset($token) ? $token : config('laravel-vtu.voice_and_text.api_token');
83
84 5
        $this->client = $this->getInstance();
85 5
        $this->request = new Request('GET', $this->baseUrl);
86 5
    }
87
88
89
    /**
90
     * @inheritDoc
91
     */
92 3
    public function buyAirtime($amount, $mobileNumber, $mobileNetwork, $callBackUrl): bool
93
    {
94
        try {
95 3
            $response = $this->client->send($this->request, [
96
                'query' => [
97 3
                    'token' => $this->username,
98 3
                    'command' => self::BUY_AIRTIME,
99 3
                    'amount' => $amount,
100 3
                    'mobileNumber' => $mobileNumber,
101 3
                    'mobileNetwork' => $mobileNetwork,
102 3
                    'callbackUrl' => $callBackUrl,
103
                ],
104
            ]);
105
106
            $response = json_decode($response->getBody()->getContents(), true);
107
            $this->response = $response['message'];
108
109
            return $response['status'] == '00' ? true : false;
110 3
        } catch (ClientException $e) {
111 3
            $this->httpError = $e;
112
113 3
            return false;
114
        } catch (\Exception $e) {
115
            $this->httpError = $e;
116
117
            return false;
118
        }
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124
    public function buyData($mobileNumber, $mobileNetwork, $dataPlan, $callbackUrl): bool
125
    {
126
        try {
127
            $response = $this->client->send($this->request, [
128
                'query' => [
129
                    'token' => $this->username,
130
                    'command' => self::BUY_DATA,
131
                    'mobileNumber' => $mobileNumber,
132
                    'mobileNetwork' => $mobileNetwork,
133
                    'dataPlan' => $dataPlan,
134
                    'CallBackUrl' => $callbackUrl,
135
                ],
136
            ]);
137
138
            $response = json_decode($response->getBody()->getContents(), true);
139
            $this->response = $response['message'];
140
141
            return $response['status'] == '00' ? true : false;
142
        } catch (ClientException $e) {
143
            $this->httpError = $e;
144
145
            return false;
146
        } catch (\Exception $e) {
147
            $this->httpError = $e;
148
149
            return false;
150
        }
151
    }
152
153
    /**
154
     * @inheritDoc
155
     */
156
    public function payUtility($smartCardNumber, $cableTv, $package, $callbackUrl): bool
157
    {
158
        try {
159
            $response = $this->client->send($this->request, [
160
                'query' => [
161
                    'token' => $this->username,
162
                    'command' => self::PAY_UTILITY,
163
                    'smartCardNo' => $smartCardNumber,
164
                    'cableTv' => $cableTv,
165
                    'package' => $package,
166
                    'CallBackUrl' => $callbackUrl,
167
                ],
168
            ]);
169
170
            $response = json_decode($response->getBody()->getContents(), true);
171
            $this->response = array_key_exists($response['message'], $this->statusCodes) ? $this->statusCodes[$response['message']] : '';
172
            $this->orderId = $response['orderId'];
173
174
            return $response['status'] == '00' ? true : false;
175
        } catch (ClientException $e) {
176
            $this->httpError = $e;
177
178
            return false;
179
        } catch (\Exception $e) {
180
            $this->httpError = $e;
181
182
            return false;
183
        }
184
    }
185
186
    public function getOrderId(): int
187
    {
188
        return $this->orderId;
189
    }
190
191
    /**Check order status
192
     * @param $orderId
193
     * @return bool
194
     */
195
    public function queryOrder($orderId)
196
    {
197
        try {
198
            $response = $this->client->send($this->request, [
199
                'query' => [
200
                    'token' => $this->username,
201
                    'command' => self::QUERY_ORDER,
202
                    'orderID' => $orderId,
203
                ],
204
            ]);
205
206
            $response = json_decode($response->getBody()->getContents(), true);
207
            $this->response = array_key_exists($response['status'], $this->statusCodes) ? $this->statusCodes[$response['status']] : '';
208
            $this->orderId = $response['orderId'];
209
210
            return $response ? true : false;
211
        } catch (ClientException $e) {
212
            $this->httpError = $e;
213
214
            return false;
215
        } catch (\Exception $e) {
216
            $this->httpError = $e;
217
218
            return false;
219
        }
220
    }
221
222
    /**Cancel ongoing order
223
     * @param $orderId
224
     * @return bool
225
     */
226
    public function cancelOrder($orderId)
227
    {
228
        try {
229
            $response = $this->client->send($this->request, [
230
                'query' => [
231
                    'token' => $this->username,
232
                    'command' => self::CANCEL_ORDER,
233
                    'orderID' => $orderId,
234
                ],
235
            ]);
236
237
            $response = json_decode($response->getBody()->getContents(), true);
238
            $this->response = array_key_exists($response['status'], $this->statusCodes) ? $this->statusCodes[$response['status']] : '';
239
            $this->orderId = $response['orderId'];
240
241
            return $response['status'] == 'ORDER_CANCELLED' ? true : false;
242
        } catch (ClientException $e) {
243
            $this->httpError = $e;
244
245
            return false;
246
        } catch (\Exception $e) {
247
            $this->httpError = $e;
248
249
            return false;
250
        }
251
    }
252
}
253