Search

Laravel 5.6 - Card Payment Integration With Instamojo Payment Gateway

post-title

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.

[ADDCODE]

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 code 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 code in it.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
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' => 'laracode101@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_decode($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_decode($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 code in this file for your design view.


@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-12">
            @if($message = Session::get('error'))
            <div class="alert alert-danger alert-dismissible" role="alert">
                <button type="button" class="close" 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 class="alert alert-success alert-dismissible" role="alert">
                <button type="button" class="close" 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 class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="card card-default">
                <div class="card-header">
                    <div class="row">
                        <div class="col-md-10">
                            <strong>Pay With Instamojo</strong>
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    <form class="form-horizontal" method="POST" action="{{ URL::route('payment') }}">
                        @csrf
                        <div class="form-group">
                            <label for="purpose" class="col-md-12 col-form-label">Purpose</label>
                            <div class="col-md-12">
                                <input id="purpose" type="text" class="form-control" name="purpose" value="" required>
                                @if ($errors->has('purpose'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('purpose') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="buyer_name" class="col-md-12 col-form-label">Buyer Name</label>
                            <div class="col-md-12">
                                <input id="buyer_name" type="text" class="form-control" name="buyer_name" value="" required>
                                @if ($errors->has('buyer_name'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('buyer_name') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="amount" class="col-md-12 col-form-label">Amount</label>
                            <div class="col-md-12">
                                <input id="amount" type="number" class="form-control" name="amount" value="" required>
                                @if ($errors->has('amount'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('amount') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="phone" class="col-md-12 col-form-label">Phone No.</label>
                            <div class="col-md-12">
                                <input id="phone" type="number" class="form-control" name="phone" value="" required>
                                @if ($errors->has('phone'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('phone') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-12 col-md-offset-3">
                                <button type="submit" class="btn btn-primary btn-block desabled" 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 Laravelcode's Forums