Laravel 5.5 – Razorpay Payment Gateway Integration

Laravel 5.5 – Razorpay Payment Gateway Integration

In this post we will give you information about Laravel 5.5 – Razorpay Payment Gateway Integration. Hear we will give you detail about Laravel 5.5 – Razorpay Payment Gateway IntegrationAnd how to use it also give you demo for it if it is necessary.

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', '[email protected]')->name('paywithrazorpay');
// Post Route For Makw Payment Request
Route::post('payment', '[email protected]')->name('payment');

[ADDCODE]

Create Controller

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


namespace AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesInput;
use RazorpayApiApi;
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 >
    <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!</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!</strong> {{ $message }}
                </div>
            @endif
            {!! Session::forget('success') !!}
            <div >
                <div >Pay With Razorpay</div>

                <div >
                    <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…

Hope this code and post will helped you for implement Laravel 5.5 – Razorpay Payment Gateway Integration. 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 code Keep reading our blogs

For More Info See :: laravel And github

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