Laravel 8 integrate Razorpay Payment Gateway

Laravel 8 integrate Razorpay Payment Gateway

In this post we will give you Laravel 8 integrate Razorpay Payment Gateway, hear for Laravel 8 integrate Razorpay Payment Gateway we will give you details about it.

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 Razorpay Payment Integration </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.0https://www.onlinecode.org/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

Testing Data of Laravel 8 integrate Razorpay Payment Gateway:

  • Visa Card No. : 4242424242424242
  • Mbile No. : 9879879871
  • OTP No. : 987456

Hope this code and post will helped you for implement Laravel integrate Razorpay 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 onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org

x