Passed
Push — master ( 5d2020...d89e70 )
by Henry
03:27
created

Paga   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 535
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 178
c 2
b 0
f 0
dl 0
loc 535
rs 9.92
wmc 31

24 Methods

Rating   Name   Duplication   Size   Complexity  
A setSecretKey() 0 3 1
A __construct() 0 5 1
A getTransactionReference() 0 3 1
A setPublicKey() 0 3 1
A setRequestService() 0 3 1
A setTest() 0 3 2
A setApiKey() 0 3 1
A getMerchantServices() 0 13 1
A depositToBank() 0 22 1
A moneyTransferBulk() 0 18 1
A airtimePurchase() 0 14 1
A onBoardMerchant() 0 18 1
A transactionHistory() 0 12 1
A getBanks() 0 12 1
A getMerchants() 0 12 1
A getMobileOperators() 0 12 1
A validateDepositToBank() 0 15 1
A validateCustomer() 0 13 1
B moneyTransfer() 0 28 7
A accountBalance() 0 12 1
A recentTransactionHistory() 0 12 1
A registerCustomer() 0 22 1
A getOperationStatus() 0 12 1
A merchantPayment() 0 22 1
1
<?php
2
3
/*
4
 * This file is part of the Laravel Paga package.
5
 *
6
 * (c) Henry Ugochukwu <[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 Phalconvee\Paga;
13
14
use GuzzleHttp\Exception\GuzzleException;
15
use Phalconvee\Paga\Services\GuzzleRequestService;
16
17
class Paga
18
{
19
    /**
20
     * Issue API key from your Paga dashboard.
21
     *
22
     * @var string
23
     */
24
    protected $apiKey;
25
26
    /**
27
     * Issue public key from your Paga dashboard.
28
     *
29
     * @var string
30
     */
31
    protected $publicKey;
32
33
    /**
34
     * Issue password from your Paga Dashboard.
35
     *
36
     * @var string
37
     */
38
    protected $secretKey;
39
40
    /**
41
     * Set server URL (e.g true = Use Test URL, false = Use Live URL).
42
     *
43
     * @var string
44
     */
45
    protected $url;
46
47
    /**
48
     * Response from requests made to Paga.
49
     *
50
     * @var mixed
51
     */
52
    protected $response;
53
54
    /**
55
     * Define live API URL.
56
     */
57
    const LIVE_API_URL = 'https://www.mypaga.com';
58
59
    /**
60
     * Define test API URL.
61
     */
62
    const TEST_API_URL = 'https://beta.mypaga.com';
63
64
    /**
65
     * Paga constructor.
66
     */
67
    public function __construct()
68
    {
69
        $this->setApiKey();
70
        $this->setPublicKey();
71
        $this->setSecretKey();
72
    }
73
74
    /**
75
     * Get API key from Paga config file.
76
     */
77
    private function setApiKey()
78
    {
79
        $this->apiKey = config('paga.apiKey');
80
    }
81
82
    /**
83
     * Get public key from Paga config file.
84
     */
85
    private function setPublicKey()
86
    {
87
        $this->publicKey = config('paga.publicKey');
88
    }
89
90
    /**
91
     * Get secret key from Paga config file.
92
     */
93
    private function setSecretKey()
94
    {
95
        $this->secretKey = config('paga.secretKey');
96
    }
97
98
    /**
99
     * Set server environment.
100
     *
101
     * @param bool $test
102
     */
103
    public function setTest($test = false)
104
    {
105
        $this->url = ($test) ? self::TEST_API_URL : self::LIVE_API_URL;
106
    }
107
108
    /**
109
     * Set service for making the client request.
110
     *
111
     * @param $hash
112
     *
113
     * @return GuzzleRequestService
114
     */
115
    private function setRequestService($hash)
116
    {
117
        return new GuzzleRequestService($this->url, $hash, $this->publicKey, $this->secretKey);
118
    }
119
120
    /**
121
     * Generate a Unique Transaction Reference.
122
     *
123
     * @return string
124
     */
125
    public function getTransactionReference()
126
    {
127
        return getHashedToken();
128
    }
129
130
    /**
131
     * Return list of banks integrated with Paga.
132
     *
133
     * @return mixed|\Psr\Http\Message\ResponseInterface|string|null
134
     * @throws Exceptions\IsNullException
135
     * @throws GuzzleException
136
     */
137
    public function getBanks()
138
    {
139
        $body = [
140
            'referenceNumber' => request()->reference,
141
        ];
142
143
        $hash = createHash($this->apiKey, $body);
144
145
        $endpoint = 'paga-webservices/business-rest/secured/getBanks';
146
147
        return $this->setRequestService($hash)
148
            ->makeHttpRequest('POST', $endpoint, $body);
149
    }
