-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
97 lines (86 loc) · 2.31 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import mongoose from "mongoose";
import express from "express";
import cors from "cors";
import { config } from "dotenv";
import crypto from 'crypto';
import Razorpay from 'razorpay';
config();
export const instance = new Razorpay({
key_id: process.env.RAZORPAY_API_KEY,
key_secret: process.env.RAZORPAY_API_SECRET
});
const schema = new mongoose.Schema({
name: String,
phone: String,
})
const app = express();
app.use(express.json());
app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: false }));
app.use(
cors({
origin: [process.env.FRONTEND_URL],
methods: ["POST"],
credentials: true,
})
)
const Book = mongoose.model("WebDevBooking", schema);
mongoose.connect(process.env.MONGO_URI, {
dbName: "webBooking",
})
.then((c)=> console.log(`Database connected with ${c.connection.host}`))
.catch((e) => console.log(e))
app.listen(process.env.PORT, ()=>{
console.log(`Server is working on port: ${process.env.PORT}`);
});
app.post("/demo", async (req, res)=>{
const {name, phone} = req.body;
try {
await Book.create({ name, phone });
res.status(200).json({
success: true,
});
} catch (error) {
res.status(400).json({
success: false,
});
}
} )
app.get('/', async (req, res) => {
res.send("This is not the main site, it is just the backend.");
})
app.get('/getkey', (req, res)=>{
res.status(200).json({key: process.env.RAZORPAY_API_KEY});
})
app.post('/checkout', async (req, res) => {
const options = {
amount : Number(req.body.amount * 100),
currency: "INR",
};
let order = "";
try{
order = await instance.orders.create(options);
}
catch(error){
res.status(200).json({
success: false,
});
}
res.status(200).json({
success: true,
order
});
})
app.post('/paymentverification', async (req, res) => {
const { razorpay_order_id, razorpay_payment_id, razorpay_signature } = req.body;
const body = razorpay_order_id + "|" + razorpay_payment_id;
const expectedSignature = crypto.createHmac('sha256', process.env.RAZORPAY_API_SECRET).update(body.toString()).digest('hex');
const isAuthentic = expectedSignature === razorpay_signature;
if(isAuthentic){
res.redirect(`${process.env.FRONTEND_URL}/paymentsuccess/${razorpay_payment_id}`);
} else{
res.status(400).json({
success: false
})
}
})