How to integrate Razorpay Payment Gateway in Laravel

Razorpay Payment Gateway Integration in Laravel

Hello everyone! today will teach you how to use the Razorpay payment gateway integration in step by step.

Today, we’re going to take a look at how to integrate Razorpay Payment Gateway in Laravel With the ever-growing e-commerce industry, it is important for businesses of all sizes and types to have access to reliable payment gateways that can make transactions easy and secure. That’s why Razorpay stands out as one of the best solutions in India when it comes to payment processing.

How to integrate Razorpay Payment Gateway in Laravel 4

Razorpay allows businesses not only to accept but also to process and disburse payments with its product suite. It supports domestic & international credit/debit cards, EMIs (Credit/Debit Cards & Cardless), PayLater options, and Netbanking from 58 banks along with UPI & 8 mobile wallets making sure you get the most extensive set of payment methods available today! The integration process itself is quite simple – just like any other payment gateway – so let’s dive right into it now!

Integrating Razorpay with your Laravel project is really easy and straightforward. All that’s required of you is to create a new account on their website using the link provided below.

Once that’s done, it’s time to create a test account so we can start testing our integration right away! This process should only take a few minutes so don’t worry if it seems intimidating at first – once everything has been set up correctly, integrating your project with Razorpay will become much easier over time as well as more secure too which makes this an even better option than other alternatives out there right now!

To integrate the Razorpay payment gateway in Laravel, you will need to follow these steps:

  1. Create your Razorpay Account
  2. Install the Razorpay PHP library
  3. Add the Razorpay API keys
  4. Create a route for payment
  5. Create a new payment controller
  6. Create a view file
  7. Test the integration

Step 1 : Create your Razorpay Account

First of all we need to create razorpay account on razorpay official website and submit all necessary documents and activate testing account for test integration.

Step 2 : Install the Razorpay PHP Library

Now we need to Install the Razorpay PHP library by running the command composer require razorpay/razorpay in your project’s root directory.

composer require razorpay/razorpay

Step 3 : Add the Razorpay API keys

Now, we need to add the Razorpay API keys (Key ID and Key Secret) to your .env file and file is available on root directory.

RAZOR_KEY=xxxxx
RAZOR_SECRET=xxxxx
How to integrate Razorpay Payment Gateway in Laravel 5

Step 4 : Create a route for payment

In this step, we are creating routes in web.php

Route::get('payment', [RazorpayController::class, 'payment'])->name('payment');
Route::post('save-payment', [RazorpayController::class, 'savepayment'])->name('save-payment');

Step 5 : Create a new payment controller

Create a new controller for handling the payment process and add the necessary functions for creating and capturing payments.

Now we are creating PaymentController.php and the location is app/Http/Controllers.

    <?php

namespace App\Http\Controllers;

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

class PaymentController extends Controller
{
    public function razorpay()
    {        
        return view('index');
    }

    public function payment(Request $request)
    {        
        $input = $request->all();        
        $api = new Api(env('RAZOR_KEY'), env('RAZOR_SECRET'));
        $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();
            }            
        }
        
        \Session::put('success', 'Payment successful.');
        return redirect()->back();
    }
}

Step 6 : Create a view file

Create a new view file for displaying the payment form and use the Razorpay JavaScript library to create a payment form and file location is resources\views.

<!DOCTYPE html>
<html>
<head>
    <title>How To Integrate Razorpay Payment Gateway In Laravel - Coding Tutes</title>
    <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.0/js/bootstrap.min.js"></script>    
</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" style="margin-top: 30px;">
                <h3>How To Integrate Razorpay Payment Gateway In Laravel - Coding Tutes</h3><br>
                <div class="panel-heading">
                    <h2>Pay With Razorpay</h2>
                
                    <form action="{!!route('payment')!!}" method="POST" >
                        <script src="https://checkout.razorpay.com/v1/checkout.js"
                                data-key="{{ env('RAZOR_KEY') }}"
                                data-amount="5000"
                                data-buttontext="Proceed to Pay"
                                data-name="Coding Tutes"
                                data-description="Test Payment"
                                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>

Test Card Details

Card TypeCard NumberCVVExpiry Date
Visa4111 1111 1111 1111Random CVVAny future date

Finally, test the integration by making a payment and check the Razorpay dashboard to confirm that the payment was successful.

You can find more information and a detailed guide on how to integrate the Razorpay payment gateway in Laravel in the Razorpay documentation.

Exit mobile version