-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
83 lines (71 loc) · 2.22 KB
/
server.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
var admin = require("firebase-admin")
const express = require('express');
const { database } = require("firebase-admin");
require('dotenv').config()
const RazorPay = require("./config/RazorPay")
const app = express()
app.use(express.json())
//Listen on 3000
app.listen(3000, function () {
console.log('Listening on 3000')
})
var serviceAccount = require(process.env.FIREBASE_CREDENTIALS);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://ThePalm.firebaseio.com'
});
console.log("Firebase Initialized...!")
const db = admin.firestore();
async function getRooms(rates) {
const snapshot = await db.collection('Rooms').get();
snapshot.forEach((doc) => {
console.log(doc.id, '=>', doc.data()['rates']);
rates[doc.id] = doc.data()['rates']
});
}
app.post('/getPaymentLink', (req, res) => {
totalCost = 0;
inputs = req.body
//Check inputs
console.log("Inputs:", inputs)
//Fetch prices from Firebase
//var Rates = { Deluxe: '200', Standard: '300', Tent: '500' };
var Rates = {}
getRooms(Rates).then(() => {
console.log("Rates", Rates);
//res.send(Rates)
//Calculate Total Cost
for (roomtype in Rates) {
totalCost = totalCost + (Number(Rates[roomtype]) * Number(inputs[roomtype]))
}
//Razorpay
totalCost = totalCost * 100 //Razorpay takes paise as input
console.log(totalCost)
const params = {
type: "link",
amount: totalCost,
currency: "INR",
description: "The Palm Payment",
customer: {
"name": req.name,
"contact": req.contact,
"email": req.email
},
callback_method: "get",
callback_url: "https://thepalm.netlify.app/#/",
}
RazorPay.invoices.create(params).then((data) => {
//Update on Database
res.status(200).json({
status: "success",
data
})
}).catch((error) => {
res.status(500).json({
status: "fail",
error
})
})
})
//res.send(totalCost.toString());
})