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