Code your dreams into reality.
Every line of code is a step towards a better future.
Embrace the bugs, they make you a better debugger.

Laravel 11 Integrate Razorpay Payment Gateway Tutorial

Last Updated on May 17, 2024 by

Razorpay provide a php library that allow users to integrate payment gateway in laravel 11 applications.

Let’s start installing razorpay php library in the application and create a form which is used to collect online payments through credit card, debit card, netbanking and upi:

Step 1: Create Account in Razorpay

Simply open https://razorpay.com url on browser, and create a account in razorpay. After that, click https://dashboard.razorpay.com/app/keys url to get api key and secret for integration.

Step 2: Configure Razorpay in Laravel

Edit .env file in application, and define Razorpay API key and secret into it:

RAZORPAY_KEY=your_key_id
RAZORPAY_SECRET=your_secret

Step 3: Install Razorpay PHP SDK

Run the following command to install Razorpay PHP SDK via Composer:

composer require razorpay/razorpay

Step 4: Add Routes

Edit routes/web.php file, and add routes to handle payment requests:

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\RazorpayPaymentController;
  
Route::get('razorpay-payment', [RazorPaymentController::class, 'index']);
Route::post('razorpay-payment', [RazorPaymentController::class, 'store'])->name('razorpay.payment.store');

Step 5: Create Payment Controller

Create a controller file name RazorPaymentController.php, to handle payment processing:

php artisan make:controller RazorPaymentController

Edit RazorPaymentController.php file from app/http/controllers directory, and create some methods into it to handle payment processing:

<?php
  
namespace App\Http\Controllers;
  
use Exception;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Razorpay\Api\Api;
  
class RazorPaymentController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(): View
    {        
        return view('payment');
    }
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function store(Request $request): RedirectResponse
    {
        $input = $request->all();
  
        $api = new Api(env('RAZORPAY_KEY'), env('RAZORPAY_SECRET'));
  
        $payment = $api->payment->fetch($input['razorpay_payment_id']);
  
        if(!empty($input['razorpay_payment_id'])) {

            try {
                $response = $api->payment->fetch($input['razorpay_payment_id'])
                                         ->capture(['amount'=>$payment['amount']]); 
  
            } catch (Exception $e) {
                return redirect()->back()
                                 ->with('error', $e->getMessage());
            }
            
        }

        return redirect()->back()
                         ->with('success', 'Payment successful');
    }
}

Step 6: Create Blade View for Payment

Create a new view file name payment.blade.php in resources/views directory to show payment collection form:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Laravel 11 Razorpay Payment Gateway Integration Tutorial</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" crossorigin="anonymous"></script>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
</head>
<body>

<div class="container">

    <div class="card mt-5">
        <h3 class="card-header p-3">Laravel 11 Razorpay Payment Gateway Integration - itcodStuff.com</h3>
        <div class="card-body">

            @session('error')
                <div class="alert alert-danger" role="alert"> 
                    {{ $value }}
                </div>
            @endsession

            @session('success')
                <div class="alert alert-success" role="alert"> 
                    {{ $value }}
                </div>
            @endsession

            <form action="{{ route('razorpay.payment.store') }}" method="POST" class="text-center">
                @csrf
                <script src="https://checkout.razorpay.com/v1/checkout.js"
                        data-key="{{ env('RAZORPAY_KEY') }}"
                        data-amount="1000"
                        data-buttontext="Pay 10 INR"
                        data-name="itcodestuff.com"
                        data-description="Rozerpay"
                        data-image="https://itcodstuff.com/wp-content/uploads/2024/03/cropped-favicon-180x180.png"
                        data-prefill.name="name"
                        data-prefill.email="email"
                        data-theme.color="#ff7529">
                </script>
            </form>
        </div>
    </div>
</div>
</body>
</html>

Step 7: Test the Integration

Start your Laravel server by using the following command:

php artisan serve

Start browser and use the following url into it for testing:

http://localhost:8000/razorpay-payment

Keep coding, keep growing!

Leave a Comment