-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
180 lines (156 loc) · 4.65 KB
/
server.js
File metadata and controls
180 lines (156 loc) · 4.65 KB
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
const express = require('express');
const bodyParser = require('body-parser');
const keys = require('./config/keys');
const stripe = require('stripe')(keys.stripeSecretKey);
const handlebar = require('express-handlebars');
const path = require('path');
const app = express();
const paypal = require('paypal-rest-sdk');
const Paytm = require('paytm-sdk');
const paytm = new Paytm('keys.merchantkey');
const cors = require("cors");
const ejs = require("ejs");
const {initPayment, responsePayment} = require("./paytm/services/index");
app.use(cors());
paypal.configure({
'mode' : 'sandbox', //sandbox or live
'client_id' : keys.client_id,
'client_secret': keys.client_secret
});
if(process.env.NODE_ENV === 'production'){
var returnUrl = `${keys.BASE_URL}/success`;
var cancelUrl = `${keys.BASE_URL}/cancel`;
} else{
var returnUrl = "http://localhost:3000/success";
var cancelUrl = "http://localhost:3000/error";
}
//Handlebars Middleware
app.engine('handlebars', handlebar({ defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
//body parser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
// set static folder
app.use(express.static(__dirname+ '/public'));
// index route
app.get('/', (req, res) => {
res.render('index.handlebars',{
stripePublishableKey: keys.stripePublishableKey
});
});
// app.get('/success', (req, res) => {
// res.render('index');
// });
// Charge route
app.post('/charge', (req, res) => {
const amount = 1000;
stripe.customers.create({
email: req.body.stripeEmail,
source: req.body.stripeToken
}).then(customer => stripe.charges.create({
amount,
description: 'Web Development ebook',
currency: 'USD',
customer:customer.id
})).then(charge => res.render('success.handlebars'));
});
app.post('/pay', (req, res) => {
const create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": returnUrl,
"cancel_url": cancelUrl
},
"transactions": [{
"item_list": {
"items": [{
"name": "E-Notes",
"sku": "001",
"price": "10.00",
"currency": "USD",
"quantity": 1
}]
},
"amount": {
"currency": "USD",
"total": "10.00"
},
"description": "Web Development ebook"
}]
};
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;
var execute_payment_json = {
"payer_id": payerId,
"transactions": [{
"amount": {
"currency": "USD",
"total": "10.00"
}
}]
};
paypal.payment.execute(paymentId, execute_payment_json, function (error, payment) {
if (error) {
console.log(error.response);
throw error;
} else {
console.log(JSON.stringify(payment));
res.render("success.handlebars");
}
});
});
app.use(express.static(__dirname + "/views"));
app.set("view engine", "ejs");
app.post("/paytm", (req, res) => {
res.redirect("/paywithpaytm?amount=700");
})
app.get("/paywithpaytm", (req, res) => {
initPayment(req.query.amount).then(
success => {
res.render("paytmRedirect.ejs", {
resultData: success,
paytmFinalUrl: 'https://securegw-stage.paytm.in/theia/processTransaction'
});
},
error => {
res.send(error);
}
);
});
app.post("/paywithpaytmresponse", (req, res) => {
responsePayment(req.body).then(
success => {
res.render("success.handlebars");
},
error => {
res.send(error);
}
);
});
app.get('/cancel', (req, res) => {
res.render("error.handlebars");
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log("--connection open--");
console.log(`server running on port ${port}.....`);
});
// himankpersonal@gmail.com priyanshu
//42424242442424
//7777777777 489871