Skip to content

Commit

Permalink
Refactor UPI QR code generation logic and update version to 1.3.21
Browse files Browse the repository at this point in the history
  • Loading branch information
bhar4t committed Jan 6, 2025
1 parent 27c3eaa commit 4b1e030
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "upiqr",
"version": "1.3.20",
"version": "1.3.21",
"description": "Generate NPCI's UPI QR code along with UPI intent link, By using it any payment is possible from UPI enabled apps.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
12 changes: 4 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { QRResult, UPIIntentParams, Base64, ImageType } from './types/upiqr'
* @param {Object} params - The parameters object containing payeeVPA and payeeName.
* @returns {string} - An error message if validation fails, otherwise an empty string.
*/
function validate({ pa, pn }: { pa: string, pn: string }): string {
function validate<T extends { pa: string, pn: string }>({ pa, pn }: T): string {
if (!pa || !pn) return "Virtual payee's address/name is compulsory"
if (pa.length < 5 || pn.length < 4) return "Virtual payee's address/name is too short."
return ''
Expand All @@ -29,15 +29,11 @@ export default function upiqr ({
minimumAmount: mam,
currency: cu,
}: UPIIntentParams, qrOptions?: QRCode.QRCodeToDataURLOptions): Promise<QRResult> {
const params = { pa, pn, am, mam, cu, mc, tid, tr, tn }
const params: any = Object.assign({ pa, pn }, Object.fromEntries(Object.entries({ am, mam, cu, mc, tid, tr, tn }).filter(([_, value]) => value)))
const error = validate(params)
if (error) return Promise.reject(new Error(error))

// IIFE: builds and returns the UPI intent URL by given params.
const intent = ((params: object): string => {
const urlParams = new URLSearchParams(Object.entries(params).filter(([_, value]) => value))
return `upi://pay?${urlParams.toString()}`
})(params);

const intent = 'upi://pay?' + new URLSearchParams(params).toString()

return new Promise((resolve, reject) => {
QRCode
Expand Down

0 comments on commit 4b1e030

Please sign in to comment.