Laravel 9 Instamojo Payment Gateway Integration Tutorial

Laravel 9 Instamojo Payment Gateway Integration Tutorial

Last Updated: 30 Nov, 2022

Dear friends, greetings. In this tutorial, we will learn How to Integrate Instamojo Payment Gateway in Laravel 9 Application. To acheive this, we will be using instamojo API. Follow the below mentioned step-by-step guidance.

How to Integrate Instamojo Payment Gateway in Laravel 9?

  • Step 1: Install Fresh Laravel 9 Application
  • Step 2: Configure Database Connection
  • Step 3: Configure Instamojo Credentials
  • Step 4: Create Database Migration & Model
  • Step 5: Create Controller
  • Step 6: Create Route
  • Step 7: Create View Pages (Blade Files)
  • Step 8: Run Laravel Application

Now, we will go through the above given steps one-by-one to and get it done.

Step 1: Install Fresh Laravel 9 Application

First of all, lets install a fresh laravel 9 application. Open your terminal and run the following command:

composer create-project laravel/laravel laravel9app

Step 2: Configure Database Connection

Next, lets update database configuration in .env file as follows:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel9db
DB_USERNAME=dbusername
DB_PASSWORD=dbpassword

Step 3: Configure Instamojo Credentials

Next, go to https://www.instamojo.com/ website and login to your account. If you do not have your account created, you can create a new account. You can get your API_KEY and AUTH_TOKEN from your instamojo panel and configure it in .env file as follows:

INSTAMOJO_API_KEY=test_ku34d3a8dmm5drdc7a5g4f067w2
INSTAMOJO_AUTH_TOKEN=test_gd329039dt432a2yt34df3kj56d
INSTAMOJO_ENDPOINT=https://test.instamojo.com/api/1.1/

Next, open app/config/services.php file and copy the below given code in it:

<?php
...
...
...

'instamojo' => [
    'api_key' => env('INSTAMOJO_API_KEY'),
    'auth_token' => env('INSTAMOJO_AUTH_TOKEN'),
    'endpoint' => env('INSTAMOJO_ENDPOINT'),
],

Step 4: Create Database Model & Migration

Next, we need to create model and migration files to capture and store payment details in it. Run below command in therminal:

php artisan make:model InstamojoPayment -m

