PHP code example of hds-solutions / bancard-sdk

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

    

hds-solutions / bancard-sdk example snippets


use HDSSolutions\Bancard\Bancard;

// Set your vPOS API credentials
Bancard::credentials(
    publicKey:  'YOUR_PUBLIC_KEY',
    privateKey: 'YOUR_PRIVATE_KEY',
);

use HDSSolutions\Bancard\Bancard;

// Switch to production
Bancard::useProduction();

// Or dynamically based on your application environment
Bancard::useProduction(config('app.env') === 'production');

use HDSSolutions\Bancard\Bancard;
use HDSSolutions\Bancard\Models\Currency;

$response = Bancard::single_buy(
    shop_process_id: $shop_process_id,
    amount:          $amount,
    description:     'Premium Subscription',
    currency:        Currency::Guarani,
    return_url:      'https://your-domain.com/payment/success',
    cancel_url:      'https://your-domain.com/payment/cancel',
);

if ($singleBuyResponse->wasSuccess()) {
    // access the generated process ID to call the Bancard <iframe>
    $process_id = $singleBuyResponse->getProcessId();
}


use HDSSolutions\Bancard\Bancard;
use HDSSolutions\Bancard\Models\Currency;

$singleBuyResponse = Bancard::single_buy_zimple(
    shop_process_id: $shop_process_id,
    amount:          $amount,
    description:     'Premium Subscription',
    currency:        Currency::Guarani,
    phone_no:        $phone_no, // this field is automatically send on the `additional_data` property of the request
    return_url:      'https://localhost/your-success-callback-path',
    cancel_url:      'https://localhost/your-cancelled-callback-path',
);

use HDSSolutions\Bancard\Bancard;

$response = Bancard::card_new(
    user_id:    $user_id,
    card_id:    $card_id,
    phone_no:   '+595991234567',
    email:      '[email protected]',
    return_url: 'https://your-domain.com/cards/callback',
);

if ($response->wasSuccess()) {
    // access the generated process ID to call the Bancard <iframe>
    $processId = $response->getProcessId();
}

use HDSSolutions\Bancard\Bancard;
use HDSSolutions\Bancard\Models\Card;

$response = Bancard::users_cards(
    user_id: $user_id,
);

if ($response->wasSuccess()) {
    foreach ($response->getCards() as $card) {
        echo "Card: {$card->card_masked_number}\n";
        echo "Brand: {$card->card_brand}\n";
        echo "Expiration: {$card->expiration_date}\n";
    }
}

use HDSSolutions\Bancard\Bancard;
use HDSSolutions\Bancard\Models\Card;
use HDSSolutions\Bancard\Models\Currency;
use HDSSolutions\Bancard\Models\Confirmation;
use HDSSolutions\Bancard\Models\SecurityInformation;

$response = Bancard::charge(
    card:            $card,
    shop_process_id: $shop_process_id,
    amount:          $amount,
    currency:        Currency::Guarani,
    description:     'Monthly Subscription',
);

if ($response->wasSuccess()) {
    // access to change Confirmation data
    $confirmation = $chargeResponse->getConfirmation();
    echo sprintf('Ticket No: %u, Authorization ID: %u',
        $confirmation->ticket_number,
        $confirmation->authorization_number);
    
    // also access to the security information data
    $securityInformation = $confirmation->getSecurityInformation();
    echo sprintf('Country: %s, Risk Index: %.2F',
        $securityInformation->card_country,
        $securityInformation->risk_index);
}

use HDSSolutions\Bancard\Bancard;
use HDSSolutions\Bancard\Models\Confirmation;

$confirmationResponse = Bancard::confirmation(
    shop_process_id: $chargeResponse->getRequest()->getShopProcessId(),
);

use HDSSolutions\Bancard\Bancard;

$rollbackResponse = Bancard::rollback(
    shop_process_id: $chargeResponse->getRequest()->getShopProcessId(),
);

use HDSSolutions\Bancard\Bancard;
use HDSSolutions\Bancard\Models\Card;

