In this tutorial you will learn about the Laravel 9 Instamojo Payment Gateway Integration Example and its application with practical example.
In this Laravel 9 Instamojo Payment Gateway Integration Example tutorial I will show you How to Integrate Instamojo Payment Gateway in Laravel 9. In this tutorial, we will learn how to integrate the instamojo payment gateway in php laravel 9 application via the instamojo PHP package.
Instamojo Payment Gateway
Instamojo is one of the popular payment gateway, that allows us to accept payment from your customer. Instamojo is very simple, hassle free and easy to integrate payment gateway. Instamojo processes refund payments, collect a payment, create payment are very easy and simple.
Laravel 9 Instamojo Payment Gateway Integration Example
In this step by step tutorial I will demonstrate you to integrate instamojo payment gateway in the laravel 9 application without using curl APIs. We will simply install instamojo PHP package and integrate into our lastest laravel based application.
- Install Laravel 9
- Connecting App to Database
- Install Instamojo PHP package
- Configure Instamojo Package
- Make Model and Migration
- Create Controller
- Make Routes
- Create Blade View file
- Start Development Server
Install Laravel 9
First of all we need to create a fresh laravel project, download and install Laravel 9 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Connecting App to Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
Install Instamojo PHP package
In this step, we will install Instamojo PHP package via the composer dependency manager. Use the following command to install Instamojo PHP package.
1 |
composer require instamojo/instamojo-php |
Configure Instamojo Package
Now, we will put following configuration detail in .env file:
1 2 3 |
IM_API_KEY=test_d883b3a8d2bc1adc7a535506713 IM_AUTH_TOKEN=test_dc229039d2232a260a2df3f7502 IM_URL=https://test.instamojo.com/api/1.1/ |
Next, Go to the app/config/services.php and put the below code here.
1 2 3 4 5 6 7 8 9 |
'instamojo' => [ 'api_key' => env('IM_API_KEY'), 'auth_token' => env('IM_AUTH_TOKEN'), 'url' => env('IM_URL'), ], |
Make Model & Migration
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Payment -m |
Once above command is executed there will be a migration file created inside database/migrations/ directory, just open migration file and update the function up() method as following:
1 2 3 4 5 6 7 8 9 10 |
public function up() { Schema::create('payments', function (Blueprint $table) { $table->increments('id'); $table->string('i_payment_id'); $table->string('user_id'); $table->string('amount'); $table->timestamps(); }); } |
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan migrate |
Create Controller
Now, lets create a controller named PayController using command given below –
1 |
php artisan make:controller PayController |
Go to app/Http/Controller/PayController.php and put the below code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class PayController extends Controller { public function index() { return view('event'); } 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/pay-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); } } |
Make Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 |
use App\Http\Controllers\PayController; Route::get('event', [PayController::class, 'index']); Route::post('pay', [PayController::class, 'pay']); Route::get('pay-success', [PayController::class, 'success']); |
Create Blade View file
In this step we will create blade file. Go to resources/views directory and create blade view file name event.blade.php. Then put the following code into event.blade.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
<!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"> <meta name="csrf-token" content="{{ csrf_token() }}"> <title>Instamojo Payment Gateway Integrate</title> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet"> <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> @if ($errors->any()) <div class="alert alert-danger"> <strong>Opps!</strong> Something went wrong<br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form action="{{ url('pay') }}" 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="100" readonly=""> </div> </div> <div class="col-md-12"> <button type="submit" class="btn btn-primary">Submit</button> </div> </div> </form> </div> </body> </html> |
Start Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/event |