Use the generateQr method to request a QR code from VNPay. This method sends a signed GET request to the VNPay payment gateway with vnp_Command = genqr.
Key behaviors:
- Amount Handling: Unlike the raw VNPay specification which requires the amount to be multiplied by 100, this library accepts the "normal" amount (e.g.,
10000) and automatically handles the multiplication for you. - Environment: This must be executed on a Server (requires
secureSecret) in a Node.js environment that supports fetch (Node 18+). - Response: Returns a JSON object containing a status
code, a message, and the qrcontent string used to render the QR code or initiate a deep-link.
Important Note: For specific field lengths, error codes, and merchant conditions, always refer to the official "Merchant hosted QR" documentation provided by VNPay.
import { ProductCode, VnpLocale, dateFormat } from 'vnpay';
const start = new Date();
const end = new Date(start.getTime() + 15 * 60 * 1000); // expires after 15 minutes
const result = await vnpay.generateQr({
vnp_Amount: 10000,
vnp_IpAddr: '192.168.1.1',
vnp_TxnRef: 'ORD-123456',
vnp_OrderInfo: 'Thanh toan don hang ORD-123456',
vnp_OrderType: ProductCode.Other,
vnp_ReturnUrl: 'https://merchant.example.com/return',
vnp_Locale: VnpLocale.VN,
vnp_CreateDate: dateFormat(start),
vnp_ExpireDate: dateFormat(end),
});
if (result.code === '00' && result.qrcontent) {
// Use result.qrcontent to draw the QR code or use as a deep-link
} else {
// Handle error: result.qrcontent is typically empty when code is not '00'
}