Laravel 5.6 – Card Payment Integration With Instamojo Payment Gateway

Laravel 5.6 – Card Payment Integration With Instamojo Payment Gateway

In this post we will give you information about Laravel 5.6 – Card Payment Integration With Instamojo Payment Gateway. Hear we will give you detail about Laravel 5.6 – Card Payment Integration With Instamojo Payment GatewayAnd how to use it also give you demo for it if it is necessary.

Today, we are share with you how to integrate card payment in laravel using Instamojo payment API. many time you need to integrate any payment method in your laravel application. you can done payment integration like paypal, stripe, rozorpay etc… and we are also write many tutorial about payment gateway integration in your laravel application. you can check all tutorils from our payment gateway category.



Here we are share one another payment gateway integration in laravel and this is instamojo payment gateway. this payment gateway easy to use in your laravel application and you can also use their many functionality.


Instamojo mainly use in india. if you want integrate card payment in your laravel application then instamojo is best option for you. here we are show you how to integrate instamojo payment gateway in your laravel application start to end. simpaly follow this step.


What we need for integration:


First things what we need for integration instamojo payment gateway in our laravel or php application. first we are integrate instamojo in test mode then after you can switch our live integration after success done integration test integration.


Now you should create one account for test integration so open this link and create your account Instamojo Test Account. some simplay details you must be fill open test account and aso live account.


After you create test account successfully then open this link Get Integration Keys. and get your Api-Key and Auth-Token. get both of keys then we are move to in our write laravel application for integrate instamojo card payment system.


Live Entry Point Url
https://www.instamojo.com/api/1.1/payment-requests/


Create Route:


Now, create following route in your routes/web.php file. add following route in it.




Route::get('payment', 'PaymentController@index')->name('payment');
Route::post('payment', 'PaymentController@payment')->name('payment');
Route::get('returnurl', 'PaymentController@returnurl')->name('returnurl');
	


Create PaymentController:


Now, create one PaymentController.php file in app/Http/Controllers/ and simpaly add followeing in it.




namespace AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesValidator;
use View;
use Session;
use Redirect;

class PaymentController extends Controller
{
    public function __construct()
    {   
    }
    public function index(Request $request)
    {
        return view('paymentwithinstamojo');
    }
    public function payment(Request $request)
    {
        $this->validate($request, [
            'amount' => 'required',
            'purpose' => 'required',
            'buyer_name' => 'required',
            'phone' => 'required',
        ]);
        
        $ch = curl_init();

        // For Live Payment
        // curl_setopt($ch, CURLOPT_URL, 'https://www.instamojo.com/api/1.1/payment-requests/');
        // For Test payment
        curl_setopt($ch, CURLOPT_URL, 'https://test.instamojo.com/api/1.1/payment-requests/');
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_HTTPHEADER,
            array("X-Api-Key:test_ba7110d4436b456238fc630d248",
                "X-Auth-Token:test_8ee7a19348456600fdf9169c9c7"));
        $payload = Array(
            'purpose' => $request->get('purpose'),
            'amount' => $request->get('amount'),
            'phone' => $request->get('phone'),
            'buyer_name' => $request->get('buyer_name'),
            'redirect_url' => url('/returnurl'),
            'send_email' => false,
            'webhook' => 'http://instamojo.com/webhook/',
            'send_sms' => true,
            'email' => 'lara101@gmail.com',
            'allow_repeated_payments' => false
        );
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
        $response = curl_exec($ch);
        $err = curl_error($ch);
        curl_close($ch); 

        if ($err) {
            Session::put('error','Payment Failed, Try Again!!');
            return redirect()->back();
        } else {
            $data = json_de($response);
        }


        if($data->success == true) {
            return redirect($data->payment_request->longurl);
        } else {
            Session::put('error','Payment Failed, Try Again!!');
            return redirect()->back();
        }

    }

    public function returnurl(Request $request)
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, 'https://test.instamojo.com/api/1.1/payments/'.$request->get('payment_id'));
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_HTTPHEADER,
            array("X-Api-Key:test_ba7110d4436b456238fc630d248",
                "X-Auth-Token:test_8ee7a19348456600fdf9169c9c7"));

        $response = curl_exec($ch);
        $err = curl_error($ch);
        curl_close($ch); 

        if ($err) {
            Session::put('error','Payment Failed, Try Again!!');
            return redirect()->route('payment');
        } else {
            $data = json_de($response);
        }
        
        if($data->success == true) {
            if($data->payment->status == 'Credit') {
                
                // Here Your Database Insert Login
                // dd($data);

                Session::put('success','Your payment has been pay successfully, Enjoy!!');
                return redirect()->route('payment');

            } else {
                Session::put('error','Payment Failed, Try Again!!');
                return redirect()->route('payment');
            }
        } else {
            Session::put('error','Payment Failed, Try Again!!');
            return redirect()->route('payment');
        }
    }

}	
	


Create View File:


Now, we are create paymentwithinstamojo.blade.php in resources/views folder and add following bootstap in this file for your design view.




@extends('layouts.app')

@section('content')
<div >
    <div >
        <div >
            @if($message = Session::get('error'))
            <div  role="alert">
                <button type="button"  data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
                <strong>Error Alert!</strong> {{ $message }}
            </div>
            @endif
            {!! Session::forget('error') !!}
            @if($message = Session::get('success'))
            <div  role="alert">
                <button type="button"  data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
                <strong>Success Alert!</strong> {{ $message }}
            </div>
            @endif
            {!! Session::forget('success') !!}
        </div>
    </div>
</div>
<div >
    <div >
        <div >
            <div >
                <div >
                    <div >
                        <div >
                            <strong>Pay With Instamojo</strong>
                        </div>
                    </div>
                </div>
                <div >
                    <form  method="POST" action="{{ URL::route('payment') }}">
                        @csrf
                        <div >
                            <label for="purpose" >Purpose</label>
                            <div >
                                <input id="purpose" type="text"  name="purpose" value="" required>
                                @if ($errors->has('purpose'))
                                    <span >
                                        <strong>{{ $errors->first('purpose') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        <div >
                            <label for="buyer_name" >Buyer Name</label>
                            <div >
                                <input id="buyer_name" type="text"  name="buyer_name" value="" required>
                                @if ($errors->has('buyer_name'))
                                    <span >
                                        <strong>{{ $errors->first('buyer_name') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        <div >
                            <label for="amount" >Amount</label>
                            <div >
                                <input id="amount" type="number"  name="amount" value="" required>
                                @if ($errors->has('amount'))
                                    <span >
                                        <strong>{{ $errors->first('amount') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        <div >
                            <label for="phone" >Phone No.</label>
                            <div >
                                <input id="phone" type="number"  name="phone" value="" required>
                                @if ($errors->has('phone'))
                                    <span >
                                        <strong>{{ $errors->first('phone') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        <div >
                            <div >
                                <button type="submit"  id="submitUser">
                                    Submit
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>  
</div>
@endsection	
	


After done your payment success then open your instamojo dashboard and you can seen your payment open by this link https://test.instamojo.com/payments/


Now we are ready to run our example so run bellow command ro quick run:




php artisan serve



Now you can open bellow URL on your browser:



	
http://localhost:8000/payment



Please also check our demo for realtime CRUD system.


We are hope you like this tutorials, if any question regarding any query please post your question in our forums click on bellow link Laravel’s Forums

Hope this and post will helped you for implement Laravel 5.6 – Card Payment Integration With Instamojo Payment Gateway. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

7  +    =  17

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US