Overview of the VNPay integration library
dayjs for time processing.website·Indexed 29 days ago
https://vnpay-lib.vercel.app/An open-source library for integrating the VNPay Payment Gateway. Documentation covers creating payment URLs, generating QR codes, verifying IPN (Instant Payment Notification) calls and return URLs, processing refunds, and querying transaction results via QueryDR. Includes installation guides for NPM and NestJS, security best practices, and full-stack sample projects using Next.js 15.
dayjs for time processing.dayjs for date handling.vnp_ReturnUrl after the transaction.app.post('/order', async (req, res) => {
// 1. Create order
const order = await createOrder(req.body);
// 2. Create payment URL
const paymentUrl = vnpay.buildPaymentUrl({
vnp_Amount: 10000,
vnp_IpAddr:
req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.ip,
vnp_TxnRef: order.orderId,
vnp_OrderInfo: `Payment for order ${order.orderId}`,
vnp_OrderType: ProductCode.Other,
vnp_ReturnUrl: 'http://localhost:3000/vnpay-return',
vnp_Locale: VnpLocale.VN,
});
// 3. Redirect customer
return res.redirect(paymentUrl);
});enableLog: true and provide a loggerFn to handle the log data.const vnpay = new VNPay({
// ... other config
enableLog: true,
loggerFn: (data) => {
console.log(JSON.stringify(data, null, 2));
// Or save to a file/database
},
});vnpay library is designed exclusively for Node.js backends. It cannot be used directly in frontend frameworks (React, Vue, Angular) because it relies on Node.js modules like fs, crypto, and path, and requires a secureSecret that must remain hidden from the client. The recommended architecture is to handle all payment logic on the backend and communicate with the frontend via JSON API calls.┌─────────────────┐ API calls ┌─────────────────┐
│ Frontend │ ──────────────► │ Backend │
│ │ │ │
│ • React/Vue │ │ • Node.js │
│ • Types only │ │ • VNPay library │
│ • UI components │ ◄────────────── │ • Payment logic │
└─────────────────┘ JSON response └─────────────────┘# Direct link to Terminal configuration (after login)
https://sandbox.vnpayment.vn/merchantv2/Account/TerminalEdit.htmFollow these steps to move from development to a stable production environment:
Testing Phase:
Production Phase:
If code works in sandbox but fails in production, check the following:
GitHub Issues: https://github.com/lehuygiang28/vnpay/issues GitHub Discussions: https://github.com/lehuygiang28/vnpay/discussions Examples: https://github.com/lehuygiang28/vnpay/blob/main/example/
To ensure the security of your VNPay integration, follow these critical practices:
secureSecret in client-side code.verifyIpnCall and verifyReturnUrl methods to verify the integrity of all data received from VNPay.Ensure reliable transaction processing by following these management rules:
vnp_TxnRef for every transaction (e.g., combining Order ID with a timestamp).buildPaymentUrl method to create a payment URL containing the necessary transaction information to redirect customers to the VNPay Payment Gateway. The library automatically handles amount calculations (no need to multiply by 100) and secure hash generation.import { ProductCode, VnpLocale } from 'vnpay';
const paymentUrl = vnpay.buildPaymentUrl({
vnp_Amount: 10000,
vnp_IpAddr: '13.160.92.202',
vnp_TxnRef: '12345',
vnp_OrderInfo: 'Payment for order 12345',
vnp_OrderType: ProductCode.Other,
vnp_ReturnUrl: 'http://localhost:3000/vnpay-return',
vnp_Locale: VnpLocale.VN, // 'vn' or 'en'
});