@@ -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