-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.js
128 lines (106 loc) · 4.56 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
const express = require("express");
const app = express();
const server = require("http").createServer(app);
const path = require("path");
const bodyparser = require("body-parser");
var PaytmChecksum = require("./PaytmChecksum/PaytmChecksum");
const checksum_lib = require('./Paytm/checksum')
const config = require('./Paytm/config');
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.set('views', path.join(__dirname, 'views'));
app.set("view engine", "ejs");
app.get('/', (req, res) => {
res.render('payment');
})
// app.get('/callback', (req, res) => {
// })
app.post("/callback", (req, res) => {
// Route for verifiying payment
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
var html = "";
var post_data = qs.parse(body);
// verify the checksum
var checksumhash = post_data.CHECKSUMHASH;
// delete post_data.CHECKSUMHASH;
var result = checksum_lib.verifychecksum(post_data, config.PaytmConfig.key, checksumhash);
// 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',
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 () {
var _result = JSON.parse(response);
if (_result.STATUS == 'TXN_SUCCESS') {
res.send('payment sucess')
res.redirect('/');
} else {
res.send('payment failed')
res.redirect('/paras');
}
});
});
// post the data
post_req.write(post_data);
post_req.end();
});
});
});
app.post('/payment', (req, res) => {
var paymentDetails = {
amount: req.body.amount,
customerId: req.body.id,
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_001' + new Date().getTime();
params['CUST_ID'] = paymentDetails.customerId;
params['TXN_AMOUNT'] = paymentDetails.amount;
params['CALLBACK_URL'] = 'http://localhost:3000/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.listen(3000, () => {
console.log('connected')
})