-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
138 lines (124 loc) · 3.81 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
var express = require("express"),
mongoose = require("mongoose"),
passport = require("passport"),
paypal = require("paypal-rest-sdk"),
bodyParser = require("body-parser"),
passportLocal = require("passport-local"),
methodOverride = require("method-override"),
flash = require("connect-flash"),
express = require("express"),
app = express();
// Database
mongoose.connect("mongodb://localhost/kala-mart", {useNewUrlParser: true});
mongoose.set('useFindAndModify', false);
// Payment
paypal.configure({
'mode': 'sandbox', //sandbox or live
'client_id': 'Acq6o6hrLTOS67A4caohHeaBzGkqmB3SSvAkW611BGetcxK417C4br-FUpxkIbWLHJwoCswKFfOZWnN-',
'client_secret': 'ELJVmkBfj7-kd6JTWW_trl0hyDXLqVJQeJwfTMZXUPxixGhUFdxduQ8tFclOXGacyVwlyLCpncOHh-ts'
});
app.post("/pay", function(req, res) {
const create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "https://artisan-ecommerce-sovinnour.c9users.io/success",
"cancel_url": "https://artisan-ecommerce-sovinnour.c9users.io/cancel"
},
"transactions": [{
"item_list": {
"items": [{
"name": 'Jaipur Blue Pottery',
"sku": "item",
"price": 2800,
"currency": "INR",
"quantity": 1
}]
},
"amount": {
"currency": "INR",
"total": 2800
},
"description": 'Tea Set in Jaipur Blue Pottery'
}]
};
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", function(req, res) {
const payerID = req.query.PayerID;
const paymentID = req.query.paymentId;
const execute_payment_json = {
"payer_id": payerID,
"transactions": [{
"amount": {
"currency": "INR",
"total": 500
}
}]
}
paypal.payment.execute(paymentID, execute_payment_json, function(err, payment) {
if(err) {
throw err;
} else {
console.log("Get Payment Response");
console.log(JSON.stringify(payment));
}
})
})
app.get('/cancel', function(req, res) {
res.send("Cancelled!");
})
// Models
var Product = require("./models/product"),
User = require("./models/user"),
Review = require("./models/review"),
Profile = require("./models/profile");
// Routes
var productRoutes = require("./routes/products"),
reviewRoutes = require("./routes/reviews"),
indexRoutes = require("./routes/index"),
profileRoutes = require("./routes/profiles");
// Default Settings
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(methodOverride("_method"));
app.use(flash());
// Passport Config
app.use(require("express-session")({
secret: "abc",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new passportLocal(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(function(req, res, next) {
res.locals.currentUser = req.user;
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next();
});
// Routing
app.use(indexRoutes);
app.use("/products", productRoutes);
app.use("/products/:id/reviews", reviewRoutes);
app.use("/profiles", profileRoutes);
app.use("/profiles/:id/reviews", profileRoutes);
app.listen(process.env.PORT, process.env.IP, function() {
console.log("KalaMart Server is Active!");
});