-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
271 lines (192 loc) · 7.14 KB
/
app.js
File metadata and controls
271 lines (192 loc) · 7.14 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
const express = require("express");
const cors = require('cors');
const path = require('path');
const admin = require('firebase-admin');
var serviceAccount = require("./serviceaccountkey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
const db = admin.firestore();
const https = require("https");
const qs = require("querystring");
const checksum_lib = require("./Paytm/checksum");
const config = require("./Paytm/config");
var tempuid;
var tempamount;
console.log(tempuid);
const app = express();
app.use(express.static(__dirname + "/views"));
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, 'front')));
const parseUrl = express.urlencoded({ extended: true });
const parseJson = express.json({ extended: true });
const { response, json } = require('express')
const PORT = process.env.PORT || 4000;
app.use(cors(
{
origin: '*',
credentials: true, //access-control-allow-credentials:true
optionSuccessStatus: 200,
}
));
app.get("/", (req, res) => {
// let amount = req.query;
// amount = tempamount;
res.sendFile("index");
});
// let amounttobepaid;
app.get("/paynow", [parseUrl, parseJson], (req, res) => {
// Route for making payment
// const{amount} = req.query;
// totalamount = amount;
let amount = req.query.amount;
tempamount = amount;
let uid = req.query.uid;
tempuid = uid;
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'] = 'customerId_' + new Date().getTime();
params['TXN_AMOUNT'] = amount;
params['CALLBACK_URL'] = `https://localhost:${PORT}/callback`;
params['EMAIL'] = uid;
// params['MOBILE_NO'] = paymentDetails.customerPhone;
// params.body ={
// "TXNToken":{
// "amount" : amount,
// "currency": "INR"
// }
// }
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) => {
// 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);
// 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");
post_res.on('end', () => {
console.log(orderId);
console.log(MID);
console.log('Response: ', response);
response = JSON.parse(response);
res.send(response.body.txnToken);
return 0;
});
var _result = JSON.parse(response);
if (_result != null) {
let tDate = _result.TXNDATE;
let data = {
'orderID': _result.ORDERID,
'tId': _result.TXNID,
'amount': _result.TXNAMOUNT,
'tDate': _result.TXNDATE,
'bname:': _result.BANKNAME,
'status': _result.RESPMSG,
'gateway': _result.GATEWAYNAME,
'bTid': _result.BANKTXNID,
'status': _result.STATUS,
}
db.collection('users').doc(tempuid).collection('payments').doc(tDate).set(data);
if (_result.STATUS == 'TXN_SUCCESS') {
var someDate = new Date();
var numberOfDaysToAdd;
if (tempamount == "1497") {
numberOfDaysToAdd = 90;
} else if (tempamount == "2495") {
numberOfDaysToAdd = 180;
} else if (tempamount = "4999") {
numberOfDaysToAdd = 360
}
var tempresult = someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
var result = new Date(tempresult).toLocaleDateString();
db.collection('users').doc(tempuid).update({
'planDate': _result.TXNDATE,
'currentPlan': tempamount,
'validTill': result,
});
}
}
if (_result.STATUS == 'TXN_SUCCESS') {
res.render('response', { 'data': _result });
// res.send('payment sucess '+'payment sucess '+ _result.TXNID+ _result.ORDERID+ _result.TXNAMOUNT+ _result.GATEWAYNAME+
// _result.BANKNAME+_result.TXNDATE+ _result.RESPMSG)
// const data = response;
// User.add(data);
// res.send("stored successfully"+ result.TXN_AMOUNT);
} else {
res.render('response', { 'data': _result })
}
// routes.get('/paymentinfo', {
// return: {
// "Result" :_result
// }
// })
});
});
// post the data
post_req.write(post_data);
post_req.end();
});
});
});
// routes.post("/data", (req, res)=>{
// })
app.get('*', (req,res)=>{
res.render('error',{
title:"404 Page Not Found.",
sub: "Something Went Wrong. Please check your connection. "
})
})
app.listen(PORT, () => {
console.log(`App is listening on Port ${PORT}`);
});