Skip to content

Commit 29fcc81

Browse files
committed
Refactor UPI QR code generation logic and update version to 1.3.19
1 parent de91e57 commit 29fcc81

File tree

3 files changed

+14
-29
lines changed

3 files changed

+14
-29
lines changed

dist/index.js

Lines changed: 1 addition & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "upiqr",
3-
"version": "1.3.18",
3+
"version": "1.3.19",
44
"description": "Generate NPCI's UPI QR code along with UPI intent link, By using it any payment is possible from UPI enabled apps.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -12,7 +12,7 @@
1212
"package.json"
1313
],
1414
"scripts": {
15-
"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",
15+
"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",
1616
"build": "tsc && npm run minify"
1717
},
1818
"repository": {
@@ -45,7 +45,6 @@
4545
"devDependencies": {
4646
"@types/node": "^20.5.6",
4747
"@types/qrcode": "^1.5.1",
48-
"jsmin": "^1.0.1",
4948
"typescript": "^5.2.2"
5049
}
5150
}

src/index.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,6 @@ function validate({ pa, pn }: { pa: string, pn: string }): string {
1212
return ''
1313
}
1414

15-
/**
16-
* Builds the UPI intent URL from the given parameters.
17-
* @param {Object} params - The parameters object containing UPI intent fields.
18-
* @returns {string} - The constructed UPI intent URL.
19-
*/
20-
function buildUrl(params: object): string {
21-
let qs = ""
22-
for (let [key, value] of Object.entries(params)) {
23-
if (value)
24-
qs += `${encodeURIComponent(key)}=${encodeURIComponent(value)}&`
25-
}
26-
return "upi://pay?" + qs.slice(0, -1) // Remove trailing '&'
27-
}
28-
2915
/**
3016
* Generates a UPI QR code and intent URL.
3117
* @param {UPIIntentParams} params - The UPI intent parameters.
@@ -45,11 +31,18 @@ export default function upiqr ({
4531
}: UPIIntentParams, qrOptions?: QRCode.QRCodeToDataURLOptions): Promise<QRResult> {
4632
const params = { pa, pn, am, mam, cu, mc, tid, tr, tn }
4733
const error = validate(params)
48-
4934
if (error) return Promise.reject(new Error(error))
50-
51-
const intent = buildUrl(params)
52-
35+
36+
// IIFE: builds and returns the UPI intent URL by given params.
37+
const intent = ((params: object): string => {
38+
const urlParams = new URLSearchParams()
39+
for (const [key, value] of Object.entries(params)) {
40+
if (value)
41+
urlParams.append(key, value as string)
42+
}
43+
return `upi://pay?${urlParams.toString()}`
44+
})(params);
45+
5346
return new Promise((resolve, reject) => {
5447
QRCode
5548
.toDataURL(intent, qrOptions)

0 commit comments

Comments
 (0)