PHP code example of codetay / bankapi-php

1. Go to this page and download the library: Download codetay/bankapi-php 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/ */

    

codetay / bankapi-php example snippets


$client = new \GuzzleHttp\Client([
    'timeout' => 10,
    'connect_timeout' => 5,
    // The API key travels in the X-API-Key header, which Guzzle does NOT
    // strip when a redirect changes host — don't follow redirects.
    'allow_redirects' => false,
]);

$bankapi = new \BankApi\BankApi('bk_live_...', 'https://api.bankapi.vn', $client);

use BankApi\BankApi;

$bankapi = new BankApi('bk_live_...');

// List transactions, newest first, and page through everything lazily.
$page = $bankapi->banking()->transactions(limit: 50);
foreach ($page->autoPaging() as $transaction) {
    echo "{$transaction->id}: {$transaction->amount} ({$transaction->direction})\n";
}

// Reconcile an inbound transaction against a payment intent.
$detail = $bankapi->banking()->matchTransaction(
    txId: 'tx_01H...',
    intentId: 'pi_01H...',
);

> $bankapi = new BankApi('bk_live_...', baseUrl: 'https://acme.bankapi.vn');
> 

$page = $bankapi->banking()->transactions(limit: 20);

foreach ($page as $transaction) {   // items on this page only
    // ...
}

$page->nextCursor;                  // '' when this was the last page

foreach ($page->autoPaging() as $transaction) {
    // fetches subsequent pages lazily as you iterate
}

$intent = $bankapi->banking()->createPaymentIntent(
    code: 'PN-1042',
    expectedAmount: 150_000,
    expiresInSecs: 900, // optional; server default when omitted
);
echo "{$intent->id}: {$intent->status}\n";

// Pass your own key so a retried request is guaranteed to replay, not double-create
$retried = $bankapi->banking()->createPaymentIntent(
    code: 'PN-1042',
    expectedAmount: 150_000,
    idempotencyKey: 'order-1042-attempt-1',
);

$current = $bankapi->banking()->paymentIntent($intent->id);

use BankApi\ErrorCode;
use BankApi\Exception\ApiException;

try {
    $bankapi->banking()->createPaymentIntent(code: 'PN-1042', expectedAmount: 150_000, idempotencyKey: $key);
} catch (ApiException $e) {
    echo $e->errorCode() . "\n";
    if ($e->errorCode() === ErrorCode::IDEMPOTENCY_IN_PROGRESS) {
        // the first request with this key is still executing — back off and retry
    }
    // $e->replayed would be true here only if this error was itself a
    // replayed response — the idempotency.* errors above never carry
    // Idempotent-Replayed, so it stays false for both of them.
}

use BankApi\Exception\SignatureVerificationException;
use BankApi\Webhook\EventType;
use BankApi\Webhook\Webhook;

$payload = file_get_contents('php://input');

try {
    $event = Webhook::constructEvent(
        payload: $payload,
        headers: getallheaders(),
        secret: $_ENV['BANKAPI_WEBHOOK_SECRET'],
    );
} catch (SignatureVerificationException $e) {
    http_response_code(400);
    exit;
}

// Dedupe retried deliveries using $event->webhookId (e.g. a unique
// constraint or a seen-ids cache) before acting on the event.
if (! already_processed($event->webhookId)) {
    if ($event->type === EventType::BANK_CREDIT) {
        handle($event->data);
    }
    mark_processed($event->webhookId);
}

http_response_code(200);

use BankApi\Exception\RateLimitException;
use BankApi\Exception\ApiException;

try {
    $bankapi->banking()->transaction('tx_does_not_exist');
} catch (RateLimitException $e) {
    sleep($e->retryAfter ?? 1);
} catch (ApiException $e) {
    log_error("bankapi error {$e->status}: {$e->detail}");
}
bash
composer