How to integrate Rozorpay Payment Gateway in Laravel 8

  383 views   1 month ago Laravel

Today, I will share with you how to integrate Razorpay payment gateway in laravel 8 application with example. in many laravel applications you need to the integrated payment system for the real transactions. currently many payment solutions available for integrate the payment systems in our laravel application. like PayPal, stripe, paycheckout, etc.

But if you want to integrate Razorpay payment gateway in your laravel 8 application then simply follow the steps.

Step - 1 : Create Razorpay Account

First, we need to create a Razorpay account from click by this link Razorpay Account. and get your Key Id from the account. see the following screenshot.

Step - 2 : Install the Package

Now, we need to install "razorpay/razorpay" package in our laravel 8 application.

composer require razorpay/razorpay

Step - 3 : Create Config File

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

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

Step - 4 : Create Route

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

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\RazorpayController;

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

Step - 5 : Create Controller

Now, we need to create "RazorpayController" in our laravel 8 application using the following artisan command in terminal

php artisan make:controller RazorpayController

now, open the created "RazorpayController.php" file and write the following code into it.

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Razorpay\Api\Api;
use Session;
use Redirect;

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

    public function payment(Request $request)
    {
        //Input items of form
        $input = $request->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();
    }
}

Step - 6 : Create Blade View

Now, simple add "resources/views/rozorpay/index.blade.php" file.

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 8 Rozorpay Payment Integration Demo - laravelcode.com</title>
    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    @yield('style')
</head>
<body>
  
<div class="container">
    <div class="row">
        <div class="col-md-12">
            @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="https://www.laravelcode.com/upload/logo.svg"
                                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>
</body>
</html>

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

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/paywithrazorpay

Preview :

Testing Data :

  • Visa Card No. : 4242424242424242
  • Mbile No. : 9898989898
  • OTP No. : 123456

i hope you like this article.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com

Related Articles