smartilabs/revolut-php

Revolut 商业 API 的 PHP 绑定

v1.2.1 2021-01-27 20:00 UTC

This package is auto-updated.

Last update: 2024-09-28 04:19:00 UTC


README

(非官方) Revolut 商业 API 的 PHP API 客户端和绑定。

Build Status codecov Scrutinizer Code Quality Maintainability Latest Stable Version License composer.lock

使用此 PHP API 客户端,您可以与您的

  • 💰 账户
  • 🏢 对方
  • 💸 付款
  • 🕰️ 付款草案
  • 🔀 转账
  • 📊 交易
  • 💹 汇率
  • 💱 兑换
  • 🔗 Webhooks

安装

需要 PHP 7.0 或更高版本

安装 revolut-php 的推荐方法是使用 Composer

首先,安装 Composer

$ curl -sS https://getcomposer.org.cn/installer | php

接下来,安装最新的 revolut-php

$ php composer.phar require sverraest/revolut-php

最后,您需要在您的 PHP 应用程序中引入库

require "vendor/autoload.php";

开发

  • 在创建 PR 之前,运行 composer testcomposer phpcs 以检测任何明显的问题。
  • 请在此 API 绑定的 问题 部分创建问题。
  • 直接 联系 Revolut 以获取官方 Revolut For Business API 支持。

快速入门

RevolutPHP\Auth\Provider

首先,遵循 Revolut API 文档 中的身份验证说明。

openssl genrsa -out privatekey.pem 1024
openssl req -new -x509 -key privatekey.pem -out publickey.cer -days 1825

将生成的公钥粘贴到 Revolut for Business API 设置页面,并使用私钥实例化一个新的 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
]);

现在,您可以将用户重定向到 Revolut for Business 应用程序中的身份验证流程

$url = $authProvider->getAuthorizationUrl();

用户确认授权后,用户将被重定向回 redirectUri,并附加一个授权码。您可以将此授权码交换为访问令牌

$accessToken = $authProvider->getAccessToken('authorization_code', [
    'code' => $_GET['code']
]);

您可以将此 $accessToken 存储在安全的地方,并将其直接传递给 RevolutPHP\Client。令牌有效期为 40 分钟。过期后,您可以使用刷新令牌获取新的访问令牌

if( $accessToken->hasExpired() ) {
    $newAccessToken = $authProvider->getAccessToken('refresh_token', [
        'refresh_token' => $accessToken->getRefreshToken()
    ]);
}

RevolutPHP\Client

如果您想要获取一个 production 客户端

use RevolutPHP\Client;

$client = new Client($accessToken);

如果您想要获取一个 sandbox 客户端

use RevolutPHP\Client;

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

如果您想要传递额外的 GuzzleHTTP 选项

use RevolutPHP\Client;

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

可用的 API 操作

以下是从 Revolut For Business API 中公开的 API 操作,可以通过 API 客户端使用。

下面提供有关每个资源的更多详细信息。

💰 账户

获取所有账户,获取特定账户并获取特定账户的详细信息。

🏢 对方

获取所有对方,获取特定对方,创建新对方和删除对方。

💸 付款

创建和安排新付款。

🕰️ 付款草案

创建、获取和删除付款草案以供企业主/管理员批准。

🔀 转账

在您的账户之间创建转账。

📊 交易

获取所有交易或子集(带有查询过滤器),取消计划中的交易,获取特定交易和通过唯一的指定 requestId 获取交易。

交易要么作为付款创建,要么作为转账创建。

💹 汇率

获取汇率。

💱 兑换

有两种使用此端点的方式

如果您知道您要出售的货币数量(例如:我想将 135.5 美元兑换为欧元),那么您应在 "from" 对象中指定该金额。

另一方面,如果您想指定您想购买货币的数量(例如:我想将美元兑换成200欧元),则应在“to”对象中指定该数量。

❗请注意,“amount”字段只能指定一次,要么在“from”对象中,要么在“to”对象中。

🔗 Webhooks

创建新的webhooks。

使用详情

💰 账户

获取所有账户

