Share this:

Hello, Today we are going to learn Integrating Razorpay Payment Gateway in Laravel 10: A Step-by-Step Guide. Online payments are an essential part of any e-commerce website. To accept online payments, businesses need a secure and reliable payment gateway. Razorpay is one such payment gateway that has gained popularity in India due to its ease of use and security features. Laravel, a popular PHP web application framework, makes it easy to integrate payment gateways like Razorpay in your application.

In this step-by-step guide, we’ll explore how to integrate Razorpay payment gateway in a Laravel 10 application. We’ll cover the necessary steps to create a Razorpay account, configure our Laravel application, and implement the Razorpay payment flow. By the end of this tutorial, you’ll have a working Razorpay payment gateway integration in your Laravel 10 application.

We’ll start by creating a Razorpay account and installing the razorpay/razorpay package. Next, we’ll configure the Razorpay credentials in our Laravel application and define routes for the payment flow. We’ll create a PaymentController that will handle the payment flow, including creating a Razorpay order and displaying the payment form. We’ll also create views for the payment form and Razorpay payment page and test the integration to ensure that everything is working correctly.

By the end of this tutorial, you’ll have the skills and knowledge necessary to integrate Razorpay payment gateway in your Laravel 10 application, allowing your customers to make online payments securely and efficiently.

Steps on Integrating Razorpay Payment Gateway in Laravel 10

  • Step 1: Create Razorpay Account
  • Step 2: Install Razorpay Package
  • Step 3: Configure Razorpay Credentials
  • Step 4: Define Routes
  • Step 5: Create Payment Controller
  • Step 6: Create Payment Form View
  • Step 7: Create Razorpay View
  • Step 8: Create Payment Success and Failure Views
  • Step 9: Test Razorpay Payment Gateway Integration
  • Conclusion

Step 1: Create Razorpay Account

Before we can integrate Razorpay in our Laravel application, we need to create a Razorpay account. Visit the Razorpay website and sign up for an account. Once you’ve signed up, you’ll be redirected to the Razorpay dashboard.

Step 2: Install Razorpay Package

To integrate Razorpay in our Laravel application, we’ll be using the razorpay/razorpay package. Open your terminal and run the following command to install the package:

composer require razorpay/razorpay

This command will install the razorpay/razorpay package and its dependencies in your Laravel application.

Step 3: Configure Razorpay Credentials

Next, we need to configure the Razorpay credentials in our Laravel application. Open the .env file in your Laravel application and add the following variables:

RAZORPAY_KEY=your_key_here
RAZORPAY_SECRET=your_secret_here

Replace your_key_here and your_secret_here with your actual Razorpay key and secret, which can be found on the Razorpay dashboard under Settings > API Keys.

Step 4: Define Routes

We need to define the routes that will handle the payment flow in our Laravel application. Open the routes/web.php file and add the following routes:

use App\Http\Controllers\PaymentController;

Route::get('/payment', [PaymentController::class, 'showPaymentForm'])->name('payment');
Route::post('/payment', [PaymentController::class, 'makePayment'])->name('payment.makePayment');
Route::get('/payment/success', [PaymentController::class, 'paymentSuccess'])->name('payment.success');
Route::get('/payment/failure', [PaymentController::class, 'paymentFailure'])->name('payment.failure');

These routes map to the methods in the PaymentController, which we’ll create in the next step.

Step 5: Create Payment Controller

We need to create a PaymentController that will handle the payment flow in our Laravel application. Run the following command in your terminal to generate the PaymentController:

php artisan make:controller PaymentController

Open the PaymentController located at app/Http/Controllers/PaymentController.php and add the following methods:

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

class PaymentController extends Controller
{
    public function showPaymentForm()
    {
        return view('payment');
    }