150
151
    /**
152
     * Return list of merchants integrated with Paga.
153
     *
154
     * @return mixed|\Psr\Http\Message\ResponseInterface|string|null
155
     * @throws Exceptions\IsNullException
156
     * @throws GuzzleException
157
     */
158
    public function getMerchants()
159
    {
160
        $body = [
161
            'referenceNumber' => request()->reference,
162
        ];
163
164
        $hash = createHash($this->apiKey, $body);
165
166
        $endpoint = 'paga-webservices/business-rest/secured/getMerchants';
167
168
        return $this->setRequestService($hash)
169
            ->makeHttpRequest('POST', $endpoint, $body);
170
    }
171
172
    /**
173
     * Return merchant services registered on Paga.
174
     *
175
     * @return mixed|\Psr\Http\Message\ResponseInterface|string|null
176
     * @throws Exceptions\IsNullException
177
     * @throws GuzzleException
178
     */
179
    public function getMerchantServices()
180
    {
181
        $body = [
182
            'referenceNumber'  => request()->reference,
183
            'merchantPublicId' => request()->merchantPublicId,
184
        ];
185
186
        $hash = createHash($this->apiKey, $body);
187
188
        $endpoint = 'paga-webservices/business-rest/secured/getMerchantServices';
189
190
        return $this->setRequestService($hash)
191
            ->makeHttpRequest('POST', $endpoint, $body);
192
    }
193
194
    /**
195
     * Return mobile operators on the Paga platform.
196
     *
197
     * @return mixed|\Psr\Http\Message\ResponseInterface|string|null
198
     * @throws Exceptions\IsNullException
199
     * @throws GuzzleException
200
     */
201
    public function getMobileOperators()
202
    {
203
        $body = [
204
            'referenceNumber' => request()->reference,
205
        ];
206
207
        $hash = createHash($this->apiKey, $body);
208
209
        $endpoint = 'paga-webservices/business-rest/secured/getMobileOperators';
210
211
        return $this->setRequestService($hash)
212
            ->makeHttpRequest('POST', $endpoint, $body);
213
    }
214
215
    /**
216
     * Return operation status per transaction.
217
     *
218
     * @return mixed|\Psr\Http\Message\ResponseInterface|string|null
219
     * @throws Exceptions\IsNullException
220
     * @throws GuzzleException
221
     */
222
    public function getOperationStatus()
223
    {
224
        $body = [
225
            'referenceNumber' => request()->reference,
226
        ];
227
228
        $hash = createHash($this->apiKey, $body);
229
230
        $endpoint = 'paga-webservices/business-rest/secured/getOperationStatus';
231
232
        return $this->setRequestService($hash)
233
            ->makeHttpRequest('POST', $endpoint, $body);
234
    }
235
236
    /**
237
     * Get account balance on Paga.
238
     *
239
     * @return mixed|\Psr\Http\Message\ResponseInterface|string|null
240
     * @throws Exceptions\IsNullException
241
     * @throws GuzzleException
242
     */
243
    public function accountBalance()
244
    {
245
        $body = [
246
            'referenceNumber' => request()->reference,
247
        ];
248
249
        $hash = createHash($this->apiKey, $body);
250
251
        $endpoint = 'paga-webservices/business-rest/secured/accountBalance';
252
253
        return $this->setRequestService($hash)
254
            ->makeHttpRequest('POST', $endpoint, $body);
255
    }
256
257
    /**
258
     * Purchase airtime via Paga.
259
     *
260
     * @return mixed|\Psr\Http\Message\ResponseInterface|string|null
261
     * @throws Exceptions\IsNullException
262
     * @throws GuzzleException
263
     */
264
    public function airtimePurchase()
265
    {
266
        $body = [
267
            'referenceNumber'        => request()->reference,
268
            'amount'                 => request()->amount,
269
            'destinationPhoneNumber' => request()->destinationPhoneNumber,
270
        ];
271
272
        $hash = createHash($this->apiKey, $body);
273
274
        $endpoint = 'paga-webservices/business-rest/secured/airtimePurchase';
275
276
        return $this->setRequestService($hash)
277
            ->makeHttpRequest('POST', $endpoint, $body);
278
    }
279
280
    /**
281
     * Operation to deposit funds into any bank account via Paga.
282
     *
283
     * @return mixed|\Psr\Http\Message\ResponseInterface|string|null
284
     * @throws Exceptions\IsNullException
285
     * @throws GuzzleException
286
     */
287
    public function depositToBank()
