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\IDINTransactionException; |
14
|
|
|
use Nimbles\CMTelecom\Exception\IssuerConnectionException; |
15
|
|
|
use Nimbles\CMTelecom\Exception\UserInfoException; |
16
|
|
|
use Nimbles\CMTelecom\Model\IDINTransaction; |
17
|
|
|
use Nimbles\CMTelecom\Model\Issuer; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class IDINClient |
21
|
|
|
*/ |
22
|
|
|
class IDINClient |
23
|
|
|
{ |
24
|
|
|
/** @var HttpClient */ |
25
|
|
|
private $httpClient; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
private $apiKey; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
private $url; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
private $applicationName; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param HttpClient $httpClient |
38
|
|
|
* @param string $apiKey |
39
|
|
|
* @param string $url |
40
|
|
|
* @param string $applicationName |
41
|
|
|
*/ |
42
|
|
|
public function __construct(HttpClient $httpClient, string $apiKey, string $url, string $applicationName) |
43
|
|
|
{ |
44
|
|
|
$this->httpClient = $httpClient; |
45
|
|
|
$this->apiKey = $apiKey; |
46
|
|
|
$this->url = $url; |
47
|
|
|
$this->applicationName = $applicationName; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return Issuer[] |
52
|
|
|
* |
53
|
|
|
* @throws IssuerConnectionException |
54
|
|
|
*/ |
55
|
|
|
public function getIssuers() : array |
56
|
|
|
{ |
57
|
|
|
$uri = sprintf('%s/directory', rtrim($this->url, '/')); |
58
|
|
|
|
59
|
|
|
$request = new Request('POST', $uri, [ |
60
|
|
|
'User-Agent' => $this->applicationName, |
61
|
|
|
'Content-Type' => 'application/json' |
62
|
|
|
], json_encode(['merchant_token' => $this->apiKey])); |
63
|
|
|
|
64
|
|
|
$response = $this->httpClient->sendRequest($request); |
65
|
|
|
|
66
|
|
|
$responseData = json_decode($response->getBody()->getContents(), true); |
67
|
|
|
|
68
|
|
|
if ($response->getStatusCode() !== 200) { |
69
|
|
|
throw new IssuerConnectionException($responseData); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (! isset($responseData[0]['issuers'])) { |
73
|
|
|
throw new IssuerConnectionException('Unable to parse issuers'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return array_map(function ($issuerData) { |
77
|
|
|
return new Issuer($issuerData['issuer_id'], $issuerData['issuer_name']); |
78
|
|
|
}, $responseData[0]['issuers']); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param Issuer $issuer |
83
|
|
|
* @param string $redirectUrl |
84
|
|
|
* |
85
|
|
|
* @return IDINTransaction |
86
|
|
|
* |
87
|
|
|
* @throws IDINTransactionException |
88
|
|
|
*/ |
89
|
|
|
public function getIDINTransaction(Issuer $issuer, string $redirectUrl, array $scopes = []) : IDINTransaction |
90
|
|
|
{ |
91
|
|
|
$uri = sprintf('%s/transaction', rtrim($this->url, '/')); |
92
|
|
|
|
93
|
|
|
$token = md5(time() . rand(1, 1000) . $issuer->getId() . $issuer->getName()); |
94
|
|
|
|
95
|
|
|
$options = [ |
96
|
|
|
'identity' => true, |
97
|
|
|
'name' => true, |
98
|
|
|
'gender' => true, |
99
|
|
|
'address' => true, |
100
|
|
|
'date_of_birth' => true, |
101
|
|
|
'18y_or_older' => true, |
102
|
|
|
'email_address' => false, |
103
|
|
|
'telephone_number' => false, |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
if (!empty($scopes)) { |
107
|
|
|
$options = array_merge( |
108
|
|
|
array_fill_keys(array_keys($options), false), |
109
|
|
|
array_fill_keys($scopes, true) |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$requestData = array_merge($options, [ |
114
|
|
|
'merchant_token' => $this->apiKey, |
115
|
|
|
'issuer_id' => $issuer->getId(), |
116
|
|
|
'entrance_code' => $token, |
117
|
|
|
'merchant_return_url' => $redirectUrl, |
118
|
|
|
'language' => 'nl', |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
$request = new Request('POST', $uri, [ |
122
|
|
|
'User-Agent' => $this->applicationName, |
123
|
|
|
'Content-Type' => 'application/json' |
124
|
|
|
], json_encode($requestData)); |
125
|
|
|
|
126
|
|
|
$response = $this->httpClient->sendRequest($request); |
127
|
|
|
|
128
|
|
|
$responseData = json_decode($response->getBody()->getContents(), true); |
129
|
|
|
|
130
|
|
|
if ($response->getStatusCode() !== 200) { |
131
|
|
|
throw new IDINTransactionException($responseData); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return new IDINTransaction( |
135
|
|
|
$responseData['transaction_id'], |
136
|
|
|
$responseData['merchant_reference'], |
137
|
|
|
$token, |
138
|
|
|
$responseData['issuer_authentication_url'] |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param IDINTransaction $IDINTransaction |
144
|
|
|
* |
145
|
|
|
* @return array |
146
|
|
|
* |
147
|
|
|
* @throws UserInfoException |
148
|
|
|
*/ |
149
|
|
|
public function getUserInfo(IDINTransaction $IDINTransaction) : array |
150
|
|
|
{ |
151
|
|
|
$uri = sprintf('%s/status', rtrim($this->url, '/')); |
152
|
|
|
|
153
|
|
|
$requestData = [ |
154
|
|
|
'merchant_token' => $this->apiKey, |
155
|
|
|
'transaction_id' => $IDINTransaction->getTransactionId(), |
156
|
|
|
'merchant_reference' => $IDINTransaction->getMerchantReference(), |
157
|
|
|
]; |
158
|
|
|
|
159
|
|
|
$request = new Request('POST', $uri, [ |
160
|
|
|
'User-Agent' => $this->applicationName, |
161
|
|
|
'Content-Type' => 'application/json' |
162
|
|
|
], json_encode($requestData)); |
163
|
|
|
|
164
|
|
|
$response = $this->httpClient->sendRequest($request); |
165
|
|
|
|
166
|
|
|
$responseData = json_decode($response->getBody()->getContents(), true); |
167
|
|
|
|
168
|
|
|
if ($response->getStatusCode() !== 200) { |
169
|
|
|
throw new UserInfoException($responseData); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $responseData; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|