Paytm Integration using nodejs |
This article is for every nodejs developer who is a beginner and keen to know how to integrate paytm wallet using nodejs in their project. This article will aware of the general payment flow and its interaction with the production server. We will take the help of some relevant links along with the topic. So Let’s integrate the paytm wallet in your project.
So guys as you know that while implementing any payment gateway, we have to obtain the merchant key and few basic details which will help us to integrate the paytm into our project. so let's do that in the first step.
If you are already having the merchant id and key then you can skip to the Second Step.
First Step:
We have to register an account with paytm.com, you can Click here to start registration with paytm.
After the successful registration, you will receive an email including Staging credentials such as below
1. Staging MID (Merchant Id)
2. Merchant Key
3. Industry Type
4. Website Name
5. Channel ID
6. App Name
The above details will help us to integrate the paytm wallet in our project.
Depending on your programming language, you can Click here to download the paytm integration toolkit from GitHub.
As here we are using the nodejs so Click here to download the paytm integration toolkit for nodejs
Second Step:
Just extract the downloaded folder and you will found checksum folder, which will help us to create the checksum to make a request to paytm.- The checksum.js file contains genchecksum() and verifychecksum() functions. The merchant module should call these methods with the appropriate set of parameters as mentioned in the API.
- Keep all the files in the folder from where you will be calling the genchecksum() and verifychecksum() methods.
So now our project structure will be likewise:
As given in the above image you can structure your files accordingly. Now we will have a look one by one for each file.
1. config folder
In the config.js file, we will keep the paytm config data and export all the essential keys.'use strict'; var PAYTM_STAG_URL = 'https://pguat.paytm.com'; var PAYTM_PROD_URL = 'https://secure.paytm.in'; var MID = 'LIVESCRIPT50235428543110'; var PAYTM_ENVIORMENT = 'TEST'; // PROD FOR PRODUCTION var PAYTM_MERCHANT_KEY = 'MERCHANT_KEYS'; var WEBSITE = 'WEBSTAGING '; var CHANNEL_ID = 'WEB'; var INDUSTRY_TYPE_ID = 'Retail'; var PAYTM_FINAL_URL = ''; if (PAYTM_ENVIORMENT== 'TEST') { PAYTM_FINAL_URL = 'https://securegw-stage.paytm.in/theia/processTransaction'; }else{ PAYTM_FINAL_URL = 'https://securegw.paytm.in/theia/processTransaction'; } module.exports = { MID: MID, PAYTM_MERCHANT_KEY :PAYTM_MERCHANT_KEY, PAYTM_FINAL_URL :PAYTM_FINAL_URL, WEBSITE: WEBSITE, CHANNEL_ID: CHANNEL_ID, INDUSTRY_TYPE_ID: INDUSTRY_TYPE_ID };
2. model folder
In the model folder, we will keep the checksum.js and crypt.js file, which previously we had downloaded for nodejs.
3. route folder
The route folder will consist of the 3 major files which are as below:
a. pgredirect.js
b. response.js
c. texttxn.js
The pgredirect.js file is responsible for redirecting our
merchant cart page to the paytm wallet page for making
transaction.
var checksum = require('../model/checksum'); module.exports = function (app) { app.get('/pgredirect', function(req,res){ console.log("in pgdirect"); res.render('pgredirect.ejs'); }); };
b. response.js
The response.js file will handle the response data from which will be
posted from the paytm page.
we have to again verify the checksum data when we will receive the
response from paytm page, if it is valid checksum then we can make
sure that the response data is valid.
var checksum = require('../model/checksum'); var config = require('../config/config'); module.exports = function (app) { app.post('/response', function(req,res){ console.log("in response post"); var paramlist = req.body; var paramarray = new Array(); console.log(paramlist); if(checksum.verifychecksum(paramlist, config.PAYTM_MERCHANT_KEY)) { console.log("true"); res. render('response.ejs',{ 'restdata' : "true" ,'paramlist' : paramlist}); }else { console.log("false"); res.render('response.ejs',{ 'restdata' : "false" , 'paramlist' : paramlist}); }; }); };
c. texttxn.js
This file will produce the HTML page from where you can update your cart details such as order no, customer id, and transaction amount.For more understanding of the above form fields and their data type you can simply Click here
Once you submit the above form, it will be posted on app.post('/testtxn') API, this API will accept the form fields and prepare the unique checksum and will redirect to the paytm wallet page for the transaction.
var checksum = require('../model/checksum'); var config = require('../config/config'); module.exports = function (app) { app.get('/testtxn', function(req,res){ res.render('testtxn.ejs',{'config' : config}); }); app.post('/testtxn',function(req, res) { console.log("POST Order start"); var paramlist = req.body; var paramarray = new Array(); console.log(paramlist); for (name in paramlist) { if (name == 'PAYTM_MERCHANT_KEY') { var PAYTM_MERCHANT_KEY = paramlist[name] ; }else{ paramarray[name] = paramlist[name] ; } } console.log(paramarray); paramarray['CALLBACK_URL'] = 'http://localhost:3000/response'; // in case if you want to send callback console.log(PAYTM_MERCHANT_KEY); checksum.genchecksum(paramarray, PAYTM_MERCHANT_KEY, function (err, result) { console.log(result); res.render('pgredirect.ejs',{ 'restdata' : result }); }); console.log("POST Order end"); }); };
4. views folder
The views folder includes the 3 major view files which will be used for displaying the data on a web page.
a. pgredirect.js
<html> <head> <title>Merchant Check Out Page</title> </head> <body> <center><h1>Please do not refresh this page...</h1></center> <form method="post" action="https://pguat.paytm.com/oltp-web/processTransaction" name="f1"> <table border="1"> <tbody> <% for (name in restdata) { console.log(name); console.log(restdata[name] ); %> <input type="hidden" name='<%= name %>' value='<%= restdata[name] %>'> <%}%> </tbody> </table> <script type="text/javascript"> document.f1.submit(); </script> </form> </body> </html>
b. response.js
<html> <head> <title>Merchant Check Out Page</title> </head> <body> <%if(restdata == 'true'){%> <h1>validation succesfull</h1> <%}else{%> <h1>validation unsuccesfull</h1> <%}%> <form method="post" name="f1"> <table border="1"> <tbody> <tr> <th>Label</th> <th>Value</th> </tr> <%for (name in paramlist){%> <tr> <td><label><%= name %></label></td> <td> <input id='<%= name %>'tabindex="1" maxlength="30" size="30" name='<%= name %>' autocomplete="off" value='<%= paramlist[name] %>'> </td> </tr> <%}%> </tbody> </table> <script type="text/javascript"> //document.f1.submit(); </script> </form> </body> </html>
c. texttxn.js
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Merchant Page</title> <meta name="GENERATOR" content="Livescript.in Merchant Pages"> </head> <body> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $('#user-submit').click(function () { //var payload = $('#form1').serializeArray(); alert('Are you sure to submit'); }); }); }); </script> <h1>Merchant Page</h1> <form id="form1" method="post" action="#"> <table border="1"> <tbody> <tr> <th>S.No</th> <th>Label</th> <th>Value</th> </tr> <tr> <td>1</td> <td><label>ORDER_ID::*</label></td> <td><input id="ORDER_ID" tabindex="1" maxlength="20" size="20" name="ORDER_ID" autocomplete="off" value="ODR001"> </td> </tr> <tr> <td>2</td> <td><label>CUSTID ::*</label></td> <td><input id="CUST_ID" tabindex="2" maxlength="12" size="12" name="CUST_ID" autocomplete="off" value="CUST001"></td> </tr> <tr> <td>3</td> <td><label>INDUSTRY_TYPE_ID ::*</label></td> <td><input id="INDUSTRY_TYPE_ID" tabindex="4" maxlength="12" size="12" name="INDUSTRY_TYPE_ID" autocomplete="off" value='<%= config.INDUSTRY_TYPE_ID %>'></td> </tr> <tr> <td>4</td> <td><label>CHANNEL ID ::*</label></td> <td><input id="CHANNEL_ID" tabindex="4" maxlength="12" size="12" name="CHANNEL_ID" autocomplete="off" value='<%= config.CHANNEL_ID %>'> </td> </tr> <tr> <td>5</td> <td><label>TRANSACTION AMOUNT*</label></td> <td><input title="TXN_AMOUNT" tabindex="10" type="text" name="TXN_AMOUNT" value="1.00"> </td> </tr> <tr> <td>6</td> <td><label>MERCHANT ID</label></td> <td><input title="MID" tabindex="10" type="text" name="MID" value='<%= config.MID %>'"> </td> </tr> <tr> <td>7</td> <td><label>WEBSITE</label></td> <td><input title="WEBSITE" tabindex="10" type="text" name="WEBSITE" value='<%= config.WEBSITE %>'></td> </tr> <tr> <td>8</td> <td><label>MERCHANT_KEY</label></td> <td><input title="WEBSITE" tabindex="10" type="text" name="PAYTM_MERCHANT_KEY" value='<%= config.PAYTM_MERCHANT_KEY %>'></td> </tr> <tr> <td></td> <td></td> <td><input value="CheckOut" type="submit" id ="user-submit" ></td> </tr> </tbody> </table> * - Mandatory Fields </form> </body> </html>
5. app.js file
This app.js file used here for the main entry point in the
application.
var express = require("express") , redirect = require("express-redirect"); var app = express(); redirect(app); var router = express.Router(); var bodyParser = require('body-parser'); var server = require('http').createServer(app); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true})); server.listen(3000, function () { var host = server.address().address; var port = server.address().port; console.log('Example app listening at http://%s:%s', host, port); }); app.use(router); require('./routes/testtxn')(app); require('./routes/pgredirect')(app); require('./routes/response')(app); app.use(express.static(__dirname + '/public')); app.set('views', __dirname + '/views'); app.set('view engine', 'ejs');
6. package.json file
All npm packages contain a file, usually in the project root,
called package.json - this file holds various metadata relevant to the project. This the file is used to give information to npm that allows it to identify the
project as well as handle the project's dependencies
{ "name": "paytm-integration-using-express-nodejs", "description": "paytm-integration-using-express-nodejs", "version": "0.0.1", "engines": { "node": "0.10.24", "npm": "2.14.0" }, "dependencies": { "async": "^1.5.2", "body-parser": "^1.15.0", "cookie-parser": "^1.4.1", "cron": "^1.1.0", "ejs": "^2.4.1", "express": "^4.13.4", "express-myconnection": "^1.0.4", "multer": "^1.1.0", "mysql": "^2.10.2", "nodemailer": "0.7.1" } }
That's ALL,
Stay tuned for more nodejs article.
Thank you, guys.
i found 404 error with post this link https://pguat.paytm.com/oltp-web/processTransaction
ReplyDeleteSorry for late reply. Kindly check with paytm website for correct URL.
DeleteEven I am getting same error i found 404 error with post this link https://pguat.paytm.com/oltp-web/processTransaction
ReplyDelete404 Not Found
nginx/1.6.2
I am also getting 404 with https://pguat.paytm.com/oltp-web/processTransaction url
DeleteIn callback url how to save user info in db as req.user not present for that request.
ReplyDeleteHey, You can save the all users details before proceeding ahead to the cart page, and this time you should save all information including the txn_id, amt etc and all others details
DeleteI proceeded, I got the form/table and from there it goes to Paytm server page as expected, BUT It says MID and Order_ID is missing (not passsed) correctly So I wanted to ask that can u dounle check whether your EJS tagging JS Var is correct?
ReplyDelete@Mahir, This is for demonstration purpose. You can do double check while you receive back all the response from server.
DeleteAwesome article, it was exceptionally helpful! I simply began in this and I'm becoming more acquainted with it better! Cheers, keep doing awesome! genuine leather messenger laptop bag
ReplyDeleteThanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info. Small leather goods manufacturer Spain Ubrique
ReplyDeletebruh
ReplyDeleteYes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!Thanks how to start a payment gateway company
ReplyDeleteThe vast majority normal around 16 thick plastic cards in their wallet, ideally your Dad doesn't convey all that in his wallet like George Costanza did in Seinfeld TV show-recollect the detonating wallet Seinfeld episode?
ReplyDeleteNFT Calendar