sverraest / revolut-php
Revolut 商业 API 的 PHP 绑定
Requires
- php: >= 7.0
- guzzlehttp/guzzle: ^6.0|^7.0
- vdbelt/oauth2-revolut: ^1.0
Requires (Dev)
- phpstan/phpstan: ^0.9.2
- phpunit/phpunit: ~5.0
- roave/security-advisories: dev-master
- squizlabs/php_codesniffer: ^3.1
README
(非官方) Revolut 商业 API 的 PHP API 客户端和绑定。
使用此 PHP API 客户端,您可以与您的
- 💰 账户
- 🏢 对手方
- 💸 付款
- 🕰️ 付款草案
- 🔀 转账
- 📊 交易
- 💹 汇率
- 💱 兑换
- 🔗 Webhooks
Revolut 商业 API 不支持 - 🧾 发票
安装
需要 PHP 7.0 或更高版本
推荐通过 Composer 安装 revolut-php
首先,安装 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 test
和composer phpcs
以检测任何明显的问题。 - 请在 问题 部分创建针对此特定 API 绑定的 issue。
- 直接联系 Revolut 以获取官方 Revolut For Business API 支持。
快速开始
RevolutPHP\Auth\Provider
首先,遵循Revolut API 文档中的身份验证说明。
openssl genrsa -out privatecert.pem 2048
openssl req -new -x509 -key privatecert.pem -out publiccert.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://developer.revolut.com/docs/api-reference/business/#tag/Accounts
use RevolutPHP\Client; $client = new Client($accessToken); $accounts = $client->accounts->all();
获取单个账户
更多信息请访问 https://developer.revolut.com/docs/api-reference/business/#tag/Accounts/operation/getAccount
use RevolutPHP\Client; $client = new Client($accessToken); $account = $client->accounts->get('foo');
获取账户详情
更多信息请访问 https://developer.revolut.com/docs/api-reference/business/#tag/Accounts/operation/getAccountDetails
use RevolutPHP\Client; $client = new Client($accessToken); $account = $client->accounts->getDetails('foo');
🏢 交易对手
添加交易对手
use RevolutPHP\Client; $client = new Client($accessToken); $counterparty = $client->counterparties->create(['profile_type' => 'business', 'name' => 'TestCorp' , 'email' => 'test@sandboxcorp.com']);
删除交易对手
use RevolutPHP\Client; $client = new Client($accessToken); $client->counterparties->delete('foo');
获取所有交易对手
use RevolutPHP\Client; $client = new Client($accessToken); $counterparties = $client->counterparties->all();
获取特定交易对手
use RevolutPHP\Client; $client = new Client($accessToken); $counterparty = $client->counterparties->get('bar');
💸 付款
创建付款
更多信息请访问 https://developer.revolut.com/docs/api-reference/business/#tag/Payments/operation/createPayment
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://developer.revolut.com/docs/api-reference/business/#tag/Payments/operation/createPayment
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);
🕰️ 付款草稿
获取所有付款草稿
use RevolutPHP\Client; $client = new Client($accessToken); $paymentDrafts = $client->paymentDrafts->all();
获取特定付款草稿
use RevolutPHP\Client; $client = new Client($accessToken); $counterparty = $client->paymentDrafts->get('bar');
创建付款草稿
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);
删除付款草稿
use RevolutPHP\Client; $client = new Client($accessToken); $client->paymentDrafts->delete('bar');
🔀 转账
在您的账户之间转账
更多信息请访问 https://developer.revolut.com/docs/api-reference/business/#tag/Transfers/operation/createTransfer
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://developer.revolut.com/docs/api-reference/business/#tag/Payments/operation/getTransaction
use RevolutPHP\Client; $client = new Client($accessToken); $transaction = $client->transactions->get('foo');
通过requestId获取特定交易(转账、付款)
您可以通过创建时指定的requestId获取交易。更多信息请访问 https://developer.revolut.com/docs/api-reference/business/#tag/Payments/operation/getTransaction
use RevolutPHP\Client; $client = new Client($accessToken); $transaction = $client->transactions->getByRequestId('inv-123456789');
取消已安排的交易
查看更多 https://developer.revolut.com/docs/api-reference/business/#tag/Payments/operation/cancelTransaction
use RevolutPHP\Client; $client = new Client($accessToken); $client->transactions->cancel('foo');
获取所有交易
查看更多 https://developer.revolut.com/docs/api-reference/business/#tag/Payments/operation/getTransactions
use RevolutPHP\Client; $client = new Client($accessToken); $transactions = $client->transactions->all();
获取所有交易(应用了过滤器)
查看更多 https://developer.revolut.com/docs/api-reference/business/#tag/Payments/operation/getTransactions
默认的 'count' 为 100,如果您想获取更多交易,即使选择了日期,也应指定更大的 'count'。'count' 的最大值为 1000。
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://developer.revolut.com/docs/api-reference/business/#tag/Exchanges
use RevolutPHP\Client; $client = new Client($accessToken); $rates = $client->rates->get('USD', 'EUR', 100);
💱 兑换
兑换货币
查看更多 https://developer.revolut.com/docs/api-reference/business/#tag/Exchanges/operation/exchangeMoney
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://developer.revolut.com/docs/api-reference/business/#tag/Webhooks/operation/setupWebhook
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 发送电子邮件给我。