UNPKG

upiqrcode

Version:

Unofficial UPI QR code generator for Node.js with TypeScript support. Educational project - not affiliated with NPCI or financial institutions.

85 lines (84 loc) 3.18 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const QRCode = require("qrcode"); function validateParams({ payeeVPA: pa, payeeName: pn }) { let error = ""; if (!pa || !pn) { error = "Virtual Payee's Address/Payee's Name is compulsory"; } else if (pa.trim().length < 5 || pn.trim().length < 4) { error = "Virtual Payee's Address/Payee's Name is too short."; } return error; } function buildUrl(baseUrl, params) { let url = baseUrl; const queryParams = []; for (const [key, value] of Object.entries(params)) { if (value && value.trim() !== '') { queryParams.push(encodeURIComponent(key) + "=" + encodeURIComponent(value)); } } if (queryParams.length > 0) { url += queryParams.join("&"); } return url; } function upiqrcode(_a) { return __awaiter(this, arguments, void 0, function* ({ payeeVPA: pa, payeeName: pn, payeeMerchantCode: me, transactionId: tid, transactionRef: tr, transactionNote: tn, amount: am, minimumAmount: mam, currency: cu, transactionRefUrl: url, }) { return new Promise((resolve, reject) => { const error = validateParams({ payeeVPA: pa, payeeName: pn }); if (error) { reject(new Error(error)); return; } let intent = "upi://pay?"; const params = {}; // Add required parameters if (pa) params.pa = pa; if (pn) params.pn = pn; // Add optional parameters if (am) params.am = am; if (mam) params.mam = mam; if (cu) params.cu = cu; if (me) params.me = me; if (tid) params.tid = tid; if (tr) params.tr = tr; if (tn) params.tn = tn; if (url) params.url = url; intent = buildUrl(intent, params); const opts = { quality: 1.0, margin: 3, scale: 10, }; QRCode.toDataURL(intent, opts, (err, qr) => { if (err) { reject(new Error("Unable to generate UPI QR Code.")); return; } resolve({ qr, intent }); }); }); }); } exports.default = upiqrcode;