Laravel

How to Integrate Razorpay Payment Gateway in Laravel?

In today’s digital era, integrating payment gateways into web applications has become a crucial aspect for businesses to facilitate online transactions seamlessly. Laravel offers robust support for integrating various payment gateways. Among the plethora of options available, Razorpay stands out as a reliable and developer-friendly payment gateway solution for Indian businesses. In this blog post, we’ll walk through the step-by-step process of integrating Razorpay payment gateway into a Laravel application, making the payment process smooth and secure.

Steps to Integrate Razorpay Payment Gateway in Laravel:

Step 1: Setting Up Razorpay Account

Before diving into the integration process, you need to sign up for a Razorpay account if you haven’t already. Once registered, you’ll obtain API keys (Key ID and Key Secret) from your Razorpay dashboard. These keys will be used to authenticate your Laravel application with Razorpay’s services.

Step 2: Installing Razorpay Package

In Laravel, integrating Razorpay is made easier with the help of community-contributed packages. One such package is anandsiddharth/laravel-razorpay, which provides a wrapper around Razorpay’s PHP SDK. You can install it via Composer:

composer require anandsiddharth/laravel-razorpay

After installation, don’t forget to publish the configuration file:

php artisan vendor:publish –provider="Anand\LaravelRazorpay\Providers\RazorpayServiceProvider"

Step 3: Configuration

Open config/razorpay.php and enter your Razorpay API Key ID and Secret Key obtained from the Razorpay dashboard.

Step 4: Creating Routes and Controllers

Set up routes to handle payment initiation and success/failure callbacks. For instance:

Route::post('/payment', 'PaymentController@create')->name('payment.create');
Route::post('/payment/success', 'PaymentController@success')->name('payment.success');
Route::post('/payment/failure', 'PaymentController@failure')->name('payment.failure');

Create a PaymentController using Artisan command:

php artisan make:controller PaymentController

Implement methods create, success, and failure in the PaymentController to handle payment initiation, success callback, and failure callback, respectively.

Step 5: Initiating Payment

In the create method of PaymentController, initiate the payment using Razorpay API:

use Razorpay\Api\Api;

public function create(Request $request)
{
    $api = new Api(config('razorpay.key'), config('razorpay.secret'));
    
    $order = $api->order->create([
        'receipt'         => 'order_rcptid_' . rand(),
        'amount'          => $request->amount * 100, // Amount in paisa
        'currency'        => 'INR',
        'payment_capture' => 1 // Auto capture payment
    ]);

    return view('payment')->with([
        'order' => $order,
        'key' => config('razorpay.key')
    ]);
}

Step 6: Integrating Razorpay Checkout

Create a view (e.g., payment.blade.php) to render the payment form. Include Razorpay’s JavaScript SDK and initiate payment using the order ID obtained from the controller.

<form action="{{ route('payment.success') }}" method="POST">
    <script src="https://checkout.razorpay.com/v1/checkout.js" data-key="{{ $key }}"></script>
    <input type="hidden" name="order_id" value="{{ $order->id }}">
    <button type="submit">Pay Now</button>
</form>

Step 7: Handling Callbacks

In the success and failure methods of PaymentController, handle the payment success and failure callbacks respectively.

Conclusion:

Integrating Razorpay payment gateway into your Laravel application simplifies the online payment process for your customers while ensuring security and reliability. With the steps outlined in this guide, you can seamlessly integrate Razorpay into your Laravel project and start accepting online payments hassle-free.

Connect with Laravel Developers to seamlessly integrate payment methods for your web application.

Happy Coding!

Click to rate this post!
[Total: 0 Average: 0]
Bharat Desai

Bharat Desai is a Co-Founder at MageComp. He is an Adobe Magento Certified Frontend Developer ? with having 8+ Years of experience and has developed 150+ Magento 2 Products with MageComp. He has an unquenchable thirst to learn new things. On off days you can find him playing the game of Chess ♟️ or Cricket ?.

Recent Posts

Upgrade Your E-commerce Store with Magento 2 Hyvä Theme

Magento 2 Hyvä Theme is quickly becoming a popular choice among e-commerce businesses for its…

18 hours ago

A Complete Guide of Hyvä Themes

In the rapidly evolving world of e-commerce, the success of an online store greatly hinges…

19 hours ago

Magento 2: How to Add Custom Button to Download Custom Created PDF Programmatically in Admin Sales Order View

Hello Magento Friends, Ever wanted to provide admins with a quick way to download custom…

19 hours ago

Mastering Tailwind CSS in Laravel: A Comprehensive Guide

Tailwind CSS has emerged as a powerful utility-first CSS framework, offering developers a unique approach…

6 days ago

React Native or Flutter in 2024

The mobile app development field has witnessed a rapid revolution over the past few years.…

1 week ago

Magento 2: How To Call JS on the Checkout Page?

Hello Magento mates, Today we will learn to add a call JS on the checkout…

2 weeks ago