IBANClient   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 59
dl 0
loc 134
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getIBANTransaction() 0 33 2
A __construct() 0 6 1
A getIssuers() 0 24 3
A getTransactionInfo() 0 24 2
1
<?php
2
/*
3
* (c) Nimbles b.v. <[email protected]>
4
*
5
* For the full copyright and license information, please view the LICENSE
6
* file that was distributed with this source code.
7
*/
8
9
namespace Nimbles\CMTelecom\Client;
10
11
use GuzzleHttp\Psr7\Request;
12
use Http\Client\HttpClient;
13
use Nimbles\CMTelecom\Exception\IBANTransactionException;
14
use Nimbles\CMTelecom\Exception\IssuerConnectionException;
15
use Nimbles\CMTelecom\Model\IBANTransaction;
16
use Nimbles\CMTelecom\Model\Issuer;
17
18
/**
19
 * Class IBANClient
20
 */
21
class IBANClient
22
{
23
    /** @var HttpClient */
24
    private $httpClient;
25
26
    /** @var string */
27
    private $apiKey;
28
29
    /** @var string */
30
    private $url;
31
32
    /** @var string */
33
    private $applicationName;
34
35
    /**
36
     * @param HttpClient $httpClient
37
     * @param string          $apiKey
38
     * @param string          $url
39
     * @param string          $applicationName
40
     */
41
    public function __construct(HttpClient $httpClient, string $apiKey, string $url, string $applicationName)
42
    {
43
        $this->httpClient             = $httpClient;
44
        $this->apiKey                 = $apiKey;
45
        $this->url                    = $url;
46
        $this->applicationName        = $applicationName;
47
    }
48
49
    /**
50
     * @return Issuer[]
51
     *
52
     * @throws IssuerConnectionException
53
     */
54
    public function getIssuers() : array
55
    {
56
        $uri = sprintf('%s/directory', rtrim($this->url, '/'));
57
58
        $request = new Request('POST', $uri, [
59
            'User-Agent' => $this->applicationName,
60
            'Content-Type' => 'application/json'
61
        ], json_encode(['merchant_token' => $this->apiKey]));
62
63
        $response = $this->httpClient->sendRequest($request);
64
65
        $responseData = json_decode($response->getBody()->getContents(), true);
66
67
        if ($response->getStatusCode() !== 200) {
68
            throw new IssuerConnectionException($responseData);
69
        }
70
71
        if ( ! isset($responseData[0]['issuers'])) {
72
            throw new IssuerConnectionException('Unable to parse issuers');
73
        }
74
75
        return array_map(function($issuerData) {
76
            return new Issuer($issuerData['issuer_id'], $issuerData['issuer_name']);
77
        }, $responseData[0]['issuers']);
78
    }
79
80
    /**
81
     * @param Issuer $issuer
82
     * @param string $redirectUrl
83
     *
84
     * @return IBANTransaction
85
     *
86
     * @throws IBANTransactionException
87
     */
88
    public function getIBANTransaction(Issuer $issuer, string $redirectUrl) : IBANTransaction
89
    {
90
        $uri = sprintf('%s/transaction', rtrim($this->url, '/'));
91
92
        $token = md5(time() . rand(1, 1000) . $issuer->getId() . $issuer->getName());
93
94
        $requestData = [
95
            'merchant_token'      => $this->apiKey,
96
            'identity'            => true,
97
            'name'                => true,
98
            'issuer_id'           => $issuer->getId(),
99
            'entrance_code'       => $token,
100
            'merchant_return_url' => $redirectUrl,
101
        ];
102
103
        $request = new Request('POST', $uri, [
104
            'User-Agent' => $this->applicationName,
105
            'Content-Type' => 'application/json'
106
        ], json_encode($requestData));
107
108
        $response = $this->httpClient->sendRequest($request);
109
110
        $responseData = json_decode($response->getBody()->getContents(), true);
111
112
        if ($response->getStatusCode() !== 200) {
113
            throw new IBANTransactionException($responseData);
114
        }
115
116
        return new IBANTransaction(
117
            $responseData['transaction_id'],
118
            $responseData['merchant_reference'],
119
            $token,
120
            $responseData['issuer_authentication_url']
121
        );
122
    }
123
124
    /**
125
     * @param IBANTransaction $IBANTransaction
126
     *
127
     * @return array
128
     *
129
     * @throws IBANTransactionException
130
     */
131
    public function getTransactionInfo(IBANTransaction $IBANTransaction) : array
132
    {
133
        $uri = sprintf('%s/status', rtrim($this->url, '/'));
134
135
        $requestData = [
136
            'merchant_token'     => $this->apiKey,
137
            'transaction_id'     => $IBANTransaction->getTransactionId(),
138
            'merchant_reference' => $IBANTransaction->getMerchantReference(),
139
        ];
140
141
        $request = new Request('POST', $uri, [
142
            'User-Agent' => $this->applicationName,
143
            'Content-Type' => 'application/json'
144
        ], json_encode($requestData));
145
146
        $response = $this->httpClient->sendRequest($request);
147
148
        $responseData = json_decode($response->getBody()->getContents(), true);
149
150
        if ($response->getStatusCode() !== 200) {
151
            throw new IBANTransactionException($responseData);
152
        }
153
154
        return $responseData;
155
    }
156
}
157