/
index.js
206 lines (176 loc) · 5.24 KB
/
index.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//import { MongoClient } from "mongodb";
const { MongoClient, ServerApiVersion } = require("mongodb");
const { getRapyPayCheckoutId } = require("./signature");
const crypto = require('crypto');
const express = require("express");
const { randomUUID } = require("crypto");
const app = express();
const PORT = process.env.PORT || 8080;
app.use(express.json()); // for parsing application/json
app.use(express.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.get("/", (req, res) => {
res.json({
msg: "Hello world",
});
});
const getHumanDate = (date) => {
return date.toLocaleDateString(undefined, {weekday: "short", day: "numeric", month: "short", year: "numeric"})
}
/*
Request {
"cartvalue": "@cartvalue",
"paynow": true,
"user_id": "1",
"merchant_id": "1",
"currency" : "USD",
}
Response {
"status": "success",
"link" : "https://blinkpay.com/pay/1"
}
1. Paynow -> Checkout Id Generation(Rapyd API) -> Checkout Id -> Paynow
2. PayLater -> Checkout Id Generation(Rapyd API)
3. DB storage of checkout id
Database Storage :
{
"paymentId" : ":paymentId",
"user_id": "1",
"merchant_id": "1",
"cartvalue": "@cartvalue",
"checkoutId" : "RapidAPI",
"dueDate" : "currentData + 45";
}
*/
app.post("/order", async (req, res) => {
const uri =
"mongodb+srv://shivam:shivam@cluster0.173wr.mongodb.net/payments?retryWrites=true&w=majority";
const client = await MongoClient.connect(uri);
const db = client.db();
const collection = db.collection("checkout");
let checkoutId = ''
const paymentId = Math.floor(Math.random() * 1000000) + 1
if (req.body.paynow === true) {
checkoutId = await getRapyPayCheckoutId(req, res);
console.log(checkoutId)
collection.insertOne({
paymentId: paymentId,
user_id: req.body.user_id,
merchant_id: req.body.merchant_id,
cartvalue: req.body.cartvalue,
checkoutId: checkoutId,
dueDate: new Date().getTime(),
humanDate: getHumanDate(new Date()),
isPaid: "true",
currency: req.body.currency
});
} else {
collection.insertOne({
paymentId: paymentId,
user_id: req.body.user_id,
merchant_id: req.body.merchant_id,
cartvalue: req.body.cartvalue,
// checkoutId: checkoutId,
dueDate: new Date().getTime() + (2 * 24 * 60 * 60 * 1000),
humanDate: getHumanDate(new Date(new Date().getTime() + (45 * 24 * 60 * 60 * 1000))),
isPaid: "false",
currency: req.body.currency
});
}
const cartValue = req.body.cartvalue;
res.json({
checkoutId: checkoutId,
paymentId: paymentId
});
});
/*
Request {
"user_id": "1"
}
Response {
"status": "success"
"payments" : []
}
1. Fetch checkoutId from DB with date range and userId
*/
app.post("/retrive-outstanding-payment", async (req, res) => {
// const cartValue = req.body.cartvalue;
const userId = req.body.user_id;
const currentDate = new Date().getTime();
const futureDate = new Date().getTime() + (2 * 24 * 60 * 60 * 1000);
const uri =
"mongodb+srv://shivam:shivam@cluster0.173wr.mongodb.net/payments?retryWrites=true&w=majority";
const client = await MongoClient.connect(uri);
const db = client.db();
const collection = db.collection("checkout");
var data = await collection
.find({ user_id: userId }, { dueDate: { $gt: currentDate, $lt: futureDate } })
.toArray();
// let paymentInfo1 = {
// merchantId: "950ae8c6-78",
// amount: 120,
// dueDate: new Date(2022, 05, 29),
// currency: "INR",
// };
// let paymentInfo2 = {
// merchantId: "950ae8c6-78",
// amount: 501,
// dueDate: new Date(2022, 05, 30),
// currency: "INR",
// };
// let paymentInfo3 = {
// merchantId: "950ae8c6-78",
// amount: 1102,
// dueDate: new Date(2022, 05, 29),
// currency: "INR",
// };
let r = []
for (let i = 0; i < data.length; i++) {
if ((data[i].isPaid === "false") && (data[i].dueDate < futureDate) && (data[i].dueDate >= currentDate)) {
r.push({
amount: data[i].cartvalue,
currency: data[i].currency,
merchantId: data[i].merchant_id,
dueDate: data[i].humanDate,
paymentId: data[i].paymentId,
message: `${data[i].paymentId}: ${data[i].currency} ${data[i].cartvalue} due on ${data[i].humanDate} to merchant ABC.`,
})
}
}
const outstandingPayments = r;
res.json({ outstandingPayments: r });
});
// /*
// Request {
// "paymentId"
// }
// */
app.post("/do-payment", async (req, res) => {
const payId = req.body.payId;
const uri =
"mongodb+srv://shivam:shivam@cluster0.173wr.mongodb.net/payments?retryWrites=true&w=majority";
const client = await MongoClient.connect(uri);
const db = client.db();
const collection = db.collection("checkout");
var data = await collection
.findOne({ paymentId: +payId });
var body = {
cartvalue: data.cartvalue,
currency: data.currency,
merchantId: data.merchant_id
}
var req1 = {
body
}
let checkoutId = await getRapyPayCheckoutId(req1, res);
console.log(checkoutId)
collection.findOneAndUpdate({ paymentId: +payId }, {
$set: {
checkoutId: checkoutId,
isPaid: "true"
}
});
res.json({
checkoutId: checkoutId
});
});
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));