Laravel 8 Instamojo Payment Gateway Integration Example Tutorial

Instamojo payment gateway integration in laravel 8. In this post, i will show you how to integrate instamojo payment geteway in laravel 8 app.

In this example, i will install instamojo payment gateway laravel package. Then create payment form in laravel, which is used to collect payment using instamojo.

And in this post, you will learn step by step on how to integrate instamojo payment gateway in laravel 8 app.

By using Instamojo Payment Gateway, you can collect payments in the easiest way.

Laravel 8 Instamojo Payment Gateway Integration Example Tutorial

  • Step 1 – Installing Laravel 8 Application
  • Step 2 – Create account in Instamojo and generate key and secret
  • Step 3 – Install Instamojo package And Configure
  • Step 4 – Database and Instamojo Key Configuration
  • Step 5 – Creating Payment Model & Migration
  • Step 6 – Create Routes
  • Step 7 – Creating Instamojo Controller
  • Step 8 – Create Directory and Blade View
    • Create Directory Name Instamojo
    • Create Instamojo Payment Blade View
  • Step 9 – Start Development Server
  • Step 10 – Run This App On Browser

Step 1 – Installing Laravel 8 Application

In step 1, open your terminal and navigate to your local web server directory using the following command:

//for windows user
cd xampp/htdocs

//for ubuntu user
cd var/www/html

Then install laravel 8 latest application using the following command:

composer create-project --prefer-dist laravel/laravel Laravel8Instamojo

Step 2 – Create account in Instamojo and generate key and secret

In step 2, Create instamojo account. Then generate key and secret.

Step 3 – Install Instamojo package And Configure

In step 3, open terminal and run the following command on it to install Instamojo packate in laravel 8 app:

composer require instamojo/instamojo-php

Next, open services.php, which is located app/config directory. And add the following code into it:

'instamojo' => [
'api_key' => env('IM_API_KEY'),
'auth_token' => env('IM_AUTH_TOKEN'),
'url' => env('IM_URL'),
],

Step 4 – Database and Instamojo Key Configuration

In step 4, open your downloaded laravel 8 app into any text editor. Then find .env file and configure database detail and instamojo key and secret like following:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db name
DB_USERNAME=db user name
DB_PASSWORD=db password

 IM_API_KEY=test_d883b3a8d2bc1adc7a535506713
 IM_AUTH_TOKEN=test_dc229039d2232a260a2df3f7502
 IM_URL=https://test.instamojo.com/api/1.1/

Step 5 – Creating Payment Model & Migration

In step 5, run the following command on terminal to create model and migration file by using the following command:

php artisan make:model Payment -m

The above command will create two files into your laravel 8 instamojo payment gateway integration tutorial app, which is located inside the following locations:

  • Laravel8Instamojo/app/Models/Payment.php
  • Laravel8Instamojo/database/migrations/create_payments_table.php

So, find create_payments_table.php file inside Laravel8Instamojo/database/migrations/ directory. Then open this file and add the following code into function up() on this file:

    public function up()
    {
        Schema::create('payments', function (Blueprint $table) {
            $table->id();
            $table->string('insta_payment_id'); // instamojo payment id
            $table->string('name');
            $table->string('email');
            $table->string('mobile');
            $table->string('amount');
            $table->timestamps();
        });
    }

Now, open again your terminal and type the following command on cmd to create tables into your selected database:

php artisan migrate

Step 6 – Create Routes

In step 6, open your web.php file, which is located inside routes directory. Then add the following routes into web.php file:

use App\Http\Controllers\InstamojoController;

Route::get('instamojo', [InstamojoController::class, 'index']);
Route::post('payment-process', [InstamojoController::class, 'process']);
Route::post('payment-success', [InstamojoController::class, 'success']);

Step 7 – Creating Instamojo Controller

In step 7, create instamojo payment controller by using the following command:

php artisan make:controller InstamojoController

The above command will create InstamojoController.php file, which is located inside Laravel8Instamojo/app/Http/Controllers/ directory.

So open InstamojoController.php file and add the following code into it:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Payment;

class InstamojoController extends Controller
{
   
   public function index()
   {
        return view('instamojo.index');
   }
   public function pay(Request $request){
 
     $api = new \Instamojo\Instamojo(
            config('services.instamojo.api_key'),
            config('services.instamojo.auth_token'),
            config('services.instamojo.url')
        );
 
    try {
        $response = $api->paymentRequestCreate(array(
            "purpose" => "FIFA 16",
            "amount" => $request->amount,
            "buyer_name" => "$request->name",
            "send_email" => true,
            "email" => "$request->email",
            "phone" => "$request->mobile_number",
            "redirect_url" => "http://127.0.0.1:8000/payment-success"
            ));
             
            header('Location: ' . $response['longurl']);
            exit();
    }catch (Exception $e) {
        print('Error: ' . $e->getMessage());
    }
 }
 
 public function success(Request $request){
     try {
 
        $api = new \Instamojo\Instamojo(
            config('services.instamojo.api_key'),
            config('services.instamojo.auth_token'),
            config('services.instamojo.url')
        );
 
        $response = $api->paymentRequestStatus(request('payment_request_id'));
 
        if( !isset($response['payments'][0]['status']) ) {
           dd('payment failed');
        } else if($response['payments'][0]['status'] != 'Credit') {
             dd('payment failed');
        } 
      }catch (\Exception $e) {
         dd('payment failed');
     }
    dd($response);
  }
}

Step 8 – Create Directory and Blade View

In step 8, create the following directory and blade view file.

  • create directory name instamojo inside resources/views directory.
  • Create Payment Blade View name index.blade.php inside resources/views/instamojo directory. Then add the following code into it:
<!DOCTYPE html>
<html>
<head>
  <title>Instamojo Payment Gateway Integration In Laravel 8</title>

  <meta name="csrf-token" content="{{ csrf_token() }}">

  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

</head>
<body>

<div class="container mt-4">

<br>  <h3 class="text-center">Laravel 8 Instamojo Payment Gateway Integration Tutorial</h3><hr>

  <div class="row">
    <div class="col-md-8">
            <form action="{{ url('payment-process') }}" method="POST" name="laravel_instamojo">
            {{ csrf_field() }}
            <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" 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" 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="200" readonly="">
                  </div>
               </div>
               <div class="col-md-12">
                  <button type="submit" class="btn btn-primary">Submit</button>
               </div>
            </div>
         </form>
    </div> <!-- col // -->
  </div> <!-- row.// -->

</div>
</body>
</html>

Step 9 – Start Development Server

Finally, open your command prompt again and run the following command to start development server for your laravel 8 instamojo payment gateway integration example:

php artisan serve

Step 10 – Run This App On Browser

In step 10, open your browser and fire the following url into your browser:

http://127.0.0.1:8000/instamojo

Testing Card Credential

 Card No : 4242 4242 4242 4242
 Month : any future month
 Year : any future Year
 CVV : 123

Leave a Comment