PHP code example of unicloudvn / klbpay-php-sdk

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

    

unicloudvn / klbpay-php-sdk example snippets






const HOST = '<HOST_URL>'; //'https://api-staging.kienlongbank.co/pay'
const CLIENT_ID = '<YOUR_CLIENT_ID>';
const SECRET_KEY = '<YOUR_SECRET_KEY>';
const ENCRYPT_KEY = '<YOUR_ENCRYPT_KEY>';
const ACCEPT_TIME_DIFF = '<YOUR_ACCEPT_TIME_DIFF>';

$kPayPacker = new KPayPacker(
    CLIENT_ID,
    ENCRYPT_KEY,
    SECRET_KEY,
    ACCEPT_TIME_DIFF,
    HOST
);

$payClient = new KPayClient($kPayPacker);

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Test Transaction</title>
    <!-- Bootstrap core CSS -->
    <link href="assets/bootstrap.min.css" rel="stylesheet"/>
    <!-- Custom styles for this template -->
    <link href="assets/jumbotron-narrow.css" rel="stylesheet">
    <script src="assets/jquery-1.11.3.min.js"></script>
</head>

<body>
<div class="container">
    <div class="header clearfix">
        <h3 class="text-muted text-center">KLBPAY DEMO</h3>
    </div>
    <h3>Tạo mới giao dịch</h3>
    <div class="table-responsive">
        <form action="CreateTransaction.php" id="create_form" method="post">
            <div class="form-group">
                <label for="refTransactionId">Mã giao dịch</label>
                <input class="form-control" id="refTransactionId" name="refTransactionId" type="text"
                       value=" try {
                           print random_int(100000, 999999);
                       } catch (Exception $e) {
                           error_log($e->getMessage());
                       } 

$response = $pay_client->createTransaction($request);



use src\transaction\model\CustomerInfo;
use src\transaction\request\CreateTransactionRequest;

amount'];
$desc = $_POST['description'];
$timeout = $_POST['timeout'];
$title = $_POST['title'];
$language = $_POST['language'];
$full_name = $_POST['full_name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$address = $_POST['address'];

$success_url = 'https://success.example.com.vn';
$fail_url = 'https://fail.example.com.vn';
$redirect_after = 5;
$bank_account_no = "";

$customer_info = new CustomerInfo($full_name, $email, $phone, $address);

$request = new CreateTransactionRequest(
    $tnx_ref,
    $amount,
    $desc,
    $timeout,
    $title,
    $language,
    $customer_info,
    $success_url,
    $fail_url,
    $redirect_after,
    $bank_account_no // hệ thống chọn tài khoản mặc định trong trường hợp null
);

try {
    if (!empty($pay_client)) {
        $response = $pay_client->createTransaction($request);
        header('Location: ' . $response->getUrl());
    }
} catch (Exception $e) {
    error_log($e->getMessage());
}

$order_id = $_POST["order-id"];

$checkRequest = new QueryTransactionRequest($order_id);

$response = $pay_client->checkTransaction($checkRequest);

$order_id = $_POST["order-id"];

$cancelRequest = new CancelTransactionRequest($order_id);

$response = $pay_client->cancelTransaction($cancelRequest);

use src\security\SecurityUtil;

 Payment Notify
 * IPN URL: Ghi nhận kết quả thanh toán từ KLBPay
 * $response_data -> Giá trị 'data' từ Request body  
 * $encrypt_key -> ENCRYPT_KEY
 * $decrypt_data -> Giá trị data sau khi decryptAES (JSON String)
 */
$decrypt_data = SecurityUtil::decryptAES($response_data, $encrypt_key)
// Kiểm tra giao dịch và trả về giá trị bool $status (true or false)
echo json_encode(['status' => $status]);

  $accountNo = $_POST['account_no'];

  $checkRequest = new CheckAccountNoRequest($accountNo);

  $response = $client->checkAccountNo($checkRequest);


   $accountNo = $_POST['account_no'];

   $checkRequest = new LinkAccountRequest($accountNo);
      ...

   $response = $client->linkAccountNo($checkRequest);

    $sessionId = $_POST['session_id'];
    $accountNo = $_POST['account_no'];
    $otp = $_POST['otp'];

    $checkRequest = new VerifyLinkAccountRequest($sessionId, $accountNo, $otp);
      ...

    $response = $client->verifyLinkAccountNo($checkRequest);


if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $order = $_POST['order'];
            $timeout = $_POST['timeout'];
            $fixAmount = $_POST['fixAmount'];
            $fixContent = $_POST['fixContent'];
            $bankAccountNo = $_POST['bankAccountNo'];
            $checkRequest = new EnableVirtualAccountRequest($order,  $timeout,  $fixAmount,  $fixContent,  $bankAccountNo);
                
                ...
            
            $response = $client->enableVirtualAccount($checkRequest);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $order = $_POST['order'];
            $checkRequest = new DisableVirtualAccountRequest($order);
            
                ...
                
            $response = $client->disableVirtualAccount($checkRequest);


if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $size = $_POST['size'] ?? null;
            $page = $_POST['page'] ?? null;
            $order = $_POST['order'] ?? null;
            $bankAccountNo = $_POST['bankAccountNo'];
            $fromDate = $_POST['fromDate'];
            $toDate = $_POST['toDate'];
            if ($size === '') {
                $size = null;
            }
            if ($page === '') {
                $page = null;
            }
            if ($order === '') {
                $order = null;
            }

            $checkRequest = new GetTransactionRequest(
                $size,
                $page,
                $order,
                $bankAccountNo,
                $fromDate,
                $toDate
            );
                    ...
            $response = $client->getTransaction($checkRequest);
            $transactions = json_encode($response, JSON_PRETTY_PRINT);
            error_log(json_encode($response));
            echo '<div class="form-group">
          <label for="transactionData">Transaction Data (JSON)</label>
          <textarea class="form-control" id="transactionData" rows="30" readonly>' . $transactions. '</textarea>

shell
sudo install php-oauth
shell
composer 
shell
php -S localhost:8000