-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (79 loc) · 2.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
const express=require("express")
const app=express()
const file=require("fs")
const path=require("path")
const razorpay=require("razorpay")
const crypto=require("crypto")
const shortid = require("shortid")
var {OrderMail}=require("./Mail.js")
var {storeDataToGoogle,storeOrderToGoogle} = require("./Google.js")
var storedata=require("./store.json")
app.listen(3000,()=>{
console.log("server started")
})
var razorpayinstance = new razorpay({
key_id: process.env.keyId,
key_secret: process.env.secretKey,
});
app.set('view-engine','ejs')
app.use(express.static('static'))
app.use(express.urlencoded({ extended:false }));
app.use(express.json());
app.get('/',async(req,res)=>{
res.render("store.ejs",{storeData:storedata})
})
app.post('/payment',async(req,res)=>{
var total=0;
var pricedata={}
try{
await storedata.forEach(x=>{
pricedata[x.quality]=x.price;
})
req.body.data.cartData.forEach(e=>{
if(e.qty==500){
total+=parseInt(pricedata[e.quality])
}else{
total+=parseInt(pricedata[e.quality])*(parseInt(e.qty)+1)
}
})
total=total*100;
var options = {
amount: total,
currency: "INR",
receipt: shortid.generate()
};
razorpayinstance.orders.create(options,async(err, order)=>{
await res.json({"id":order.id})
storeDataToGoogle(req.body.data,order.id)
});
}catch(err){
res.sendStatus(400)
}
})
app.post('/payment/pod',(req,res)=>{
try{
OrderMail(req.body.data,"Cash on Delivery")
storeOrderToGoogle(req.body.data,"cod","uuid")
res.sendStatus(200)
}catch(err){
res.sendStatus(400)
}
})
app.post('/payment/details',async(req,res)=>{
try{
let body=req.body.response.razorpay_order_id + "|" + req.body.response.razorpay_payment_id;
var Signature = crypto.createHmac('sha256', process.env.secretKey).update(body.toString()).digest('hex');
if(Signature==req.body.response.razorpay_signature){
OrderMail(req.body.data,"Pay with Razorpay",req.body.response)
storeOrderToGoogle(req.body.data,"razorpay",req.body.response)
await res.json({"payment":"success"})
}else{
await res.json({"payment":"failed"})
}
}catch(err){
res.sendStatus(400)
}
})
app.get("/:any",(req,res)=>{
res.render("pageNotfound.ejs")
})