Integrating Paytm payment gateway using Node.js

Online payment has become a common thing now. There are many websites which use payment gateways. Sometimes we have to integrate the payment gateway on our website as well. Here I will show you how to integrate Paytm payment gateway on our website using Node.js. For the demo payment gateway visit,

Photo by Paul Felberbauer on Unsplash


For using Paytm payment gateway, you’ll need a developer account. If you don’t have you can create one here. After getting into the account, click on developer settings at bottom of the left navigation pane. click on generate API keys, you’ll get your test keys. For production keys, you’ll need to activate the account.

Let’s build the app

Since this tutorial is backend oriented, I won’t be telling much about fronted. We are not using any frameworks or libraries here, so you don’t want to worry about frontend. The frontend is written using just HTML and CSS. Below is the project structure.

First, create a file called index.html for our frontend. Since we are not focusing on the frontend, I won’t be detailing about frontend. Copy the following code into index.html

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<form action="/paynow" method="POST">
<label for="email">Email:</label>
<input type="email" name="email">
<label for="phone">Phone:</label>
<input type="text" name="phone">
<label for="amount">Amount:</label>
<input type="text" name="amount">
<button type="submit">Submit</button>

This code will generate a basic form which submits the data to /paynow route.

For this application, we are not using any dependencies. we’re going to build it in pure Node.js.

Our app should have two basic routes

  • /paynow -for initializing the transaction
  • /callback -for receiving the response from Paytm after the transaction.

Create a file called index.js . All code will be residing in this file.

import the modules needed for our project.

const http = require('http')
const https = require('https')
const path = require('path')
const fs = require('fs')
const qs = require('querystring')

Initialize the server and create a route for serving our HTML form. we will be using Node’s builtin http.createServer method for creating the server.

Add this to your index.js file.

const server = http.createServer()server.on('request', (req, res) => {
switch (req.url) {
case "/":
fs.readFile(path.join(__dirname + '/index.html'), (err, data) => {
if (err) {
case "/paynow":
//Handle order creation and initiate transaction here
case "/callback":
//Handle callback and reponse verification here
server.listen(3000, 'localhost', () => {
console.log("Server listening on port: 3000")

The above code creates a server using Node’s built-in http.createServer method. the server listens for requests on port 3000. if we visit http://localhost:3000, we will get the HTML form served in our browser.

After the routes have been created, create a folder called config(you can give any name). Inside configcreate two files called checksum.js and config.js . Inside config.js copy the following code.

var PaytmConfig = {
website: "XXXXXXXXXX"
module.exports.PaytmConfig = PaytmConfig

Update the above details with the test API keys. you can get the test API keys from your developer account. Inside the checksum.js file copy the code from here. It is for checksum generation and validation.

As we said above we now need to update the two routes /paynow and /callback . These routes will be POST routes. When we click the submit button the data will be posted to /paynow route where we create our order and initializes our transaction. copy the following code into /paynow route.

In the above code, we listen for request and when the user clicks the button we parse the query string into data variable. Then we create an order and generate a txnToken by calling Initiate Transaction API. we create an object called paytmParams for generating the checksum hash to avoid tampering.

To know more about checksum generation and validation visit this link. You can get code snippets and Github links for the checksum utility from the above link provided.

We pass the paytmParams and the merchant key to the generateSignature method. It returns a checksum hash. Then we call the https.request method with paytmParams as the post data to receive the txnToken . then we auto submit an HTML form with mid, orderId and txnToken as the payload which redirects us to Paytm's payment gateway. Here the user fills the payment details and redirected to bank page for authorization. After the transaction is completed Paytm returns the status to our website via the callback URL. You can see the response attributes here.

After we get the response we need to verify the checksum hash in the /callback route. For verifying the checksum we need to call the isVerifySignature method. if the checksum is verified correctly then we call a Server to Server request to verify the transaction status using the Transaction Status API. Below is the code for transaction verification.

In the above code, we call the isVerifySignature method for verifying the checksum. If the checksum verification pass then we call a Server to Server request using the transaction status API. This status of this request will be considered as the final status. we will send a payment success message if the status is TXN_SUCCESS else we will send a payment failed message. You can change the failure message and send the reason for failure using the response code (for the time being I,m not doing that). You can get the list of response code here.

Wrapping Up

Congrats! you have integrated Paytm payment gateway in your application.

If you need the full code, you can get it from this GitHub Repo.

If you have any doubts or need any further clarifications, let me know in the comment section. I’m happy to help you.

If you find this article helpful or interesting, consider giving a clap.


Node.js | Golang | Developer |