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
|
|
|
|
26
|
|
|
use \Exception as phpException; |
27
|
|
|
class Payant { |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var $private_key |
32
|
|
|
*/ |
33
|
|
|
protected $private_key; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @var $base_uri |
39
|
|
|
* |
40
|
|
|
*/ |
41
|
|
|
protected $base_uri = 'https://api.demo.payant.ng'; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var $client |
46
|
|
|
* |
47
|
|
|
*/ |
48
|
|
|
protected $client; |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* [__construct sets the needed variables got from Payant config file] |
53
|
|
|
*/ |
54
|
|
|
public function __construct() |
55
|
|
|
{ |
56
|
|
|
$this->setKey(); |
57
|
|
|
$this->setBaseUrl(); |
58
|
|
|
$this->setRequestOptions(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get Base Url from Payant config file |
63
|
|
|
*/ |
64
|
|
|
public function setBaseUrl() |
65
|
|
|
{ |
66
|
|
|
if(Config::get('payant.mode') == 'LIVE') |
67
|
|
|
{ |
68
|
|
|
$this->base_uri = "https://api.payant.ng"; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get private key from Payant config file |
74
|
|
|
*/ |
75
|
|
|
public function setKey() |
76
|
|
|
{ |
77
|
|
|
$this->private_key = Config::get('payant.private_key'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Set options for making the Client request |
83
|
|
|
*/ |
84
|
|
|
private function setRequestOptions() |
85
|
|
|
{ |
86
|
|
|
$authorization_string = 'Bearer '. $this->private_key; |
87
|
|
|
|
88
|
|
|
//Set up Guzzle |
89
|
|
|
$this->client = new Client( [ |
90
|
|
|
'base_uri' => $this->base_uri, |
91
|
|
|
'protocols' => ['https'], |
92
|
|
|
'headers' => [ |
93
|
|
|
'Authorization' => $authorization_string, |
94
|
|
|
'Content-Type' => 'application/json', |
95
|
|
|
'Accept' => 'application/json' |
96
|
|
|
] |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* [getStates Get States in a country (Nigeria)] |
103
|
|
|
* @return object [list of banks and their respective bank_ids] |
104
|
|
|
*/ |
105
|
|
|
public function getBanks(){ |
106
|
|
|
return $this->sendRequest('get', '/banks'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* [resolveAccount description] |
113
|
|
|
* @param array $client_data [description] |
114
|
|
|
* Required fields - 'settlement_bank', 'account_number' |
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
|
|
|
*/ |
138
|
|
|
public function addClient(array $client_data){ |
139
|
|
|
// Mandatory fields |
140
|
|
|
$required_values = ['first_name', 'last_name', 'email', 'phone']; |
141
|
|
|
|
142
|
|
|
if(!array_keys_exist($client_data, $required_values)){ |
143
|
|
|
throw new RequiredValuesMissing("Missing required values :("); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$url = '/clients'; |
147
|
|
|
|
148
|
|
|
return $this->sendRequest('post', $url, ['form_params' => $client_data]); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
|
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* [getClient Get client Details] |
156
|
|
|
* @param string $client_id |
157
|
|
|
* @return object |
158
|
|
|
*/ |
159
|
|
View Code Duplication |
public function getClient($client_id = null){ |
|
|
|
|
160
|
|
|
if(!$client_id || $client_id === null){ |
|
|
|
|
161
|
|
|
throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Client Id"); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$url = "/clients/{$client_id}"; |
165
|
|
|
|
166
|
|
|
return $this->sendRequest('get', $url); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
|
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* [editClient - Edit Existing Client] |
175
|
|
|
* @param string $client_id |
176
|
|
|
* @param array $client_data |
177
|
|
|
* Required fields - 'first_name', 'last_name', 'email', 'phone' |
178
|
|
|
* Optional - 'address', 'company_name', 'type', 'settlement_bank', 'account_number' |
179
|
|
|
*/ |
180
|
|
|
public function editClient($client_id = null, array $client_data){ |
181
|
|
|
if(!$client_id || $client_id === null){ |
|
|
|
|
182
|
|
|
throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Client Id"); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$url = "/clients/{$client_id}"; |
186
|
|
|
|
187
|
|
|
// Mandatory fields |
188
|
|
|
$required_values = ['first_name', 'last_name', 'email', 'phone']; |
189
|
|
|
|
190
|
|
|
if(!array_keys_exist($client_data, $required_values)){ |
191
|
|
|
throw new RequiredValuesMissing("Missing required values :("); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $this->sendRequest('put', $url, ['form_params' => $client_data]); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
|
199
|
|
|
|
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* [deleteClient] |
203
|
|
|
* @param string $client_id [description] |
204
|
|
|
*/ |
205
|
|
View Code Duplication |
public function deleteClient($client_id = null){ |
|
|
|
|
206
|
|
|
if(!$client_id || $client_id === null){ |
|
|
|
|
207
|
|
|
throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Client Id"); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$url = "/clients/{$client_id}"; |
211
|
|
|
|
212
|
|
|
return $this->sendRequest('delete', $url); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
|
216
|
|
|
|
217
|
|
|
|
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* [addInvoice description] |
221
|
|
|
* @param string $client_id [Optional - if client_data is supplied] |
222
|
|
|
* @param array|null $client_data [Optional - if client_id is supplied] |
223
|
|
|
* Required Keys - 'first_name', 'last_name', 'email', 'phone' |
224
|
|
|
* Optional - 'address', 'company_name', 'lga', 'state' |
225
|
|
|
* @param string $due_date [Mandatory, Format - DD/MM/YYYY] |
226
|
|
|
* @param string $fee_bearer [Mandatory] |
227
|
|
|
* @param array $items [Mandatory] |
228
|
|
|
*/ |
229
|
|
|
public function addInvoice($client_id = null, array $client_data, $due_date = null, $fee_bearer = null, array $items){ |
230
|
|
|
// Mandatory Client fields |
231
|
|
|
$required_client_values = ['first_name', 'last_name', 'email', 'phone']; |
232
|
|
|
|
233
|
|
|
|
234
|
|
|
// Vaild fee_bearer types: 'account' and 'client' |
235
|
|
|
$valid_fee_bearers = ['account', 'client']; |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
// Either the client Id is supplied or a new client data is provided |
239
|
|
|
if(!$client_id || $client_id === null && !array_keys_exist($client_data, $required_client_values)){ |
|
|
|
|
240
|
|
|
throw new RequiredValuesMissing("Missing required values :( - Provide client_id or client_data"); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
if(!$due_date || $due_date === null){ |
|
|
|
|
244
|
|
|
throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Due Date"); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
if(!$fee_bearer || $fee_bearer === null){ |
|
|
|
|
248
|
|
|
throw new IsNull("Error Processing Request - Null Fee Bearer"); |
249
|
|
|
}elseif (!in_array($fee_bearer, $valid_fee_bearers)) { |
250
|
|
|
throw new InvalidFeeBearer("Invalid Fee Bearer - Use either 'account' or 'client'"); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
if(!is_array($items)){ |
254
|
|
|
throw new IsInvalid("Error Processing Request - Invalid Items"); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$url = "/invoices"; |
258
|
|
|
|
259
|
|
|
$post_data = [ |
260
|
|
|
'due_date' => $due_date, |
261
|
|
|
'fee_bearer' => $fee_bearer, |
262
|
|
|
'items' => $items |
263
|
|
|
]; |
264
|
|
|
|
265
|
|
|
($client_id) ? $post_data['client_id'] = $client_id : null; |
266
|
|
|
($client_data) ? $post_data['client'] = $client_data : null; |
267
|
|
|
|
268
|
|
|
return $this->sendRequest('post', $url, ['form_params' => $post_data]); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
|
272
|
|
|
|
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* [getInvoice ] |
276
|
|
|
* @param string $reference_code [Mandatory - Invoice Reference Code] |
277
|
|
|
* @return object |
278
|
|
|
*/ |
279
|
|
View Code Duplication |
public function getInvoice($reference_code = null){ |
|
|
|
|
280
|
|
|
if(!$reference_code || $reference_code === null){ |
|
|
|
|
281
|
|
|
throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code"); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
$url = "/invoices/{$reference_code}"; |
285
|
|
|
|
286
|
|
|
return $this->sendRequest('get', $url); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* [sendInvoice] |
291
|
|
|
* @param string $reference_code [Mandatory - Invoice Reference Code] |
292
|
|
|
* @return object |
293
|
|
|
*/ |
294
|
|
View Code Duplication |
public function sendInvoice($reference_code = null){ |
|
|
|
|
295
|
|
|
if(!$reference_code || $reference_code === null){ |
|
|
|
|
296
|
|
|
throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code"); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
$url = "/invoices/send/{$reference_code}"; |
300
|
|
|
|
301
|
|
|
return $this->sendRequest('get', $url); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
|
305
|
|
|
|
306
|
|
|
|
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* [getInvoiceHistory] |
310
|
|
|
* @param string $period [Mandatory || Valid Options ["today", "week", "month", "30", "90", "year", "custom"]] |
311
|
|
|
* @param string $start [Format - DD/MM/YYYY] |
312
|
|
|
* @param string $end [Format - DD/MM/YYYY] |
313
|
|
|
* @return object |
314
|
|
|
*/ |
315
|
|
View Code Duplication |
public function getInvoiceHistory($period = null, $start = null, $end = null){ |
|
|
|
|
316
|
|
|
if(!$period || $period === null){ |
|
|
|
|
317
|
|
|
throw new RequiredValueMissing("Error Processing Request - period Missing"); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
//Validate Period |
321
|
|
|
$valid_period_options = ["today", "week", "month", "30", "90", "year", "custom"]; |
322
|
|
|
|
323
|
|
|
if (!in_array($period, $valid_period_options)) { |
324
|
|
|
throw new IsInvalid("Invalid Period - Available options: today, week, month, 30, 90, year or custom"); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
$post_data = [ |
328
|
|
|
'period' => $period |
329
|
|
|
]; |
330
|
|
|
|
331
|
|
|
if ($period == 'custom'){ |
332
|
|
|
if (!$start || !$end || $start === null || $end === null){ |
|
|
|
|
333
|
|
|
throw new IsNull("Invalid custom Start or End date"); |
334
|
|
|
} |
335
|
|
|
$post_data['start'] = $start; |
336
|
|
|
$post_data['end'] = $end; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
$url = "/invoices/history"; |
340
|
|
|
|
341
|
|
|
return $this->sendRequest('post', $url, ['form_params' => $post_data]); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
|
345
|
|
|
|
346
|
|
|
|
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* [deleteInvoice] |
350
|
|
|
* @param string $reference_code [Mandatory - Invoice Reference Code] |
351
|
|
|
* @return object |
352
|
|
|
*/ |
353
|
|
View Code Duplication |
public function deleteInvoice($reference_code = null){ |
|
|
|
|
354
|
|
|
if(!$reference_code || $reference_code === null){ |
|
|
|
|
355
|
|
|
throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code"); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
$url = "/invoices/{$reference_code}"; |
359
|
|
|
|
360
|
|
|
return $this->sendRequest('delete', $url); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
|
364
|
|
|
|
365
|
|
|
|
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* [addTransfer description] |
369
|
|
|
* @param array $client_data [description] |
370
|
|
|
* Required fields - 'first_name', 'last_name', 'email', 'phone', 'settlement_bank', 'account_number', |
371
|
|
|
* Optional - 'address', 'company_name', 'type', |
372
|
|
|
* @param string $amount [Mandatory] |
373
|
|
|
*/ |
374
|
|
|
public function addTransfer(array $client_data, string $amount = null){ |
375
|
|
|
// Mandatory Client fields |
376
|
|
|
$required_client_values = ['first_name', 'last_name', 'email', 'phone', 'settlement_bank', 'account_number']; |
377
|
|
|
|
378
|
|
|
if(!array_keys_exist($client_data, $required_client_values)){ |
379
|
|
|
throw new RequiredValuesMissing("Missing required values :( - Provide client_data"); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
if(!$amount || $amount === null){ |
|
|
|
|
383
|
|
|
throw new IsNullOrInvalid("Error Processing Request - Null/Invalid amount"); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
$url = "/transfers"; |
387
|
|
|
|
388
|
|
|
$post_data = [ |
389
|
|
|
'client' => $client_data, |
390
|
|
|
'amount' => $amount, |
391
|
|
|
]; |
392
|
|
|
|
393
|
|
|
return $this->sendRequest('post', $url, ['form_params' => $post_data]); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
|
397
|
|
|
|
398
|
|
|
|
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* [getTransfer ] |
402
|
|
|
* @param string $reference_code [Mandatory - Transfer Reference Code] |
403
|
|
|
* @return object |
404
|
|
|
*/ |
405
|
|
View Code Duplication |
public function getTransfer($reference_code = null){ |
|
|
|
|
406
|
|
|
if(!$reference_code || $reference_code === null){ |
|
|
|
|
407
|
|
|
throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code"); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
$url = "/transfers/{$reference_code}"; |
411
|
|
|
|
412
|
|
|
return $this->sendRequest('get', $url); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
|
416
|
|
|
|
417
|
|
|
|
418
|
|
|
|
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* [getTransferHistory] |
422
|
|
|
* @param string $period [Mandatory || Valid Options ["today", "week", "month", "30", "90", "year", "custom"]] |
423
|
|
|
* @param string $start [Format - DD/MM/YYYY] |
424
|
|
|
* @param string $end [Format - DD/MM/YYYY] |
425
|
|
|
* @return object |
426
|
|
|
*/ |
427
|
|
View Code Duplication |
public function getTransferHistory($period = null, $start = null, $end = null){ |
|
|
|
|
428
|
|
|
if(!$period || $period === null){ |
|
|
|
|
429
|
|
|
throw new RequiredValueMissing("Error Processing Request - period Missing"); |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
//Validate Period |
433
|
|
|
$valid_period_options = ["today", "week", "month", "30", "90", "year", "custom"]; |
434
|
|
|
|
435
|
|
|
if (!in_array($period, $valid_period_options)) { |
436
|
|
|
throw new IsInvalid("Invalid Period - Available options: today, week, month, 30, 90, year or custom"); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
$post_data = [ |
440
|
|
|
'period' => $period |
441
|
|
|
]; |
442
|
|
|
|
443
|
|
|
if ($period == 'custom'){ |
444
|
|
|
if (!$start || !$end || $start === null || $end === null){ |
|
|
|
|
445
|
|
|
throw new IsNull("Invalid custom Start or End date"); |
446
|
|
|
} |
447
|
|
|
$post_data['start'] = $start; |
448
|
|
|
$post_data['end'] = $end; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
$url = "/transfers/history"; |
452
|
|
|
|
453
|
|
|
return $this->sendRequest('post', $url, ['form_params' => $post_data]); |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
|
457
|
|
|
|
458
|
|
|
|
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* [deleteTransfer] |
462
|
|
|
* @param string $reference_code [Mandatory - Invoice Reference Code] |
463
|
|
|
* @return object |
464
|
|
|
*/ |
465
|
|
View Code Duplication |
public function deleteTransfer($reference_code = null){ |
|
|
|
|
466
|
|
|
if(!$reference_code || $reference_code === null){ |
|
|
|
|
467
|
|
|
throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code"); |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
$url = "/transfers/{$reference_code}"; |
471
|
|
|
|
472
|
|
|
return $this->sendRequest('delete', $url); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
|
476
|
|
|
|
477
|
|
|
|
478
|
|
|
|
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* [addPayment] |
482
|
|
|
* @param string $reference_code [Mandatory - Invoice Reference Code] |
483
|
|
|
* @param string $date [Mandatory - [Format - DD/MM/YYYY]] |
484
|
|
|
* @param string $amount [Mandatory] |
485
|
|
|
* @param string $channel [Mandatory - valid ["Cash", "BankTransfer", "POS", "Cheque"]] |
486
|
|
|
*/ |
487
|
|
View Code Duplication |
public function addPayment(string $reference_code = null, string $date = null, string $amount = null, string $channel = null){ |
|
|
|
|
488
|
|
|
if(!$reference_code || $reference_code === null){ |
|
|
|
|
489
|
|
|
throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code"); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
if(!$due_date || $due_date === null){ |
|
|
|
|
493
|
|
|
throw new IsNullOrInvalid("Error Processing Request - Null/Invalid date"); |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
if(!$amount || $amount === null){ |
|
|
|
|
497
|
|
|
throw new IsNullOrInvalid("Error Processing Request - Null/Invalid amount"); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
$valid_channels = ["Cash", "BankTransfer", "POS", "Cheque"]; |
501
|
|
|
|
502
|
|
|
if(!$channel || $channel === null){ |
|
|
|
|
503
|
|
|
throw new IsNull("Error Processing Request - Null/Invalid amount"); |
504
|
|
|
}elseif (!in_array(ucfirst($channel), $valid_channels)) { |
505
|
|
|
throw new IsInvalid("Invalid Channel - Cash, BankTransfer, POS or Cheque"); |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
$url = "/payments"; |
509
|
|
|
|
510
|
|
|
$post_data = [ |
511
|
|
|
'reference_code' => $reference_code, |
512
|
|
|
'date' => $date, |
513
|
|
|
'amount' => $amount, |
514
|
|
|
'channel' => $channel |
515
|
|
|
]; |
516
|
|
|
|
517
|
|
|
return $this->sendRequest('post', $url, ['form_params' => $post_data]); |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
|
521
|
|
|
|
522
|
|
|
|
523
|
|
|
|
524
|
|
|
/** |
525
|
|
|
* [getPayment] |
526
|
|
|
* @param string $reference_code [Mandatory - Invoice Reference Code] |
527
|
|
|
*/ |
528
|
|
View Code Duplication |
public function getPayment($reference_code = null){ |
|
|
|
|
529
|
|
|
if(!$reference_code || $reference_code === null){ |
|
|
|
|
530
|
|
|
throw new IsNullOrInvalid("Error Processing Request - Null/Invalid reference_code"); |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
$url = "/payments/{$reference_code}"; |
534
|
|
|
|
535
|
|
|
return $this->sendRequest('get', $url); |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
|
539
|
|
|
|
540
|
|
|
|
541
|
|
|
|
542
|
|
|
/** |
543
|
|
|
* [getPaymentHistory] |
544
|
|
|
* @param string $period [Mandatory || Valid Options ["today", "week", "month", "30", "90", "year", "custom"]] |
545
|
|
|
* @param string $start [Format - DD/MM/YYYY || Optional if $period !== 'custom'] |
546
|
|
|
* @param string $end [Format - DD/MM/YYYY || Optional if $period !== 'custom'] |
547
|
|
|
* @return object |
548
|
|
|
*/ |
549
|
|
View Code Duplication |
public function getPaymentHistory(string $period = null, string $start = null, string $end = null){ |
|
|
|
|
550
|
|
|
if(!$period || $period === null){ |
|
|
|
|
551
|
|
|
throw new RequiredValueMissing("Error Processing Request - period Missing"); |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
//Validate Period |
555
|
|
|
$valid_period_options = ["today", "week", "month", "30", "90", "year", "custom"]; |
556
|
|
|
|
557
|
|
|
if (!in_array(strtolower($period), $valid_period_options)) { |
558
|
|
|
throw new IsInvalid("Invalid Period - Available options: today, week, month, 30, 90, year or custom"); |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
$post_data = [ |
562
|
|
|
'period' => $period |
563
|
|
|
]; |
564
|
|
|
|
565
|
|
|
if ($period == 'custom'){ |
566
|
|
|
if (!$start || !$end || $start === null || $end === null){ |
|
|
|
|
567
|
|
|
throw new IsNull("Invalid custom Start or End date"); |
568
|
|
|
} |
569
|
|
|
$post_data['start'] = $start; |
570
|
|
|
$post_data['end'] = $end; |
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
$url = "/payments/history"; |
574
|
|
|
|
575
|
|
|
return $this->sendRequest('post', $url, ['form_params' => $post_data]); |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
|
579
|
|
|
|
580
|
|
|
|
581
|
|
|
|
582
|
|
|
/** |
583
|
|
|
* [addProduct] |
584
|
|
|
* @param string $name [Mandatory - Product's name] |
585
|
|
|
* @param string $description [Mandatory - Product's description] |
586
|
|
|
* @param string $unit_cost [Mandatory - Product's unit cost] |
587
|
|
|
* @param string $type [Mandatory - Product type 'product' or 'service'] |
588
|
|
|
*/ |
589
|
|
View Code Duplication |
public function addProduct(string $name = null, string $description = null, string $unit_cost = null, string $type = null){ |
|
|
|
|
590
|
|
|
if(!$name || $name === null){ |
|
|
|
|
591
|
|
|
throw new IsNull("Error Processing Request - Null/Invalid name"); |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
if(!$description || $description === null){ |
|
|
|
|
595
|
|
|
throw new IsNull("Error Processing Request - Null/Invalid description"); |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
if(!$unit_cost || $unit_cost === null){ |
|
|
|
|
599
|
|
|
throw new IsNull("Error Processing Request - Null/Invalid unit_cost"); |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
//Validate Product Type |
603
|
|
|
$valid_product_type = ["product", "service"]; |
604
|
|
|
|
605
|
|
|
if(!$type || $type === null){ |
|
|
|
|
606
|
|
|
throw new IsNull("Error Processing Request - Null/Invalid type"); |
607
|
|
|
}elseif (!in_array(strtolower($type), $valid_product_type)) { |
608
|
|
|
throw new IsInvalid("Invalid Type - Available options: 'product' or 'service'"); |
609
|
|
|
} |
610
|
|
|
|
611
|
|
|
$url = "/products"; |
612
|
|
|
|
613
|
|
|
$post_data = [ |
614
|
|
|
'name' => $name, |
615
|
|
|
'description' => $description, |
616
|
|
|
'unit_cost' => $unit_cost, |
617
|
|
|
'type' => $type |
618
|
|
|
]; |
619
|
|
|
|
620
|
|
|
return $this->sendRequest('post', $url, ['form_params' => $post_data]); |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
|
624
|
|
|
|
625
|
|
|
|
626
|
|
|
|
627
|
|
|
/** |
628
|
|
|
* [getProduct] |
629
|
|
|
* @param int $product_id [Mandatory - Product ID] |
630
|
|
|
* @return object |
631
|
|
|
*/ |
632
|
|
View Code Duplication |
public function getProduct($product_id = null){ |
|
|
|
|
633
|
|
|
if(!$product_id || $product_id === null){ |
|
|
|
|
634
|
|
|
throw new IsNullOrInvalid("Error Processing Request - Null/Invalid product_id"); |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
$url = "/products/{$product_id}"; |
638
|
|
|
|
639
|
|
|
return $this->sendRequest('get', $url); |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
|
643
|
|
|
|
644
|
|
|
|
645
|
|
|
|
646
|
|
|
/** |
647
|
|
|
* [editProduct] |
648
|
|
|
* @param string $product_id [Mandatory - Product ID] |
649
|
|
|
* @param array $product_data [description] |
650
|
|
|
* @return object |
651
|
|
|
*/ |
652
|
|
|
public function editProduct($product_id = null, array $product_data){ |
653
|
|
|
if(!$product_id || $product_id === null){ |
|
|
|
|
654
|
|
|
throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Product Id"); |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
//Validate Type |
658
|
|
|
$product_type = strtolower(array_get($product_data, 'type', 'none')); |
659
|
|
|
|
660
|
|
|
$valid_product_type = ["product", "service"]; |
661
|
|
|
|
662
|
|
|
if(!$product_type){ |
663
|
|
|
throw new IsNull("Error Processing Request - Null/Invalid type"); |
664
|
|
|
}elseif (!in_array($product_type, $valid_product_type)) { |
665
|
|
|
throw new IsInvalid("Invalid Type - Available options: 'product' or 'service'"); |
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
$url = "/products/{$product_id}"; |
669
|
|
|
|
670
|
|
|
// Mandatory fields |
671
|
|
|
$required_values = ['name', 'description', 'unit_cost', 'type']; |
672
|
|
|
|
673
|
|
|
if(!array_keys_exist($client_data, $required_values)){ |
|
|
|
|
674
|
|
|
throw new RequiredValuesMissing("Missing required values :("); |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
return $this->sendRequest('put', $url, ['form_params' => $post_data]); |
|
|
|
|
678
|
|
|
} |
679
|
|
|
|
680
|
|
|
|
681
|
|
|
|
682
|
|
|
|
683
|
|
|
/** |
684
|
|
|
* [getProducts] |
685
|
|
|
* @return object |
686
|
|
|
*/ |
687
|
|
|
public function getProducts(){ |
688
|
|
|
$url = "/products"; |
689
|
|
|
|
690
|
|
|
return $this->sendRequest('get', $url); |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
|
694
|
|
|
|
695
|
|
|
|
696
|
|
|
/** |
697
|
|
|
* [deleteProduct] |
698
|
|
|
* @param $product_id [Mandatory - Product ID] |
699
|
|
|
* @return object |
700
|
|
|
*/ |
701
|
|
View Code Duplication |
public function deleteProduct($product_id = null){ |
|
|
|
|
702
|
|
|
if(!$product_id || $product_id === null){ |
703
|
|
|
throw new IsNullOrInvalid("Error Processing Request - Null/Invalid Product Id"); |
704
|
|
|
} |
705
|
|
|
|
706
|
|
|
$url = "/products/{$product_id}"; |
707
|
|
|
|
708
|
|
|
return $this->sendRequest('delete', $url); |
709
|
|
|
} |
710
|
|
|
|
711
|
|
|
|
712
|
|
|
|
713
|
|
|
/** |
714
|
|
|
* [addPayment] |
715
|
|
|
* @param string $method [Mandatory - request method <get | post | put | delete> ] |
716
|
|
|
* @param string $url [Mandatory - url to send request to] |
717
|
|
|
* @param array $params [data to post to request url] |
718
|
|
|
*/ |
719
|
|
|
public function sendRequest($method, $url, $params=[]) |
720
|
|
|
{ |
721
|
|
|
try{ |
722
|
|
|
if (strtolower($method) == 'get'){ |
723
|
|
|
$result = $this->client->request('GET', $url); |
724
|
|
|
}elseif (strtolower($method) == 'post'){ |
725
|
|
|
$result = $this->client->request('POST', $url, $params); |
726
|
|
|
}elseif (strtolower($method) == 'put'){ |
727
|
|
|
$result = $this->client->request('PUT', $url, $params); |
728
|
|
|
}elseif (strtolower($method) == 'delete'){ |
729
|
|
|
$result = $this->client->request('DELETE', $url); |
730
|
|
|
} |
731
|
|
|
|
732
|
|
|
return cleanResponse($result); |
|
|
|
|
733
|
|
|
} |
734
|
|
|
catch( Exception $e){ |
|
|
|
|
735
|
|
|
throw $e; |
736
|
|
|
} |
737
|
|
|
} |
738
|
|
|
} |
739
|
|
|
|
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.