-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
367 lines (286 loc) · 7.39 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//jshint esversion:6
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require('mongoose');
const https = require("https");
const { url } = require("inspector");
const paypal = require('paypal-rest-sdk');
require('dotenv').config({ path: '.env' });
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
// add your username and password in string below
const mongo = process.env.MONGO_AUTH;
//mongoose.connect(mongo, {useNewUrlParser: true});
const donorSchema = {
type : String,
name : String,
email : String,
phone : String,
password :String,
dob : Date,
gender : String,
bloodGroup : String,
rh: String,
previousc: String,
height: String,
weight :String,
previousd : String,
fatherN :String,
motherN :String,
identity :String,
document : Object,
nominee1 : String,
nominee2 :String,
address :String,
pincode :String,
state: String,
terms : Boolean
};
const contactSchema = {
name: String,
email: String,
subject: String,
message: String
};
const donationSchema = {
amount :String,
details : Array
}
const User = mongoose.model("User", donorSchema );
const Contact = mongoose.model("Contact" , contactSchema);
const Donation = mongoose.model("Donation" , donationSchema);
app.post("/form", function(req, res){
const newUser = new User({
type : req.body.type,
name : req.body.name,
email : req.body.email,
phone : req.body.phone,
password :req.body.password,
dob : req.body.dob,
gender : req.body.gender,
bloodGroup : req.body.bloodGroup,
rh: req.body.rh,
previousc: req.body.previousc,
height: req.body.height,
weight :req.body.weight,
previousd : req.body.previousd,
fatherN :req.body.fatherN,
motherN :req.body.motherN,
identity :req.body.identity,
document : req.body.document,
nominee1 : req.body.nominee1,
nominee2 :req.body.nominee2,
address :req.body.address,
pincode :req.body.pincode,
state: req.body.state,
terms : req.body.terms
});
newUser.save(function(err){
if (!err){
res.render("/");
}
else{
res.render("/form");
console.log(err);
}
});
});
app.post("/contact", function(req, res){
const contact = new Contact({
name: req.body.name,
email: req.body.email,
subject: req.body.subject,
message: req.body.message
});
contact.save(function(err){
if (!err){
res.render("contact" ,{
responseHead: "Success",
responseBody: "We will revert back to you within 7 working days"
});
}
else{
res.render("contact" ,{
responseHead: "Failure",
responseBody: "Please try again"
});
}
});
});
app.post("/newsletter",function(request,res){
const email = request.body.email;
const data = {
members:[
{
email_address:email,
status:"subscribed",
merge_fields:{
FNAME:"def",
LNAME:"def"
}
}
]
};
const jsondata = JSON.stringify(data);
const url = process.env.MAILCHIMP_ID;
const options = {
method:"POST",
auth:process.env.MAILCHIMP_AUTH
}
const r = https.request(url, options, function(response){
if((data)==0)
{
res.send("fail");
console.log(data);
console.log(data.error_count);
}
else{
res.send("success")
}
})
r.write(jsondata);
r.end();
});
// paypal
paypal.configure({
'mode': 'sandbox', //sandbox or live
'client_id': process.env.PAYPAL_CLIENT,
'client_secret': process.env.PAYPAL_SECRET
});
app.post('/donateus', (req, res) => {
const create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://localhost:3000/success",
"cancel_url": "http://localhost:3000/cancel"
},
"transactions": [{
"item_list": {
"items": [{
"name": "Redhock Bar Soap",
"sku": "001",
"price": req.body.amount,
"currency": "USD",
"quantity": 1
}]
},
"amount": {
"currency": "USD",
"total": req.body.amount
},
"description": "Donations to donately"
}]
};
paypal.payment.create(create_payment_json, function (error, payment) {
if (error) {
throw error;
} else {
for(let i = 0;i < payment.links.length;i++){
if(payment.links[i].rel === 'approval_url'){
res.redirect(payment.links[i].href);
}
}
}
});
});
app.get('/success', (req, res) => {
const payerId = req.query.PayerID;
const paymentId = req.query.paymentId;
const execute_payment_json = {
"payer_id": payerId,
"transactions": [{
"amount": {
"currency": "USD",
"total": "25.00"
}
}]
};
// Obtains the transaction details from paypal
paypal.payment.execute(paymentId, execute_payment_json, function (error, payment) {
//When error occurs when due to non-existent transaction, throw an error else log the transaction details in the console then send a Success string reposponse to the user.
if (error) {
console.log(error.response);
throw error;
} else {
// console.log(JSON.stringify(payment));
res.send('Success');
const newdonation = new Donation({
details: JSON.stringify(payment)
});
newdonation.save(function(err){
if (!err){
console.log(error);
}
});
// twillo
const accountSid = process.env.TWILLO_SID;
const authToken = process.env.TWILLO_AUTH;
const client = require('twilio')(accountSid, authToken);
client.messages.create({
body: 'there is a new donation',
from: process.env.TWILLO_NUMBER,
to: process.env.MY_NUMBER,
})
.then(message => console.log(message.sid));
}
});
});
app.get('/cancel', (req, res) => res.send('Cancelled'));
app.get("/", function(req, res){
res.render("index");
});
app.get("/choice", function(req, res){
res.render("choice");
});
app.get("/about", function(req, res){
res.render("about");
});
app.get("/login", function(req, res){
res.render("login");
});
app.get("/form", function(req, res){
res.render("form");
});
app.get("/read-more", function(req, res){
res.render("readmore");
});
app.get("/bloodbanks", function(req, res){
res.render("bloodbanks");
});
app.get("/organbanks", function(req, res){
res.render("organbanks");
});
app.get("/hospitals", function(req, res){
res.render("hospitals");
});
app.get("/oxygencylinders", function(req, res){
res.render("oxygencylinders");
});
app.get("/organdonor", function(req, res){
res.render("organdonor");
});
app.get("/blooddonor", function(req, res){
res.render("blooddonor");
});
app.get("/blood_organrecepient", function(req, res){
res.render("blood_organrecepient");
});
app.get("/recepients", function(req, res){
res.render("recepients");
});
app.get("/donateus", function(req, res){
res.render("donateus");
});
let port = process.env.PORT;
if (port == null || port == "") {
port = 3000;
}
app.listen(port, function() {
console.log("Server started on port 3000");
});