288
    {
289
        $body = [
290
            'referenceNumber'              => request()->reference,
291
            'amount'                       => request()->amount,
292
            'destinationBankUUID'          => request()->destinationBankUUID,
293
            'destinationBankAccountNumber' => request()->destinationBankAccountNumber,
294
            'recipientPhoneNumber'         => request()->recipientPhoneNumber,
295
            'currency'                     => request()->currency,
296
        ];
297
298
        $hash = createHash($this->apiKey, [
299
            $body['referenceNumber'],
300
            $body['amount'],
301
            $body['destinationBankUUID'],
302
            $body['destinationBankAccountNumber'],
303
        ]);
304
305
        $endpoint = 'paga-webservices/business-rest/secured/depositToBank';
306
307
        return $this->setRequestService($hash)
308
            ->makeHttpRequest('POST', $endpoint, $body);
309
    }
310
311
    /**
312
     * Validate deposit operation to bank via Paga.
313
     *
314
     * @return mixed|\Psr\Http\Message\ResponseInterface|string|null
315
     * @throws Exceptions\IsNullException
316
     * @throws GuzzleException
317
     */
318
    public function validateDepositToBank()
319
    {
320
        $body = [
321
            'referenceNumber'              => request()->reference,
322
            'amount'                       => request()->amount,
323
            'destinationBankUUID'          => request()->destinationBankUUID,
324
            'destinationBankAccountNumber' => request()->destinationBankAccountNumber,
325
        ];
326
327
        $hash = createHash($this->apiKey, $body);
328
329
        $endpoint = 'paga-webservices/business-rest/secured/validateDepositToBank';
330
331
        return $this->setRequestService($hash)
332
            ->makeHttpRequest('POST', $endpoint, $body);
333
    }
334
335
    /**
336
     * Transfer funds from a variety of sources via Paga.
337
     *
338
     * @return mixed|\Psr\Http\Message\ResponseInterface|string|null
339
     * @throws Exceptions\IsNullException
340
     * @throws GuzzleException
341
     */
342
    public function moneyTransfer()
343
    {
344
        $body = [
345
            'referenceNumber'          => request()->reference,
346
            'amount'                   => request()->amount,
347
            'destinationAccount'       => request()->destinationAccount,
348
            'senderPrincipal'          => request()->senderPrincipal,
349
            'senderCredentials'        => request()->senderCredentials,
350
            'currency'                 => request()->currency,
351
            'destinationBank'          => request()->destinationBank,
352
            'sendWithdrawalCode'       => (request()->sendWithdrawalCode) ? request()->sendWithdrawalCode : null,
353
            'transferReference'        => (request()->transferReference) ? request()->transferReference : null,
354
            'suppressRecipientMessage' => (request()->suppressRecipientMessage) ? true : false,
355
            'alternateSenderName'      => (request()->alternateSenderName) ? request()->alternateSenderName : null,
356
            'minRecipientKYCLevel'     => (request()->minRecipientKYCLevel) ? request()->minRecipientKYCLevel : null,
357
            'holdingPeriod'            => (request()->holdingPeriod) ? request()->holdingPeriod : null,
358
        ];
359
360
        $hash = createHash($this->apiKey, [
361
            $body['referenceNumber'],
362
            $body['amount'],
363
            $body['destinationAccount'],
364
        ]);
365
366
        $endpoint = 'paga-webservices/business-rest/secured/moneyTransfer';
367
368
        return $this->setRequestService($hash)
369
            ->makeHttpRequest('POST', $endpoint, $body);
370
    }
371
372
    /**
373
     * Make bulk money transfer via Paga.
374
     *
375
     * @return mixed|\Psr\Http\Message\ResponseInterface|string|null
376
     * @throws Exceptions\IsNullException
377
     * @throws GuzzleException
378
     */
379
    public function moneyTransferBulk()
380
    {
381
        $body = [
382
            'bulkReferenceNumber' => request()->bulkReferenceNumber,
383
            'items'               => request()->moneyTransferItems,
384
        ];
385
386
        $hash = createHash($this->apiKey, [
387
            $body['items'][0]['referenceNumber'],
388
            $body['items'][0]['amount'],
389
            $body['items'][0]['destinationAccount'],
390
            count($body['items']),
391
        ]);
392
393
        $endpoint = 'paga-webservices/business-rest/secured/moneyTransferBulk';
394
395
        return $this->setRequestService($hash)
396
            ->makeHttpRequest('POST', $endpoint, $body);
397
    }
398
399
    /**
400
     * OnBoard merchant -> create sub organizations on Paga.
401
     *
402
     * @return mixed|\Psr\Http\Message\ResponseInterface|string|null
403
     * @throws Exceptions\IsNullException
404
     * @throws GuzzleException
405
     */
406
    public function onBoardMerchant()
407
    {
408
        $body = [
409
            'reference'          => request()->reference,
410
            'merchantExternalId' => request()->merchantExternalId,
411
            'merchantInfo'       => request()->merchantInfo,
412
            'integration'        => request()->integration,
413
        ];
414
415
        $hash = createHash($this->apiKey, [
416
            $body['reference'],
417
            $body['merchantExternalId'],
418
        ]);
419
420
        $endpoint = 'paga-webservices/business-rest/secured/onboardMerchant';
421
422
        return $this->setRequestService($hash)
423
            ->makeHttpRequest('POST', $endpoint, $body);
424
    }
