In this tutorial you will learn about the Laravel 7/6 Instamojo Payment Gateway Integration Example and its application with practical example.
In this Laravel 7/6 Instamojo Payment Gateway Integration Example tutorial I will show you How to Integrate Instamojo Payment Gateway in Laravel 7/6. In this tutorial, we will learn how to integrate the instamojo payment gateway in the php laravel application via the instamojo PHP package.
In this step by step tutorial I will demonstrate you to integrate instamojo payment gateway in the laravel app without using curl APIs. We will simply install instamojo PHP package and integrate into our lastest laravel based application.
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. Integrating Instamojo payment gateway in laravel 7 is a breeze. Instamojo processes refund payments, collect a payment, create payment are very easy and simple.
Laravel 7/6 Instamojo Payment Gateway Integration Example
In this tutorial, you will learn to integrate Instamojo payment gateway in your laravel 7 project. Follow this step by step tutorial to learn Instamojo payment gateway integration in laravel 7.
- Install Laravel Fresh Setup
- Install Instamojo PHP package
- Configuration .env file
- Create Controller
- Make Route
- Create Blade View file
- Start Development Server
Step 1: Install Laravel Fresh Project
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: 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 |
Step 3: Configuration in .env
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'), ], |
Step 4: 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 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); } } |
Step 5: Make Route
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 |
<?php Route::get('event', 'PayController@index'); Route::post('pay', 'PayController@pay'); Route::get('pay-success', 'PayController@success'); |
Step 6: Create Blade View file
Now, we will create blade views file, Go to app/resources/views/ and create one file name 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> |
Step 7: 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 2 3 |
http://localhost:8000/event Or direct hit in your browser http://localhost/blog/public/event |
Testing Card Credential
1 2 3 4 5 |
Card No : 4242424242424242 Month : any future month Year : any future Year CVV : 111 Password : 1221 |