PHP code example of pisethchhun / bakong-khqr-php

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

    

pisethchhun / bakong-khqr-php example snippets


use Piseth\BakongKhqr\Models\IndividualInfo;
use Piseth\BakongKhqr\BakongKHQR;

// Create individual information
$individualInfo = new IndividualInfo(
    'username@wing',    // Account ID
    'Your Name',        // Merchant Name
    'Phnom Penh'        // Merchant City
);

// Set optional properties
$individualInfo->merchantID = '012345678';
$individualInfo->currency = 116; // KHR (840 for USD)
$individualInfo->amount = 1000.0;

// Generate the KHQR
$result = BakongKHQR::generateIndividual($individualInfo);

// Get the QR code string
$qrString = $result->getData()['qr'];

// Get the QR code image URL
$qrImageUrl = $result->getData()['qrImageUrl'];

use Piseth\BakongKhqr\Models\MerchantInfo;
use Piseth\BakongKhqr\BakongKHQR;

// Create merchant information
$merchantInfo = new MerchantInfo(
    'merchant@wing',      // Account ID
    'Merchant Name',      // Merchant Name
    'Phnom Penh',         // Merchant City
    '987654321',          // Merchant ID
    'Bank Name'           // Acquiring Bank
);

// Set optional properties
$merchantInfo->currency = 840; // USD (116 for KHR)
$merchantInfo->amount = 25.50;

// Generate the KHQR
$result = BakongKHQR::generateMerchant($merchantInfo);

use Piseth\BakongKhqr\BakongKHQR;

// Verify the KHQR code
$verificationResult = BakongKHQR::verify($qrString);

// Check if valid
if ($verificationResult->isValid()) {
    echo "KHQR is valid!";
}

use Piseth\BakongKhqr\BakongKHQR;

// Decode the KHQR code
$decodedResult = BakongKHQR::decode($qrString);

// Access decoded data
$decodedData = $decodedResult->getData();
echo "Merchant Name: " . $decodedData['merchantName'];
echo "Amount: " . ($decodedData['amount'] ?? 'Not specified');

// Set all possible additional fields
$additionalFields = [
    'merchantID' => '123456789',
    'acquiringBank' => 'Test Bank',
    'currency' => 116,
    'amount' => 15000.0,
    'mobileNumber' => '85512345678',
    'billNumber' => 'BILL-2023-001',
    'storeLabel' => 'Main Store',
    'terminalLabel' => 'TERM-001',
    'purposeOfTransaction' => 'Payment for services',
    'languagePreference' => 'KM',
    'merchantNameAlternateLanguage' => 'អ្នកលក់',
    'merchantCityAlternateLanguage' => 'ភ្នំពេញ',
    'upiMerchantAccount' => 'UPI-123456'
];

// Apply additional fields to individual info
foreach ($additionalFields as $field => $value) {
    if (property_exists($individualInfo, $field)) {
        $individualInfo->$field = $value;
    }
}

use Piseth\BakongKhqr\BakongKHQR;

// Initialize with token
$token = 'your-bakong-api-token';
$bakongKHQR = new BakongKHQR($token);

// Check if a Bakong account exists
$accountResult = $bakongKHQR->checkBakongAccount('test@wing');

// Check transaction status
$transactionMD5 = md5('transaction-reference');
$transactionResult = $bakongKHQR->checkTransactionByMD5($transactionMD5);

use Piseth\BakongKhqr\BakongKHQR;

// Generate deep link from KHQR code
$deepLinkResult = BakongKHQR::generateDeepLink($qrString);
$deepLink = $deepLinkResult->getData()['deeplink'];
bash
composer 
bash
composer analyse