UNPKG

@sk-py/upi-qr

Version:

Generate NPCI-compliant UPI QR codes for Indian digital payments. Supports payee VPA, amount, transaction note, and more.

71 lines (62 loc) 2.4 kB
// generateUPIQR.js (or .ts if you're using TypeScript) import QRCode from "qrcode"; /** * Generate UPI QR Code * @param {Object} options - UPI payment options * @param {string} options.payeeVPA - UPI ID/VPA (required) * @param {string} options.payeeName - Payee name (required) * @param {string} [options.amount] - Payment amount (optional) * @param {boolean} [options.fixedAmount=true] - If false, allows user to enter the amount * @param {string} [options.transactionNote] - Transaction note (optional) * @param {string} [options.transactionRef] - Transaction reference (optional) * @param {string} [options.purpose] - Purpose code (optional) * @param {string} [options.mode] - Purpose code (optional) * @param {string} [options.merchantCode] - Merchant code (optional) * @param {string} [options.currency=INR] - Currency code (default: INR) * @param {string} [options.url] - Transaction reference URL (optional) * @returns {Promise<Object>} Object containing QR code and UPI intent */ export async function generateUPIQR(options) { const { payeeVPA, payeeName, amount, fixedAmount = true, transactionNote, merchantCode, purpose, mode, transactionRef, url, currency = "INR", } = options; // Validation if (!payeeVPA || !payeeName) { throw new Error("payeeVPA and payeeName are required"); } // Build UPI URL const upiURL = new URL("upi://pay"); upiURL.searchParams.set("pa", payeeVPA); upiURL.searchParams.set("pn", payeeName); upiURL.searchParams.set("cu", currency); // Only add amount if fixedAmount is true and amount is provided if (fixedAmount && amount) { upiURL.searchParams.set("am", amount); } if (transactionNote) upiURL.searchParams.set("tn", transactionNote); if (transactionRef) upiURL.searchParams.set("tr", transactionRef); if (purpose) upiURL.searchParams.set("purpose", purpose); if (purpose) upiURL.searchParams.set("mode", mode); if (merchantCode) upiURL.searchParams.set("mc", merchantCode); if (url) upiURL.searchParams.set("url", url); const upiString = upiURL.toString(); try { const qrCode = await QRCode.toDataURL(upiString); return { qr: qrCode, intent: upiString, }; } catch (error) { throw new Error(`QR generation failed: ${error.message}`); } }