itsoft/revolut

Revolut API集成在一个PHP类文件中,无需任何依赖。

v1.1.0 2021-03-14 13:58 UTC

This package is auto-updated.

Last update: 2024-09-14 21:16:05 UTC


README

simple PHP library for the Revolut Business API

Build

🚀 功能

  • 一个文件
  • 无外部依赖
  • 支持旧版本的PHP

💡 动机

Revolut API存在以下缺陷

  • 不支持作用域。例如,您不能仅对您的Revolut应用程序授予只读访问权限。这真令人遗憾。
  • 没有撤销令牌的可能性。这同样令人遗憾。您可以请求新的令牌(刷新令牌),但不保存它,但这是一种拙劣的解决方案。
  • 无法向交易对手添加第二个账户(IBAN)。由于旧付款与交易对手uuid相关联,因此删除和创建是一个糟糕的解决方案。我们可以添加具有不同IBAN的不同交易对手,并可能混淆两个同名公司。
  • 在通过API发送付款时没有MFA非常糟糕。我们需要第二个独立的通道来确认(签名)付款。可以是短信或TOTP。

Revolut API非常不安全。

Revolut Business API 文档.

🍭 示例

您可以在以下位置找到使用此库的示例应用程序:https://github.com/maslick/revolutor

✅ 安装

composer require itsoft/revolut

🎱 使用

cd your_secret_keys_dir/revolut
openssl genrsa -out privatekey.pem 1024
openssl req -new -x509 -key privatekey.pem -out publickey.cer -days 1825
more publickey.cer

将publickey.cer的内容添加到Revolut Business API设置页面,并指向您的OAuth重定向URI。保存ClientId并启用API访问您的账户。

创建不安全的revolut.cfg.php

此配置将令牌保存到文件中。您可以将它们保存到数据库或其他位置,但出于安全原因,最好不保存它们,请参阅下一节Secured revolut.cfg.php。

<?php
$ROOT_PATH = '/www/your-crm...';

require_once $ROOT_PATH . 'vendor/autoload.php';
$path2token = $ROOT_PATH . 'token/revolut.txt';
$path2refresh_token = $ROOT_PATH . 'token/revolut_refresh_token.txt';


$a_token = json_decode(file_get_contents($path2token));
$r_token = json_decode(file_get_contents($path2refresh_token));

$params = [
	'apiUrl' => 'https://b2b.revolut.com/api/1.0',
	'authUrl' => 'https://business.revolut.com/app-confirm', 
	'clientId' => 'YOUR-CLIENT-ID',
	'privateKey' => file_get_contents('your_secret_keys_dir/revolut/privatekey.pem'),
	'redirectUri' => 'https://your_site.com/redirect_uri/', //OAuth redirect URI
	'accessToken' => $a_token->access_token,
	'accessTokenExpires' => $a_token->expires,
	'refreshToken' => $r_token->refresh_token,
	'refreshTokenExpires' => $r_token->expires,
	'saveAccessTokenCallback' => function ($access_token, $expires) use ($path2token) {file_put_contents($path2token, json_encode(['access_token' => $access_token, 'expires' => $expires]));},
	'saveRefreshTokenCallback' => function ($refresh_token, $expires) use ($path2refresh_token) {file_put_contents($path2refresh_token, json_encode(['refresh_token' => $refresh_token, 'expires' => $expires]));},
	'logErrorCallback' => function ($error){mail('your_email@domin.com', 'Revolut API Error', $error);}
];


// for debug you can use
// $params['apiUrl'] =  'https://sandbox-b2b.revolut.com/api/1.0';
// $params['authUrl'] = 'https://sandbox-business.revolut.com/app-confirm';

$revolut = new \ITSOFT\Revolut\Revolut($params);

Secured revolut.cfg.php

<?php
$ROOT_PATH = '/www/your-crm...';

require_once $ROOT_PATH . 'vendor/autoload.php';
$params = [
	'apiUrl' => 'https://b2b.revolut.com/api/1.0',
	'authUrl' => 'https://business.revolut.com/app-confirm',
	'clientId' => 'YOUR-CLIENT-ID',
	'privateKey' => file_get_contents('your_secret_keys_dir/revolut/privatekey.pem'),
	'redirectUri' => 'https://your_site.com/redirect_uri/', //OAuth redirect URI
	'accessToken' => '',
	'accessTokenExpires' => '',
	'refreshToken' => '',
	'refreshTokenExpires' => '',
	'saveAccessTokenCallback' => function ($access_token, $expires){},
	'saveRefreshTokenCallback' => function ($refresh_token, $expires){},
	'logErrorCallback' => function ($error){mail('your_email@domin.com', 'Revolut API Error', $error);}
];


// for debug you can use
// $params['apiUrl'] =  'https://sandbox-b2b.revolut.com/api/1.0';
// $params['authUrl'] = 'https://sandbox-business.revolut.com/app-confirm';


$revolut = new \ITSOFT\Revolut\Revolut($params);

OAuth重定向URI的代码(https://your_site.com/redirect_uri/)

require_once 'revolut.cfg.php';

if(isset($_GET['code'])) $revolut->exchangeCodeForAccessToken();
elseif(!$revolut->accessToken) $revolut->goToConfirmationURL();
  
print "<pre><h2>accounts</h2>\n";
print_r($revolut->accounts());

print "<h2>counterparties</h2>\n";
print_r($revolut->counterparties());

print "<h2>getExchangeRate</h2>\n";
print_r($revolut->getExchangeRate(['from'=>'USD', 'to'=>'EUR']));
print_r($revolut->getExchangeRate(['from'=>'EUR', 'to'=>'USD']));

print "<h2>transactions</h2>\n";
print_r($revolut->transactions());

创建付款和其他方法

$revolut->createPayment($params); 

有关$param,请参阅tutorials-make-a-payment-create-paymentapi-reference createPayment

有关其他方法,请参阅https://github.com/itsoft7/revolut-php/blob/master/src/Revolut.php