Above command will create a model file named InstamojoPayment.php and a migration file named databse/migrations/*_create_instamojo_payments_table.php. Open the migration file and copy below code in it:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('instamojo_payments', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email');
            $table->string('mobile');
            $table->string('amount');
            $table->string('purpose');
            $table->string('payment_request_id');
            $table->string('payment_link');
            $table->string('payment_status')->nullable();
            $table->string('payment_id')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('instamojo_payments');
    }
};

Next, run the below command to migrate the table:

php artisan migrate

Step 5: Create Controller

Next, create app/Http/Controllers/InstamojoPaymentController.php file and copy below given code in it:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Validator;

use App\Models\InstamojoPayment;

class InstamojoPaymentController extends Controller
{
    protected $rpp = 10;

    public function index(Request $request)
    {
        $request_data = $request->all();

        $view_data = [];
        
        if (!$request->ajax()){

            $view_data['title'] = 'Payment List';
            return view('instamojo.payments.index')->with($view_data);

        } else {

            $page = $request_data['page'];
            $view_data['page'] = $page;
        
            $records = InstamojoPayment::orderBy('created_at', 'desc')->paginate($this->rpp);
            
            $record_starts = $this->rpp * ($page - 1) + 1;
            $record_ends = $this->rpp * ($page - 1) + count($records);
            
            $view_data['records'] = $records;
            $view_data['record_starts'] = $record_starts;
            $view_data['record_ends'] = $record_ends;
            return view('instamojo.payments.list')->with($view_data);
        }
    }

    public function create(Request $request)
    {
        $view_data['title'] = 'Create New Payment';
        return view('instamojo.payments.create')->with($view_data);
    }

    public function store(Request $request)
    {
        $posted_data = $request->all();

        $validator = Validator::make($posted_data, array(
            'name' => 'required|min:3',
            'email' => 'required',
            'mobile' => 'required',
            'amount' => 'required'
        ), array(
            'name.required' => 'Enter your name.',
            'name.min' => 'Name must be min of 3 characters.',
            'email.required' => 'Enter your email address.',
            'mobile.required' => 'Enter your mobile number.',
            'amount.required' => 'Enter your amount.'
        ));

        if ($validator->fails()) {
            return redirect()->back()->withInput($request->only('name', 'email', 'mobile', 'amount'))->withErrors($validator->errors());
        }

        try {
            $instamojo = config('services.instamojo');
            $payload = array(
                "purpose" => "IMORDER" . Str::random(9),
                "amount" => intval($request->amount),
                "buyer_name" => $request->name,
                "email" => $request->email,
                "phone" => $request->mobile,
                "send_email" => true,
                "send_sms" => true,
                "redirect_url" => url('/instamojo/payments/success')
            );

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $instamojo['endpoint'].'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:".$instamojo['api_key'],
                "X-Auth-Token:".$instamojo['auth_token']
            ));
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
            $response = curl_exec($ch);
            curl_close($ch);

            $response = json_decode($response, true);

            if ($response['success'] == 1) {
                $payment_request = $response['payment_request'];
                InstamojoPayment::insert([
                    'name' => $payment_request['buyer_name'],
                    'email' => $payment_request['email'],
                    'mobile' => $payment_request['phone'],
                    'amount' => $payment_request['amount'],
                    'purpose' => $payment_request['purpose'],
                    'payment_request_id' => $payment_request['id'],
                    'payment_link' => $payment_request['longurl'],
                    'payment_status' => $payment_request['status'],
                    'created_at' => now(),
                    'updated_at' => now()
                ]);
                header('Location: ' . $payment_request['longurl']);
                exit();
            } else {
                echo "<pre>";
                print_r($response);
                exit;
            }
        }catch (Exception $e) {
            echo "<pre>";
            print('Error: ' . $e->getMessage());
            exit;
        }
    }

    public function success(Request $request)
    {
        $request_data = $request->all();

        $payment_id = $request_data['payment_id'];
        $payment_status = $request_data['payment_status'];
        $payment_request_id = $request_data['payment_request_id'];
        
        $im_payment = InstamojoPayment::where('payment_request_id', $payment_request_id)->first();
        
        if ($payment_status == 'Credit') {
            $im_payment->payment_status = $payment_status;
            $im_payment->payment_id = $payment_id;
            $im_payment->save();
            dd('Payment Successful');
        } else {
            dd('Payment Failed!');
        }
        dd($request_data);
    }
}

Step 6: Create Route

Next, open routes/web.php file and copy the below code in it:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\InstamojoPaymentController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::group(['prefix' => 'instamojo'], function () {
    Route::group(['prefix' => 'payments'], function () {
        Route::get('/', [InstamojoPaymentController::class, 'index']);
        Route::get('/create', [InstamojoPaymentController::class, 'create']);
        Route::post('/', [InstamojoPaymentController::class, 'store']);
        Route::any('/success', [InstamojoPaymentController::class, 'success']);
    });
});

Step 7: Create View Pages (Blade Files)

Next, create the below given blade files under resources/views/instamojo/payments directory:

resources/views/instamojo/payments/index.blade.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Laravel 9 Instamojo Payment Gateway Integration Tutorial</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
</head>
<body>
<div class="container mt-3">
    <div class="row text-center"><h1>Laravel 9 Instamojo Payment Gateway Integration Tutorial</h1></div>
    <div class="row" id="result-div"><strong style="color: #0cf;text-align: center;">Fetching records...</strong></div>
</div>
<script>
var pageNo = 1;
$(function(){
    $.ajax({
        url: "{{url('instamojo/payments')}}",
        type: "GET",
        dataType: "HTML",
        data: {
            'page': pageNo
        }
    }).done(function(data){
        $("#result-div").empty().html(data);
    }).fail(function(jqXHR, ajaxOptions, thrownError){
        console.log('No response from server');
    }).always(function(){
    });
});
</script>
</body>
</html>

resources/views/instamojo/payments/list.blade.php

<div class="col-12 mb-3">
    <div class="card text-white bg-success mb-3">
        <div class="card-header">
            <h3 class="card-title" style="display: inline-block;">Payments List</h3>
            <a href="{{url('/instamojo/payments/create')}}" class="btn btn-primary float-end">Create New Payment</a>
        </div>
        <div class="card-body table-responsive">
            <table class="table table-striped text-white">
                <thead>
                <tr style="text-transform: uppercase;">
                    <th>#</th>
                    <th>Name</th>
                    <th>EMail</th>
                    <th>Mobile</th>
                    <th>Amount</th>
                    <th>Purpose</th>
                    <th>Status</th>
                    <th>PaymentId</th>
                    <th>CreatedAt</th>
                </tr>
                </thead>
                <tbody>
                @if($records->count() > 0)
                    @foreach($records as $rec)
                    <tr>
                        <td>{{$loop->iteration}}</td>
                        <td>{{$rec->name}}</td>
                        <td>{{$rec->email}}</td>
                        <td>{{$rec->mobile}}</td>
                        <td>{{$rec->amount}}</td>
                        <td>{{$rec->purpose}}</td>
                        <td><button class="btn btn-info">{{$rec->payment_status}}</button></td>
                        <td>{{$rec->payment_id}}</td>
                        <td>{{$rec->created_at}}</td>
                    </tr>
                    @endforeach
                @else
                    <tr class="text-center"><td colspan="8">Record not found!!</td></tr>
                @endif
                </tbody>
            </table>
            {!! $records->links() !!}
        </div>
    </div>
</div>

resources/views/instamojo/payments/create.blade.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Laravel 9 Instamojo Payment Gateway Integration Tutorial</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
    <div class="container mt-3">
        <div class="row justify-content-center">
            <div class="col-12 col-md-6 mb-3">
                <div class="card text-dark bg-success mb-3">
                    <div class="card-header">
                        <h3 class="card-title text-white" style="display: inline-block;">Create New Payment</h3>
                        <a href="{{url('/instamojo/payments')}}" class="btn btn-primary float-end">Payments List</a>
                    </div>
                    <div class="card-body">
                        <form action="{{ url('instamojo/payments') }}" method="POST" name="laravel9-instamojo-integration">
                            {{ csrf_field() }}
                            <div class="form-floating">
                              <input type="text" class="form-control" name="name" id="name" value="{{ old('name') }}" placeholder="name">
                              <label for="name">Full Name</label>
                            </div>
                            <div class="form-floating">
                              <input type="email" class="form-control" name="email" id="email" value="{{ old('email') }}" placeholder="email">
                              <label for="email">Email Address</label>
                            </div>
                            <div class="form-floating">
                              <input type="text" class="form-control" name="mobile" id="mobile" value="{{ old('mobile') }}" placeholder="mobile">
                              <label for="mobile">Mobile Number</label>
                            </div>
                            <div class="form-floating">
                              <input type="text" class="form-control" name="amount" id="amount" value="{{ old('amount') }}" placeholder="amount">
                              <label for="amount">Amount</label>
                            </div>
                            <button class="w-100 btn btn-lg btn-primary" type="submit">Place Order</button>
                        </form>
                        @if ($errors->any())
                        <div class="alert alert-danger text-start" role="alert">
                            <strong>Opps!</strong> Something went wrong<br>
                            <ul>
                            @foreach ($errors->all() as $error)
                                <li>{{ $error }}</li>
                            @endforeach
                            </ul>
                        </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

Step 8: Run Laravel Application

Great, we are all set. Lets start the application now by using below command in terminal:

php artisan serve

Once development server is started, in your browser, open below URL:

http://127.0.0.1:8000/instamojo/payments

HAPPY LEARNING:)

Note: You can use below given Test Card Credentials to test your application:

CardNo: 4242 4242 4242 4242
Date: Any valid future date
CVV: 111
Name: abc
3D-secure Password: 1221

 

Thank You, Please Share.