How to integrate Razorpay API in Laravel

Integrating payment gateways into web applications has become an essential part of e-commerce websites. In this tutorial, we will discuss how to integrate the Razorpay API into a Laravel application to accept online payments efficiently.

**Step 1: Register on Razorpay**

Before integrating the Razorpay API into your Laravel application, you need to create an account on the Razorpay platform. Once you have registered and logged in to your Razorpay account, you can access the API keys required for integration.

**Step 2: Install the Razorpay Package**

To integrate the Razorpay API into your Laravel application, you need to install the Razorpay PHP package. You can do this using Composer by running the following command in your terminal:

composer require razorpay/razorpay

**Step 3: Configure Razorpay API Keys**

After installing the Razorpay package, you need to configure your API keys in the .env file of your Laravel application. Add the following lines to your .env file:

RAZORPAY_KEY=YOUR_RAZORPAY_KEY
RAZORPAY_SECRET=YOUR_RAZORPAY_SECRET

Replace `YOUR_RAZORPAY_KEY` and `YOUR_RAZORPAY_SECRET` with your actual Razorpay API keys.

**Step 4: Create a Razorpay Payment Form**



Next, create a payment form in your Laravel application where users can enter their payment details. You can design the form as per your requirements, and on form submission, you need to process the payment using Razorpay API.

**Step 5: Process Payment Using Razorpay API**

In your Laravel controller, you can write the code to process the payment using the Razorpay API. Here is an example of how you can initiate a payment using Razorpay:

php
use Razorpay\Api\Api;

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

    $payment = $api->payment->create([
        'amount' => $request->amount * 100,
        'currency' => 'INR',
        'receipt' => 'receipt_' . uniqid(),
        'payment_capture' => 1
    ]);

    return response()->json($payment);
}

In this code snippet, we are creating a payment request with the specified amount, currency, and payment capture settings. You can customize the payment parameters as required for your application.

**Step 6: Handle Payment Response**

After initiating the payment using Razorpay API, you need to handle the payment response to update the payment status in your database. You can listen for the payment success or failure events and update the payment status accordingly.

By following these steps, you can seamlessly integrate the Razorpay API into your Laravel application to accept online payments securely. Razorpay provides comprehensive documentation and support for developers to integrate their payment gateway effectively.

Post Your Questions on our forum

Post a question on Forum

Ajith Jojo Joseph

Self taught, dedicated young entrepreneur with many licensed products under his sleeve. Passionate about technology, business and excellence in general.

Share with your friends:

Comments are closed.

How to integrate Paypal API in Laravel

Are you looking to integrate Paypal API in your Laravel project for seamless payment processing? Look no further! In this […]

April 3, 2024

How to integrate Razorpay API in Laravel

Integrating payment gateways into web applications has become an essential part of e-commerce websites. In this tutorial, we will discuss […]

April 3, 2024

Laravel 11 Ajax CRUD Operation Tutorial Example

**Mastering CRUD Operations with Laravel 11 Ajax: A Comprehensive Tutorial** In the world of web development, interaction between the front-end […]

April 3, 2024

Login as Client in Laravel – Login with user id

**Unlock the Power of Laravel with Login as Client – Login with User ID** Laravel, the popular PHP framework, offers […]

April 3, 2024

Digital Marketing Toolkit

Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits

Get Free Access Now