-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
159 lines (130 loc) · 4.18 KB
/
app.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
let express = require("express");
const dotenv = require("dotenv");
const cors = require("cors");
const path = require("path");
const bodyParser = require("body-parser");
const crypto = require("crypto");
const User = require('./user.js');
const connectDB = require("./db");
const Razorpay = require("razorpay");
const shortid = require('shortid');
const sendEmail = require('./email.js');
dotenv.config();
connectDB();
let app = express();
const instance = new Razorpay({
key_id: process.env.ID,
key_secret: process.env.KEY_SECRET,
});
//Middlewares
app.use(cors());
app.use(express.json());
app.use(
bodyParser.urlencoded({
extended: false,
})
);
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.set("view engine", "ejs");
//Routes
app.get("/register", (req, res) => {
res.render("register", { key: process.env.ID });
});
app.post("/api/payment/order", (req, res) => {
params = req.body;
instance.orders
.create(params)
.then((data) => {
res.send({ sub: data, status: "success" });
})
.catch((error) => {
res.send({ sub: error, status: "failed" });
});
});
app.post("/api/payment/verify", (req, res) => {
body = req.body.razorpay_order_id + "|" + req.body.razorpay_payment_id;
var expectedSignature = crypto
.createHmac("sha256", process.env.KEY_SECRET)
.update(body.toString())
.digest("hex");
console.log("sig" + req.body.razorpay_signature);
console.log("sig" + expectedSignature);
var response = { status: "failure" };
if (expectedSignature === req.body.razorpay_signature)
response = { status: "success" };
res.send(response);
});
app.post('/api/payment/findUser', async(req, res) => {
const { email } = req.body;
try {
const user = await User.findOne({"email": email});
if(user) {
res.send({ sub: user, status: "failed"});
} else {
res.send({ status: "success" });
}
} catch(e) {
res.send({ status: "success" });
}
})
app.post('/api/payment/incrementReferral', async(req, res) => {
const { referralCode = "idontknow" } = req.body;
try {
const user = await User.findOne({"referralCode": referralCode});
user.referralCount += 1;
await user.save();
res.send({ status: "success" });
} catch(e) {
res.send({ status: "failed" });
}
})
app.post('/api/payment/register', async (req, res) => {
const {name, email, college} = req.body;
// const referralCode = shortid.generate();
var referralCode = "linuxdiary3";
for(const i of email) {
if(i === '@') break;
referralCode += i;
}
// console.log(params);
try {
const user = await User.create({
name,
email,
college,
referralCode
});
var response = {
error: "true"
}
if(user) {
response = {
name: user.name,
email: user.email,
error: "false",
referralCode: referralCode
}
try {
// var message = `Dear ${user.name},\nWe are reaching out to thank you for registering to our national event - "LINUXDIARY 3.0" that will be held on 3rd and 4th of September, 2022 in Walchand College of Engineering, Sangli. \n\nPlease feel free to share the event as we want as many talent like you.\nYour Referral Code : ${referralCode}\n\nThank you again, and have a great day.\nRegards,\nWalchand College of Engineering.`;
var message = `<p>Dear ${user.name},</p><p>We are reaching out to thank you for registering for our national event - <strong>"LINUXDIARY 3.0"</strong> that will be held on September 3rd and 4th 2022, in Walchand College of Engineering, Sangli.</p><p>Please feel free to share the event as we want as many talented people as possible.</p><p>Your Referral Code : <strong>${referralCode}</strong></p><p>Thank you again, and have a great day.</p><br/><p>Regards,<br/>Walchand Linux Users' Group.</p>`;
console.log(message);
sendEmail(user.email, "Thank You For Registering For LINUXDIARY 3.0", message);
// response = {
// esent: "true"
// }
} catch (err) {
}
}
res.send(response);
} catch(err) {
var response = {
error: "true"
}
res.send(response);
}
})
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log("server started");
});