-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitPaymentUPI.js
More file actions
62 lines (52 loc) · 1.98 KB
/
Copy pathsplitPaymentUPI.js
File metadata and controls
62 lines (52 loc) · 1.98 KB
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
// paymentSplitter.js
class PaymentSplitter {
constructor(totalAmount, upiIds = []) {
this.totalAmount = totalAmount;
this.upiIds = upiIds;
this.paymentLinks = [];
}
// Method to add UPI IDs
addUpiId(upiId) {
this.upiIds.push(upiId);
}
// Method to remove UPI ID
removeUpiId(upiId) {
this.upiIds = this.upiIds.filter(id => id !== upiId);
}
// Method to split the total amount equally among UPI IDs
splitEqually() {
if (this.upiIds.length === 0) {
throw new Error("No UPI IDs available to split the payment.");
}
const splitAmount = (this.totalAmount / this.upiIds.length).toFixed(2);
this.paymentLinks = this.upiIds.map(upiId => this.generateUpiLink(upiId, splitAmount));
return this.paymentLinks;
}
// Method to split the total amount by custom ratios (e.g., 50%, 30%, 20%)
splitByRatios(ratios = []) {
if (this.upiIds.length !== ratios.length) {
throw new Error("UPI IDs and ratio count mismatch.");
}
const totalRatio = ratios.reduce((acc, val) => acc + val, 0);
this.paymentLinks = this.upiIds.map((upiId, index) => {
const share = (this.totalAmount * ratios[index] / totalRatio).toFixed(2);
return this.generateUpiLink(upiId, share);
});
return this.paymentLinks;
}
// Helper method to generate UPI payment links
generateUpiLink(upiId, amount) {
return `upi://pay?pa=${upiId}&am=${amount}&cu=INR`;
}
// Method to display payment links for all participants
displayPaymentLinks() {
if (this.paymentLinks.length === 0) {
throw new Error("No payment links available. Please split the payment first.");
}
this.paymentLinks.forEach((link, index) => {
console.log(`UPI ID: ${this.upiIds[index]}, Link: ${link}`);
});
}
}
// Export the module for use in other files
module.exports = PaymentSplitter;