prosperoking/kudaopenapi

一个用于与kuda openapi通信的简单库

v0.2.3 2022-09-15 08:53 UTC

This package is auto-updated.

Last update: 2024-09-15 13:30:05 UTC


README

我需要与kuda openapi工作,但似乎openapi-php库的工作进展还没有准备好。这个库可以帮助填补这个空白,直到他们准备好为止。

安装

> composer require prosperoking/kudaopenapi

用法

用法考虑了kudaOpenApi v1和v2。v1使用私有和公钥对,而v2使用apiKey和电子邮件组合,无需加密正文。以下是如何利用任何API的方法

对于api v2

您可以从kuda开发者门户这里获取您的apiKey/keys

正在使用的测试和实时环境URL如下

include './vendor/autoload.php';


use Prosperoking\KudaOpenApi\KudaOpenApiV2;

$email = "youremail@here.com"
// You can generate this on the developer dashboard
$apiKey = 'xxxxxxxxxxx';

// initialize the object and pass the path or string of your key in pem format
$openApi = KudaOpenApiV2::initLive($email, $apiKey);
// And for test environment 
$openApi = KudaOpenApiV2::initTest($email, $apiKey);

// You can also Use the constructor
$openApi = new KudaOpenApiV2($email,$openApi, null, $baseUrl )
    
try {
    // this will return the account NIBBS information
    var_dump($openApi->getAccountInfo([
        'beneficiaryAccountNumber'=> '1115744617',
        'beneficiaryBankCode'=>'999058',
    ]));

} catch (\Exception $th) {
    var_dump($th->getMessage());
}

v2附带缓存驱动器,可避免不必要的认证调用,使每次12分钟请求1次,有助于提高请求速度。您也可以编写自己的缓存驱动器并供客户端使用,例如,与laravel一起的示例将如下所示

缓存应持续最长15分钟

use Prosperoking\KudaOpenApi\Contracts\IAuthCacheDriver
class MyAwesomeCacheDriver implements IAuthCacheDriver {
    public function setAuthToken(string $value): bool
    {
        // You may decide to encrypt  the value before saving it
        return Cache::set('kuda_key', $value, now()->addMinutes(12);
    }
    public function getAuthToken(): ?string
    {
        // And Decrypt it here before returning it 
        return Cache::get('kuda_key');
    }
}

并通过任何方法将其传递给客户端

include './vendor/autoload.php';


use Prosperoking\KudaOpenApi\KudaOpenApiV2;

$email = "youremail@here.com"
// You can generate this on the developer dashboard
$apiKey = 'xxxxxxxxxxx';

// init your cache driver 
$myCacheDriver = new MyAwesomeCacheDriver()

// initialize the object and pass the path or string of your key in pem format
$openApi = KudaOpenApiV2::initLive($email, $apiKey, $myCacheDriver);
// And for test environment 
$openApi = KudaOpenApiV2::initTest($email, $apiKey, $myCacheDriver);

// You can set the cache driver using the set cache driver method

$openApi->setCacheDriver($myCacheDriver);

// You can also Use the constructor
$openApi = new KudaOpenApiV2($email,$openApi, $myCacheDriver, $baseUrl )

对于api v1

include './vendor/autoload.php';


use Prosperoking\KudaOpenApi\KudaOpenApi;

$client = "your_client_id_here"
$baseUrl = 'https://sandbox.kudabank.com/v1';

// initialize the object and pass the path or string of your key in pem format
$openApi = new KudaOpenApi(
    __DIR__.'/private.pem',
    __DIR__.'/public.pem',
    
try {
    // this will return the account NIBSS information
    var_dump($openApi->getAccountInfo([
        'beneficiaryAccountNumber'=> '1115744617',
        'beneficiaryBankCode'=>'999058',
    ]));
    
    // or You can make use of makeRequest method
    $payload = [
        'beneficiaryAccountNumber'=> '1115744615',
        'beneficiaryBankCode'=>'999058'
    ];
    $requestRef= "myunique_identifier"
    // this will return the account information
    var_dump($openApi->makeRequest(
        ServiceTypes::NAME_ENQUIRY,
        $payload,
        $requestRef
    ));

} catch (\Exception $th) {
    var_dump($th->getMessage());
}

注意,如果您不传递referenceid,库将使用php bin2hex(random_bytes(10))为您生成一个

您也可以使用makeRequest方法进行请求

use Prosperoking\KudaOpenApi\KudaOpenApi;
use Prosperoking\KudaOpenApi\ServiceTypes;

$client = "your client id here"
$baseUrl = 'https://sandbox.kudabank.com/v1';
// initialize the object and pass the path or string of your key in pem format
$openApi = new KudaOpenApi(
    __DIR__.'/private.pem',
    __DIR__.'/public.pem',
    $clientId,
    $baseUrl
);
try {
    $payload = [
        'beneficiaryAccountNumber'=> '1115744615',
        'beneficiaryBankCode'=>'999058'
    ];
    $requestRef= "myunique_identifier"
    // this will return the account information
    var_dump($openApi->makeRequest(
        ServiceTypes::NAME_ENQUIRY,
        $payload,
        $requestRef
    ));

} catch (\Exception $th) {
    var_dump($th->getMessage());
}

路线图

以下是我打算添加的内容

  • 创建一个类似于kuda openapi-node的简单API基础 ☑

  • 创建简单的类来创建用于在makeRequest对象中使用的有效负载,以帮助智能感应支持。 ⏳

  • 添加对laravel的支持 ⏳

  • 创建测试 ⏳