PHP code example of zrkb / bancard

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

    

zrkb / bancard example snippets


use Bancard\Bancard;

$bancard = new Bancard(
    publicKey: 'YOUR_PUBLIC_KEY',
    privateKey: 'YOUR_PRIVATE_KEY',
    staging: true, // optional, defaults to false
    guzzle: ['timeout' => 15], // optional Guzzle options
);

use Bancard\Util\Currency;

$response = $bancard->singleBuy([
    'shop_process_id' => '7777777',
    'amount' => '10000.00',
    'currency' => Currency::PYG->value,
    'return_url' => 'https://app.test/return',
    'cancel_url' => 'https://app.test/cancel',
]);

if ($response->isSuccessful()) {
    $processId = $response->getProcessId();
}

$response = $bancard->singleBuyZimple([
    'shop_process_id' => '7777777',
    'amount' => '10000.00',
    'currency' => Currency::PYG->value,
    'return_url' => 'https://app.test/return',
    'cancel_url' => 'https://app.test/cancel',
]);

$response = $bancard->singleBuyConfirm([
    'shop_process_id' => '7777777',
    'amount' => '10000.00',
    'currency' => Currency::PYG->value,
]);

$token = $response->getToken();

<div id="iframe-container"></div>
<script src="<?= $bancard->checkoutScriptUrl() 

use Bancard\Callback\ConfirmationCallback;
use Bancard\Exception\InvalidConfirmationException;

try {
    $confirmation = $bancard->parseConfirmation((string) file_get_contents('php://input'));
} catch (InvalidConfirmationException $e) {
    http_response_code(400); // tampered, unsigned or malformed
    exit;
}

if ($confirmation->isApproved()) {
    $confirmation->getShopProcessId();
    $confirmation->getAuthorizationNumber();
    $confirmation->getRiskIndex(); // 1-3 low, 4-6 medium, 7-9 high, 0 not available
} else {
    $confirmation->getResponseDescription(); // e.g. "Fondos insuficientes"
}

header('Content-Type: application/json');
echo json_encode(ConfirmationCallback::ACKNOWLEDGEMENT); // {"status":"success"}

$response = $bancard->singleBuyGetConfirmation([
    'shop_process_id' => '7777777',
    'extra_response_attributes' => ['confirmation.extra_attributes'], // optional
]);

if ($response->isApproved()) {
    $ticket = $response->getTicketNumber();
    $type = $response->getPaymentCardType(); // "credit", "debit" or "account_debit"
    $response->getPaymentCardBrand();         // e.g. "MC"
    $response->getPaymentMaskedCardNumber();  // e.g. "541863******0014"
    $response->getPaymentOrigin();            // e.g. "VPOS-FORM", "VPOS-TOKEN"
}

$response = $bancard->singleBuyRollback([
    'shop_process_id' => '7777777',
]);

$response = $bancard->cardsNew([
    'card_id' => '123',
    'user_id' => '456',
    'user_cell_phone' => '0981123456',
    'user_mail' => '[email protected]',
    'return_url' => 'https://app.test/return',
]);

$processId = $response->getProcessId();

$response = $bancard->usersCards([
    'user_id' => '456',
    'extra_response_attributes' => ['cards.bancard_proccesed'], // optional: adds bancard_proccesed (true/false) per card
]);

foreach ($response->getCards() as $card) {
    echo $card->card_masked_number; // also card_id, card_brand, card_type, expiration_date, alias_token
}

$response = $bancard->charge([
    'shop_process_id' => '7777777',
    'amount' => '10000.00',
    'currency' => Currency::PYG->value,
    'alias_token' => 'card_alias_token',
    'description' => 'Order 7777777', // roved()) {
    $response->getAuthorizationNumber();
} else {
    $response->getResponseDescription();
}

use Bancard\Exception\CardNotFoundException;

try {
    $response = $bancard->chargeCard([
        'user_id' => '456',
        'card_id' => '123',
        'shop_process_id' => '7777778',
        'amount' => 10000,
        'currency' => Currency::PYG,
        'description' => 'Monthly plan',
    ]);
} catch (CardNotFoundException $e) {
    // Card was deleted or never registered for this user
}

$response = $bancard->deleteCard([
    'user_id' => '456',
    'alias_token' => 'card_alias_token',
]);

$response = $bancard->preauthorizationConfirm([
    'shop_process_id' => '7777777',
]);

if ($response->isApproved()) {
    // Preauthorization confirmed
}

$response = $bancard->billingClientInfo([
    'client_ruc' => '123456-7',
]);

$client = $response->getClient(); // ->name, ->email

$response = $bancard->billingCancel([
    'shop_process_id' => '7777777',
]);

use Bancard\Exception\ApiException;
use Bancard\Exception\BancardException;
use Bancard\Exception\NetworkException;
use Bancard\Exception\ValidationException;

try {
    $response = $bancard->singleBuy($payload);
} catch (ValidationException $e) {
    // Missing  // Timeout, DNS or TLS failure; $e->getPrevious() is Guzzle's exception
} catch (BancardException $e) {
    // Response was not valid JSON (e.g. gateway error page); $e->getCode() is the HTTP status
}

use Bancard\Util\Currency;

$bancard->singleBuy([
    'shop_process_id' => '7777777',
    'amount' => 10000,             // sent as "10000.00"
    'currency' => Currency::PYG,   // sent as "PYG"
]);

use Bancard\Util\Amount;

$formatted = Amount::format(10000);      // "10000.00"
$normalized = Amount::normalize('10.5'); // "10.50" (null if invalid)