-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
146 lines (124 loc) · 4.35 KB
/
index.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
const express = require("express");
const path = require("path");
const https = require("https");
const qs = require("querystring");
const ejs = require("ejs");
const app = express();
require("dotenv").config();
// Middleware for body parsing
const parseUrl = express.urlencoded({ extended: false });
const parseJson = express.json({ extended: false });
// Set the view engine to ejs
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
const checksum_lib = require("./Paytm/checksum");
const config = require("./Paytm/config");
const { response } = require("express");
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname + "/index.html"));
});
app.post("/paynow", [parseUrl, parseJson], (req, res) => {
if (!req.body.amount || !req.body.email || !req.body.phone) {
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"] = "customer_001";
params["TXN_AMOUNT"] = req.body.amount.toString();
params["CALLBACK_URL"] = "http://localhost:3000/callback";
params["EMAIL"] = req.body.email;
params["MOBILE_NO"] = req.body.phone.toString();
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);
res.render("response", {
data: _result,
});
});
});
// post the data
post_req.write(post_data);
post_req.end();
}
);
});
});
const port = 3000;
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});