Passed
Push — main ( 5dbf08...9589c4 )
by Brian
02:38
created

Disbursement   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 54
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A send() 0 21 1
A getTransaction() 0 9 1
1
<?php
2
3
namespace Bmatovu\AirtelMoney\Products;
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 2
    public function __construct(ClientInterface $http)
17
    {
18 2
        $this->http = $http;
19 2
        $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_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