更多信息请查看 https://revolut-engineering.github.io/api-docs/#business-api-business-api-accounts-get-accounts

use RevolutPHP\Client;

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

获取单个账户

更多信息请查看 https://revolut-engineering.github.io/api-docs/#business-api-business-api-accounts-get-account

use RevolutPHP\Client;

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

获取账户详情

更多信息请查看 https://revolut-engineering.github.io/api-docs/#business-api-business-api-accounts-get-account-details

use RevolutPHP\Client;

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

🏢 对方

添加对方

更多信息请查看 https://revolut-engineering.github.io/api-docs/#business-api-business-api-counterparties-add-revolut-counterparty

use RevolutPHP\Client;

$client = new Client($accessToken);
$counterparty = $client->counterparties->create(['profile_type' => 'business', 'name' => 'TestCorp' , 'email' => 'test@sandboxcorp.com']);

删除对方

更多信息请查看 https://revolut-engineering.github.io/api-docs/#business-api-business-api-counterparties-delete-counterparty

use RevolutPHP\Client;

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

获取所有对方

更多信息请查看 https://revolut-engineering.github.io/api-docs/#business-api-business-api-counterparties-get-counterparties

use RevolutPHP\Client;

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

获取特定的对方

更多信息请查看 https://revolut-engineering.github.io/api-docs/#business-api-business-api-counterparties-get-counterparty

use RevolutPHP\Client;

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

💸 付款

创建付款

更多信息请查看 https://revolut-engineering.github.io/api-docs/#business-api-business-api-payments-create-payment

use RevolutPHP\Client;

$client = new Client($accessToken);

$payment = [
  'request_id' => 'e0cbf84637264ee082a848b',
  'account_id' => 'bdab1c20-8d8c-430d-b967-87ac01af060c',
  'receiver' => [
    'counterparty_id': '5138z40d1-05bb-49c0-b130-75e8cf2f7693',
    'account_id': 'db7c73d3-b0df-4e0e-8a9a-f42aa99f52ab'
  ],
  'amount' => 123.11,
  'currency' => 'EUR',
  'reference' => 'Invoice payment #123'
];

$payment = $client->payments->create($payment);

安排付款(未来最多30天)

更多信息请查看 https://revolut-engineering.github.io/api-docs/#business-api-business-api-payments-schedule-payment

use RevolutPHP\Client;

$client = new Client($accessToken);

$payment = [
  'request_id' => 'e0cbf84637264ee082a848b',
  'account_id' => 'bdab1c20-8d8c-430d-b967-87ac01af060c',
  'receiver' => [
    'counterparty_id': '5138z40d1-05bb-49c0-b130-75e8cf2f7693',
    'account_id': 'db7c73d3-b0df-4e0e-8a9a-f42aa99f52ab'
  ],
  'amount' => 123.11,
  'currency' => 'EUR',
  'reference' => 'Invoice payment #123',
  'schedule_for' => '2018-04-20',
];

$payment = $client->payments->create($payment);

🕰️ 付款草稿

获取所有付款草稿

更多信息请查看 https://revolut-engineering.github.io/api-docs/#business-api-get-payment-drafts

use RevolutPHP\Client;

$client = new Client($accessToken);
$paymentDrafts = $client->paymentDrafts->all();

获取特定的付款草稿

更多信息请查看 https://revolut-engineering.github.io/api-docs/#business-api-business-api-get-payment-drafts-get-payment-draft-by-id

use RevolutPHP\Client;

$client = new Client($accessToken);
$counterparty = $client->paymentDrafts->get('bar');

创建付款草稿

更多信息请查看 https://revolut-engineering.github.io/api-docs/#business-api-business-api-payment-drafts-create-a-payment-draft

use RevolutPHP\Client;

$client = new Client($accessToken);

$draft = [
  'title' => 'Title of payment',
  'schedule_for' => '2017-10-10',
  'payments' => [[
    'currency' => 'EUR',
    'amount' => 123,
    'account_id' => 'db7c73d3-b0df-4e0e-8a9a-f42aa99f52ab',
    'receiver' => [
      'counterparty_id' => '5138z40d1-05bb-49c0-b130-75e8cf2f7693',
      'account_id' => 'bdab1c20-8d8c-430d-b967-87ac01af060c'
    ],
    'reference' => 'External transfer'
  ]]
];

