PHP code example of kangopenbanking / sdk

1. Go to this page and download the library: Download kangopenbanking/sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kangopenbanking / sdk example snippets


use KangOpenBanking\KangOpenBanking;

$kob = new KangOpenBanking([
    'client_id'     => getenv('KOB_CLIENT_ID'),
    'client_secret' => getenv('KOB_CLIENT_SECRET'),
    'environment'   => 'production',
]);

// First API call triggers a token fetch + cache automatically.
$accounts = $kob->accounts->list();

$token = $kob->getToken([
    'grant_type' => 'client_credentials',
    'scope'      => 'accounts payments',
]);

// 1. Generate PKCE in your app (example helper):
$verifier  = rtrim(strtr(base64_encode(random_bytes(64)), '+/', '-_'), '=');
$challenge = rtrim(strtr(base64_encode(hash('sha256', $verifier, true)), '+/', '-_'), '=');

// 2. Build the authorization URL and redirect the user.
$authUrl = $kob->buildAuthorizationUrl([
    'redirect_uri'          => 'https://yourapp.com/callback',
    'scope'                 => 'openid accounts payments',
    'state'                 => $state,
    'code_challenge'        => $challenge,
    'code_challenge_method' => 'S256',
]);

// 3. On callback, exchange the code for an access token.
$token = $kob->getToken([
    'grant_type'    => 'authorization_code',
    'code'          => $_GET['code'],
    'redirect_uri'  => 'https://yourapp.com/callback',
    'code_verifier' => $verifier,
]);

$kob->setAccessToken($token['access_token'], $token['expires_in']);

$kob = new KangOpenBanking([
    'client_id'   => 'your_client_id',
    'api_key'     => 'sbx_your_sandbox_key',
    'environment' => 'sandbox',
]);

use KangOpenBanking\KangOpenBanking;

$kob = new KangOpenBanking([
    'client_id'     => getenv('KOB_CLIENT_ID'),
    'client_secret' => getenv('KOB_CLIENT_SECRET'),
    'environment'   => 'sandbox',
]);

// AISP — list a user's accounts
$accounts = $kob->accounts->list();

// Gateway — create a Mobile Money charge
$charge = $kob->charges->create([
    'merchant_id'    => 'mch_uuid',
    'amount'         => 5000,
    'currency'       => 'XAF',
    'channel'        => 'mobile_money',
    'customer_phone' => '237677123456',
    'tx_ref'         => 'order_001',
]);

// Verify the charge status
$verified = $kob->charges->verify($charge['id']);

use KangOpenBanking\KangOpenBanking;

$isValid = KangOpenBanking::verifyWebhookSignature(
    $rawBody,
    $signatureHeader,
    getenv('KOB_WEBHOOK_SECRET')
);

use KangOpenBanking\Laravel\Middleware\VerifyWebhookSignature;

Route::post('/webhooks/kob', [WebhookController::class, 'handle'])
    ->middleware(VerifyWebhookSignature::class);

use KangOpenBanking\Exceptions\KOBException;

try {
    $charge = $kob->charges->create([ /* … */ ]);
} catch (KOBException $e) {
    // $e->getMessage(), $e->statusCode, $e->errorCode, $e->errorId
}
bash
php artisan vendor:publish --tag=kob-config