oaklabs/psd2

支持 PSD2 API 的银行 API 客户端。

v1.0 2017-11-24 10:09 UTC

This package is not auto-updated.

Last update: 2024-09-25 08:47:10 UTC


README

License Latest Stable Version Latest Unstable Version Build Status

简介

Psd2 是一个支持 PSD2 API 和 OAuth2 验证的银行 API 客户端。

需要 PHP 7.1+。

安装

Psd2 可以通过 Composer 安装,只需在 composer.json 中包含 "oaklabs/psd2": "^1.0" 并运行 composer updatecomposer install

支持的银行

使用方法

示例将使用 Fidor Bank Gateway 进行描述,但所有银行网关使用相同的方法。

一旦我们在银行 OAuth2 界面中填写了自己的银行详细信息,我们必须处理带有 statecode 变量的回调,以检索访问令牌,从而能够使用银行 API。

Connector 类是负责实例化银行网关的类。创建 Connector 实例是使用 Psd2 的第一步。

沙箱模式

所有银行网关都可以在沙箱模式下使用,这将使银行调用沙箱 API 端点以进行测试。

检索访问令牌

// Let's suppose we saved the state token in a $state variable,
// the random code in $code and we have a boolean $useSandbox variable

// First of all we need to create an Authorization instance

$authorization = new \OakLabs\Psd2\Authorization\Authorization([
    'code' => $code,
    'state' => $state,
    'redirect_uri' => 'the redirect_uri your set in your Bank API configuration,
    'client_id' => 'the client_id of the bank API,
    'client_secret' => 'the client secret of the bank API'
]);

// Let's now instantiate the Bank Gateway through the Connector
$tokens = (new Connector($authorization))
    ->getBankGateway(
        'fidor',
        $useSandbox
    )
    ->retrieveTokens()
    ->getTokens();

// $tokens is now an instance of \League\OAuth2\Client\Token

$accessToken = $tokens->getToken();
$refreshToken = $tokens->getRefreshToken();
$expiration = $tokens->getExpires();
$hasExpired = $tokens->hasExpired();
$jsonSerialized = $tokens->jsonSerialize();

检索账户

// After we got the Access Token and we saved it in a $tokens variable
// we can interact with the Bank API

// In case of a new request, create again the Authorization instance,
// but this time we don't need state and code

$authorization = new Authorization([
    'redirect_uri' => 'the redirect_uri your set in your Bank API configuration,
    'client_id' => 'the client_id of the bank API,
    'client_secret' => 'the client secret of the bank API'
]);

$accounts = (new Connector($authorization))
    ->getBankGateway(
        'fidor',
        $useSandbox
    )
    ->setAccessToken($accessToken)
    ->getAccountDetails();

// $accounts is an array of \OakLabs\Psd2\Psd\AccountDetail

foreach ($accounts as $account) {
    // $account->getAccountNumber()
    // $account->getBic()
    // $account->getBalance()
    // $account->getBalanceAvailable()
    // $account->getCreatedAt()
    // $account->getCurrency()
    // $account->getCustomers()
    // $account->getIban()
    // $account->getId()
}

检索 SEPA 交易

// After we got the Access Token and we saved it in a $tokens variable
// we can interact with the Bank API

// In case of a new request, create again the Authorization instance,
// but this time we don't need state and code

$authorization = new Authorization([
    'redirect_uri' => 'the redirect_uri your set in your Bank API configuration,
    'client_id' => 'the client_id of the bank API,
    'client_secret' => 'the client secret of the bank API'
]);

// Let's now retrieve the SEPA Transactions using the API Pagination

$transactions = (new Connector($authorization))
    ->getBankGateway(
        'fidor',
        $useSandbox
    )
    ->setAccessToken($accessToken)
    ->getSepaTransactions($page, $limit);

// $transactions is an array of \OakLabs\Psd2\Transaction

foreach ($transactions as $transaction) {
    // $transaction->getExternalUid()
    // $transaction->getAccountUid()
    // $transaction->getTransactionUid()
    // $transaction->getAmount()
    // $transaction->getIban()
    // $transaction->getBic()
    // $transaction->getDescription()
    // $transaction->getCreatedAt()
}

创建 SEPA 交易

// After we got the Access Token and we saved it in a $tokens variable
// we can interact with the Bank API

// In case of a new request, create again the Authorization instance,
// but this time we don't need state and code

$authorization = new Authorization([
    'redirect_uri' => 'the redirect_uri your set in your Bank API configuration,
    'client_id' => 'the client_id of the bank API,
    'client_secret' => 'the client secret of the bank API'
]);

// Let's suppose we have a $data array with the transaction we want to create
$data = [
    'external_uid' => '1234567890', // Some uid defined by us
    'account_id' => '12345', // The account_id comes from the Bank API and must be retrieved through getAccountDetails . It is NOT the account number
    'amount' => 10, // Amount of the transfer
    'remote_iban' => 'DE0000000000000000', // IBAN to transfer the money to
    'bic' => 'ABCDEFGH', // BIC
    'subject' => 'My Description' // Description
];

$transaction = (new Connector($authorization))
    ->getBankGateway(
        'fidor',
        $useSandbox
    )
    ->setAccessToken($accessToken)
    ->createSepaTransaction($data);

// Transaction is an instance of \OakLabs\Psd2\Transaction

// $transaction->getExternalUid()
// $transaction->getAccountUid()
// $transaction->getTransactionUid()
// $transaction->getAmount()
// $transaction->getIban()
// $transaction->getBic()
// $transaction->getDescription()
// $transaction->getCreatedAt()

测试

只需调用 vendor/bin/phpunit tests 运行测试。

贡献指南

PSD2 遵循 PSR-1、PSR-2 和 PSR-4 PHP 编码标准和语义版本控制。

欢迎提交拉取请求。

许可证

PSD2 是在 MIT 许可证下免费分发的软件。