In this tutorial, we will tell you how to integrate the Instamojo payment gateway using the Laravel Framework (Laravel 7 Instamojo payment gateway integration example tutorial).
The Instamojo payment gateway is a popular payment gateway method and it is easily used for the project. so many developers prefer that payment gateway method.
Let’s go we follow the below steps for integrate the Instamojo payment gateway in laravel 7 version.
Overview
Step 1: Install Laravel 7
Step 2: Install Package
Step 3: Configuration of API Key
Step 4: Create Route
Step 5: Create a Controller
Step 6: Create Blade File
Step 7: Run The Application
Step 1: Install Laravel 7
We are going to install laravel 7, so first open the command prompt or terminal and go to go to xampp htdocs folder directory using the command prompt. after then run the below command.
composer create-project --prefer-dist laravel/laravel laravel7_instamojo
Step 2: Install Package
Now, we are going to install the Instamojo package using the below command.
composer require instamojo/instamojo-php
Step 3: Configuration of API Key
Now, we are going to configuration of api key in app/config/services.php file.
'instamojo' => [ 'api_key' => 'IM_API_KEY', 'auth_token' => 'IM_AUTH_TOKEN', 'url' => 'IM_URL', ],
Step 4: Create Route
Add the below following route code in the “routes/web.php” file.
<?php /* |-------------------------------------------------------------------------- | 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::get('/', function () { return view('welcome'); }); Route::get('event', 'PayController@index'); Route::post('pay', 'PayController@pay'); Route::get('pay-success', 'PayController@success'); ?>
Step 5: Create a Controller
Here below command help to create the Authcontroller and paste below following code.
php artisan make:controller PayController
PayController.php
<?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); } } ?>
Step 6: Create Blade File
Finally, We will create event.blade.php file in the “resources/views/” folder directory and paste below code.
event.blade.php
<!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>laravel 7 Instamojo payment gateway integration example tutorial - XpertPhp</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>
Step 7: Run The Application
We can start the server and run this application using the below command.
php artisan serve
Now we will run our example using the below Url in the browser.
http://127.0.0.1:8000/event