-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupi.js
97 lines (86 loc) · 2.15 KB
/
upi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"use strict"
const _ = require('./utils')
class UPI {
constructor(pa, pn, isDynamic=false, am=0) {
this.isDynamic = isDynamic
this.base = 'upi://pay'
this.params = new Map()
if (!pa) {
throw new Error('Cannot initialize UPI without Payee VPA/ UPI ID');
}
if (!_.validateVPA(pa)) {
throw new Error('Payee VPA /UPI ID format is not known');
}
this.params.set('pa', pa)
if (!pn) {
throw new Error('Cannot initialize UPI without Payee Name');
}
this.params.set('pn', pn)
if ( am > 0) {
this.params.set('am',am)
} else if (this.isDynamic) {
throw new Error('Cannot initialize Dynamic mode Tags without an Ammount');
}
// Currency code. Currently
// ONLY "INR" is the supported
// value
this.params.set('cu','INR')
}
setMerchant(mc, tid) {
if (!mc) {
throw new Error('Merchant Code code cannot be empty');
}
this.params.set('mc', mc)
if (!! tid) {
this.params.set('tid', tid)
}
return this
}
setTxRef(refid, note) {
if (!refid) {
throw new Error('Reference id cannot be empty');
}
this.params.set('tr', refid)
if (!! note) {
this.params.set('tn', note)
}
return this
}
setMinAmount(amount=0) {
if (amount<=0 || amount >= this.am) {
throw new Error('Invalid Min Amount');
}
this.params.set('mam',amount)
return this
}
setCallback(url) {
this.params.set('url', encodeURI(url))
return this
}
_validate() {
if (this.isDynamic && !this.params.has('tr')) {
throw new Error('Transaction reference ID Mandatory for dynamic URL generation');
}
if (this.params.has('mc') && !this.params.has('tr')) {
throw new Error('Transaction reference ID Mandatory for Merchant transactions');
}
}
_buildQS() {
let qs = '?'
this.params.forEach((value, key) => {
if(!!value && !! key){
if (qs !== '?') {
qs += '&'
}
qs += key+"="+value
}
})
return qs
}
getLink() {
this._validate()
let uri = this.base + this._buildQS()
return _.encodeUPI(uri)
}
}
module.exports = UPI