Instamojo payment gateway integration in Laravel

By | February 19, 2019

This easy tutorial is about Instamojo payment gateway integration in Laravel. This is an Indian payment gateway which is widely used by Indian merchants and startups.

There are many payment gateways available for Indian businesses like Razorpay, PayUMoney, CCAvenue and many more.

Also, if you want to check Razorpay and Stripe payment gateway integration then we have already written an article about that.

If you want to integrate payment gateway for Indian customer then Instamojo is the best solution.

So the first thing to follow is to create an instamojo account from here Insta mojo test account.

After the successful integration with the test mode, you can change test keys Api-Key and Auth-Token to live Api-Key and Auth-Token keys.

Create Route:

Now in your route file (Inside routes folder web.php file) you just need to add this three routes.

Route::get('payment', 'PaymentController@index')->name('payment');

Route::post('payment', 'PaymentController@payment')->name('payment');

Route::get('returnurl', 'PaymentController@returnurl')->name('returnurl');

Here we add three routes “payment” for “GET” and “POST” method. And also, we will add “returnurl” for getting a response.

Also we will create a controller file in controller folder (app/Http/Controllers/) named PaymentController.php

create PaymentController.php file and add these three functions in it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use View;
use Session;
use Redirect;

class PaymentController extends Controller
{
    public function __construct()
    {   
    }
    public function index(Request $request)
    { 
        return view('payment');
    }
    public function payment(Request $request)
    { 
        $this->validate($request, [
            'amount' => 'required',
            'purpose' => 'required',
            'buyer_name' => 'required',
            'phone' => 'required',
        ]);
        
        $ch = curl_init();

        // For Live Payment change CURLOPT_URL to https://www.instamojo.com/api/1.1/payment-requests/
       
        curl_setopt($ch, CURLOPT_URL, 'https://test.instamojo.com/api/1.1/payment-requests/');
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_HTTPHEADER,
            array("X-Api-Key:test_ba7110d4436b456238fc630d248",
                "X-Auth-Token:test_8ee7a19348456600fdf9169c9c7"));
        $payload = Array(
            'purpose' => $request->get('purpose'),
            'amount' => $request->get('amount'),
            'phone' => $request->get('phone'),
            'buyer_name' => $request->get('buyer_name'),
            'redirect_url' => url('/returnurl'),
            'send_email' => false,
            'webhook' => 'http://instamojo.com/webhook/',
            'send_sms' => true,
            'email' => 'laracode101@gmail.com',
            'allow_repeated_payments' => false
        );
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
        $response = curl_exec($ch);
        $err = curl_error($ch);
        curl_close($ch); 

        if ($err) {
            \Session::put('error','Payment Failed, Try Again!!');
            return redirect()->back();
        } else {
            $data = json_decode($response);
        }


        if($data->success == true) {
            return redirect($data->payment_request->longurl);
        } else { 
            \Session::put('error','Payment Failed, Try Again!!');
            return redirect()->back();
        }

    }

    public function returnurl(Request $request)
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, 'https://test.instamojo.com/api/1.1/payments/'.$request->get('payment_id'));
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_HTTPHEADER,
            array("X-Api-Key:test_db0a410a56987ea05606074061b",
                "X-Auth-Token:test_3c74aba44420e52746ff056e276"));

        $response = curl_exec($ch);
        $err = curl_error($ch);
        curl_close($ch); 

        if ($err) {
            \Session::put('error','Payment Failed, Try Again!!');
            return redirect()->route('payment');
        } else {
            $data = json_decode($response);
        }
        
        if($data->success == true) {
            if($data->payment->status == 'Credit') {
                
                // From here you can save respose data in database from $data
            

                \Session::put('success','Your payment has been pay successfully, Enjoy!!');
                return redirect()->route('payment');

            } else {
                \Session::put('error','Payment Failed, Try Again!!');
                return redirect()->route('payment');
            }
        } else {
            \Session::put('error','Payment Failed, Try Again!!');
            return redirect()->route('payment');
        }
    }

}	

Create View File:

Now we will create the view file in view folder which is located here “resources/views“. For example, we will create a payment.blade.php file and add this code in it.




<div class="container">
    <div class="row">
        <div class="col-md-12">
            @if($message = Session::get('error'))
            <div class="alert alert-danger alert-dismissible" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
                <strong>Error Alert!</strong> {{ $message }}
            </div>
            @endif
            {!! Session::forget('error') !!}
            @if($message = Session::get('success'))
            <div class="alert alert-success alert-dismissible" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
                <strong>Success Alert!</strong> {{ $message }}
            </div>
            @endif
            {!! Session::forget('success') !!}
        </div>
    </div>
</div>
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="card card-default">
                <div class="card-header">
                    <div class="row">
                        <div class="col-md-10">
                            <strong>Pay With Instamojo</strong>
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    <form class="form-horizontal" method="POST" action="{{ URL::route('payment') }}">
                        @csrf
                        <div class="form-group">
                            <label for="purpose" class="col-md-12 col-form-label">Purpose</label>
                            <div class="col-md-12">
                                <input id="purpose" type="text" class="form-control" name="purpose" value="" required>
                                @if ($errors->has('purpose'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('purpose') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="buyer_name" class="col-md-12 col-form-label">Buyer Name</label>
                            <div class="col-md-12">
                                <input id="buyer_name" type="text" class="form-control" name="buyer_name" value="" required>
                                @if ($errors->has('buyer_name'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('buyer_name') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="amount" class="col-md-12 col-form-label">Amount</label>
                            <div class="col-md-12">
                                <input id="amount" type="number" class="form-control" name="amount" value="" required>
                                @if ($errors->has('amount'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('amount') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="phone" class="col-md-12 col-form-label">Phone No.</label>
                            <div class="col-md-12">
                                <input id="phone" type="number" class="form-control" name="phone" value="" required>
                                @if ($errors->has('phone'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('phone') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-12 col-md-offset-3">
                                <button type="submit" class="btn btn-primary btn-block desabled" id="submitUser">
                                    Submit
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>  
</div>

After successfully test payment you can check the record of payment here https://test.instamojo.com/payments/

Now you can check this payment page via browsing this type of local URL

http://localhost:8000/payment

So now you are done with Instamojo payment gateway integration in Laravel Tada !!

Instamojo payment gateway
Instamojo payment gateway

8 thoughts on “Instamojo payment gateway integration in Laravel

  1. coque iphone

    Very good fantastic content. My spouse and i experimented with that plus it beautifully proved helpful.
    coque iphone

    Reply
  2. top wellness company in india

    Fantastic items from you, man. I have understand your stuff previous to and you’re just extremely magnificent.

    I actually like what you have acquired here, certainly like what
    you’re saying and the way through which you are saying it.
    You make it enjoyable and you continue to take care of to keep it smart.
    I can not wait to learn much more from you.
    That is really a wonderful site.

    Reply
  3. top toy company in india

    Greetings! Very useful advice in this particular article!
    It’s the little changes which will make the biggest changes.
    Thanks a lot for sharing!

    Reply

Leave a Reply

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