-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
executable file
·173 lines (132 loc) · 4.59 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
162
163
164
165
166
167
168
169
170
171
172
173
// in sublime
var express = require('express');
var port = process.env.PORT || 3000;
var app = express();
const https = require('https');
const checksum_lib = require('./checksum');
app.use(express.json()); // to support JSON-encoded bodies
app.use(express.urlencoded()); //
app.get('/', function(req, res) {
console.log(req);
res.send(JSON.stringify({ Hello: 'World' }));
});
app.post('/generateTxnToken', function(request, res) {
// console.log(request);
console.log(request.body);
/* initialize an object */
var paytmParams = {};
var MID = request.body.mid;
var orderId = request.body.orderId;
var amount = parseFloat(String(request.body.amount));
var custId = request.body.custId;
var key_secret = request.body.key_secret;
var callbackUrl = request.body.callbackUrl;
var mode = request.body.mode;
var website = request.body.website;
var testing = String(request.body.testing);
console.log(callbackUrl);
console.log(mode);
/* query parameters */
paytmParams.body = {
/* for custom checkout value is 'Payment' and for intelligent router is 'UNI_PAY' */
"requestType": "Payment",
/* Find your MID in your Paytm Dashboard at https://dashboard.paytm.com/next/apikeys */
"mid": MID,
/* Find your Website Name in your Paytm Dashboard at https://dashboard.paytm.com/next/apikeys */
"websiteName": website == undefined ? "DEFAULT" : website,
/* Enter your unique order id */
"orderId": orderId,
/* on completion of transaction, we will send you the response on this URL */
// "callbackUrl": "https://mrdishant.com",
"callbackUrl": callbackUrl,
/* Order Transaction Amount here */
"txnAmount": {
/* Transaction Amount Value */
"value": amount,
/* Transaction Amount Currency */
"currency": "INR",
},
/* Customer Infomation here */
"userInfo": {
/* unique id that belongs to your customer */
"custId": custId,
},
};
console.log("Mode");
console.log(mode);
if (mode == "1") {
console.log("Mode 1 So Net Banking");
paytmParams.body[
"enablePaymentMode"] = [{
"mode": "NET_BANKING",
}]
} else if (mode == "0") {
console.log("Mode 0 So BALANCE");
paytmParams.body[
"enablePaymentMode"] = [{
"mode": "BALANCE",
}]
} else if (mode == "2") {
console.log("Mode 2 So UPI");
paytmParams.body[
"enablePaymentMode"] = [{
"mode": "UPI",
}]
} else if (mode == "3") {
console.log("Mode 3 So CC");
paytmParams.body[
"enablePaymentMode"] = [{
"mode": "CREDIT_CARD"
}]
}
console.log(JSON.stringify(paytmParams));
/**
* Generate checksum by parameters we have in body
* Find your Merchant Key in your Paytm Dashboard at https://dashboard.paytm.com/next/apikeys
*/
checksum_lib.genchecksumbystring(JSON.stringify(paytmParams.body), key_secret, (err, checksum) => {
if (err) {
return;
}
/* head parameters */
paytmParams.head = {
/* put generated checksum value here */
"signature": checksum
};
/* prepare JSON string for request */
var post_data = JSON.stringify(paytmParams);
var options = {
/* for Staging */
/* for Production */
hostname: testing == "0" ? 'securegw-stage.paytm.in' : 'securegw.paytm.in',
port: 443,
path: '/theia/api/v1/initiateTransaction?mid=' + MID + '&orderId=' + orderId,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': post_data.length
}
};
// Set up the request
var response = "";
var post_req = https.request(options, (post_res) => {
post_res.on('data', (chunk) => {
response += chunk;
});
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;
});
});
// post the data
post_req.write(post_data);
post_req.end();
});
});
app.listen(port, function() {
console.log(`Example app listening on port !`);
});