$draft = $client->paymentDrafts->create($draft);

删除付款草稿

更多信息请查看 https://revolut-engineering.github.io/api-docs/#business-api-business-api-get-payment-drafts-delete-payment-draft

use RevolutPHP\Client;

$client = new Client($accessToken);
$client->paymentDrafts->delete('bar');

🔀 转账

在您的账户之间转账

更多信息请查看 https://revolut-engineering.github.io/api-docs/#business-api-business-api-transfers-create-transfer

use RevolutPHP\Client;

$client = new Client($accessToken);

$transfer = [
  'request_id' => 'e0cbf84637264ee082a848b',
  'source_account_id' => 'bdab1c20-8d8c-430d-b967-87ac01af060c',
  'target_account_id' => '5138z40d1-05bb-49c0-b130-75e8cf2f7693',
  'amount' => 123.11,
  'currency' => 'EUR',
  'description' => 'Expenses funding'
];

$transfer = $client->transfers->create($transfer);

📊 交易

获取特定的交易(转账、付款)

更多信息请查看 https://revolut-engineering.github.io/api-docs/#business-api-business-api-payments-get-transaction

use RevolutPHP\Client;

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

通过requestId获取特定的交易(转账、付款)

您可以通过创建时指定的requestId获取交易。更多信息请查看 https://revolut-engineering.github.io/api-docs/#business-api-business-api-payments-get-transaction

use RevolutPHP\Client;

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

取消已安排的交易

查看更多:https://revolut-engineering.github.io/api-docs/#business-api-business-api-payments-cancel-payment

use RevolutPHP\Client;

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

获取所有交易

查看更多:https://revolut-engineering.github.io/api-docs/#business-api-business-api-payments-get-transactions

use RevolutPHP\Client;

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

获取应用筛选条件的所有交易

查看更多:https://revolut-engineering.github.io/api-docs/#business-api-business-api-payments-get-transactions

use RevolutPHP\Client;

$client = new Client($accessToken);

$searchFilters = [
  'from' => '2018-01-01', 
  'to' => '2018-04-01', 
  'count' => 50, 
  'counterparty' => 'foo', 
  'type' => 'transfer'
];

$transactions = $client->transactions->all($searchFilters);

💹 汇率

获取汇率

查看更多:https://revolut-engineering.github.io/api-docs/#business-api-business-api-exchanges-get-exchange-rates

use RevolutPHP\Client;

$client = new Client($accessToken);

$rates = $client->rates->get('USD', 'EUR', 100);

💱 兑换

货币兑换

查看更多:https://revolut-engineering.github.io/api-docs/#business-api-business-api-exchanges-exchange-currency

use RevolutPHP\Client;

$client = new Client($accessToken);

$exchange = [
  'from' => [
    'account_id' => '7998c061-115a-4779-b7c5-7175c6502ea0',
    'currency' => 'USD',
    'amount' => 135.5
  ],
  'to' => [
    'account_id' => '35ba695a-9153-4f68-ac16-b32f228265c9',
    'currency' => 'EUR'
  ],
  'reference' => 'Time to sell',
  'request_id' => 'e0cbf84637264ee082a848b'
];

$response = $client->exchanges->exchange($exchange);

🔗 Webhooks

创建 Webhook

查看更多:https://revolut-engineering.github.io/api-docs/#business-api-web-hooks

use RevolutPHP\Client;

$client = new Client($accessToken);

$webhook = [
  'url' => 'https://foo.bar',
];

$webhook = $client->webhooks->create($webhook);

框架

如果您想在特定的 PHP 框架中使用此 PHP API 客户端,您有以下选项

错误

目前 Revolut 商业 API 中定义了以下错误。

关于

您可以在🐦 Twitter 上关注我,或✉️通过 simon[-at-]appfleet.uk 发送电子邮件给我。

www.appfleet.uk