Instamojo Payment Gateway Integration in PHP

Instamojo provides PHP SDK for developers which helps developers to integrate Instamojo payment gateway in PHP applications. In this tutorial, you will learn how to integrate Instamojo payment gateway in PHP.

How to Integrate Instamojo Payment Gateway in PHP?

Steps to integrate Instamojo payment gateway:

1. Create Instamojo Account Get Secret Credentials

First of all, we need to secret the publisher key and secret key from the instamojo. So we need to log in or register from the Instamojo and then we get the private 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.

If you have already registered with instamojo, so click this link and login on instamojo and get the private and secret key here.

2. Install Instamojo Package Via Composer

In the second step, we need to install or download the Instamojo PHP package for your project, So go to the command prompt and follow the below commands:

first command

cd/your_project_root_directory

Then Run the below command to install or download the Instamojo package for PHP.

composer require instamojo/instamojo-php

After you the above command, you will see on command prompt screen look like:

3. Create a Payment Form

Next step, we need to create one view file name index.php, Where we will show the payment form.

After that you need to update the below code into your index.php file.

<!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 - Tutsmake.com</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>

4. 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.

Now, you need to update the below code into your 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());
    }
?>

Note:- Once got payment success it gets redirected to success you page (redirect_url)

5. 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.

In other words, after completing the payment process, the response we receive from instamojo prints in this page.

<!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>
	    <br><br><br>
	</article>
<br><br><br>
</article>
</body>
</html>

Recommended PHP Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *