forked from Sourav-Bhadra/ELECTRO-MARKET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
161 lines (127 loc) · 5.42 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
const express = require('express')
const cors = require('cors')
const mongoose = require('mongoose')
// require("dotenv").config();
const cookieParser = require('cookie-parser');
const routes = require('./routes/userRoutes')
const { checkUser } = require('./middleware/authMiddleware')
const qs = require("querystring");
const checksum_lib = require("./Paytm/checksum");
const config = require("./Paytm/config");
const app = express();
app.use(express.static('public'))
app.set('view engine', 'ejs')
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
const parseUrl = express.urlencoded({ extended: false });
const parseJson = express.json({ extended: false });
const PORT = process.env.PORT || 5000;
console.log("Starting Server");
app.listen(PORT, () => console.log(`server started on port :${PORT}`));
const dbURI = 'mongodb+srv://admin:admin@electro-market.gu6z6.mongodb.net/electromarket?retryWrites=true&w=majority';
mongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true })
.then((result) => {
console.log('connected to database')
console.log(`connected to port ${PORT}`)
})
.catch((err) => {
console.log(err)
});
// routes
app.get('*', checkUser)
app.get('/', (req, res) => {
// res.send('hello')
res.render("index")
})
app.post("/paynow", [parseUrl, parseJson], (req, res) => {
// Route for making payment
var paymentDetails = {
amount: req.body.amount,
customerId: req.body.name,
customerEmail: req.body.email,
customerPhone: req.body.phone
}
if(!paymentDetails.amount || !paymentDetails.customerId || !paymentDetails.customerEmail || !paymentDetails.customerPhone) {
res.status(400).send('Payment failed')
} else {
var params = {};
params['MID'] = config.PaytmConfig.mid;
params['WEBSITE'] = config.PaytmConfig.website;
params['CHANNEL_ID'] = 'WEB';
params['INDUSTRY_TYPE_ID'] = 'Retail';
params['ORDER_ID'] = 'TEST_' + new Date().getTime();
params['CUST_ID'] = paymentDetails.customerId;
params['TXN_AMOUNT'] = paymentDetails.amount;
params['CALLBACK_URL'] = 'http://localhost:5000/callback';
params['EMAIL'] = paymentDetails.customerEmail;
params['MOBILE_NO'] = paymentDetails.customerPhone;
checksum_lib.genchecksum(params, config.PaytmConfig.key, function (err, checksum) {
var txn_url = "https://securegw-stage.paytm.in/theia/processTransaction"; // for staging
// var txn_url = "https://securegw.paytm.in/theia/processTransaction"; // for production
var form_fields = "";
for (var x in params) {
form_fields += "<input type='hidden' name='" + x + "' value='" + params[x] + "' >";
}
form_fields += "<input type='hidden' name='CHECKSUMHASH' value='" + checksum + "' >";
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('<html><head><title>Merchant Checkout Page</title></head><body><center><h1>Please do not refresh this page...</h1></center><form method="post" action="' + txn_url + '" name="f1">' + form_fields + '</form><script type="text/javascript">document.f1.submit();</script></body></html>');
res.end();
});
}
});
app.post('/callback', (req, res) => {
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
var html = "";
var post_data = qs.parse(body);
// received params in callback
console.log('Callback Response: ', post_data, "\n");
// verify the checksum
var checksumhash = post_data.CHECKSUMHASH;
// delete post_data.CHECKSUMHASH;
var result = checksum_lib.verifychecksum(post_data, config.PaytmConfig.key, checksumhash);
console.log("Checksum Result => ", result, "\n");
// Send Server-to-Server request to verify Order Status
var params = {"MID": config.PaytmConfig.mid, "ORDERID": post_data.ORDERID};
checksum_lib.genchecksum(params, config.PaytmConfig.key, function (err, checksum) {
params.CHECKSUMHASH = checksum;
post_data = 'JsonData='+JSON.stringify(params);
var options = {
hostname: 'securegw-stage.paytm.in', // for staging
// hostname: 'securegw.paytm.in', // for production
port: 443,
path: '/merchant-status/getTxnStatus',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': post_data.length
}
};
// Set up the request
var response = "";
var post_req = https.request(options, function(post_res) {
post_res.on('data', function (chunk) {
response += chunk;
});
post_res.on('end', function(){
console.log('S2S Response: ', response, "\n");
var _result = JSON.parse(response);
if(_result.STATUS == 'TXN_SUCCESS') {
res.send('payment sucess')
}else {
res.send('payment failed')
}
});
});
// post the data
post_req.write(post_data);
post_req.end();
});
});
})
app.use(routes)