unicloudvn / klbpay-php-sdk
适用于Kien Long银行支付的客户端库
v1.0
2023-09-13 04:05 UTC
Requires
- php: >=7.3
- ext-json: *
- ext-mbstring: *
- ext-oauth: *
- ext-openssl: *
- guzzlehttp/guzzle: ^7.0
This package is auto-updated.
Last update: 2024-09-13 07:11:27 UTC
README
将KLBPay集成到商家支付系统的用户集成库
PHP
Github: https://github.com/unicloudvn/KLBPay-PHP-SDK.git
安装和使用
要求
PHP版本:7.4及以上。此外还需要一些PHP扩展,如php-oauth。
Linux上的安装示例
sudo install php-oauth
或者
sudo install php<version>-oauth sudo install php7.4-oauth
Composer
要使用Composer安装依赖项,请访问Composer。如果您尚未安装Composer,请按照安装指南进行操作。
运行以下命令
composer require unicloudvn/klbpay-php-sdk
请检查composer.json文件中的以下部分
{ "require": { "unicloudvn/klbpay-php-sdk": "^1.0" } }
最后,请确保已包含autoloader
require_once '/path/to/your-project/vendor/autoload.php';
手动安装
步骤 1. 将sdk目录下载到您的项目目录中。
步骤 2. 切换到sdk目录下的composer.json所在位置。运行命令:composer install
。
使用方法
导入
require_once '/path/to/your-project/vendor/autoload.php';
常量
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);
基本示例
在您的项目中
步骤 1. 创建/assets
目录 -> 创建文件
- Bootstrap v3.3.4 -
bootstrap.min.css
。 - jQuery v1.11.3 -
jquery-1.11.3.min.js
。 - Jumbotron-narrow -
jumbotron-narrow.css
/* Space out content a bit */ body { padding-top: 20px; padding-bottom: 20px; } /* Everything but the jumbotron gets side spacing for mobile first views */ .header, .marketing, .footer { padding-right: 15px; padding-left: 15px; } /* Custom page header */ .header { padding-bottom: 20px; border-bottom: 1px solid #e5e5e5; } /* Make the masthead heading the same height as the navigation */ .header h3 { margin-top: 0; margin-bottom: 0; line-height: 40px; } /* Custom page footer */ .footer { padding-top: 19px; color: #777; border-top: 1px solid #e5e5e5; } /* Customize container */ @media (min-width: 768px) { .container { max-width: 970px; } } .container-narrow > hr { margin: 30px 0; } /* Main marketing message and sign up button */ .jumbotron { text-align: center; border-bottom: 1px solid #e5e5e5; } .jumbotron .btn { padding: 14px 24px; font-size: 21px; } /* Supporting marketing content */ .marketing { margin: 40px 0; } .marketing p + h4 { margin-top: 28px; } /* Responsive: Portrait tablets and up */ @media screen and (min-width: 768px) { /* Remove the padding we set earlier */ .header, .marketing, .footer { padding-right: 0; padding-left: 0; } /* Space out the masthead */ .header { margin-bottom: 30px; } /* Remove the bottom border on the jumbotron for visual effect */ .jumbotron { border-bottom: 0; } } .pay-success { color: blue; } .pay-unsuccess { color: black; } .pay-error { color: red; } .footer { text-align: center } /* Pager */ .pager { margin: 8px 3px; padding: 3px; } .pager .disabled { border: 1px solid #ddd; color: #999; margin-top: 4px; padding: 3px; text-align: center; } .pager .current { background-color: #6ea9bf; border: 1px solid #6e99aa; color: #fff; font-weight: bold; margin-top: 4px; padding: 3px 5px; text-align: center; } .pager span, .pager a { margin: 4px 3px; } .pager a { border: 1px solid #aaa; padding: 3px 5px; text-align: center; text-decoration: none; }
步骤 2. 在项目根目录创建index.php文件。
<!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="<?php try { print random_int(100000, 999999); } catch (Exception $e) { error_log($e->getMessage()); } ?>"/> </div> <div class="form-group"> <label for="amount">Số tiền</label> <input class="form-control" id="amount" name="amount" type="number" value="10000"/> </div> <div class="form-group"> <label for="title">Nội dung thanh toán</label> <textarea class="form-control" cols="20" id="title" name="description" rows="2">Noi dung thanh toan</textarea> </div> <div class="form-group"> <label for="title">Tiêu đề</label> <input class="form-control" id="title" name="title" type="text" value="Thanh toán hóa đơn tiền điện"/> </div> <div class="form-group"> <label for="timeout">timeout</label> <input class="form-control" id="timeout" name="timeout" type="number" value="10000"/> </div> <div class="form-group"> <label for="language">Ngôn ngữ</label> <select name="language" id="language" class="form-control"> <option value="vn">Tiếng Việt</option> <option value="en">English</option> </select> </div> <div class="form-group"> <h3>Thông tin khách hàng</h3> </div> <div class="form-group"> <label for="fullName">Họ tên (*)</label> <input class="form-control" id="fullName" name="fullName" type="text" value="NGUYEN VAN XO"/> </div> <div class="form-group"> <label for="email">Email (*)</label> <input class="form-control" id="email" name="email" type="text" value="xonv@KLBPAY.vn"/> </div> <div class="form-group"> <label for="phone">Số điện thoại (*)</label> <input class="form-control" id="phone" name="phone" type="text" value="0934998386"/> </div> <div class="form-group"> <label for="address">Địa chỉ (*)</label> <input class="form-control" id="address" name="address" type="text" value="22 Lang Ha"/> </div> <button type="submit" class="btn btn-primary" id="btnPopup">Tạo giao dịch</button> <button type="button" onclick="window.location.href='query.php'" name="redirect" id="redirect" class="btn btn-default">Kiểm tra giao dịch</button> </form> </div> <p> </p> <footer class="footer"> <p>© KLBPAY <?php echo date('Y') ?></p> </footer> </div> </body> </html>
创建交易:
$response = $pay_client->createTransaction($request);
步骤 3. 创建CreateTransaction.php
文件
<?php use src\transaction\model\CustomerInfo; use src\transaction\request\CreateTransactionRequest; require 'kpay-php-sdk/vendor/autoload.php'; include_once 'Config.php'; // Input data $tnx_ref = $_POST['ref_transaction_id']; $amount = $_POST['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);
最后一步:运行内置的php web服务器
php -S localhost:8000
然后,打开浏览器访问指定的主机和端口(例如上述,https://:8000)。
支付回调(Webhook)
交易通知
use src\security\SecurityUtil; require 'vendor/autoload.php'; include_once 'config.php'; /* 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>