Node.js Express Instamojo API Payment Gateway Integration + Invoice Email in PDF Full Tutorial For Beginners 2020

Node.js Express Instamojo API Payment Gateway Integration + Invoice Email in PDF Full Tutorial For Beginners 2020

 

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

 

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

 

 

<!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>

 

See also  JavaScript Program To Check If A Variable Is undefined or null Full Project For Beginners

 

 

 

 

 

 

Now we need to make the post request to the /pay route to initiate the instamojo link

 

 

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

 

 

 

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

See also  JavaScript Program to Replace all Instances of a Character in a String Using Regular Expression Full Project For Beginners

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Leave a Reply