1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace tbclla\RevolutMerchant; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
6
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
7
|
|
|
use tbclla\RevolutMerchant\Exceptions\MerchantException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @method \tbclla\RevolutMerchant\Resources\Order order() |
11
|
|
|
* @method \tbclla\RevolutMerchant\Resources\Webhook webhook() |
12
|
|
|
*/ |
13
|
|
|
class Client |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The Revolut merchant Sandbox URL |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
const SANDBOX_URL = 'https://sandbox-merchant.revolut.com'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The Revolut merchant production URL |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
const PRODUCTION_URL = 'https://merchant.revolut.com'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The Revolut merchant API endpoint |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
const API_ENDPOINT = '/api'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The Revolut merchant API version |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
const API_VERSION = '1.0'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The API key |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $apiKey; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The HTTP client |
52
|
|
|
* |
53
|
|
|
* @var \GuzzleHttp\Client |
54
|
|
|
*/ |
55
|
|
|
private $httpClient; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Create a new client |
59
|
|
|
* |
60
|
|
|
* @param string $apiKey |
61
|
|
|
*/ |
62
|
|
|
public function __construct(string $apiKey) |
63
|
|
|
{ |
64
|
|
|
$this->apiKey = $apiKey; |
65
|
|
|
$this->httpClient = new GuzzleClient(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $name |
70
|
|
|
* @param mixed $args |
71
|
|
|
* @return mixed |
72
|
|
|
* @throws \tbclla\RevolutMerchant\Exceptions\MerchantException |
73
|
|
|
*/ |
74
|
|
|
public function __call($name, $args) |
75
|
|
|
{ |
76
|
|
|
$class = __NAMESPACE__ . '\\Resources\\' . ucfirst($name); |
77
|
|
|
if (!class_exists($class)) { |
78
|
|
|
throw new MerchantException('"' . $class . '" is not a valid API resource.'); |
79
|
|
|
} |
80
|
|
|
return new $class($this); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Perform a 'POST' request |
85
|
|
|
* |
86
|
|
|
* @param string $endpoint The request endpoint |
87
|
|
|
* @param array $options The request options |
88
|
|
|
* @return array The response body |
89
|
|
|
* @throws \tbclla\RevolutMerchant\Exceptions\MerchantException |
90
|
|
|
*/ |
91
|
|
|
public function post(string $endpoint, array $options = []) |
92
|
|
|
{ |
93
|
|
|
return $this->request('POST', $endpoint, $options); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Perform a 'GET' request |
98
|
|
|
* |
99
|
|
|
* @param string $endpoint The request endpoint |
100
|
|
|
* @param array $options The request options |
101
|
|
|
* @return array The response body |
102
|
|
|
* @throws \tbclla\RevolutMerchant\Exceptions\MerchantException |
103
|
|
|
*/ |
104
|
|
|
public function get(string $endpoint, array $options = []) |
105
|
|
|
{ |
106
|
|
|
return $this->request('GET', $endpoint, $options); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Perform a request |
111
|
|
|
* |
112
|
|
|
* @param string $method The request method |
113
|
|
|
* @param string $endpoint The request endpoint |
114
|
|
|
* @param array $options The request options |
115
|
|
|
* @return array The response body |
116
|
|
|
* @throws \tbclla\RevolutMerchant\Exceptions\MerchantException |
117
|
|
|
*/ |
118
|
|
|
private function request(string $method, string $endpoint, array $options) |
119
|
|
|
{ |
120
|
|
|
try { |
121
|
|
|
$response = $this->httpClient->request($method, self::baseUri() . $endpoint, $this->buildOptions($options)); |
122
|
|
|
} catch (GuzzleException $e) { |
123
|
|
|
throw new MerchantException($e->getMessage(), $e->getCode(), $e); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return json_decode($response->getBody(), true); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Build the base URI for all API requests |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public static function baseUri() |
135
|
|
|
{ |
136
|
|
|
$url = config('revolut-merchant.sandbox', true) |
137
|
|
|
? self::SANDBOX_URL |
138
|
|
|
: self::PRODUCTION_URL; |
139
|
|
|
|
140
|
|
|
return $url . self::ApiUri(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Build the API URI |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
public static function ApiUri() |
149
|
|
|
{ |
150
|
|
|
return self::API_ENDPOINT . '/' . self::API_VERSION; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Build the request options |
155
|
|
|
* |
156
|
|
|
* @param array $options |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
private function buildOptions(array $options = []) |
160
|
|
|
{ |
161
|
|
|
return array_merge($options, [ |
162
|
|
|
'headers' => [ |
163
|
|
|
'Authorization' => 'Bearer ' . $this->apiKey |
164
|
|
|
], |
165
|
|
|
]); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|