How To Integrate Instamojo Payment Gateway in PHP

Throghout PHP Instamojo payment gateway tutorial, you will learn how to integrate instamojo payment gateway in PHP. In this below example we have added simple and easy way to integrate instamojo payment gateway with live demo.

PHP Instamojo Instamojo Payment Gateway

Follow the following steps to integrate instamojo payment gateway in PHP; is as follows:

  • Step 1: Create Instamojo Account & Get Credentials
  • Step 2: Install Instamojo Package
  • Step 3: Create Payment Form
  • Step 4: Make Payment Process
  • Step 5: Create a Success Page

Create Instamojo Account & Get Credentials

First we will generate secret publisher key and secret key from the instamojo. Lets log in or register from the instamojo and then we got the private and and secret key from the instamojo.

If you don’t have private key and secret key. So, first of all, you need to register here https://www.instamojo.com/ and get private and secret key.

Install Instamojo Package

Now you need to install or download the Instamojo PHP package in our project.

composer require instamojo/instamojo-php

Create aPayment Form

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Instamojo Payment Gateway Integrate in PHP</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
    <style>
        .mt40{
            margin-top: 40px;
        }
    </style>
</head>
    <body>
        <div class="container">
        <div class="row">
            <div class="col-lg-12 mt40">
                <div class="card-header" style="background: #0275D8;">
                    <h2>Register for Event</h2>
                </div>
            </div>
        </div>
            <form action="http://localhost/instamojo/payment-proccess.php" method="POST" name="instamojo_payment">
                <div class="row">
                    <div class="col-md-12">
                        <div class="form-group">
                            <strong>Name</strong>
                            <input type="text" name="name" class="form-control" placeholder="Enter Name" required>
                        </div>
                    </div>
                    <div class="col-md-12">
                        <div class="form-group">
                            <strong>Mobile Number</strong>
                            <input type="text" name="mobile_number" class="form-control" placeholder="Enter Mobile Number" maxlength="10" required>
                        </div>
                    </div>
                    <div class="col-md-12">
                        <div class="form-group">
                            <strong>Email Id</strong>
                            <input type="text" name="email" class="form-control" placeholder="Enter Email id" maxlength="50" required>
                        </div>
                    </div>
                    <div class="col-md-12">
                        <div class="form-group">
                            <strong>Event Fees</strong>
                            <input type="text" name="amount" class="form-control" placeholder="" value="100" readonly="">
                        </div>
                    </div>
                    <div class="col-md-12">
                        <button type="submit" class="btn btn-primary">Submit</button>
                    </div>
                </div>
            </form>
        </div>
    </body>
</html>

Make Payment Process

Now, we need to create a new PHP file name payment-proccess.php.
In this file we will use the instamojo package and do further payment process in this file.

<?php
    require_once('vendor/autoload.php');
    $API_KEY = "test_d883b3a8d2bc1adc7a535506713";
    $AUTH_TOKEN = "test_dc229039d2232a260a2df3f7502";
    $URL = "https://test.instamojo.com/api/1.1/";
    $api = new Instamojo\Instamojo($API_KEY, $AUTH_TOKEN, $URL);

    try {
        $response = $api->paymentRequestCreate(array(
            "purpose" => "FIFA 16",
            "amount" => $_POST["amount"],
            "buyer_name" => $_POST["name"],
            "send_email" => true,
            "email" => $_POST["email"],
            "phone" => $_POST["phone"],
            "redirect_url" => "http://localhost/instamojo/payment-success.php"
        ));
        header('Location: ' . $response['longurl']);
        exit();
    } catch (Exception $e) {
        print('Error: ' . $e->getMessage());
    }
?>

Create a Success Page

In the final step, we need to create one file name payment-success.php. Here we will show payment-related info like payer name, email, mobile, the status of payment etc.

<!DOCTYPE html>
<html>
    <head>
        <title>Thank You - Tutsmake</title>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
    </head>
    <body class="">
        <br><br><br><br>
        <article class="bg-secondary mb-3">  
            <div class="card-body text-center">
                <h4 class="text-white">Thank you for payment<br></h4>
                    <?php
                        require_once('vendor/autoload.php');
                        $API_KEY = "test_d883b3a8d2bc1adc7a535506713";
                        $AUTH_TOKEN = "test_dc229039d2232a260a2df3f7502";
                        $URL = "https://test.instamojo.com/api/1.1/";
                        $api = new Instamojo\Instamojo($API_KEY, $AUTH_TOKEN,$URL);
                        $payid = $_GET["payment_request_id"];

                        try {
                            $response = $api->paymentRequestStatus($payid);
                            echo "<h5>Payment ID: " . $response['payments'][0]['payment_id'] . "</h5>" ;
                            echo "<h5>Payment Name: " . $response['payments'][0]['buyer_name'] . "</h5>" ;
                            echo "<h5>Payment Email: " . $response['payments'][0]['buyer_email'] . "</h5>" ;
                            echo "<h5>Payment Mobile: " . $response['payments'][0]['buyer_phone'] . "</h5>" ;
                            echo "<h5>Payment status: " . $response['payments'][0]['status'] . "</h5>" ;
                            echo "<pre>";
                        }
                        catch (Exception $e) {
                            print('Error: ' . $e->getMessage());
                        }
                    ?>
                <br>
                <p><a class="btn btn-warning" target="_blank" href="https://www.tutsmake.com/"> Tutsmake.com  
                <i class="fa fa-window-restore "></i></a></p>
            </div>
        </article>
    </body>
</html>

Hope you enjoy it.

Leave a Comment