Skip to content

Commit

Permalink
Refactor UPI QR code generation logic and update version to 1.3.19
Browse files Browse the repository at this point in the history
  • Loading branch information
bhar4t committed Jan 2, 2025
1 parent de91e57 commit 29fcc81
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 29 deletions.
9 changes: 1 addition & 8 deletions dist/index.js

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

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "upiqr",
"version": "1.3.18",
"version": "1.3.19",
"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 All @@ -12,7 +12,7 @@
"package.json"
],
"scripts": {
"minify": "jsmin -o dist/index.d.ts dist/index.d.ts && jsmin -o dist/index.js dist/index.js && jsmin -o dist/types/upiqr.d.ts dist/types/upiqr.d.ts",
"minify": "jsmin -o dist/types/upiqr.d.ts dist/types/upiqr.d.ts && jsmin -o dist/index.d.ts dist/index.d.ts && uglifyjs -o dist/index.js dist/index.js --mangle",
"build": "tsc && npm run minify"
},
"repository": {
Expand Down Expand Up @@ -45,7 +45,6 @@
"devDependencies": {
"@types/node": "^20.5.6",
"@types/qrcode": "^1.5.1",
"jsmin": "^1.0.1",
"typescript": "^5.2.2"
}
}
29 changes: 11 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,6 @@ function validate({ pa, pn }: { pa: string, pn: string }): string {
return ''
}

/**
* Builds the UPI intent URL from the given parameters.
* @param {Object} params - The parameters object containing UPI intent fields.
* @returns {string} - The constructed UPI intent URL.
*/
function buildUrl(params: object): string {
let qs = ""
for (let [key, value] of Object.entries(params)) {
if (value)
qs += `${encodeURIComponent(key)}=${encodeURIComponent(value)}&`
}
return "upi://pay?" + qs.slice(0, -1) // Remove trailing '&'
}

/**
* Generates a UPI QR code and intent URL.
* @param {UPIIntentParams} params - The UPI intent parameters.
Expand All @@ -45,11 +31,18 @@ export default function upiqr ({
}: UPIIntentParams, qrOptions?: QRCode.QRCodeToDataURLOptions): Promise<QRResult> {
const params = { pa, pn, am, mam, cu, mc, tid, tr, tn }
const error = validate(params)

if (error) return Promise.reject(new Error(error))

const intent = buildUrl(params)


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

return new Promise((resolve, reject) => {
QRCode
.toDataURL(intent, qrOptions)
Expand Down

0 comments on commit 29fcc81

Please sign in to comment.