    public function makePayment(Request $request)
    {
        $api = new Api(env('RAZORPAY_KEY'), env('RAZORPAY_SECRET'));

        $order = $api->order->create([
            'amount' => $request->get('amount') * 100,
            'currency' => 'INR',
            'payment_capture' => 1,
        ]);

        $orderId = $order['id'];

        return view('razorpay', compact('orderId'));
    }

    public function paymentSuccess()
    {
        return view('payment-success');
    }

    public function paymentFailure()
    {
        return view('payment-failure');
    }
}

In the showPaymentForm() method, we’re returning a view that displays the payment form. In the makePayment() method, we’re creating a Razorpay order and returning a view with the order ID. The paymentSuccess() and paymentFailure() methods display views that indicate whether the payment was successful or not.

Step 6: Create Payment Form View

We need to create a view that displays the payment form. Create a new file called payment.blade.php in the resources/views directory and add the following code:

<form action="{{ route('payment.makePayment') }}" method="POST">
    @csrf
    <div class="form-group">
        <label for="amount">Amount</label>
        <input type="text" class="form-control" id="amount" name="amount" placeholder="Enter amount">
    </div>
    <button type="submit" class="btn btn-primary">Pay Now</button>
</form>

This form has an input field for the payment amount and a submit button.

Step 7: Create Razorpay View

We also need to create a view that displays the Razorpay payment form. Create a new file called razorpay.blade.php in the resources/views directory and add the following code:

<form action="{{ route('payment.success') }}" method="GET">
    <script
        src="https://checkout.razorpay.com/v1/checkout.js"
        data-key="{{ env('RAZORPAY_KEY') }}"
        data-amount="{{ $orderId }}"
        data-currency="INR"
        data-name="My Store"
        data-description="Test Transaction"
        data-image="https://your-awesome-site.com/logo.png"
        data-prefill.name="John Doe"
        data-prefill.email="johndoe@example.com"
        data-theme.color="#F37254"
    ></script>
    <input type="hidden" value="{{ $orderId }}" name="razorpay_order_id">
    <input type="hidden" value="INR" name="razorpay_currency">
    <input type="hidden" value="{{ $orderId }}" name="razorpay_amount">
    <input type="hidden" value="My Store" name="razorpay_merchant_name">
    <input type="hidden" value="Payment for Order ID: {{ $orderId }}" name="razorpay_description">
    <input type="hidden" value="{{ env('APP_URL') }}/payment/failure" name="razorpay_cancel_url">
    <button type="submit" class="btn btn-primary">Pay Now</button>
</form>

This view contains the Razorpay payment form, which we’re embedding using the Razorpay Checkout script. We’re also passing in the necessary data such as the Razorpay key, order ID, amount, currency, and prefill data.

Step 8: Create Payment Success and Failure Views

We also need to create views that indicate whether the payment was successful or not. Create two new files called payment-success.blade.php and payment-failure.blade.php in the resources/views directory and add the following code:

<!-- payment-success.blade.php -->
<h1>Payment Success</h1>
<p>Your payment was successful.</p>
<!-- payment-failure.blade.php -->
<h1>Payment Failure</h1>
<p>Your payment was not successful.</p>

Step 9: Test Razorpay Payment Gateway Integration

That’s it! You can now test the Razorpay payment gateway integration in your Laravel application. Visit the /payment route in your browser to see the payment form. Enter an amount and click the “Pay Now” button to initiate the payment flow. You’ll be redirected to the Razorpay payment page where you can complete the payment process. If the payment is successful, you’ll be redirected to the /payment/success route. If the payment fails, you’ll be redirected to the /payment/failure route.

Conclusion

In this tutorial, we learned how to integrate the Razorpay payment gateway in a Laravel 10 application. We installed the razorpay/razorpay package, configured Razorpay credentials, defined routes, created a PaymentController with methods to handle the payment flow, created views for the payment form and Razorpay payment page, and tested the integration. With the Razorpay payment gateway integration in place, your Laravel 10 application can now accept online payments securely and efficiently.

Share this:

Categorized in: