Collection   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 38
c 2
b 0
f 0
dl 0
loc 109
rs 10
ccs 48
cts 48
cp 1
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getBalance() 0 7 1
A getTransaction() 0 9 1
A getUser() 0 11 1
A receive() 0 24 1
A __construct() 0 4 1
A refund() 0 13 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 Collection
11
{
12
    protected ClientInterface $http;
13
14
    protected Repository $config;
15
16 5
    public function __construct(ClientInterface $http)
17
    {
18 5
        $this->http = $http;
19 5
        $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 receive(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.collection.payment_uri');
32
33 1
        $response = $this->http->request('POST', $paymentUri, [
34 1
            'json' => [
35 1
                'reference' => $reference ?? 'Collection',
36 1
                'subscriber' => [
37 1
                    'country' => $this->config->get('airtel-money.country'),
38 1
                    'currency' => $this->config->get('airtel-money.currency'),
39 1
                    'msisdn' => $phoneNumber,
40 1
                ],
41 1
                'transaction' => [
42 1
                    'amount' => $amount,
43 1
                    'country' => $this->config->get('airtel-money.country'),
44 1
                    'currency' => $this->config->get('airtel-money.currency'),
45 1
                    'id' => $id ?? Uuid::uuid4()->toString(),
46 1
                ],
47 1
            ],
48 1
        ]);
49
50 1
        return json_decode((string) $response->getBody(), true);
51
    }
52
53
    /**
54
     * @return array<string, mixed>
55
     *
56
     * @throws \GuzzleHttp\Exception\TransferException
57
     */
58 1
    public function refund(string $airtelMoneyId): array
59
    {
60 1
        $refundUri = $this->config->get('airtel-money.collection.refund_uri');
61
62 1
        $response = $this->http->request('POST', $refundUri, [
63 1
            'json' => [
64 1
                'transaction' => [
65 1
                    'airtel_money_id' => $airtelMoneyId,
66 1
                ],
67 1
            ],
68 1
        ]);
69
70 1
        return json_decode((string) $response->getBody(), true);
71
    }
72
73
    /**
74
     * @return array<string, mixed>
75
     *
76
     * @throws \GuzzleHttp\Exception\TransferException
77
     */
78 1
    public function getTransaction(string $transactionId): array
79
    {
80 1
        $transactionUri = $this->config->get('airtel-money.collection.transaction_inquiry_uri');
81
82 1
        $transactionUri = str_replace(':transactionId', $transactionId, $transactionUri);
83
84 1
        $response = $this->http->request('GET', $transactionUri);
85
86 1
        return json_decode((string) $response->getBody(), true);
87
    }
88
89
    /**
90
     * @return array<string, mixed>
91
     *
92
     * @throws \GuzzleHttp\Exception\TransferException
93
     */
94 1
    public function getBalance(): array
95
    {
96 1
        $balanceUri = $this->config->get('airtel-money.collection.balance_inquiry_uri');
97
98 1
        $response = $this->http->request('GET', $balanceUri);
99
100 1
        return json_decode((string) $response->getBody(), true);
101
    }
102
103
    /**
104
     * @return array<string, mixed>
105
     *
106
     * @throws \GuzzleHttp\Exception\TransferException
107
     */
108 1
    public function getUser(string $phoneNumber): array
109
    {
110 1
        $phoneNumber = substr($phoneNumber, -9);
111
112 1
        $kycUri = $this->config->get('airtel-money.kyc_uri');
113
114 1
        $kycUri = str_replace(':phoneNumber', $phoneNumber, $kycUri);
115
116 1
        $response = $this->http->request('GET', $kycUri);
117
118 1
        return json_decode((string) $response->getBody(), true);
119
    }
120
}
121