link0/bunq

bunq 的 PHP API 客户端实现

dev-master 2017-06-28 22:25 UTC

This package is not auto-updated.

Last update: 2024-09-20 23:13:45 UTC


README

Latest Stable Version Total Downloads License Scrutinizer Code Quality Code Coverage Build Status

此库实现了 bunq API

欢迎通过问题和/或拉取请求提供反馈和测试。

基本用法

以下代码示例做了几件事情,以便您尽快开始

  1. 创建一个 InstallationServer
  2. 创建一个 DeviceServer
  3. 从 SessionServer 获取会话,以便能够使用 API

在复制/粘贴此代码之前,请确保您有一个可用的 API 密钥。当开发测试 API 时,您可能不想在生产环境中使用 API 密钥。相反,您想使用开发者 API 密钥。开发者 API 密钥可以通过联系 bunq 的客服台(使用 bunq 应用)获得。一旦您获得了该 API 密钥,您就可以将其填充到 $apiKey 变量中。

函数 registerInstallationAndDeviceServer() 只应调用一次。第一次调用后,您可以取消注释此规则。

<?php

use Link0\Bunq\Client;
use Link0\Bunq\Domain\Keypair;
use Link0\Bunq\Domain\Keypair\PublicKey;
use Link0\Bunq\Environment\Production;
use Link0\Bunq\Environment\Sandbox;
use Link0\Bunq\Service\InstallationService;

require_once('vendor/autoload.php');

/**
 * @param $installationService
 * @param $keypair
 * @param $apiKey
 * @return mixed
 */
function registerInstallationAndDeviceServer(InstallationService $installationService, $keypair, $apiKey)
{
    $installation = $installationService->createInstallation($keypair);

    $installationToken = $installation[1];
    $serverPublicKey = $installation[2];

    // Cache the server public key somewhere
    file_put_contents('server-public-key.txt', $serverPublicKey);

    // Cache the installation token somehere
    file_put_contents('installation-token.txt', $installationToken);

    $installationService->createDeviceServer($installationToken, $apiKey, 'I pasted this from README.md');
}

// openssl genpkey -algorithm RSA -out private.pem -pkeyopt rsa_keygen_bits:2048
// openssl rsa -pubout -in private.pem -out public.pem
$keypair = Keypair::fromStrings(
    file_get_contents('public.pem'),
    file_get_contents('private.pem')
);

// Replace this with what you received from the app
$apiKey = 'your-api-key';

$debugMode = true;

//$environment = new Production($debugMode);
$environment = new Sandbox($debugMode);
$client = new Client($environment, $keypair);

$installationService = new InstallationService($client);

registerInstallationAndDeviceServer($installationService, $keypair, $apiKey);

$installationToken = file_get_contents('installation-token.txt');

$sessionServer = $installationService->createSessionServer($installationToken, $apiKey);

$sessionServerId = $sessionServer[0];
$sessionToken = $sessionServer[1];
$user = $sessionServer[2];

file_put_contents('session-token.txt', $sessionToken);

// After this, you can use the client with all other services as followed
$client = new Client(
    $environment,
    $keypair,
    new PublicKey(file_get_contents('server-public-key.txt')),
    file_get_contents('session-token.txt')
);