@@ -4,28 +4,28 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Rate extends Resource |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * The enpoint for rate requests |
|
| 9 | - * |
|
| 10 | - * @var string |
|
| 11 | - */ |
|
| 12 | - const ENDPOINT = '/rate'; |
|
| 7 | + /** |
|
| 8 | + * The enpoint for rate requests |
|
| 9 | + * |
|
| 10 | + * @var string |
|
| 11 | + */ |
|
| 12 | + const ENDPOINT = '/rate'; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Get an exchange rate |
|
| 16 | - * |
|
| 17 | - * @see https://revolut-engineering.github.io/api-docs/business-api/#exchanges-get-exchange-rates Official API documentation |
|
| 18 | - * @param string $from 3-letter ISO base currency |
|
| 19 | - * @param string $to 3-letter ISO target currency |
|
| 20 | - * @param float $amount decimal amount, default is 1.00 |
|
| 21 | - * @return array The response body |
|
| 22 | - * @throws \tbclla\Revolut\Exceptions\ApiException if the client responded with a 4xx-5xx response |
|
| 23 | - * @throws \tbclla\Revolut\Exceptions\AppUnauthorizedException if the app needs to be re-authorized |
|
| 24 | - */ |
|
| 25 | - public function get(string $from, string $to, float $amount = 1) |
|
| 26 | - { |
|
| 27 | - return $this->client->get(self::ENDPOINT, [ |
|
| 28 | - 'query' => compact('from', 'to', 'amount') |
|
| 29 | - ]); |
|
| 30 | - } |
|
| 14 | + /** |
|
| 15 | + * Get an exchange rate |
|
| 16 | + * |
|
| 17 | + * @see https://revolut-engineering.github.io/api-docs/business-api/#exchanges-get-exchange-rates Official API documentation |
|
| 18 | + * @param string $from 3-letter ISO base currency |
|
| 19 | + * @param string $to 3-letter ISO target currency |
|
| 20 | + * @param float $amount decimal amount, default is 1.00 |
|
| 21 | + * @return array The response body |
|
| 22 | + * @throws \tbclla\Revolut\Exceptions\ApiException if the client responded with a 4xx-5xx response |
|
| 23 | + * @throws \tbclla\Revolut\Exceptions\AppUnauthorizedException if the app needs to be re-authorized |
|
| 24 | + */ |
|
| 25 | + public function get(string $from, string $to, float $amount = 1) |
|
| 26 | + { |
|
| 27 | + return $this->client->get(self::ENDPOINT, [ |
|
| 28 | + 'query' => compact('from', 'to', 'amount') |
|
| 29 | + ]); |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -6,13 +6,13 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Revolut extends Facade |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Get the registered name of the component. |
|
| 11 | - * |
|
| 12 | - * @return string |
|
| 13 | - */ |
|
| 14 | - protected static function getFacadeAccessor() |
|
| 15 | - { |
|
| 16 | - return 'revolut'; |
|
| 17 | - } |
|
| 9 | + /** |
|
| 10 | + * Get the registered name of the component. |
|
| 11 | + * |
|
| 12 | + * @return string |
|
| 13 | + */ |
|
| 14 | + protected static function getFacadeAccessor() |
|
| 15 | + { |
|
| 16 | + return 'revolut'; |
|
| 17 | + } |
|
| 18 | 18 | } |
@@ -20,191 +20,191 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | class Client |
| 22 | 22 | { |
| 23 | - /** |
|
| 24 | - * The production URL |
|
| 25 | - * |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - const PRODUCTION_URL = 'https://b2b.revolut.com'; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * The sandbox URL |
|
| 32 | - * |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - const SANDBOX_URL = 'https://sandbox-b2b.revolut.com'; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The API URI |
|
| 39 | - * |
|
| 40 | - * @var string |
|
| 41 | - */ |
|
| 42 | - const API_ENDPOINT = '/api'; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * The API version |
|
| 46 | - * |
|
| 47 | - * @var string |
|
| 48 | - */ |
|
| 49 | - const API_VERSION = '1.0'; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * The token manager |
|
| 53 | - * |
|
| 54 | - * @var \tbclla\Revolut\Auth\TokenManager |
|
| 55 | - */ |
|
| 56 | - private $tokenManager; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * the HTTP client |
|
| 60 | - * |
|
| 61 | - * @var \tbclla\Revolut\Interfaces\MakesHttpRequests |
|
| 62 | - */ |
|
| 63 | - private $httpClient; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * The access token |
|
| 67 | - * |
|
| 68 | - * @var \tbclla\Revolut\Auth\AccessToken |
|
| 69 | - */ |
|
| 70 | - private $accessToken; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Create the client instance |
|
| 74 | - * |
|
| 75 | - * @param \tbclla\Revolut\Auth\TokenManager $tokenManager |
|
| 76 | - * @param \tbclla\Revolut\Interfaces\MakesHttpRequests $httpClient |
|
| 77 | - * @return void |
|
| 78 | - */ |
|
| 79 | - public function __construct(TokenManager $tokenManager, MakesHttpRequests $httpClient) |
|
| 80 | - { |
|
| 81 | - $this->tokenManager = $tokenManager; |
|
| 82 | - $this->httpClient = $httpClient; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @param string $name |
|
| 87 | - * @param mixed $arguments |
|
| 88 | - * @return \tbclla\Revolut\Resources\Resource |
|
| 89 | - * @throws \tbclla\Revolut\Exceptions\RevolutException |
|
| 90 | - */ |
|
| 91 | - public function __call($name, $arguments) |
|
| 92 | - { |
|
| 93 | - $resource = __NAMESPACE__ . '\\Resources\\' . ucfirst($name); |
|
| 94 | - if (!class_exists($resource)) { |
|
| 95 | - throw new RevolutException($resource . ' is not a valid API resource'); |
|
| 96 | - } |
|
| 97 | - return new $resource($this); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Set the access token |
|
| 102 | - * |
|
| 103 | - * @param \tbclla\Revolut\Auth\AccessToken $accessToken |
|
| 104 | - * @return void |
|
| 105 | - */ |
|
| 106 | - public function setAccessToken(AccessToken $accessToken): void |
|
| 107 | - { |
|
| 108 | - $this->accessToken = $accessToken; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Create and set a fresh access token |
|
| 113 | - * |
|
| 114 | - * @return void |
|
| 115 | - */ |
|
| 116 | - public function refreshAccessToken(): void |
|
| 117 | - { |
|
| 118 | - $this->setAccessToken($this->tokenManager->refreshAccessToken()); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Get the base URI for all API requests |
|
| 123 | - * |
|
| 124 | - * @param string $endpoint |
|
| 125 | - * @return string |
|
| 126 | - */ |
|
| 127 | - public static function buildUri(string $endpoint = '') |
|
| 128 | - { |
|
| 129 | - $url = config('revolut.sandbox', true) ? self::SANDBOX_URL : self::PRODUCTION_URL; |
|
| 130 | - |
|
| 131 | - return $url . self::apiUri() . $endpoint; |
|
| 132 | - } |
|
| 23 | + /** |
|
| 24 | + * The production URL |
|
| 25 | + * |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + const PRODUCTION_URL = 'https://b2b.revolut.com'; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * The sandbox URL |
|
| 32 | + * |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + const SANDBOX_URL = 'https://sandbox-b2b.revolut.com'; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The API URI |
|
| 39 | + * |
|
| 40 | + * @var string |
|
| 41 | + */ |
|
| 42 | + const API_ENDPOINT = '/api'; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * The API version |
|
| 46 | + * |
|
| 47 | + * @var string |
|
| 48 | + */ |
|
| 49 | + const API_VERSION = '1.0'; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * The token manager |
|
| 53 | + * |
|
| 54 | + * @var \tbclla\Revolut\Auth\TokenManager |
|
| 55 | + */ |
|
| 56 | + private $tokenManager; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * the HTTP client |
|
| 60 | + * |
|
| 61 | + * @var \tbclla\Revolut\Interfaces\MakesHttpRequests |
|
| 62 | + */ |
|
| 63 | + private $httpClient; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * The access token |
|
| 67 | + * |
|
| 68 | + * @var \tbclla\Revolut\Auth\AccessToken |
|
| 69 | + */ |
|
| 70 | + private $accessToken; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Create the client instance |
|
| 74 | + * |
|
| 75 | + * @param \tbclla\Revolut\Auth\TokenManager $tokenManager |
|
| 76 | + * @param \tbclla\Revolut\Interfaces\MakesHttpRequests $httpClient |
|
| 77 | + * @return void |
|
| 78 | + */ |
|
| 79 | + public function __construct(TokenManager $tokenManager, MakesHttpRequests $httpClient) |
|
| 80 | + { |
|
| 81 | + $this->tokenManager = $tokenManager; |
|
| 82 | + $this->httpClient = $httpClient; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @param string $name |
|
| 87 | + * @param mixed $arguments |
|
| 88 | + * @return \tbclla\Revolut\Resources\Resource |
|
| 89 | + * @throws \tbclla\Revolut\Exceptions\RevolutException |
|
| 90 | + */ |
|
| 91 | + public function __call($name, $arguments) |
|
| 92 | + { |
|
| 93 | + $resource = __NAMESPACE__ . '\\Resources\\' . ucfirst($name); |
|
| 94 | + if (!class_exists($resource)) { |
|
| 95 | + throw new RevolutException($resource . ' is not a valid API resource'); |
|
| 96 | + } |
|
| 97 | + return new $resource($this); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Set the access token |
|
| 102 | + * |
|
| 103 | + * @param \tbclla\Revolut\Auth\AccessToken $accessToken |
|
| 104 | + * @return void |
|
| 105 | + */ |
|
| 106 | + public function setAccessToken(AccessToken $accessToken): void |
|
| 107 | + { |
|
| 108 | + $this->accessToken = $accessToken; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Create and set a fresh access token |
|
| 113 | + * |
|
| 114 | + * @return void |
|
| 115 | + */ |
|
| 116 | + public function refreshAccessToken(): void |
|
| 117 | + { |
|
| 118 | + $this->setAccessToken($this->tokenManager->refreshAccessToken()); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Get the base URI for all API requests |
|
| 123 | + * |
|
| 124 | + * @param string $endpoint |
|
| 125 | + * @return string |
|
| 126 | + */ |
|
| 127 | + public static function buildUri(string $endpoint = '') |
|
| 128 | + { |
|
| 129 | + $url = config('revolut.sandbox', true) ? self::SANDBOX_URL : self::PRODUCTION_URL; |
|
| 130 | + |
|
| 131 | + return $url . self::apiUri() . $endpoint; |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - /** |
|
| 135 | - * Get the URI for API requests |
|
| 136 | - * |
|
| 137 | - * @return string |
|
| 138 | - */ |
|
| 139 | - public static function apiUri() |
|
| 140 | - { |
|
| 141 | - return self::API_ENDPOINT . '/' . self::API_VERSION; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Perform a POST request against a specified endpoint |
|
| 146 | - * |
|
| 147 | - * @param string $endpoint |
|
| 148 | - * @param array $options |
|
| 149 | - * @return array The response body |
|
| 150 | - * @throws \tbclla\Revolut\Exceptions\ApiException if the client responded with a 4xx-5xx response |
|
| 151 | - * @throws \tbclla\Revolut\Exceptions\AppUnauthorizedException if the app needs to be re-authorized |
|
| 152 | - */ |
|
| 153 | - public function post(string $endpoint, array $options = []) |
|
| 154 | - { |
|
| 155 | - return $this->httpClient->post($this->buildUri($endpoint), $this->buildOptions($options)); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Perform a GET request against a specified endpoint |
|
| 160 | - * |
|
| 161 | - * @param string $endpoint |
|
| 162 | - * @return array The response body |
|
| 163 | - * @throws \tbclla\Revolut\Exceptions\ApiException if the client responded with a 4xx-5xx response |
|
| 164 | - * @throws \tbclla\Revolut\Exceptions\AppUnauthorizedException if the app needs to be re-authorized |
|
| 165 | - */ |
|
| 166 | - public function get(string $endpoint, array $options = []) |
|
| 167 | - { |
|
| 168 | - return $this->httpClient->get($this->buildUri($endpoint), $this->buildOptions($options)); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Perform a DELETE request against a specified endpoint |
|
| 173 | - * |
|
| 174 | - * @param string $endpoint |
|
| 175 | - * @return void |
|
| 176 | - * @throws \tbclla\Revolut\Exceptions\ApiException if the client responded with a 4xx-5xx response |
|
| 177 | - * @throws \tbclla\Revolut\Exceptions\AppUnauthorizedException if the app needs to be re-authorized |
|
| 178 | - */ |
|
| 179 | - public function delete(string $endpoint) |
|
| 180 | - { |
|
| 181 | - $this->httpClient->delete($this->buildUri($endpoint), $this->buildOptions()); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Build the request options |
|
| 186 | - * |
|
| 187 | - * @param array $options |
|
| 188 | - * @return array |
|
| 189 | - */ |
|
| 190 | - private function buildOptions(array $options = []) |
|
| 191 | - { |
|
| 192 | - if (!$this->accessToken) { |
|
| 193 | - $this->setAccessToken($this->tokenManager->getAccessToken()); |
|
| 194 | - } else if ($this->accessToken->hasExpired()) { |
|
| 195 | - $this->refreshAccessToken(); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - return array_merge($options, ['headers' => ['Authorization' => 'Bearer ' . $this->accessToken->value]]); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Generate a v4 UUID to use as a request ID |
|
| 203 | - * |
|
| 204 | - * @return string |
|
| 205 | - */ |
|
| 206 | - public static function generateRequestId() |
|
| 207 | - { |
|
| 208 | - return (string) Str::Uuid(); |
|
| 209 | - } |
|
| 134 | + /** |
|
| 135 | + * Get the URI for API requests |
|
| 136 | + * |
|
| 137 | + * @return string |
|
| 138 | + */ |
|
| 139 | + public static function apiUri() |
|
| 140 | + { |
|
| 141 | + return self::API_ENDPOINT . '/' . self::API_VERSION; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Perform a POST request against a specified endpoint |
|
| 146 | + * |
|
| 147 | + * @param string $endpoint |
|
| 148 | + * @param array $options |
|
| 149 | + * @return array The response body |
|
| 150 | + * @throws \tbclla\Revolut\Exceptions\ApiException if the client responded with a 4xx-5xx response |
|
| 151 | + * @throws \tbclla\Revolut\Exceptions\AppUnauthorizedException if the app needs to be re-authorized |
|
| 152 | + */ |
|
| 153 | + public function post(string $endpoint, array $options = []) |
|
| 154 | + { |
|
| 155 | + return $this->httpClient->post($this->buildUri($endpoint), $this->buildOptions($options)); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Perform a GET request against a specified endpoint |
|
| 160 | + * |
|
| 161 | + * @param string $endpoint |
|
| 162 | + * @return array The response body |
|
| 163 | + * @throws \tbclla\Revolut\Exceptions\ApiException if the client responded with a 4xx-5xx response |
|
| 164 | + * @throws \tbclla\Revolut\Exceptions\AppUnauthorizedException if the app needs to be re-authorized |
|
| 165 | + */ |
|
| 166 | + public function get(string $endpoint, array $options = []) |
|
| 167 | + { |
|
| 168 | + return $this->httpClient->get($this->buildUri($endpoint), $this->buildOptions($options)); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Perform a DELETE request against a specified endpoint |
|
| 173 | + * |
|
| 174 | + * @param string $endpoint |
|
| 175 | + * @return void |
|
| 176 | + * @throws \tbclla\Revolut\Exceptions\ApiException if the client responded with a 4xx-5xx response |
|
| 177 | + * @throws \tbclla\Revolut\Exceptions\AppUnauthorizedException if the app needs to be re-authorized |
|
| 178 | + */ |
|
| 179 | + public function delete(string $endpoint) |
|
| 180 | + { |
|
| 181 | + $this->httpClient->delete($this->buildUri($endpoint), $this->buildOptions()); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Build the request options |
|
| 186 | + * |
|
| 187 | + * @param array $options |
|
| 188 | + * @return array |
|
| 189 | + */ |
|
| 190 | + private function buildOptions(array $options = []) |
|
| 191 | + { |
|
| 192 | + if (!$this->accessToken) { |
|
| 193 | + $this->setAccessToken($this->tokenManager->getAccessToken()); |
|
| 194 | + } else if ($this->accessToken->hasExpired()) { |
|
| 195 | + $this->refreshAccessToken(); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + return array_merge($options, ['headers' => ['Authorization' => 'Bearer ' . $this->accessToken->value]]); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Generate a v4 UUID to use as a request ID |
|
| 203 | + * |
|
| 204 | + * @return string |
|
| 205 | + */ |
|
| 206 | + public static function generateRequestId() |
|
| 207 | + { |
|
| 208 | + return (string) Str::Uuid(); |
|
| 209 | + } |
|
| 210 | 210 | } |
@@ -9,51 +9,51 @@ |
||
| 9 | 9 | |
| 10 | 10 | class GuzzleHttpClient implements MakesHttpRequests |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var \GuzzleHttp\Client |
|
| 14 | - */ |
|
| 15 | - private $client; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * @return void |
|
| 19 | - */ |
|
| 20 | - public function __construct() |
|
| 21 | - { |
|
| 22 | - $this->client = new Client(); |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - public function post(string $url, array $options = []) |
|
| 26 | - { |
|
| 27 | - return $this->send('POST', $url, $options); |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - public function get(string $url, array $options = []) |
|
| 31 | - { |
|
| 32 | - return $this->send('GET', $url, $options); |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - public function delete(string $url, array $options = []) : void |
|
| 36 | - { |
|
| 37 | - $this->send('DELETE', $url, $options); |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Perform a request |
|
| 42 | - * |
|
| 43 | - * @param string $method The request method |
|
| 44 | - * @param string $url The request url |
|
| 45 | - * @param array $options The request options |
|
| 46 | - * @return array|null The response body |
|
| 47 | - * @throws \tbclla\Revolut\Exceptions\ApiException if the client responded with a 4xx-5xx response |
|
| 48 | - */ |
|
| 49 | - private function send(string $method, string $url, array $options) |
|
| 50 | - { |
|
| 51 | - try { |
|
| 52 | - $response = $this->client->request($method, $url, $options); |
|
| 53 | - } catch (BadResponseException $e) { |
|
| 54 | - throw new ApiException($e->getMessage(), $e->getCode(), $e); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - return json_decode($response->getBody(), true); |
|
| 58 | - } |
|
| 12 | + /** |
|
| 13 | + * @var \GuzzleHttp\Client |
|
| 14 | + */ |
|
| 15 | + private $client; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * @return void |
|
| 19 | + */ |
|
| 20 | + public function __construct() |
|
| 21 | + { |
|
| 22 | + $this->client = new Client(); |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + public function post(string $url, array $options = []) |
|
| 26 | + { |
|
| 27 | + return $this->send('POST', $url, $options); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + public function get(string $url, array $options = []) |
|
| 31 | + { |
|
| 32 | + return $this->send('GET', $url, $options); |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + public function delete(string $url, array $options = []) : void |
|
| 36 | + { |
|
| 37 | + $this->send('DELETE', $url, $options); |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Perform a request |
|
| 42 | + * |
|
| 43 | + * @param string $method The request method |
|
| 44 | + * @param string $url The request url |
|
| 45 | + * @param array $options The request options |
|
| 46 | + * @return array|null The response body |
|
| 47 | + * @throws \tbclla\Revolut\Exceptions\ApiException if the client responded with a 4xx-5xx response |
|
| 48 | + */ |
|
| 49 | + private function send(string $method, string $url, array $options) |
|
| 50 | + { |
|
| 51 | + try { |
|
| 52 | + $response = $this->client->request($method, $url, $options); |
|
| 53 | + } catch (BadResponseException $e) { |
|
| 54 | + throw new ApiException($e->getMessage(), $e->getCode(), $e); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + return json_decode($response->getBody(), true); |
|
| 58 | + } |
|
| 59 | 59 | } |
@@ -8,38 +8,38 @@ |
||
| 8 | 8 | |
| 9 | 9 | class AuthorizeCommand extends Command |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * The name and signature of the console command. |
|
| 13 | - * |
|
| 14 | - * @var string |
|
| 15 | - */ |
|
| 16 | - protected $signature = 'revolut:authorize {--redirect= : A destination to be redirected to after the authorization}'; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * The console command description. |
|
| 20 | - * |
|
| 21 | - * @var string |
|
| 22 | - */ |
|
| 23 | - protected $description = 'Authorize API access for you app'; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Execute the console command. |
|
| 27 | - * |
|
| 28 | - * @return mixed |
|
| 29 | - */ |
|
| 30 | - public function handle() |
|
| 31 | - { |
|
| 32 | - $redirect = $this->option('redirect') ?? null; |
|
| 33 | - |
|
| 34 | - if (!$route = config('revolut.auth_route.name')) { |
|
| 35 | - $this->error('Route name invalid or missing'); |
|
| 36 | - $this->error('Check you configuration and verify that "auth_route.name" is valid'); |
|
| 37 | - return; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - $url = route($route, ['after_success' => $redirect]); |
|
| 41 | - |
|
| 42 | - $this->info('Follow the link to complete the authorization:'); |
|
| 43 | - $this->line('<fg=black;bg=yellow>' . $url . '</>'); |
|
| 44 | - } |
|
| 11 | + /** |
|
| 12 | + * The name and signature of the console command. |
|
| 13 | + * |
|
| 14 | + * @var string |
|
| 15 | + */ |
|
| 16 | + protected $signature = 'revolut:authorize {--redirect= : A destination to be redirected to after the authorization}'; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * The console command description. |
|
| 20 | + * |
|
| 21 | + * @var string |
|
| 22 | + */ |
|
| 23 | + protected $description = 'Authorize API access for you app'; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Execute the console command. |
|
| 27 | + * |
|
| 28 | + * @return mixed |
|
| 29 | + */ |
|
| 30 | + public function handle() |
|
| 31 | + { |
|
| 32 | + $redirect = $this->option('redirect') ?? null; |
|
| 33 | + |
|
| 34 | + if (!$route = config('revolut.auth_route.name')) { |
|
| 35 | + $this->error('Route name invalid or missing'); |
|
| 36 | + $this->error('Check you configuration and verify that "auth_route.name" is valid'); |
|
| 37 | + return; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + $url = route($route, ['after_success' => $redirect]); |
|
| 41 | + |
|
| 42 | + $this->info('Follow the link to complete the authorization:'); |
|
| 43 | + $this->line('<fg=black;bg=yellow>' . $url . '</>'); |
|
| 44 | + } |
|
| 45 | 45 | } |
@@ -9,31 +9,31 @@ |
||
| 9 | 9 | |
| 10 | 10 | class CleanupCommand extends Command |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * The name and signature of the console command. |
|
| 14 | - * |
|
| 15 | - * @var string |
|
| 16 | - */ |
|
| 17 | - protected $signature = 'revolut:cleanup'; |
|
| 12 | + /** |
|
| 13 | + * The name and signature of the console command. |
|
| 14 | + * |
|
| 15 | + * @var string |
|
| 16 | + */ |
|
| 17 | + protected $signature = 'revolut:cleanup'; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The console command description. |
|
| 21 | - * |
|
| 22 | - * @var string |
|
| 23 | - */ |
|
| 24 | - protected $description = 'Delete all expired Revolut access tokens and refresh tokens.'; |
|
| 19 | + /** |
|
| 20 | + * The console command description. |
|
| 21 | + * |
|
| 22 | + * @var string |
|
| 23 | + */ |
|
| 24 | + protected $description = 'Delete all expired Revolut access tokens and refresh tokens.'; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Execute the console command. |
|
| 28 | - * |
|
| 29 | - * @return mixed |
|
| 30 | - */ |
|
| 31 | - public function handle() |
|
| 32 | - { |
|
| 33 | - $accessTokens = AccessToken::clearExpired(); |
|
| 34 | - $this->info('Deleted ' . $accessTokens . ' expired access ' . Str::plural('token', $accessTokens)); |
|
| 26 | + /** |
|
| 27 | + * Execute the console command. |
|
| 28 | + * |
|
| 29 | + * @return mixed |
|
| 30 | + */ |
|
| 31 | + public function handle() |
|
| 32 | + { |
|
| 33 | + $accessTokens = AccessToken::clearExpired(); |
|
| 34 | + $this->info('Deleted ' . $accessTokens . ' expired access ' . Str::plural('token', $accessTokens)); |
|
| 35 | 35 | |
| 36 | - $refreshTokens = RefreshToken::clearExpired(); |
|
| 37 | - $this->info('Deleted ' . $refreshTokens . ' expired refresh ' . Str::plural('token', $refreshTokens)); |
|
| 38 | - } |
|
| 36 | + $refreshTokens = RefreshToken::clearExpired(); |
|
| 37 | + $this->info('Deleted ' . $refreshTokens . ' expired refresh ' . Str::plural('token', $refreshTokens)); |
|
| 38 | + } |
|
| 39 | 39 | } |
@@ -10,81 +10,81 @@ |
||
| 10 | 10 | |
| 11 | 11 | class CacheTokenRepository implements TokenRepository |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * The cache key prefix |
|
| 15 | - * |
|
| 16 | - * @var string |
|
| 17 | - */ |
|
| 18 | - const PREFIX = 'revolut_'; |
|
| 13 | + /** |
|
| 14 | + * The cache key prefix |
|
| 15 | + * |
|
| 16 | + * @var string |
|
| 17 | + */ |
|
| 18 | + const PREFIX = 'revolut_'; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * A cache repository |
|
| 22 | - * |
|
| 23 | - * @var \Illuminate\Cache\Repository |
|
| 24 | - */ |
|
| 25 | - private $cache; |
|
| 20 | + /** |
|
| 21 | + * A cache repository |
|
| 22 | + * |
|
| 23 | + * @var \Illuminate\Cache\Repository |
|
| 24 | + */ |
|
| 25 | + private $cache; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @param \Illuminate\Contracts\Cache\Factory $cache |
|
| 29 | - * @param string $driver |
|
| 30 | - * @return void |
|
| 31 | - */ |
|
| 32 | - public function __construct(CacheFactory $cache, string $driver = null) |
|
| 33 | - { |
|
| 34 | - $this->cache = $cache->store($driver); |
|
| 35 | - } |
|
| 27 | + /** |
|
| 28 | + * @param \Illuminate\Contracts\Cache\Factory $cache |
|
| 29 | + * @param string $driver |
|
| 30 | + * @return void |
|
| 31 | + */ |
|
| 32 | + public function __construct(CacheFactory $cache, string $driver = null) |
|
| 33 | + { |
|
| 34 | + $this->cache = $cache->store($driver); |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - public function getAccessToken() |
|
| 38 | - { |
|
| 39 | - return $this->cache->get($this->getKey(AccessToken::TYPE)); |
|
| 40 | - } |
|
| 37 | + public function getAccessToken() |
|
| 38 | + { |
|
| 39 | + return $this->cache->get($this->getKey(AccessToken::TYPE)); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - public function getRefreshToken() |
|
| 43 | - { |
|
| 44 | - return $this->cache->get($this->getKey(RefreshToken::TYPE)); |
|
| 45 | - } |
|
| 42 | + public function getRefreshToken() |
|
| 43 | + { |
|
| 44 | + return $this->cache->get($this->getKey(RefreshToken::TYPE)); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - public function createAccessToken(string $value) |
|
| 48 | - { |
|
| 49 | - $this->createToken($accessToken = new AccessToken([ |
|
| 50 | - 'value' => $value |
|
| 51 | - ])); |
|
| 47 | + public function createAccessToken(string $value) |
|
| 48 | + { |
|
| 49 | + $this->createToken($accessToken = new AccessToken([ |
|
| 50 | + 'value' => $value |
|
| 51 | + ])); |
|
| 52 | 52 | |
| 53 | - return $accessToken; |
|
| 54 | - } |
|
| 53 | + return $accessToken; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - public function createRefreshToken(string $value) |
|
| 57 | - { |
|
| 58 | - $this->createToken($refreshToken = new RefreshToken([ |
|
| 59 | - 'value' => $value |
|
| 60 | - ])); |
|
| 56 | + public function createRefreshToken(string $value) |
|
| 57 | + { |
|
| 58 | + $this->createToken($refreshToken = new RefreshToken([ |
|
| 59 | + 'value' => $value |
|
| 60 | + ])); |
|
| 61 | 61 | |
| 62 | - return $refreshToken; |
|
| 63 | - } |
|
| 62 | + return $refreshToken; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Put the token into the cache |
|
| 67 | - * |
|
| 68 | - * @param \tbclla\Revolut\Interfaces\PersistableToken $token |
|
| 69 | - * @return void |
|
| 70 | - */ |
|
| 71 | - private function createToken(PersistableToken $token) |
|
| 72 | - { |
|
| 73 | - $this->cache->put( |
|
| 74 | - $this->getKey($token->getType()), |
|
| 75 | - $token, |
|
| 76 | - $token->getExpiration() |
|
| 77 | - ); |
|
| 78 | - } |
|
| 65 | + /** |
|
| 66 | + * Put the token into the cache |
|
| 67 | + * |
|
| 68 | + * @param \tbclla\Revolut\Interfaces\PersistableToken $token |
|
| 69 | + * @return void |
|
| 70 | + */ |
|
| 71 | + private function createToken(PersistableToken $token) |
|
| 72 | + { |
|
| 73 | + $this->cache->put( |
|
| 74 | + $this->getKey($token->getType()), |
|
| 75 | + $token, |
|
| 76 | + $token->getExpiration() |
|
| 77 | + ); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Get the cache key |
|
| 82 | - * |
|
| 83 | - * @param string $type |
|
| 84 | - * @return string |
|
| 85 | - */ |
|
| 86 | - public function getKey(string $type) |
|
| 87 | - { |
|
| 88 | - return self::PREFIX . $type; |
|
| 89 | - } |
|
| 80 | + /** |
|
| 81 | + * Get the cache key |
|
| 82 | + * |
|
| 83 | + * @param string $type |
|
| 84 | + * @return string |
|
| 85 | + */ |
|
| 86 | + public function getKey(string $type) |
|
| 87 | + { |
|
| 88 | + return self::PREFIX . $type; |
|
| 89 | + } |
|
| 90 | 90 | } |
@@ -6,35 +6,35 @@ |
||
| 6 | 6 | |
| 7 | 7 | class AuthorizationCode implements GrantsAccessTokens |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * The token value |
|
| 11 | - * |
|
| 12 | - * @var string |
|
| 13 | - */ |
|
| 14 | - public $value; |
|
| 9 | + /** |
|
| 10 | + * The token value |
|
| 11 | + * |
|
| 12 | + * @var string |
|
| 13 | + */ |
|
| 14 | + public $value; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Create a new authorization code instance |
|
| 18 | - * |
|
| 19 | - * @param string $value The authorization code supplied by Revolut |
|
| 20 | - */ |
|
| 21 | - public function __construct(string $value) |
|
| 22 | - { |
|
| 23 | - $this->value = $value; |
|
| 24 | - } |
|
| 16 | + /** |
|
| 17 | + * Create a new authorization code instance |
|
| 18 | + * |
|
| 19 | + * @param string $value The authorization code supplied by Revolut |
|
| 20 | + */ |
|
| 21 | + public function __construct(string $value) |
|
| 22 | + { |
|
| 23 | + $this->value = $value; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - public function getValue() |
|
| 27 | - { |
|
| 28 | - return $this->value; |
|
| 29 | - } |
|
| 26 | + public function getValue() |
|
| 27 | + { |
|
| 28 | + return $this->value; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - public static function getType() |
|
| 32 | - { |
|
| 33 | - return 'code'; |
|
| 34 | - } |
|
| 31 | + public static function getType() |
|
| 32 | + { |
|
| 33 | + return 'code'; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - public static function getGrantType() |
|
| 37 | - { |
|
| 38 | - return 'authorization_code'; |
|
| 39 | - } |
|
| 36 | + public static function getGrantType() |
|
| 37 | + { |
|
| 38 | + return 'authorization_code'; |
|
| 39 | + } |
|
| 40 | 40 | } |
@@ -9,107 +9,107 @@ |
||
| 9 | 9 | |
| 10 | 10 | class TokenManager |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * The token repository |
|
| 14 | - * |
|
| 15 | - * @var \tbclla\Revolut\Interfaces\TokenRepository |
|
| 16 | - */ |
|
| 17 | - private $tokenRepository; |
|
| 12 | + /** |
|
| 13 | + * The token repository |
|
| 14 | + * |
|
| 15 | + * @var \tbclla\Revolut\Interfaces\TokenRepository |
|
| 16 | + */ |
|
| 17 | + private $tokenRepository; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The access token request |
|
| 21 | - * |
|
| 22 | - * @var \tbclla\Revolut\Auth\Requests\AccessTokenRequest |
|
| 23 | - */ |
|
| 24 | - private $accessTokenRequest; |
|
| 19 | + /** |
|
| 20 | + * The access token request |
|
| 21 | + * |
|
| 22 | + * @var \tbclla\Revolut\Auth\Requests\AccessTokenRequest |
|
| 23 | + */ |
|
| 24 | + private $accessTokenRequest; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Create a token manager |
|
| 28 | - * |
|
| 29 | - * @param \tbclla\Revolut\Interfaces\TokenRepository $tokenRepository |
|
| 30 | - * @param \tbclla\Revolut\Auth\Requests\AccessTokenRequest $accessTokenRequest |
|
| 31 | - * @return void |
|
| 32 | - */ |
|
| 33 | - public function __construct(TokenRepository $tokenRepository, AccessTokenRequest $accessTokenRequest) |
|
| 34 | - { |
|
| 35 | - $this->tokenRepository = $tokenRepository; |
|
| 36 | - $this->accessTokenRequest = $accessTokenRequest; |
|
| 37 | - } |
|
| 26 | + /** |
|
| 27 | + * Create a token manager |
|
| 28 | + * |
|
| 29 | + * @param \tbclla\Revolut\Interfaces\TokenRepository $tokenRepository |
|
| 30 | + * @param \tbclla\Revolut\Auth\Requests\AccessTokenRequest $accessTokenRequest |
|
| 31 | + * @return void |
|
| 32 | + */ |
|
| 33 | + public function __construct(TokenRepository $tokenRepository, AccessTokenRequest $accessTokenRequest) |
|
| 34 | + { |
|
| 35 | + $this->tokenRepository = $tokenRepository; |
|
| 36 | + $this->accessTokenRequest = $accessTokenRequest; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Get an access token from the repository, |
|
| 41 | - * or request a new access token |
|
| 42 | - * |
|
| 43 | - * @return \tbclla\Revolut\Auth\AccessToken |
|
| 44 | - */ |
|
| 45 | - public function getAccessToken() |
|
| 46 | - { |
|
| 47 | - $accessToken = $this->tokenRepository->getAccessToken(); |
|
| 39 | + /** |
|
| 40 | + * Get an access token from the repository, |
|
| 41 | + * or request a new access token |
|
| 42 | + * |
|
| 43 | + * @return \tbclla\Revolut\Auth\AccessToken |
|
| 44 | + */ |
|
| 45 | + public function getAccessToken() |
|
| 46 | + { |
|
| 47 | + $accessToken = $this->tokenRepository->getAccessToken(); |
|
| 48 | 48 | |
| 49 | - return $accessToken ?? $this->refreshAccessToken(); |
|
| 50 | - } |
|
| 49 | + return $accessToken ?? $this->refreshAccessToken(); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Get a refresh token from the repository |
|
| 54 | - * |
|
| 55 | - * @return \tbclla\Revolut\Auth\RefreshToken|null |
|
| 56 | - */ |
|
| 57 | - public function getRefreshToken() |
|
| 58 | - { |
|
| 59 | - return $this->tokenRepository->getRefreshToken(); |
|
| 60 | - } |
|
| 52 | + /** |
|
| 53 | + * Get a refresh token from the repository |
|
| 54 | + * |
|
| 55 | + * @return \tbclla\Revolut\Auth\RefreshToken|null |
|
| 56 | + */ |
|
| 57 | + public function getRefreshToken() |
|
| 58 | + { |
|
| 59 | + return $this->tokenRepository->getRefreshToken(); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Store a new access token |
|
| 64 | - * |
|
| 65 | - * @param string $value |
|
| 66 | - * @return \tbclla\Revolut\Auth\AccessToken |
|
| 67 | - */ |
|
| 68 | - public function createAccessToken(string $value) |
|
| 69 | - { |
|
| 70 | - return $this->tokenRepository->createAccessToken($value); |
|
| 71 | - } |
|
| 62 | + /** |
|
| 63 | + * Store a new access token |
|
| 64 | + * |
|
| 65 | + * @param string $value |
|
| 66 | + * @return \tbclla\Revolut\Auth\AccessToken |
|
| 67 | + */ |
|
| 68 | + public function createAccessToken(string $value) |
|
| 69 | + { |
|
| 70 | + return $this->tokenRepository->createAccessToken($value); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Store a new refresh token |
|
| 75 | - * |
|
| 76 | - * @param string $value |
|
| 77 | - * @return \tbclla\Revolut\Auth\RefreshToken |
|
| 78 | - */ |
|
| 79 | - public function createRefreshToken(string $value) |
|
| 80 | - { |
|
| 81 | - return $this->tokenRepository->createRefreshToken($value); |
|
| 82 | - } |
|
| 73 | + /** |
|
| 74 | + * Store a new refresh token |
|
| 75 | + * |
|
| 76 | + * @param string $value |
|
| 77 | + * @return \tbclla\Revolut\Auth\RefreshToken |
|
| 78 | + */ |
|
| 79 | + public function createRefreshToken(string $value) |
|
| 80 | + { |
|
| 81 | + return $this->tokenRepository->createRefreshToken($value); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Exchange a refresh token for a new access token |
|
| 86 | - * |
|
| 87 | - * @return \tbclla\Revolut\Auth\AccessToken |
|
| 88 | - * @throws \tbclla\Revolut\Exceptions\AppUnauthorizedException |
|
| 89 | - */ |
|
| 90 | - public function refreshAccessToken() |
|
| 91 | - { |
|
| 92 | - if (!$refreshToken = $this->getRefreshToken()) { |
|
| 93 | - throw new AppUnauthorizedException('No refresh token found. Re-authorization required.'); |
|
| 94 | - } |
|
| 84 | + /** |
|
| 85 | + * Exchange a refresh token for a new access token |
|
| 86 | + * |
|
| 87 | + * @return \tbclla\Revolut\Auth\AccessToken |
|
| 88 | + * @throws \tbclla\Revolut\Exceptions\AppUnauthorizedException |
|
| 89 | + */ |
|
| 90 | + public function refreshAccessToken() |
|
| 91 | + { |
|
| 92 | + if (!$refreshToken = $this->getRefreshToken()) { |
|
| 93 | + throw new AppUnauthorizedException('No refresh token found. Re-authorization required.'); |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - return $this->requestAccessToken($refreshToken); |
|
| 97 | - } |
|
| 96 | + return $this->requestAccessToken($refreshToken); |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Request a new access token |
|
| 101 | - * |
|
| 102 | - * @param \tbclla\Revolut\Interfaces\GrantsAccessTokens $token |
|
| 103 | - * @return \tbclla\Revolut\Auth\AccessToken |
|
| 104 | - */ |
|
| 105 | - public function requestAccessToken(GrantsAccessTokens $token) |
|
| 106 | - { |
|
| 107 | - $response = $this->accessTokenRequest->exchange($token); |
|
| 99 | + /** |
|
| 100 | + * Request a new access token |
|
| 101 | + * |
|
| 102 | + * @param \tbclla\Revolut\Interfaces\GrantsAccessTokens $token |
|
| 103 | + * @return \tbclla\Revolut\Auth\AccessToken |
|
| 104 | + */ |
|
| 105 | + public function requestAccessToken(GrantsAccessTokens $token) |
|
| 106 | + { |
|
| 107 | + $response = $this->accessTokenRequest->exchange($token); |
|
| 108 | 108 | |
| 109 | - if (isset($response['refresh_token'])) { |
|
| 110 | - $this->createRefreshToken($response['refresh_token']); |
|
| 111 | - } |
|
| 109 | + if (isset($response['refresh_token'])) { |
|
| 110 | + $this->createRefreshToken($response['refresh_token']); |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - return $this->createAccessToken($response['access_token']); |
|
| 114 | - } |
|
| 113 | + return $this->createAccessToken($response['access_token']); |
|
| 114 | + } |
|
| 115 | 115 | } |