1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RevolutPHP; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
6
|
|
|
use GuzzleHttp\Psr7\Response; |
7
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
|
10
|
|
|
class Client |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
const REVOLUT_API_VERSION = '1.0'; |
14
|
|
|
const REVOLUT_SANDBOX_ENDPOINT = 'https://sandbox-b2b.revolut.com/api/'; |
15
|
|
|
const REVOLUT_PRODUCTION_ENDPOINT = 'https://b2b.revolut.com/api/'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var \GuzzleHttp\Client |
19
|
|
|
*/ |
20
|
|
|
private $httpClient; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \League\OAuth2\Client\Token\AccessToken |
24
|
|
|
*/ |
25
|
|
|
private $accessToken; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $mode; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $clientOptions; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $baseUrl; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Accounts |
44
|
|
|
*/ |
45
|
|
|
public $accounts; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Counterparties |
49
|
|
|
*/ |
50
|
|
|
public $counterparties; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var Transfers |
54
|
|
|
*/ |
55
|
|
|
public $transfers; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var Payments |
59
|
|
|
*/ |
60
|
|
|
public $payments; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var PaymentDrafts |
64
|
|
|
*/ |
65
|
|
|
public $paymentDrafts; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var Transactions |
69
|
|
|
*/ |
70
|
|
|
public $transactions; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var Rates |
74
|
|
|
*/ |
75
|
|
|
public $rates; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var Exchanges |
79
|
|
|
*/ |
80
|
|
|
public $exchanges; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var Webhooks |
84
|
|
|
*/ |
85
|
|
|
public $webhooks; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Client constructor. |
89
|
|
|
* @param AccessToken $accessToken |
90
|
|
|
* @param string $mode |
91
|
|
|
* @param array $clientOptions |
92
|
|
|
*/ |
93
|
|
|
public function __construct(AccessToken $accessToken, $mode = 'production', array $clientOptions = []) |
94
|
|
|
{ |
95
|
|
|
$this->accessToken = $accessToken; |
96
|
|
|
$this->mode = $mode; |
97
|
|
|
$this->baseUrl = ($mode === 'production' ? self::REVOLUT_PRODUCTION_ENDPOINT : self::REVOLUT_SANDBOX_ENDPOINT); |
98
|
|
|
$this->clientOptions = $clientOptions; |
99
|
|
|
|
100
|
|
|
$this->initiateHttpClient(); |
101
|
|
|
|
102
|
|
|
$this->accounts = new Accounts($this); |
103
|
|
|
$this->counterparties = new Counterparties($this); |
104
|
|
|
$this->payments = new Payments($this); |
105
|
|
|
$this->paymentDrafts = new PaymentDrafts($this); |
106
|
|
|
$this->transfers = new Transfers($this); |
107
|
|
|
$this->transactions = new Transactions($this); |
108
|
|
|
$this->rates = new Rates($this); |
109
|
|
|
$this->exchanges = new Exchanges($this); |
110
|
|
|
$this->webhooks = new Webhooks($this); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param GuzzleClient $client |
115
|
|
|
*/ |
116
|
|
|
public function setClient(GuzzleClient $client) |
117
|
|
|
{ |
118
|
|
|
$this->httpClient = $client; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Initiates the HttpClient with required headers |
123
|
|
|
*/ |
124
|
|
|
private function initiateHttpClient() |
125
|
|
|
{ |
126
|
|
|
$options = [ |
127
|
|
|
'headers' => [ |
128
|
|
|
'Authorization' => 'Bearer ' . $this->accessToken->getToken(), |
129
|
|
|
] |
130
|
|
|
]; |
131
|
|
|
|
132
|
|
|
$this->httpClient = new GuzzleClient(array_replace_recursive($this->clientOptions, $options)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
private function buildBaseUrl() |
136
|
|
|
{ |
137
|
|
|
return $this->baseUrl.self::REVOLUT_API_VERSION.'/'; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param Response $response |
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
|
|
private function handleResponse(Response $response) |
145
|
|
|
{ |
146
|
|
|
$stream = $response->getBody(); |
147
|
|
|
$data = json_decode($stream); |
148
|
|
|
|
149
|
|
|
return $data; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $endpoint |
154
|
|
|
* @param array $json |
155
|
|
|
* @return mixed |
156
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
157
|
|
|
*/ |
158
|
|
|
public function post($endpoint, $json) |
159
|
|
|
{ |
160
|
|
|
$response = $this->httpClient->request('POST', $this->buildBaseUrl().$endpoint, ['json' => $json]); |
161
|
|
|
return $this->handleResponse($response); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $endpoint |
166
|
|
|
* @param array $json |
167
|
|
|
* @return mixed |
168
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
169
|
|
|
*/ |
170
|
|
|
public function patch($endpoint, $json) |
171
|
|
|
{ |
172
|
|
|
$response = $this->httpClient->request('PATCH', $this->buildBaseUrl().$endpoint, ['json' => $json]); |
173
|
|
|
return $this->handleResponse($response); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string $endpoint |
178
|
|
|
* @return mixed |
179
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
180
|
|
|
*/ |
181
|
|
|
public function get($endpoint) |
182
|
|
|
{ |
183
|
|
|
$response = $this->httpClient->request('GET', $this->buildBaseUrl().$endpoint); |
184
|
|
|
return $this->handleResponse($response); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param string $endpoint |
189
|
|
|
* @return mixed |
190
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
191
|
|
|
*/ |
192
|
|
|
public function delete($endpoint) |
193
|
|
|
{ |
194
|
|
|
$response = $this->httpClient->request('DELETE', $this->buildBaseUrl().$endpoint); |
195
|
|
|
return $this->handleResponse($response); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|