Laravel 8 - How to integrate Instamojo Payment Gateway with Example

Today we visually perceive How to integrate Instamojo payment gateway in laravel 8. In this tutorial, we will learn Instamojo payment gateway integration in laravel application via the Instamojo package install.

Now, we will show you facile to step by step on how to Instamojo payment gateway integration.

When we take mazuma from someone, by offering accommodations through your website, you require a payment gateway, for this, we utilize a payment gateway. we optically discern Instamojo payment gateway integration in PHP. like e-commerce, package buys, etc.

Instamojo Payment Gateway Integration Example

  • Step 1: Install Laravel 8 App
  • Step 2: Connecting App to Database
  • Step 3: Install Instamojo PHP package
  • Step 4: Configure Instamojo Package
  • Step 5: Make Model and Migration
  • Step 6: Create Controller
  • Step 7: Make Routes
  • Step 8: Create a Blade View file
  • Step 9: Start Development Server

Step 1: Install Laravel 8 App

In this step, install Laravel 8 application by executing the following command on the terminal:

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

Step 2: Connecting App To Database

Visit laravel 8 app root directory and open the .env file. Then add the database details:

DB_DATABASE=here your database name here
DB_USERNAME=here database username here
DB_PASSWORD=here database password here

Step 3: Install Instamojo PHP Package

In this step, execute the following command on the terminal to install the Instamojo package. We learn Instamojo payment gateway:

composer require instamojo/instamojo-php

Step 4: Configure Instamojo Package

First of all, visit the and create an account on it. Then you will get the client id and secret id from Instamojo.

After that, open the .env file And set the API keyauth-token and URL in the .env file like the following:


Next, Open the services.php file and add the following code into it, which is inside the app/config directory. few step in Instamojo payment gateway integration in PHP.

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

Step 5: Make Model & Migration

Create a table name Payment and it’s migration file. use the below command.

php artisan make:model Payment -m

This command will create one model name Payment and also create one migration file for the Payment table. instamojo payment gateway integration in PHP source code. After successfully run the command go to database/migrations/Payments.php file and replace the function, below here :

public function up()
 Schema::create('payments', function (Blueprint $table) {

Next, migrate the table using the below command. It will create two new tables in the database.

php artisan migrate

Step 6: Create Controller

In this step, Create the controller name PaymentController using the below command.

php artisan make:controller PaymentController

Then open PaymentController.php and add the following code into it, which is placed on app/Http/Controller/ directory:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PaymentController extends Controller
   public function index()
        return view('event');
   public function pay(Request $request){
     $api = new \Instamojo\Instamojo(
    try {
        $response = $api->paymentRequestCreate(array(
            "purpose" => "For text Php Coding Stuff",
            "amount" => $request->amount,
            "buyer_name" => "$request->name",
            "send_email" => true,
            "email" => "$request->email",
            "phone" => "$request->mobile_number",
            "redirect_url" => ""
            header('Location: ' . $response['longurl']);
    }catch (Exception $e) {
        print('Error: ' . $e->getMessage());
 public function success(Request $request){
     try {
        $api = new \Instamojo\Instamojo(
        $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');

Step 7: Make Routes

In this step, open the web.php file and add the following routes into it, which is placed inside the routes directory:

use App\Http\Controllers\PaymentController;<br> 
Route::get('event', [PaymentController::class, 'index']);
Route::post('pay', [PaymentController::class, 'pay']);
Route::get('pay-success', [PaymentController::class, 'success']);<br>

Step 8: Create Blade View File

In this step, Visit the resources/views directory and create blade view file name event.blade.php.

Then add the following code into event.blade.php. We learn How to integrate Instamojo payment gateway in laravel.

<!DOCTYPE html>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <meta name="csrf-token" content="{{ csrf_token() }}">
      <title>Instamojo Payment Gateway HackTheStuff</title>
      <link href="" rel="stylesheet">
         margin-top: 40px;
      <div class="container">
         <div class="row">
            <div class="col-lg-12 mt40">
               <div class="card-header" style="background: #0275D8;">
                  <h2>Register for Event</h2>
         @if ($errors->any())
         <div class="alert alert-danger">
            <strong>Opps!</strong> Something went wrong<br>
               @foreach ($errors->all() as $error)
               <li>{{ $error }}</li>
         <form action="{{ url('pay') }}" method="POST" name="laravel_instamojo">
            {{ csrf_field() }}
            <div class="row">
               <div class="col-md-12">
                  <div class="form-group">
                     <input type="text" name="name" class="form-control" placeholder="Enter Name" required>
               <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 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 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 class="col-md-12">
                  <button type="submit" class="btn btn-primary">Submit</button>

Step 9: Start Development Server

In this step, execute the php artisan serve command on the terminal to start the development server:

php artisan serve

Testing Card Credential

Card No : 4242424242424242
 Month : any future month
 Year : any future Year
 CVV : 111
 Password : 1221

You have learned Step by Step - How to integration Instamojo payment gateway in laravel 8.

i hope you like this article.