|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bunq\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Bunq\Certificate\CertificateType; |
|
6
|
|
|
use Bunq\Certificate\Storage\CertificateStorage; |
|
7
|
|
|
use Bunq\Exception\BunqException; |
|
8
|
|
|
use Bunq\Token\Token; |
|
9
|
|
|
use GuzzleHttp\ClientInterface; |
|
10
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
|
|
13
|
|
|
final class DefaultInstallationServiceService implements InstallationService |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var ClientInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $httpClient; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var CertificateStorage |
|
22
|
|
|
*/ |
|
23
|
|
|
private $certificateStorage; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $apiKey; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $permittedIps; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The installation service requires a http client without the bunq middleware's |
|
37
|
|
|
* |
|
38
|
|
|
* @param ClientInterface $httpClient |
|
39
|
|
|
* @param CertificateStorage $certificateStorage |
|
40
|
|
|
* @param string $apiKey |
|
41
|
|
|
* @param array $permittedIps |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct( |
|
44
|
|
|
ClientInterface $httpClient, |
|
45
|
|
|
CertificateStorage $certificateStorage, |
|
46
|
|
|
$apiKey, |
|
47
|
|
|
array $permittedIps |
|
48
|
|
|
) { |
|
49
|
|
|
$this->httpClient = $httpClient; |
|
50
|
|
|
$this->certificateStorage = $certificateStorage; |
|
51
|
|
|
$this->apiKey = (string)$apiKey; |
|
52
|
|
|
$this->permittedIps = $permittedIps; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Registers your public key with the Bunq API. |
|
57
|
|
|
* |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public function install() |
|
61
|
|
|
{ |
|
62
|
|
|
$publicKey = $this->certificateStorage->load(CertificateType::PUBLIC_KEY()); |
|
63
|
|
|
|
|
64
|
|
|
$response = $this->sendInstallationPostRequest( |
|
65
|
|
|
'/v1/installation', |
|
66
|
|
|
[ |
|
67
|
|
|
'json' => [ |
|
68
|
|
|
'client_public_key' => $publicKey->toString(), |
|
69
|
|
|
], |
|
70
|
|
|
] |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$responseArray = \json_decode((string)$response->getBody(), true); |
|
74
|
|
|
|
|
75
|
|
|
return [ |
|
76
|
|
|
'token' => $responseArray['Response'][1]['Token']['token'], |
|
77
|
|
|
'public_key' => $responseArray['Response'][2]['ServerPublicKey']['server_public_key'], |
|
78
|
|
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Registers a device with the Bunq API. |
|
83
|
|
|
* |
|
84
|
|
|
* @param Token $token |
|
85
|
|
|
* |
|
86
|
|
|
* return void |
|
87
|
|
|
*/ |
|
88
|
|
|
public function registerDevice(Token $token) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->sendInstallationPostRequest( |
|
91
|
|
|
'/v1/device-server', |
|
92
|
|
|
[ |
|
93
|
|
|
'headers' => [ |
|
94
|
|
|
'X-Bunq-Client-Authentication' => $token->toString(), |
|
95
|
|
|
], |
|
96
|
|
|
'json' => [ |
|
97
|
|
|
'description' => 'Bunq PHP API Client', |
|
98
|
|
|
'secret' => $this->apiKey, |
|
99
|
|
|
'permitted_ips' => $this->permittedIps, |
|
100
|
|
|
], |
|
101
|
|
|
] |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Registers a session with the Bunq API. |
|
107
|
|
|
* |
|
108
|
|
|
* @param Token $token |
|
109
|
|
|
* |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
|
|
public function createSession(Token $token) |
|
113
|
|
|
{ |
|
114
|
|
|
$response = $this->sendInstallationPostRequest( |
|
115
|
|
|
'/v1/session-server', |
|
116
|
|
|
[ |
|
117
|
|
|
'headers' => [ |
|
118
|
|
|
'X-Bunq-Client-Authentication' => $token->toString(), |
|
119
|
|
|
], |
|
120
|
|
|
'json' => [ |
|
121
|
|
|
'secret' => $this->apiKey, |
|
122
|
|
|
], |
|
123
|
|
|
] |
|
124
|
|
|
); |
|
125
|
|
|
|
|
126
|
|
|
$responseArray = \json_decode((string)$response->getBody(), true); |
|
127
|
|
|
|
|
128
|
|
|
return $responseArray['Response'][1]['Token']['token']; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Sends a post request using the installation HTTP Client |
|
133
|
|
|
* |
|
134
|
|
|
* @param $url |
|
135
|
|
|
* @param array $options |
|
136
|
|
|
* |
|
137
|
|
|
* @return ResponseInterface |
|
138
|
|
|
* @throws BunqException |
|
139
|
|
|
*/ |
|
140
|
|
|
private function sendInstallationPostRequest($url, array $options = []) |
|
141
|
|
|
{ |
|
142
|
|
|
try { |
|
143
|
|
|
return $this->httpClient->request('POST', $url, $options); |
|
144
|
|
|
} catch (ClientException $exception) { |
|
145
|
|
|
throw new BunqException($exception); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|