NodeJS
Thư viện người dùng tích hợp KLBPay vào hệ thống thanh toán của Merchant
Install
Tạo file .npmrc trong thư mục root với nội dung bên dưới:
@unicloudvn:registry=https://npm.pkg.github.com/unicloudvn
//npm.pkg.github.com/:_authToken=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
Sau đó tiến hành cài đặt
# Để cài đặt sử dụng lệnh
npm install @unicloudvn/kpay-nodejs-sdk
Hoặc
npm install @unicloudvn/kpay-nodejs-sdk --save-dev
Usage
Lấy thông tin tích hợp từ: Klb Pay Portal
Thông tin tích hợp bao gồm: Host, ClientId, SecretKey, EncryptKey
Import sử dụng kpay-sdk
import {Payment} from 'kpay-nodejs-sdk';
Ví dụ cơ bản
// tạo thông qua consctructor
const payment = new Payment(clientId, encryptKey, secretKey, host);
Hoặc
// tạo thông qua biến môi trường từ file .env
KLB_HOST = Host
ENCRYPT_KEY = EncryptKey
CLIENT_ID = ClientId
SECRET_KEY = SecretKey
Tạo giao dịch với JavaScript
Request data
const data = {
refTransactionId: "123456",
amount: 123,
description: "Mo ta thanh toan",
timeout: 10000,
title: "Thanh Toan",
language: "Viet Nam",
customerInfo: {
fullName: "Nguyen Van A",
email: "email@gmail.com",
phone: "0123456789",
address: "Ho Chi Minh"
},
successUrl: "https://success.example.com.vn",
failUrl: "https://fail.example.com.vn",
redirectAfter: 5,
bankAccountId: "",
}
const create = payment.create(data).then(res => {
// handle success response
})
.catch(err => {
// handle error response
})
Response data
{
transactionId: '6db5cf6e-e952-4ab5-8c55-aa82400fdd35',
refTransactionId: '123456',
payLinkCode: 'QCg8oFIf',
timeout: 10000,
url: 'https://pay-staging.kienlongbank.co/paylink/QCg8oFIf',
virtualAccount: '10372212086747084',
description: 'Mo ta thanh toan',
amount: 123,
qrCodeString: '00020101021238610010A000000727013100069704520117103722120867470840208QRIBFTTA530370454031235802VN62220818TT Don hang 12345663046352',
status: 'CREATED',
time: '2022-12-08T15:12:28.967953'
}
Kiểm tra trạng thái giao dịch
Request data
const data = {
transactionId: '6db5cf6e-e952-4ab5-8c55-aa82400fdd35'
}
const check = payment.check(data).then(res => {
// handle success response
})
.catch(err => {
// handle error response
})
Response data
{
status: 'CREATED',
refTransactionId: '123456',
amount: 123
}
Huỷ giao dịch
Request data
const data = {
transactionId: '6db5cf6e-e952-4ab5-8c55-aa82400fdd35'
}
const cancel = payment.cancel(data).then(res => {
// handle success response
})
.catch(err => {
// handle error response
})
Response data
{ success: true }
Tạo giao dịch với TypeScript
Cần import thêm Model từ sdk
import {Payment, Model} from 'kpay-nodejs-sdk';
Request data: Model.CreatePaymentRequest
const data: Model.CreatePaymentRequest = {
refTransactionId: "123456",
amount: 123,
description: "Mo ta thanh toan",
timeout: 10000,
title: "Thanh Toan",
language: "Viet Nam",
customerInfo: {
fullName: "Nguyen Van A",
email: "email@gmail.com",
phone: "0123456789",
address: "Ho Chi Minh"
},
successUrl: "https://success.example.com.vn",
failUrl: "https://fail.example.com.vn",
redirectAfter: 5,
bankAccountId: "",
}
const create = payment.create(data).then(res => {
// handle success response
})
.catch(err => {
// handle error response
})
Response data: Model.CreatePaymentResponse
{
transactionId: '6db5cf6e-e952-4ab5-8c55-aa82400fdd35',
refTransactionId: '123456',
payLinkCode: 'QCg8oFIf',
timeout: 10000,
url: 'https://pay-staging.kienlongbank.co/paylink/QCg8oFIf',
virtualAccount: '10372212086747084',
description: 'Mo ta thanh toan',
amount: 123,
qrCodeString: '00020101021238610010A000000727013100069704520117103722120867470840208QRIBFTTA530370454031235802VN62220818TT Don hang 12345663046352',
status: 'CREATED',
time: '2022-12-08T15:12:28.967953'
}
Kiểm tra trạng thái giao dịch: Model.CheckPaymentRequest
Request data
const data: Model.CheckPaymentRequest = {
transactionId: '6db5cf6e-e952-4ab5-8c55-aa82400fdd35'
}
const check = payment.check(data).then(res => {
// handle success response
})
.catch(err => {
// handle error response
})
Response data: Model.CheckPaymentResponse
{
status: 'CREATED',
refTransactionId: '123456',
amount: 123
}
Huỷ giao dịch
Request data: Model.CancelPaymentRequest
const data: Model.CancelPaymentRequest = {
transactionId: '6db5cf6e-e952-4ab5-8c55-aa82400fdd35'
}
const cancel = payment.cancel(data).then(res => {
// handle success response
})
.catch(err => {
// handle error response
})
Response data: Model.CancelPaymentResponse
{ success: true }
Response Code
PAYMENT_SECURITY_VIOLATION = new ResponseCode(1601, 'Security violation');
PAYMENT_CLIENT_ID_INVALID = new ResponseCode(1602, 'Invalid client id');
PAYMENT_INVALID_TRANSACTION_ID = new ResponseCode(1603, 'Invalid transactionId');
DUPLICATE_REFERENCE_TRANSACTION_ID = new ResponseCode(1604, 'Duplicate ref transaction id');
PAYMENT_TYPE_INVALID = new ResponseCode(1605, 'Invalid payment type');
PAYMENT_INVALID_DATA = new ResponseCode(1606, 'Invalid data');
PAYMENT_ORDER_COMPLETED = new ResponseCode(1607, 'Order was completed');
PAYMENT_TRANSACTION_TIMEOUT = new ResponseCode(1608, 'Scan QR code timeout');
PAYMENT_TRANSACTION_CANCELED = new ResponseCode(1609, 'Canceled transaction');
PAYMENT_TRANSACTION_EXPIRED = new ResponseCode(1610, 'Transaction expired');
PAYMENT_TRANSACTION_FAILED = new ResponseCode(1611, 'Transaction failed');
PAYMENT_SERVICE_UNAVAILABLE = new ResponseCode(1612, 'Service unavailable');
PAYMENT_TRANSACTION_STATUS_INVALID = new ResponseCode(1613, 'Invalid transaction status');