Skip to content

Commit

Permalink
Merge pull request #10 from vdbelt/feat/oauth2
Browse files Browse the repository at this point in the history
Added support for OAuth2
  • Loading branch information
sverraest authored Feb 11, 2020
2 parents 6bbcb46 + e6a25ba commit 108dfdf
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 38 deletions.
81 changes: 58 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,58 @@ require "vendor/autoload.php";


## Quick Start
### RevolutPHP\Client
First get your `production` or `sandbox` API key from [Revolut for Business](https://business.revolut.com/settings/api).
### RevolutPHP\Auth\Provider
Start by following the authentication instructions in the [Revolut API docs](https://revolutdev.github.io/business-api/#revolut-api-authentication):
```
openssl genrsa -out privatekey.pem 1024
openssl req -new -x509 -key privatekey.pem -out publickey.cer -days 1825
```

Paste the generated public key on the Revolut for Business API settings page, and use the private key to instantiate a new `RevolutPHP\Auth\Provider`:
```
$authProvider = new \RevolutPHP\Auth\Provider([
'clientId' => '{clientId}', // As shown when uploading your key
'privateKey' => 'file://{privateKeyPath}',
'redirectUri' => 'https://example.com', // The URL to redirect the user to after the authorisation step
'isSandbox' => true
]);
```
You can now redirect the user to the authorisation flow in the Revolut for Business app:
```
$url = $authProvider->getAuthorizationUrl();
```

Once the user has confirmed authorisation, the user will be redirected back to the redirectUri with an authorisation code attached. You can exchange this authorisation code for an access token:
```
$accessToken = $authProvider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
```

You can save this `$accessToken` somewhere safe and pass it directly to the RevolutPHP\Client. The token is valid for 40 minutes. To request a new token after expiration, you can use the refresh token to get a new access token:
```
if( $accessToken->hasExpired() ) {
$newAccessToken = $authProvider->getAccessToken('refresh_token', [
'refresh_token' => $accessToken->getRefreshToken()
]);
}
```

### RevolutPHP\Client
If you want to get a `production` client:

```php
use RevolutPHP\Client;

$client = new Client('apikey');
$client = new Client($accessToken);
```

If you want to get a `sandbox` client:

```php
use RevolutPHP\Client;

$client = new Client('apikey', 'sandbox');
$client = new Client($accessToken, 'sandbox');
```

If you want to pass additional [GuzzleHTTP](https://github.com/guzzle/guzzle) options:
Expand All @@ -78,7 +113,7 @@ If you want to pass additional [GuzzleHTTP](https://github.com/guzzle/guzzle) op
use RevolutPHP\Client;

$options = ['headers' => ['foo' => 'bar']];
$client = new Client('apikey', 'sandbox', $options);
$client = new Client($accessToken, 'sandbox', $options);
```

## Available API Operations
Expand Down Expand Up @@ -138,7 +173,7 @@ See more at [https://revolutdev.github.io/business-api/#get-accounts](https://re
```php
use RevolutPHP\Client;

$client = new Client('apikey');
$client = new Client($accessToken);
$accounts = $client->accounts->all();
```

Expand All @@ -148,7 +183,7 @@ See more at [https://revolutdev.github.io/business-api/#get-account](https://rev
```php
use RevolutPHP\Client;

$client = new Client('apikey');
$client = new Client($accessToken);
$account = $client->accounts->get('foo');
```

Expand All @@ -158,7 +193,7 @@ See more at [https://revolutdev.github.io/business-api/#get-account-details](htt
```php
use RevolutPHP\Client;

$client = new Client('apikey');
$client = new Client($accessToken);
$account = $client->accounts->getDetails('foo');
```

Expand All @@ -169,7 +204,7 @@ See more at [https://revolutdev.github.io/business-api/#add-revolut-counterparty
```php
use RevolutPHP\Client;

$client = new Client('apikey');
$client = new Client($accessToken);
$counterparty = $client->counterparties->create(['profile_type' => 'business', 'name' => 'TestCorp' , 'email' => '[email protected]']);
```

Expand All @@ -179,7 +214,7 @@ See more at [https://revolutdev.github.io/business-api/#delete-counterparty](htt
```php
use RevolutPHP\Client;

$client = new Client('apikey');
$client = new Client($accessToken);
$client->counterparties->delete('foo');
```

Expand All @@ -189,7 +224,7 @@ See more at [https://revolutdev.github.io/business-api/#get-counterparties](http
```php
use RevolutPHP\Client;

$client = new Client('apikey');
$client = new Client($accessToken);
$counterparties = $client->counterparties->all();
```

Expand All @@ -199,7 +234,7 @@ See more at [https://revolutdev.github.io/business-api/#get-counterparty](https:
```php
use RevolutPHP\Client;

$client = new Client('apikey');
$client = new Client($accessToken);
$counterparty = $client->counterparties->get('bar');
```

Expand All @@ -210,7 +245,7 @@ See more at [https://revolutdev.github.io/business-api/#create-payment](https://
```php
use RevolutPHP\Client;

$client = new Client('apikey');
$client = new Client($accessToken);

$payment = [
'request_id' => 'e0cbf84637264ee082a848b',
Expand All @@ -233,7 +268,7 @@ See more at [https://revolutdev.github.io/business-api/#schedule-payment](https:
```php
use RevolutPHP\Client;

$client = new Client('apikey');
$client = new Client($accessToken);

$payment = [
'request_id' => 'e0cbf84637264ee082a848b',
Expand All @@ -258,7 +293,7 @@ See more at [https://revolutdev.github.io/business-api/#transfer](https://revolu
```php
use RevolutPHP\Client;

$client = new Client('apikey');
$client = new Client($accessToken);

$transfer = [
'request_id' => 'e0cbf84637264ee082a848b',
Expand All @@ -279,7 +314,7 @@ See more at [https://revolutdev.github.io/business-api/#check-payment-status](ht
```php
use RevolutPHP\Client;

$client = new Client('apikey');
$client = new Client($accessToken);
$transaction = $client->transactions->get('foo');
```

Expand All @@ -290,7 +325,7 @@ See more at [https://revolutdev.github.io/business-api/#check-payment-status](ht
```php
use RevolutPHP\Client;

$client = new Client('apikey');
$client = new Client($accessToken);
$transaction = $client->transactions->getByRequestId('inv-123456789');
```

Expand All @@ -300,7 +335,7 @@ See more at [https://revolutdev.github.io/business-api/#cancel-payment](https://
```php
use RevolutPHP\Client;

$client = new Client('apikey');
$client = new Client($accessToken);
$client->transactions->cancel('foo');
```

Expand All @@ -310,7 +345,7 @@ See more at [https://revolutdev.github.io/business-api/#get-transactions](https:
```php
use RevolutPHP\Client;

$client = new Client('apikey');
$client = new Client($accessToken);
$transactions = $client->transactions->all();
```

Expand All @@ -320,7 +355,7 @@ See more at [https://revolutdev.github.io/business-api/#get-transactions](https:
```php
use RevolutPHP\Client;

$client = new Client('apikey');
$client = new Client($accessToken);

$searchFilters = [
'from' => '2018-01-01',
Expand All @@ -340,7 +375,7 @@ See more at [https://revolutdev.github.io/business-api/#get-exchange-rates](http
```php
use RevolutPHP\Client;

$client = new Client('apikey');
$client = new Client($accessToken);

$rates = $client->rates->get('USD', 'EUR', 100);
```
Expand All @@ -352,7 +387,7 @@ See more at [https://revolutdev.github.io/business-api/#exchange-currency](https
```php
use RevolutPHP\Client;

$client = new Client('apikey');
$client = new Client($accessToken);

$exchange = [
'from' => [
Expand All @@ -378,7 +413,7 @@ See more at [https://revolutdev.github.io/business-api/#web-hooks](https://revol
```php
use RevolutPHP\Client;

$client = new Client('apikey');
$client = new Client($accessToken);

$webhook = [
'url' => 'https://foo.bar',
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"type": "library",
"require": {
"guzzlehttp/guzzle": "~6.0",
"php": ">= 7.0"
"php": ">= 7.0",
"vdbelt/oauth2-revolut": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
Loading

0 comments on commit 108dfdf

Please sign in to comment.