Welcome folks today in this tutorial we will be integrating the instamojo payment gateway inside our node.js and express
app. All the source code of the application is given below. A step by step youtube video is also shown below.
Get Started
In order to get started we need to install these dependencies which are given below
npm i express
npm i nodemon
npm i instamojo-nodejs
Now create your app.js
file which is the starting point of the node.js application
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 |
const Insta = require("instamojo-nodejs"); const bodyParser = require("body-parser"); const express = require("express"); const API_KEY = "####yourapikey####"; const AUTH_KEY = "###yourauthkey####"; Insta.setKeys(API_KEY, AUTH_KEY); Insta.isSandboxMode(true); const PORT = process.env.PORT || 3000; const app = express(); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); app.get("/", (req, res) => { res.sendfile(__dirname + "/index.html"); }); app.listen(PORT, () => { console.log(`App is listening on ${PORT}`); }); |
Now you need to replace your private keys and auth keys from instamojo dashboard like this
Now you need to make the index.html file for our application
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 |
<!DOCTYPE html> <html> <head> <title>Instamojo Integration in Node.js and Express</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" /> </head> <body> <div class="container"> <h1 class="text-center">Instamojo Payment</h1> <form action="/pay" method="POST"> <div class="form-group"> <input type="text" class="form-control" required name="name" id="" placeholder="name" /> </div> <div class="form-group"> <input type="email" class="form-control" required name="email" id="" placeholder="email" /> </div> <div class="form-group"> <input type="number" class="form-control" required name="amount" id="" placeholder="amount" /> </div> <div class="form-group"> <button class="btn btn-danger btn-block"> Create Payment Link </button> </div> </form> </div> </body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> </html> |
Now we need to make the post
request to the /pay
route to initiate the instamojo link
1 2 3 4 5 6 7 |
var data = new Insta.PaymentData(); const REDIRECT_URL = "http://localhost:3000/success"; data.setRedirectUrl(REDIRECT_URL); data.send_email = "True"; data.purpose = "Test"; // REQUIRED |
In this block of code we are setting the options such as redirect_url and also send_email option and also the purpose
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
app.post("/pay", (req, res) => { var name = req.body.name; var email = req.body.email; var amount = req.body.amount; data.amount = amount; data.name = name; data.email = email; // REQUIRED Insta.createPayment(data, function (error, response) { if (error) { // some error } else { // Payment redirection link at response.payment_request.longurl res.send("Please check your email to make payment") } }); }); |
Now if you give the details and click the button then it will say email is sent to check the payment link of instamojo