Laravel 5.5 - Razorpay Payment Gateway Integration

Today, we are share with you in this tutorial How To Integration Razorpay Payment Gateway In Laravel 5.5 with easy example and demo.

Currently many payment gatway available in market like paypal, stripe, paycheckout, CCAvenue etc.., if you are built your product forr indian market then Razorpay is best for make online payment. it very easy to use in any programing language.

Simple following this step and easyly integrate Razorpay in your laravel application.

Create Razorpay Account

First, we need to create Razorpay account fron click by this link Razorpay Account. and get your razor_key and razor_secret from the account.

See following screenshot for how to create key from account.

Install package

Next, we need to install razorpay/razorpay laravel package in our application rub by following command.


composer require razorpay/razorpay

Create Config File

Nex, we need to create one custom.php file in config folder. in this file we are set our Razorpay's account razor_key and razor_secret so we are use in our integration for make payment.


return [
	'razor_key' => 'rzp_test_razor_key',
	'razor_secret' => 'rzp_test_razor_secret'
];

Create Route

Next, we need to create following two route one for show payment form and second one is post action route. so, open your routes/web.php and create following two route.


// Get Route For Show Payment Form
Route::get('paywithrazorpay', 'RazorpayController@payWithRazorpay')->name('paywithrazorpay');
// Post Route For Makw Payment Request
Route::post('payment', 'RazorpayController@payment')->name('payment');

Create Controller

Now we are create controller, so create RazorpayController.php file in your app/Http/Controllers folder.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Razorpay\Api\Api;
use Session;
use Redirect;

class RazorpayController extends Controller
{    
    public function payWithRazorpay()
    {        
        return view('payWithRazorpay');
    }

    public function payment()
    {
        //Input items of form
        $input = Input::all();
        //get API Configuration 
        $api = new Api(config('custom.razor_key'), config('custom.razor_secret'));
        //Fetch payment information by razorpay_payment_id
        $payment = $api->payment->fetch($input['razorpay_payment_id']);

        if(count($input)  && !empty($input['razorpay_payment_id'])) {
            try {
                $response = $api->payment->fetch($input['razorpay_payment_id'])->capture(array('amount'=>$payment['amount'])); 

            } catch (\Exception $e) {
                return  $e->getMessage();
                \Session::put('error',$e->getMessage());
                return redirect()->back();
            }

            // Do something here for store payment details in database...
        }
        
        \Session::put('success', 'Payment successful, your order will be despatched in the next 48 hours.');
        return redirect()->back();
    }
}
	
Create View

Now we are create controller, so create payWithRazorpay.blade.php file in your resources/views/ folder.

Info Message!!
#First check jQuery should be add in head section..


@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-4 col-md-offset-4">
            @if($message = Session::get('error'))
                <div class="alert alert-danger alert-dismissible fade in" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <strong>Error!</strong> {{ $message }}
                </div>
            @endif
            {!! Session::forget('error') !!}
            @if($message = Session::get('success'))
                <div class="alert alert-info alert-dismissible fade in" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <strong>Success!</strong> {{ $message }}
                </div>
            @endif
            {!! Session::forget('success') !!}
            <div class="panel panel-default">
                <div class="panel-heading">Pay With Razorpay</div>

                <div class="panel-body text-center">
                    <form action="{!!route('payment')!!}" method="POST" >
                        <!-- Note that the amount is in paise = 50 INR -->
                        <!--amount need to be in paisa-->
                        <script src="https://checkout.razorpay.com/v1/checkout.js"
                                data-key="{{ Config::get('custom.razor_key') }}"
                                data-amount="1000"
                                data-buttontext="Pay 10 INR"
                                data-name="Laravelcode"
                                data-description="Order Value"
                                data-image="yout_logo_url"
                                data-prefill.name="name"
                                data-prefill.email="email"
                                data-theme.color="#ff7529">
                        </script>
                        <input type="hidden" name="_token" value="{!!csrf_token()!!}">
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

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/paywithrazorpay
Testing Data

Visa Card No. : 4242424242424242

Mbile No. : 9898989898

OTP No. : 123456

If you want to any problem then please write comment and also suggest for new topic for make tutorials in future. Thanks...

 Prev post

Next post