425
426
    /**
427
     * Make payments to registered merchants on Paga.
428
     *
429
     * @return mixed|\Psr\Http\Message\ResponseInterface|string|null
430
     * @throws Exceptions\IsNullException
431
     * @throws GuzzleException
432
     */
433
    public function merchantPayment()
434
    {
435
        $body = [
436
            'referenceNumber'         => request()->reference,
437
            'amount'                  => request()->amount,
438
            'merchantAccount'         => request()->merchantAccount,
439
            'merchantReferenceNumber' => request()->merchantReferenceNumber,
440
            'currency'                => request()->currency,
441
            'merchantService'         => request()->merchantService,
442
        ];
443
444
        $hash = createHash($this->apiKey, [
445
            $body[ 'referenceNumber' ],
446
            $body[ 'amount' ],
447
            $body[ 'merchantAccount' ],
448
            $body[ 'merchantReferenceNumber' ],
449
        ]);
450
451
        $endpoint = 'paga-webservices/business-rest/secured/merchantPayment';
452
453
        return $this->setRequestService($hash)
454
            ->makeHttpRequest('POST', $endpoint, $body);
455
    }
456
457
    /**
458
     * Get merchant Paga transaction history.
459
     * Pass a transaction reference to fetch a specific transaction.
460
     *
461
     * @return mixed|\Psr\Http\Message\ResponseInterface|string|null
462
     * @throws Exceptions\IsNullException
463
     * @throws GuzzleException
464
     */
465
    public function transactionHistory()
466
    {
467
        $body = [
468
            'referenceNumber' => request()->reference,
469
        ];
470
471
        $hash = createHash($this->apiKey, $body);
472
473
        $endpoint = 'paga-webservices/business-rest/secured/transactionHistory';
474
475
        return $this->setRequestService($hash)
476
            ->makeHttpRequest('POST', $endpoint, $body);
477
    }
478
479
    /**
480
     * Get merchant recent Paga transaction history.
481
     * Return last 5 transactions on their Paga account.
482
     *
483
     * @return mixed|\Psr\Http\Message\ResponseInterface|string|null
484
     * @throws Exceptions\IsNullException
485
     * @throws GuzzleException
486
     */
487
    public function recentTransactionHistory()
488
    {
489
        $body = [
490
            'referenceNumber' => request()->reference,
491
        ];
492
493
        $hash = createHash($this->apiKey, $body);
494
495
        $endpoint = 'paga-webservices/business-rest/secured/recentTransactionHistory';
496
497
        return $this->setRequestService($hash)
498
            ->makeHttpRequest('POST', $endpoint, $body);
499
    }
500
501
    /**
502
     * Register customers on Paga.
503
     *
504
     * @return mixed|\Psr\Http\Message\ResponseInterface|string|null
505
     * @throws Exceptions\IsNullException
506
     * @throws GuzzleException
507
     */
508
    public function registerCustomer()
509
    {
510
        $body = [
511
            'referenceNumber'     => request()->reference,
512
            'customerPhoneNumber' => request()->customerPhoneNumber,
513
            'customerFirstName'   => request()->customerFirstName,
514
            'customerLastName'    => request()->customerLastName,
515
            'customerEmail'       => request()->customerEmail,
516
            'customerDateOfBirth' => request()->customerDateOfBirth,
517
        ];
518
519
        $hash = createHash($this->apiKey, [
520
            $body['referenceNumber'],
521
            $body['customerPhoneNumber'],
522
            $body['customerFirstName'],
523
            $body['customerLastName'],
524
        ]);
525
526
        $endpoint = 'paga-webservices/business-rest/secured/registerCustomer';
527
528
        return $this->setRequestService($hash)
529
            ->makeHttpRequest('POST', $endpoint, $body);
530
    }
531
532
    /**
533
     * Validate customer created on Paga.
534
     *
535
     * @return mixed|\Psr\Http\Message\ResponseInterface|string|null
536
     * @throws Exceptions\IsNullException
537
     * @throws GuzzleException
538
     */
539
    public function validateCustomer()
540
    {
541
        $body = [
542
            'referenceNumber'    => request()->reference,
543
            'customerIdentifier' => request()->customerIdentifier,
544
        ];
545
546
        $hash = createHash($this->apiKey, $body);
547
548
        $endpoint = 'paga-webservices/business-rest/secured/validateCustomer';
549
550
        return $this->setRequestService($hash)
551
            ->makeHttpRequest('POST', $endpoint, $body);
552
    }
553
}
554