forked from sastava007/DSA-Coding-Practice
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stripe-design.js
158 lines (132 loc) · 3.94 KB
/
stripe-design.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
let networks=new Map();
let charges=new Map();
let merchants=new Map();
let charge=(url)=>
{
let charge_id=url.searchParams.get("charge_id");
let merchant_id=url.searchParams.get("merchant_id");
let amount=url.searchParams.get("amount");
let network=url.searchParams.get("network");
let newCharge={
"id": charge_id,
"merchant_id": merchant_id,
"amount": amount,
"network": network
}
// console.log(merchants.has(merchant_id));
if(!merchants.has(merchant_id))
{
merchants.set(merchant_id,{"merchant_id": merchant_id, "payout": 0});
console.log(merchants.get(merchant_id));
}
charges.set(charge_id,newCharge);
console.log(charges.get(charge_id));
}
let payout=(url)=>
{
// console.log(url);
let merchant_id=url.searchParams.get("merchant_id");
console.log("Payout to: ", merchant_id);
console.log(merchants.has(merchant_id));
if(merchants.has(merchant_id))
{
let payout_balance=Math.ceil(merchants.get(merchant_id).payout);
merchants.get(merchant_id).payout=0;
console.log(merchant_id,",",payout_balance);
}
}
let confirm=(url)=>
{
let charge_id=url.searchParams.get("charge_id");
console.log("ChargeID: ", charge_id);
console.log(charges.has(charge_id));
if(charges.has(charge_id))
{
let charge_details=charges.get(charge_id);
let {amount, merchant_id, network}=charge_details;
let percent=networks.get(network).percent;
amount=parseFloat(amount);
let payout_balance=amount-((2+percent)*0.01*amount);
console.log("payout_balance: ",payout_balance);
merchants.get(merchant_id).payout+=payout_balance;
charges.delete(charge_id);
}
}
let refund=(url)=>
{
let charge_id=url.searchParams.get("charge_id");
console.log("ChargeID: ", charge_id);
// if(charges.has(charge_id))
// {
let charge_details=charges.get(charge_id);
let {amount, merchant_id, network}=charge_details;
amount=parseFloat(amount);
let percent=networks.get(network).percent;
let payout_balance=percent* 0.01*amount;
console.log("To be deducted: ",payout_balance);
merchants.get(merchant_id).payout-=payout_balance;
charges.delete(charge_id);
// }
}
function process_actions(actions)
{
let m,n;
n=Number(actions[0]);
m=Number(actions[n+1]);
console.log("inside actions");
let i=1
for(;i<=n;i++)
{
let card_details=actions[i].split(" ");
console.log(card_details);
networks.set(card_details[0],{"card_network": card_details[0], "percent": parseFloat(card_details[1])});
console.log(networks.get(card_details[0]));
}
console.log(networks);
i++;
for(;i<actions.length; i++)
{
let url=new URL(actions[i], "https://stripe.com");
let action=url.pathname;
console.log("action: ",action);
switch(action) {
case "/charge":
{
charge(url);
}
break;
case "/payout" :
{
payout(url);
}
break;
case "/confirm" :
{
confirm(url);
}
break;
case "/refund" :
{
refund(url);
}
break;
default :
console.log("Error action not found");
}
}
}
let actions=[
"2",
"visa 2.0",
"mastercard 3.0",
"8",
"/charge?network=visa&amount=1000&merchant_id=m001&charge_id=c001",
"/charge?merchant_id=m001&amount=1000&network=mastercard&charge_id=c002",
"/confirm?charge_id=c001",
"/confirm?charge_id=c002",
"/payout?merchant_id=m001",
"/charge?network=visa&amount=1000&merchant_id=m001&charge_id=c003",
"/confirm?charge_id=c003",
"/payout?merchant_id=m001"
]
process_actions(actions);