PHP 7 Instamojo API Payment Gateway Integration Using PHP SDK Full Tutorial For Beginners 2020

You are currently viewing PHP 7 Instamojo API Payment Gateway Integration Using PHP SDK Full Tutorial For Beginners 2020
  • Post author:
  • Post category:PHP
  • Post comments:0 Comments

 

Welcome folks today in this tutorial we will be integrating instamojo payment gateway in php 7 using instamojo php sdk. All the source code of the tutorial will be given below. A step by step youtube video is also shown below.

 

 

 

 

 

 

 

Get Started

 

 

In order to get started first of all you need to install this dependency either by composer or download the library from below source code

 

`

$ php composer.phar require instamojo/instamojo-php

 

 

 

After this make a index.html and copy paste this code to make a simple html form to collect payment

 

index.html

 

 

<!DOCTYPE html>
<html>
  <head>
    <title>Instamojo Integration in PHP</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.php" 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="text"
              class="form-control"
              required
              name="purpose"
              id=""
              placeholder="purpose"
            />
          </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 will be making the POST request which is there inside the form. So for that make the pay.php file inside the root directory of your project and copy paste the following code to it.

 

 

<?php
require('./instamojo.php');
const API_KEY ="####yourapikey####";
const AUTH_TOKEN = "####yourauthtoken####";


if(isset($_POST['purpose']) && isset($_POST['name']) && isset($_POST['email']) && isset($_POST['amount']))
{
    $api = new Instamojo\Instamojo(API_KEY, AUTH_TOKEN,'https://test.instamojo.com/api/1.1/');
    
    try {
        $response = $api->paymentRequestCreate(array(
            "purpose" => $_POST['purpose'],
            "buyer_name" => $_POST['name'],
            "amount" => $_POST['amount'],
            "send_email" => true,
            "email" => $_POST['email'],
            "redirect_url" => "http://localhost/instamojopayment/success.html"
            ));
        header('Location:'. $response['longurl']);
    }
    catch (Exception $e) {
        print('Error: ' . $e->getMessage());
    }
}
?>

 

 

 

So now if you run this application and after submitting the html form it should redirect to this payment page of instamojo

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

DOWNLOAD SOURCE CODE

 

 

 

Leave a Reply