Disbursement   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 72
rs 10
ccs 31
cts 31
cp 1
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getUser() 0 11 1
A getTransaction() 0 9 1
A send() 0 21 1
A __construct() 0 4 1
1
<?php
2
3
namespace Bmatovu\AirtelMoney;
4
5
use GuzzleHttp\ClientInterface;
6
use Illuminate\Container\Container;
7
use Illuminate\Contracts\Config\Repository;
8
use Ramsey\Uuid\Uuid;
9
10
class Disbursement
11
{
12
    protected ClientInterface $http;
13
14
    protected Repository $config;
15
16 3
    public function __construct(ClientInterface $http)
17
    {
18 3
        $this->http = $http;
19 3
        $this->config = Container::getInstance()->make('config');
20
    }
21
22
    /**
23
     * @return array<string, mixed>
24
     *
25
     * @throws \GuzzleHttp\Exception\TransferException
26
     */
27 1
    public function send(string $phoneNumber, float $amount, ?string $id = null, ?string $reference = null): array
28
    {
29 1
        $phoneNumber = substr($phoneNumber, -9);
30
31 1
        $paymentUri = $this->config->get('airtel-money.disbursement.payment_uri');
32
33 1
        $response = $this->http->request('POST', $paymentUri, [
34 1
            'json' => [
35 1
                'payee' => [
36 1
                    'msisdn' => $phoneNumber,
37 1
                ],
38 1
                'reference' => $reference ?? 'Disbursement',
39 1
                'pin' => $this->config->get('airtel-money.encrypted_pin'),
40 1
                'transaction' => [
41 1
                    'amount' => $amount,
42 1
                    'id' => $id ?? Uuid::uuid4()->toString(),
43 1
                ],
44 1
            ],
45 1
        ]);
46
47 1
        return json_decode((string) $response->getBody(), true);
48
    }
49
50
    /**
51
     * @return array<string, mixed>
52
     *
53
     * @throws \GuzzleHttp\Exception\TransferException
54
     */
55 1
    public function getTransaction(string $transactionId): array
56
    {
57 1
        $transactionUri = $this->config->get('airtel-money.disbursement.transaction_inquiry_uri');
58
59 1
        $transactionUri = str_replace(':transactionId', $transactionId, $transactionUri);
60
61 1
        $response = $this->http->request('GET', $transactionUri);
62
63 1
        return json_decode((string) $response->getBody(), true);
64
    }
65
66
    /**
67
     * @return array<string, mixed>
68
     *
69
     * @throws \GuzzleHttp\Exception\TransferException
70
     */
71 1
    public function getUser(string $phoneNumber): array
72
    {
73 1
        $phoneNumber = substr($phoneNumber, -9);
74
75 1
        $kycUri = $this->config->get('airtel-money.kyc_uri');
76
77 1
        $kycUri = str_replace(':phoneNumber', $phoneNumber, $kycUri);
78
79 1
        $response = $this->http->request('GET', $kycUri);
80
81 1
        return json_decode((string) $response->getBody(), true);
82
    }
83
}
84