$response = Bancard::card_delete(
    card: $card,  // must be an instance of Card, obtained from Bancard::users_cards()
);

$response = Bancard::single_buy(/* ... */);

if (! $response->wasSuccess()) {
    foreach ($response->getMessages() as $message) {
        echo sprintf(
            "Error: [%s] %s (Level: %s)\n",
            $message->key,
            $message->description,
            $message->level
        );
    }
}

$response = Bancard::charge(/* ... */);

if ($response->wasSuccess()) {
    $confirmation = $response->getConfirmation();
    
    // Access confirmation details
    echo "Response: {$confirmation->response}\n";
    echo "Response Details: {$confirmation->response_details}\n";
    echo "Response Description: {$confirmation->response_description}\n";
    
    // Access security information
    $security = $confirmation->getSecurityInformation();
    echo "Customer IP: {$security->customer_ip}\n";
    echo "Card Country: {$security->card_country}\n";
    echo "Risk Index: {$security->risk_index}\n";
}

if (! $response->wasSuccess()) {
    // Get request details
    $request = $response->getRequest();
    echo "Request Body: {$request->getBody()->getContents()}\n";
    
    // Get response details
    echo "Response Status: {$response->getStatusCode()}\n";
    echo "Response Body: {$response->getBody()->getContents()}\n";
    
    // Log for debugging
    error_log(sprintf(
        "Bancard API Error: %s, Status: %d, Body: %s",
        $response->getMessages()[0]->description ?? 'Unknown error',
        $response->getStatusCode(),
        $response->getBody()->getContents()
    ));
}

use HDSSolutions\Bancard\Bancard;

Bancard::qr_credentials(
    serviceUrl:     'YOUR_QR_ASSIGNED_DOMAIN',
    publicKey:      'YOUR_QR_PUBLIC_KEY',
    privateKey:     'YOUR_QR_PRIVATE_KEY',
    qrCommerceCode: 1234,
    qrBranchCode:   123,
);

$response = Bancard::qr_generate(
    amount:      $amount,
    description: 'Product Purchase',
);

if ($response->wasSuccess()) {
    // access the generated QR data
    $qrExpress = $qrGenerateResponse->getQRExpress();
    echo sprintf('QR Payment ID: %s, QR Image url: %s, QR Data: %s',
        $qrExpress->hook_alias,
        $qrExpress->url,
        $qrExpress->qr_data);
    
    // access the list of supported clients
    $supportedClients = $qrGenerateResponse->getSupportedClients();
    foreach ($supportedClients as $supportedClient) {
        echo sprintf('Client name: %s, Client Logo url: %s',
            $supportedClient->name,
            $supportedClient->logo_url);
    }
}

$response = Bancard::qr_revert(
    hook_alias: $qrExpress->hook_alias,
);

if ($response->wasSuccess()) {
    echo "Payment successfully reverted\n";
}

// From response to request
$request = $response->getRequest();
echo "Request Body: " . $request->getBody()->getContents() . "\n";

// From request to response
$response = $request->getResponse();
echo "Response Body: " . $response->getBody()->getContents() . "\n";

use HDSSolutions\Bancard\Bancard;
use HDSSolutions\Bancard\Models\Currency;

$singleBuyRequest = Bancard::newSingleBuyRequest(
    shop_process_id: $shop_process_id,
    amount:          $amount,
    description:     'Premium Subscription',
    currency:        Currency::Guarani,
    return_url:      'https://your-domain.com/payment/success-callback-path',
    cancel_url:      'https://your-domain.com/payment/cancel-callback-path',
);
// for example, enable Zimple flag for this request
$singleBuyRequest->enableZimple();
// for Zimple, you need to specify the user's phone number on the additional data property
$singleBuyRequest->setAdditionalData($phone_no);

// after building the request, you can call the execute() method to send the request to Bancard
if (! $singleBuyRequest->execute()) {
    // if failed, you can access the response, and the messages
    $singleBuyRequest->getResponse()->getMessages();
}