Integrating Paytm payment gateway using Node.js

Photo by Paul Felberbauer on Unsplash

Introduction

Let’s build the app

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>
<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>
</form>
</div>
</body>
</html>

Let’s build the backend

  • /paynow -for initializing the transaction
  • /callback -for receiving the response from Paytm after the transaction.
const http = require('http')
const https = require('https')
const path = require('path')
const fs = require('fs')
const qs = require('querystring')
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":
//Handle order creation and initiate transaction here
break
})
case "/callback":
//Handle callback and reponse verification here
break
})
server.listen(3000, 'localhost', () => {
console.log("Server listening on port: 3000")
})
var PaytmConfig = {
mid: "XXXXXXXXXXXXXXXXXXXX",
key: "XXXXXXXXXXXXXXXX",
website: "XXXXXXXXXX"
}
module.exports.PaytmConfig = PaytmConfig

Wrapping Up

 by the author.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store