-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
202 lines (163 loc) · 7.48 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
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
const http = require('http')
const https = require('https')
const path = require('path')
const fs = require('fs')
const qs = require('querystring')
// Import paytm checksum utility
const PaytmChecksum = require('./config/cheksum')
const PaytmConfig = require('./config/config')
const server = http.createServer()
server.on('request', (req, res) => {
switch (req.url) {
case "/":
fs.readFile(path.join(__dirname + '/index.html'), (err, data) => {
if (err) {
res.writeHead(404)
res.end(JSON.stringify(err))
return
}
res.writeHead(200)
res.end(data)
})
break
case "/paynow":
let body = ''
const orderId = 'TEST_' + new Date().getTime()
req.on('error', (err) => {
console.error(err.stack)
}).on('data', (chunk) => {
body += chunk
}).on('end', () => {
let data = qs.parse(body)
const paytmParams = {}
paytmParams.body = {
"requestType": "Payment",
"mid": PaytmConfig.PaytmConfig.mid,
"websiteName": PaytmConfig.PaytmConfig.website,
"orderId": orderId,
"callbackUrl": "http://localhost:3000/callback",
"txnAmount": {
"value": data.amount,
"currency": "INR",
},
"userInfo": {
"custId": data.email,
},
};
PaytmChecksum.generateSignature(JSON.stringify(paytmParams.body), PaytmConfig.PaytmConfig.key).then(function (checksum) {
paytmParams.head = {
"signature": checksum
};
var post_data = JSON.stringify(paytmParams);
var options = {
/* for Staging */
hostname: 'securegw-stage.paytm.in',
/* for Production */
// hostname: 'securegw.paytm.in',
port: 443,
path: `/theia/api/v1/initiateTransaction?mid=${PaytmConfig.PaytmConfig.mid}&orderId=${orderId}`,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': post_data.length
}
};
var response = "";
var post_req = https.request(options, function (post_res) {
post_res.on('data', function (chunk) {
response += chunk;
});
post_res.on('end', function () {
response = JSON.parse(response)
console.log('txnToken:', response);
res.writeHead(200, { 'Content-Type': 'text/html' })
res.write(`<html>
<head>
<title>Show Payment Page</title>
</head>
<body>
<center>
<h1>Please do not refresh this page...</h1>
</center>
<form method="post" action="https://securegw-stage.paytm.in/theia/api/v1/showPaymentPage?mid=${PaytmConfig.PaytmConfig.mid}&orderId=${orderId}" name="paytm">
<table border="1">
<tbody>
<input type="hidden" name="mid" value="${PaytmConfig.PaytmConfig.mid}">
<input type="hidden" name="orderId" value="${orderId}">
<input type="hidden" name="txnToken" value="${response.body.txnToken}">
</tbody>
</table>
<script type="text/javascript"> document.paytm.submit(); </script>
</form>
</body>
</html>`)
res.end()
});
});
post_req.write(post_data);
post_req.end();
});
})
break
case '/callback':
let callbackResponse = ''
req.on('error', (err) => {
console.error(err.stack)
}).on('data', (chunk) => {
callbackResponse += chunk
}).on('end', () => {
let data = qs.parse(callbackResponse)
console.log(data)
data = JSON.parse(JSON.stringify(data))
const paytmChecksum = data.CHECKSUMHASH
var isVerifySignature = PaytmChecksum.verifySignature(data, PaytmConfig.PaytmConfig.key, paytmChecksum)
if (isVerifySignature) {
console.log("Checksum Matched");
var paytmParams = {};
paytmParams.body = {
"mid": PaytmConfig.PaytmConfig.mid,
"orderId": data.ORDERID,
};
PaytmChecksum.generateSignature(JSON.stringify(paytmParams.body), PaytmConfig.PaytmConfig.key).then(function (checksum) {
paytmParams.head = {
"signature": checksum
};
var post_data = JSON.stringify(paytmParams);
var options = {
/* for Staging */
hostname: 'securegw-stage.paytm.in',
/* for Production */
// hostname: 'securegw.paytm.in',
port: 443,
path: '/v3/order/status',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'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('Response: ', response);
res.write(response)
res.end()
});
});
// post the data
post_req.write(post_data);
post_req.end();
});
} else {
console.log("Checksum Mismatched");
}
})
}
})
server.listen(3000, 'localhost', () => {
console.log("Server listening on port: 3000")
})