A utility package for generating and handling VietQR codes.
npm install uni-vqr
const { generateVietQR } = require('uni-vqr');
// Example: Generate a VietQR code
const qrCode = generateVietQR({
bankId: '970418',
accountNumber: '123456789',
accountName: 'John Doe',
amount: 1000.5,
message: 'Payment for invoice #123'
});
console.log(qrCode);
Generates a VietQR code string based on the provided parameters.
options
(Object): The options for generating the QR code.bankId
(string, required): The bank ID (NAPAS standard).accountNumber
(string, required): The recipient's account number.accountName
(string, optional): The recipient's account name.amount
(number, optional): The transfer amount.message
(string, optional): The transfer message.
string
: The generated VietQR code string.
const qrCode = generateVietQR({
bankId: '970418',
accountNumber: '123456789',
accountName: 'John Doe',
amount: 1000.5,
message: 'Payment for invoice #123'
});
console.log(qrCode);
// Output: A string representing the VietQR code
Throws an error if required fields (bankId
or accountNumber
) are missing.
try {
generateVietQR({ accountNumber: '123456789' });
} catch (error) {
console.error(error.message); // Output: "bankId and accountNumber are required fields."
}
Decodes a VietQR code string and extracts its components.
qrCode
(string, required): The VietQR code string to decode.
Object
: An object containing the decoded components:bankId
(string): The bank ID.accountNumber
(string): The recipient's account number.accountName
(string, optional): The recipient's account name.amount
(number, optional): The transfer amount.message
(string, optional): The transfer message.
const { decodeVietQR } = require('uni-vqr');
const qrCode = '000201010211260600A0000007279704180112312345678902541000.5062Payment for invoice #1236304';
const result = decodeVietQR(qrCode);
console.log(result);
// Output:
// {
// bankId: '970418',
// accountNumber: '123456789',
// amount: 1000.5,
// message: 'Payment for invoice #123'
// }
Throws an error if the QR code string is invalid.
try {
decodeVietQR('');
} catch (error) {
console.error(error.message); // Output: "Invalid QR code string."
}
Generates a MoMo QR code string based on the provided parameters.
options
(Object): The options for generating the QR code.partnerCode
(string, required): The MoMo partner code.partnerRefId
(string, required): The reference ID for the transaction.amount
(number, required): The transaction amount.description
(string, optional): The transaction description.
string
: The generated MoMo QR code string.
const qrCode = generateMoMoQR({
partnerCode: 'MOMO',
partnerRefId: '123456789',
amount: 50000,
description: 'Payment for order #123'
});
console.log(qrCode);
Decodes a MoMo QR code string and extracts its components.
qrCode
(string, required): The MoMo QR code string to decode.
Object
: An object containing the decoded components:partnerCode
(string): The MoMo partner code.partnerRefId
(string): The reference ID for the transaction.amount
(number): The transaction amount.description
(string, optional): The transaction description.
const result = decodeMoMoQR('0002010102113804MOMO3909123456789545000062Payment for order #1236304');
console.log(result);
// Output:
// {
// partnerCode: 'MOMO',
// partnerRefId: '123456789',
// amount: 50000,
// description: 'Payment for order #123'
// }
Generates a ZaloPay QR code string based on the provided parameters.
options
(Object): The options for generating the QR code.appId
(string, required): The ZaloPay app ID.zpTransId
(string, required): The transaction ID for ZaloPay.amount
(number, required): The transaction amount.description
(string, optional): The transaction description.
string
: The generated ZaloPay QR code string.
const qrCode = generateZaloPayQR({
appId: 'ZALO',
zpTransId: '123456789',
amount: 100000,
description: 'Payment for order #123'
});
console.log(qrCode);
Decodes a ZaloPay QR code string and extracts its components.
qrCode
(string, required): The ZaloPay QR code string to decode.
Object
: An object containing the decoded components:appId
(string): The ZaloPay app ID.zpTransId
(string): The transaction ID for ZaloPay.amount
(number): The transaction amount.description
(string, optional): The transaction description.
const result = decodeZaloPayQR('0002010102113804ZALO39091234567895410000062Payment for order #1236304');
console.log(result);
// Output:
// {
// appId: 'ZALO',
// zpTransId: '123456789',
// amount: 100000,
// description: 'Payment for order #123'
// }
✅ Generate QR codes based on VietQR format
✅ Parse existing VietQR codes to extract info like bank, account number, amount, message, etc.
✅ Bank-agnostic — works with many Vietnamese banks
✅ Follows the EMVCo standard (the global QR payment format used in VietQR)
test
: Runs the test suite using Mocha.build
: Placeholder for build steps (not required for this package).publish
: Publishes the package to npm.
MIT