Payant::addInvoice()   D
last analyzed

Complexity

Conditions 9
Paths 9

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 21
nc 9
nop 5
dl 0
loc 41
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Laravel Payant package.
5
 *
6
 * (c) Emmanuel Awotunde <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Olaoluwa98\Payant;
13
14
use GuzzleHttp\Client;
15
use Illuminate\Support\Facades\Config;
16
// use Olaoluwa98\Payant\Exceptions\ApiRequestError;
17
// use Olaoluwa98\Payant\Exceptions\InvalidCredentials;
18
use Olaoluwa98\Payant\Exceptions\InvalidFeeBearer;
19
// use Olaoluwa98\Payant\Exceptions\InvalidParameterType;
20
use Olaoluwa98\Payant\Exceptions\IsInvalid;
21
use Olaoluwa98\Payant\Exceptions\IsNull;
22
use Olaoluwa98\Payant\Exceptions\IsNullOrInvalid;
23
use Olaoluwa98\Payant\Exceptions\RequiredValueMissing;
24
use Olaoluwa98\Payant\Exceptions\RequiredValuesMissing;
25
use Exception;
26
class Payant {
27
    
28
29
    /**
30
     * @var $private_key
31
    */
32
    protected $private_key;
33
    
34
35
    /**
36
     *
37
     * @var $base_uri
38
     *
39
    */
40
    protected $base_uri = 'https://api.demo.payant.ng';
41
    
42
    
43
    /**
44
     * @var $client
45
     *
46
    */
47
    protected $client;
48
49
    
50
    /**
51
     * [__construct sets the needed variables got from Payant config file]
52
     */
53
    public function __construct()
54
    {
55
        $this->setKey();
56
        $this->setBaseUrl();
57
        $this->setRequestOptions();        
58
    }
59
60
    /**
61
    * Get Base Url from Payant config file
62
    */
63
    public function setBaseUrl()
64
    {
65
        if(Config::get('payant.mode') == 'LIVE')
66
        {
67
            $this->base_uri = "https://api.payant.ng";
68
        }
69
    }
70
71
    /**
72
     * Get private key from Payant config file
73
     */
74
    public function setKey()
75
    {
76
        $this->private_key = Config::get('payant.private_key');
77
    }
78
79
80
    /**
81
     * Set options for making the Client request
82
     */
83
    private function setRequestOptions()
84
    {       
85
       $authorization_string = 'Bearer '. $this->private_key;
86
87
        //Set up Guzzle
88
        $this->client = new Client( [
89
            'base_uri' => $this->base_uri,
90
            'protocols' => ['https'],
91
            'headers' => [
92
                'Authorization' => $authorization_string,
93
                'Content-Type'  => 'application/json',
94
                'Accept'        => 'application/json'
95
            ]
96
        ]);
97
    }
98
99
100
    /**
101
     * [getBanks Get Banks and their ids (Nigeria)]
102
     * @return object [list of banks and their respective bank_ids]
103
    */
104
    public function getBanks(){
105
        return $this->sendRequest('get', '/banks');
106
    }
107
    
108
109
    
110
    /**
111
     * [resolveAccount description]
112
     * @param array $client_data [description]
113
     * Required fields - 'settlement_bank', 'account_number'
114
     * @return object
115
    */
116
    public function resolveAccount( array $client_data){
117
        // Mandatory fields
118
        $required_values = ['settlement_bank', 'account_number'];
119
120
        if(!array_keys_exist($client_data, $required_values)){
121
         throw new RequiredValuesMissing("Missing required values :(");
122
        }
123
124
        $url = '/resolve-account';
125
126
        return $this->sendRequest('post', $url, ['form_params' => $client_data]);
127
    }
128
129
130
131
    
132
    /**
133
     * [addClient description]
134
     * @param array $client_data [description]
135
     * Required fields - 'first_name', 'last_name', 'email', 'phone'
136
     * Optional - 'address', 'company_name', 'type', 'settlement_bank', 'account_number'
137
     * @return object
138
    */
139
    public function addClient( array $client_data){
140
        // Mandatory fields
141
        $required_values = ['first_name', 'last_name', 'email', 'phone'];
142
143
        if(!array_keys_exist($client_data, $required_values)){
144
         throw new RequiredValuesMissing("Missing required values :(");
145
        }
146
147
        $url = '/clients';
148
149
        return $this->sendRequest('post', $url, ['form_params' => $client_data]);
150
    }
151
152
    
153
154
155
    /**
156
     * [getClient Get client Details]
157
     * @param  string $client_id
158
     * @return object
159
    */
160 View Code Duplication
    public function getClient($client_id = null){
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...
161
        if(!$client_id){
0 ignored issues
show
Bug Best Practice introduced by
The expression $client_id of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
162
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Client Id");
163
        }
164
165
        $url = "/clients/{$client_id}";
166
167
        return $this->sendRequest('get', $url);
168
    }
169
170
    
171
172
173
174
    /**
175
    * [editClient - Edit Existing Client]
176
    * @param string $client_id
177
    * @param array $client_data
178
    *        Required fields - 'first_name', 'last_name', 'email', 'phone'
179
    *        Optional - 'address', 'company_name', 'type', 'settlement_bank', 'account_number'
180
    */
181 View Code Duplication
    public function editClient( $client_id, array $client_data){
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...
182
        if(!$client_id){
183
           throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Client Id");
184
        }
185
186
        $url = "/clients/{$client_id}";
187
188
        // Mandatory fields
189
        $required_values = ['first_name', 'last_name', 'email', 'phone'];
190
191
        if(!array_keys_exist($client_data, $required_values)){
192
             throw new RequiredValuesMissing("Missing required values :(");
193
        }
194
195
        return $this->sendRequest('put', $url, ['form_params' => $client_data]);
196
    }
197
198
    
199
200
201
202
    /**
203
     * [deleteClient]
204
     * @param  string $client_id [description]
205
     */
206 View Code Duplication
    public function deleteClient($client_id = null){
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...
207
        if(!$client_id){
0 ignored issues
show
Bug Best Practice introduced by
The expression $client_id of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
208
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Client Id");
209
        }
210
211
        $url = "/clients/{$client_id}";
212
213
        return $this->sendRequest('delete', $url);
214
    }
215
216
217
218
219
220
    /**
221
     * [addInvoice description]
222
     * @param string      $client_id   [Optional - if client_data is supplied]
223
     * @param array|null    $client_data [Optional - if client_id is supplied]
224
     *      Required Keys - 'first_name', 'last_name', 'email', 'phone'
225
     *      Optional - 'address', 'company_name', 'lga', 'state'                        
226
     * @param string      $due_date    [Mandatory, Format - DD/MM/YYYY]
227
     * @param string      $fee_bearer  [Mandatory]
228
     * @param array         $items       [Mandatory]
229
     */
230
    public function addInvoice($client_id, array $client_data, $due_date, $fee_bearer, array $items){
231
        // Mandatory Client fields
232
        $required_client_values = ['first_name', 'last_name', 'email', 'phone'];
233
        
234
235
        // Vaild fee_bearer types: 'account' and 'client'
236
        $valid_fee_bearers = ['account', 'client'];
237
238
        
239
        // Either the client Id is supplied or a new client data is provided
240
        if(!$client_id && !array_keys_exist($client_data, $required_client_values)){
241
            throw new RequiredValuesMissing("Missing required values :( - Provide client_id or client_data");
242
        }
243
244
        if(!$due_date){
245
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Due Date");
246
        }
247
248
        if(!$fee_bearer){
249
            throw new IsNull("Error Processing Request - Null Fee Bearer");
250
        }elseif (!in_array($fee_bearer, $valid_fee_bearers)) {
251
            throw new InvalidFeeBearer("Invalid Fee Bearer - Use either 'account' or 'client'");
252
        }
253
254
        if(!is_array($items)){
255
            throw new IsInvalid("Error Processing Request - Invalid Items");
256
        }
257
258
        $url = "/invoices";
259
260
        $post_data = [
261
            'due_date' => $due_date,
262
            'fee_bearer' => $fee_bearer,
263
            'items' => $items
264
        ];
265
266
        ($client_id) ? $post_data['client_id'] = $client_id : null;
267
        ($client_data) ? $post_data['client'] = $client_data : null;
268
269
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
270
    }
271
272
273
274
275
    /**
276
    * [getInvoice ]
277
    * @param  string $reference_code [Mandatory - Invoice Reference Code]
278
    * @return object               
279
    */
280 View Code Duplication
    public function getInvoice(string $reference_code){
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...
281
        if(!$reference_code){
282
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
283
        }
284
285
        $url = "/invoices/{$reference_code}";
286
287
        return $this->sendRequest('get', $url);
288
    }
289
290
291
292
293
294
    /**
295
    * [sendInvoice]
296
    * @param  string $reference_code [Mandatory - Invoice Reference Code]
297
    * @return object               
298
    */
299 View Code Duplication
    public function sendInvoice(string $reference_code = null){
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...
300
        if(!$reference_code){
0 ignored issues
show
Bug Best Practice introduced by
The expression $reference_code of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
301
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
302
        }
303
304
            $url = "/invoices/send/{$reference_code}";
305
306
            return $this->sendRequest('get', $url);
307
    }
308
309
310
311
312
313
    /**
314
    * [getInvoiceHistory]
315
    * @param  string $period [Mandatory || Valid Options ["today", "week", "month", "30", "90", "year", "custom"]]
316
    * @param  string $start  [Format - DD/MM/YYYY]
317
    * @param  string $end    [Format - DD/MM/YYYY]
318
    * @return object         
319
    */
320 View Code Duplication
    public function getInvoiceHistory($period, $start = null, $end = null){
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...
321
        if(!$period){
322
            throw new RequiredValueMissing("Error Processing Request - period Missing");
323
        }
324
325
        $post_data = checkHistory($period, $start, $end);        
326
327
        $url = "/invoices/history";
328
329
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
330
    }
331
332
333
334
335
336
    /**
337
    * [deleteInvoice]
338
    * @param  string $reference_code [Mandatory - Invoice Reference Code]
339
    * @return object                 
340
    */
341 View Code Duplication
    public function deleteInvoice(string $reference_code){
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...
342
        if(!$reference_code){
343
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
344
        }
345
346
        $url = "/invoices/{$reference_code}";
347
348
        return $this->sendRequest('delete', $url);
349
    }
350
351
352
353
354
355
    /**
356
     * [addTransfer description]
357
     * @param array $client_data [description]
358
     * Required fields - 'first_name', 'last_name', 'email', 'phone', 'settlement_bank', 'account_number',
359
     * Optional - 'address', 'company_name', 'type',
360
     * @param string      $amount    [Mandatory]
361
     * @return  object
362
     */
363
    public function addTransfer(array $client_data, string $amount){
364
        // Mandatory Client fields
365
        $required_client_values = ['first_name', 'last_name', 'email', 'phone', 'settlement_bank', 'account_number'];        
366
        
367
        if(!array_keys_exist($client_data, $required_client_values)){
368
            throw new RequiredValuesMissing("Missing required values :( - Provide client_data");
369
        }
370
371
        if(!$amount){
372
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid amount");
373
        }
374
375
        $url = "/transfers";
376
377
        $post_data = [
378
            'client' => $client_data,
379
            'amount' => $amount,
380
            ];
381
382
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
383
    }
384
385
386
387
388
389
    /**
390
    * [getTransfer ]
391
    * @param  string $reference_code [Mandatory - Transfer Reference Code]
392
    * @return object               
393
    */
394 View Code Duplication
    public function getTransfer(string $reference_code){
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...
395
        if(!$reference_code){
396
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
397
        }
398
399
        $url = "/transfers/{$reference_code}";
400
401
        return $this->sendRequest('get', $url);
402
    }
403
404
405
406
407
408
409
    /**
410
    * [getTransferHistory]
411
    * @param  string $period [Mandatory || Valid Options ["today", "week", "month", "30", "90", "year", "custom"]]
412
    * @param  string $start  [Format - DD/MM/YYYY]
413
    * @param  string $end    [Format - DD/MM/YYYY]
414
    * @return object         
415
    */
416 View Code Duplication
    public function getTransferHistory($period, $start = null, $end = null){
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...
417
        if(!$period){
418
            throw new RequiredValueMissing("Error Processing Request - period Missing");
419
        }
420
421
        // validate period
422
        $post_data = checkHistory($period, $start, $end);
423
424
        $url = "/transfers/history";
425
426
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
427
    }
428
429
430
431
432
433
    /**
434
    * [deleteTransfer]
435
    * @param  string $reference_code [Mandatory - Invoice Reference Code]
436
    * @return object                 
437
    */
438 View Code Duplication
    public function deleteTransfer(string $reference_code){
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...
439
        if(!$reference_code){
440
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
441
        }
442
443
        $url = "/transfers/{$reference_code}";
444
445
        return $this->sendRequest('delete', $url);
446
    }
447
448
449
450
451
452
453
    /**
454
    * [addPayment]
455
    * @param string $reference_code [Mandatory - Invoice Reference Code]
456
    * @param string $due_date           [Mandatory - [Format - DD/MM/YYYY]]
457
    * @param string $amount         [Mandatory]
458
    * @param string $channel        [Mandatory - valid ["Cash", "BankTransfer", "POS", "Cheque"]]
459
    * @return object
460
    */
461 View Code Duplication
    public function addPayment(string $reference_code, string $due_date, string $amount, string $channel){
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...
462
        if(!$reference_code){
463
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
464
        }
465
466
        if(!$due_date){
467
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid date");
468
        }
469
470
        if(!$amount){
471
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid amount");
472
        }
473
474
        $valid_channels = ["Cash", "BankTransfer", "POS", "Cheque"];
475
476
        if(!$channel){
477
            throw new IsNull("Error Processing Request - Null/Invalid amount");
478
        }elseif (!in_array(ucfirst($channel), $valid_channels)) {
479
            throw new IsInvalid("Invalid Channel - Cash, BankTransfer, POS or Cheque");
480
        }
481
482
        $url = "/payments";
483
484
        $post_data = [
485
            'reference_code' => $reference_code,
486
            'date' => $due_date,
487
            'amount' => $amount,
488
            'channel' => $channel
489
        ];
490
491
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
492
    }
493
494
495
496
497
498
    /**
499
    * [getPayment]
500
    * @param string $reference_code [Mandatory - Invoice Reference Code]
501
    * @return object
502
    */
503 View Code Duplication
    public function getPayment(string $reference_code){
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...
504
        if(!$reference_code){
505
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
506
        }
507
508
        $url = "/payments/{$reference_code}";
509
510
        return $this->sendRequest('get', $url);
511
    }
512
513
    
514
515
516
517
    /**
518
    * [getPaymentHistory]
519
    * @param  string $period [Mandatory || Valid Options ["today", "week", "month", "30", "90", "year", "custom"]]
520
    * @param  string $start  [Format - DD/MM/YYYY || Optional if $period !== 'custom']
521
    * @param  string $end    [Format - DD/MM/YYYY || Optional if $period !== 'custom']
522
    * @return object         
523
    */
524 View Code Duplication
    public function getPaymentHistory(string $period, string $start, string $end){
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...
525
        if(!$period){
526
            throw new RequiredValueMissing("Error Processing Request - period Missing");
527
        }
528
529
        // validate period
530
        $post_data = checkHistory($period, $start, $end);
531
532
        $url = "/payments/history";
533
534
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
535
    }
536
537
538
539
540
541
    /**
542
    * [addWallet]
543
    * @param string $name        [Mandatory - Wallet's name]
544
    * @param string $passcode [Mandatory - Wallet's passcode]
545
    * @return object
546
    */
547 View Code Duplication
    public function addWallet(string $name, string $passcode){
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...
548
        if(!$name){
549
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid name");
550
        }
551
552
        if(!$passcode || strlen($passcode) < 6){
553
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid passcode/ length");
554
        }
555
556
        $url = "/wallets";
557
558
        $post_data = [
559
            'name' => $name,
560
            'passcode' => $passcode,
561
        ];
562
563
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
564
    }
565
566
567
568
569
570
    /**
571
    * [getWallet]
572
    * @param  string $reference_code [Mandatory - Wallet's Reference Code]
573
    * @return object 
574
    */
575
    public function getWallet(string $reference_code){
576
        if(!$reference_code){
577
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
578
        }
579
580
        $url = "/wallets/{$reference_code}";
581
582
        return $this->sendRequest('get', $url);
583
    }
584
585
586
587
588
589
    /**
590
    * [changeWalletPasscode]
591
    * @param  string $reference_code [Mandatory - Wallet's Reference Code]
592
    * @param  string $old_passcode [Mandatory - Wallet's Old Passcode]
593
    * @param  string $passcode [Mandatory - Wallet's Passcode]    
594
    * @return object 
595
    */
596
    public function changeWalletPasscode(string $reference_code, string $old_passcode, string $passcode){
597
        if(!$reference_code){
598
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
599
        }
600
601
        if(!$old_passcode){
602
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid old_passcode");
603
        }
604
605
        if(!$passcode || strlen($passcode) < 6){
606
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid passcode");
607
        }
608
609
        $post_data = [
610
            'old_passcode' => $old_passcode,
611
            'passcode' => $passcode,
612
        ];
613
614
        $url = "/wallets/{$reference_code}";
615
616
        return $this->sendRequest('put', $url, ['form_params' => $post_data]);
617
    }
618
619
620
621
622
623
    /**
624
    * [getWallets]
625
    * @return object
626
    */
627
    public function getWallets(){
628
629
        $url = "/wallets";
630
631
        return $this->sendRequest('get', $url);
632
    }
633
634
635
636
637
638
    /**
639
    * [setWalletStatus]
640
    * @param  string $reference_code [Mandatory - Wallet's Reference Code]
641
    * @return object 
642
    */
643
    public function setWalletStatus(string $reference_code){
644
        if(!$reference_code){
645
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
646
        }
647
648
        $url = "/wallets/status/{$reference_code}";
649
650
        return $this->sendRequest('get', $url);
651
    }
652
653
654
655
656
657
    /**
658
    * [withdrawFromWallet]
659
    * @param  string $reference_code [Mandatory - Wallet's Reference Code]
660
    * @param  array $client_data [Mandatory - Client Data]
661
    * Required fields - 'settlement_bank', 'account_number'
662
    * @param  string $amount [Mandatory - Amount to send]
663
    * @param  string $passcode [Mandatory - Wallet's Passcode]
664
    * @return object 
665
    */
666
    public function withdrawFromWallet(string $reference_code, array $client_data, string $amount, string $passcode){
667
        if(!$reference_code){
668
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
669
        }
670
671
        // Mandatory fields
672
        $required_values = ['settlement_bank', 'account_number'];
673
674
        if(!array_keys_exist($client_data, $required_values)){
675
         throw new RequiredValuesMissing("Missing required values :(");
676
        }
677
678
        if(!$amount){
679
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid amount");
680
        }
681
682
        if(!$passcode){
683
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid passcode");
684
        }
685
686
        $post_data = [
687
            'settlement_bank' => $client_data['settlement_bank'],
688
            'account_number' => $client_data['account_number'],
689
            'amount' => $amount,
690
            'passcode' => $passcode,
691
        ];
692
693
        $url = "/wallets/withdraw/{$reference_code}";
694
695
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
696
    }
697
698
699
700
701
702
    /**
703
    * [getWalletTransactions]
704
    * @param  string $reference_code [Mandatory - Wallet's Reference Code]
705
    * @param  string $period [Mandatory || Valid Options ["today", "week", "month", "30", "90", "year", "custom"]]
706
    * @param  string $start  [Format - DD/MM/YYYY]
707
    * @param  string $end    [Format - DD/MM/YYYY]
708
    * @return object         
709
    */
710
    public function getWalletTransactions(string $reference_code, $period, $start = null, $end = null){
711
        if(!$reference_code){
712
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code");
713
        }
714
715
        if(!$period){
716
            throw new RequiredValueMissing("Error Processing Request - period Missing");
717
        }
718
719
        $post_data = checkHistory($period, $start, $end);        
720
721
        $url = "/wallets/transactions/{$reference_code}";
722
723
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
724
    }
725
726
    
727
728
729
730
    /**
731
    * [addProduct]
732
    * @param string $name        [Mandatory - Product's name]
733
    * @param string $description [Mandatory - Product's description]
734
    * @param string $unit_cost   [Mandatory - Product's unit cost]
735
    * @param string $type        [Mandatory - Product type 'product' or 'service']
736
    * @return object
737
    */
738 View Code Duplication
    public function addProduct(string $name, string $description, string $unit_cost, string $type){
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...
739
        if(!$name){
740
            throw new IsNull("Error Processing Request - Null/Invalid name");
741
        }
742
743
        if(!$description){
744
            throw new IsNull("Error Processing Request - Null/Invalid description");
745
        }
746
747
        if(!$unit_cost){
748
            throw new IsNull("Error Processing Request - Null/Invalid unit_cost");
749
        }
750
751
        //Validate Product Type
752
        $valid_product_type = ["product", "service"];
753
754
        if(!$type){
755
            throw new IsNull("Error Processing Request - Null/Invalid type");
756
        }elseif (!in_array(strtolower($type), $valid_product_type)) {
757
            throw new IsInvalid("Invalid Type - Available options: 'product' or 'service'");
758
        }
759
760
        $url = "/products";
761
762
        $post_data = [
763
            'name' => $name,
764
            'description' => $description,
765
            'unit_cost' => $unit_cost,
766
            'type' => $type
767
        ];
768
769
        return $this->sendRequest('post', $url, ['form_params' => $post_data]);
770
    }
771
772
        
773
774
775
776
    /**
777
    * [getProduct]
778
    * @param  int $product_id [Mandatory - Product ID]
779
    * @return object 
780
    */
781 View Code Duplication
    public function getProduct($product_id){
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...
782
        if(!$product_id){
783
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid product_id");
784
        }
785
786
        $url = "/products/{$product_id}";
787
788
        return $this->sendRequest('get', $url);
789
    }
790
791
    
792
793
794
795
    /**
796
    * [editProduct]
797
    * @param  string $product_id   [Mandatory - Product ID]
798
    * @param  array  $product_data [description]
799
    * @return object               
800
    */
801
    public function editProduct($product_id, array $product_data){
802
        if(!$product_id){
803
               throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Product Id");
804
        }
805
806
        //Validate Type
807
        $product_type = strtolower(array_get($product_data, 'type', 'none'));
808
809
        $valid_product_type = ["product", "service"];
810
811
        if(!$product_type){
812
            throw new IsNull("Error Processing Request - Null/Invalid type");
813
        }elseif (!in_array($product_type, $valid_product_type)) {
814
            throw new IsInvalid("Invalid Type - Available options: 'product' or 'service'");
815
        }
816
817
       $url = "/products/{$product_id}";
818
819
       // Mandatory fields
820
       $required_values = ['name', 'description', 'unit_cost', 'type'];
821
822
        if(!array_keys_exist($product_data, $required_values)){
823
             throw new RequiredValuesMissing("Missing required values :(");
824
        }
825
826
        return $this->sendRequest('put', $url, ['form_params' => $product_data]);
827
    }
828
829
    
830
831
832
    /**
833
    * [getProducts]
834
    * @return object
835
    */
836
    public function getProducts(){
837
        $url = "/products";
838
839
        return $this->sendRequest('get', $url);
840
    }
841
842
        
843
844
845
    /**
846
    * [deleteProduct]
847
    * @param $product_id [Mandatory - Product ID]
848
    * @return object           
849
    */
850 View Code Duplication
    public function deleteProduct($product_id){
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...
851
        if(!$product_id){
852
            throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Product Id");
853
        }
854
855
        $url = "/products/{$product_id}";
856
857
        return $this->sendRequest('delete', $url);
858
    }
859
860
861
862
    /**
863
    * [sendRequest]
864
    * @param string $method       [Mandatory - request method <get | post | put | delete> ]
865
    * @param string $url           [Mandatory - url to send request to]
866
    * @param array $params         [data to post to request url]
867
    * @return object
868
    */
869
    public function sendRequest($method, $url, $params=[])
870
    {
871
        try{
872
            if (strtolower($method) == 'get'){
873
                $result = $this->client->request('GET', $url);
874
            }elseif (strtolower($method) == 'post'){
875
                $result = $this->client->request('POST', $url, $params);
876
            }elseif (strtolower($method) == 'put'){
877
                $result = $this->client->request('PUT', $url, $params);
878
            }elseif (strtolower($method) == 'delete'){
879
                $result = $this->client->request('DELETE', $url);
880
            }
881
882
            return cleanResponse($result);
0 ignored issues
show
Bug introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
883
        }
884
        catch( Exception $e){
885
            throw $e;
886
        }
887
    }
888
}
889