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

Collection::getTransaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 Collection
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 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_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