Last active
September 11, 2020 16:24
-
-
Save amalj07/bdaa89095061cd52fedf617158c69d68 to your computer and use it in GitHub Desktop.
Order creation and Initiate transaction method for Paytm payment gateway
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment