How to INTEGRATION Razorpay Payment Gateway in Laravel 10

Spread the love

Hello Guys,

In this guide, we will delve into the seamless integration of Laravel 10 with the Razorpay Payment Gateway. You’ll find a practical Laravel 8 integration Razorpay payment gateway example and a comprehensive, step-by-step guide on how to connect Laravel 8, Laravel 9, Laravel 10, and Razorpay. Explore the interaction between Laravel 8 and Razorpay, and then, let’s proceed to create a Laravel 8 Razorpay API integration in a few straightforward steps.

Razorpay, India’s prominent payment gateway, offers a plethora of payment options for users, including credit cards, debit cards, UPI, PhonePe, Google Pay, and Paytm. If you’re looking to seamlessly integrate Razorpay into your Laravel application, you’re in the right place.

then follow the instructions listed below for INTEGRATION Razorpay Payment Gateway:

Step 1: Install Laravel

To obtain a new Laravel version application, begin by launching your terminal or command prompt and entering the following command:

Also Read: How to Run Specific Seeder in Laravel?

composer create-project --prefer-dist laravel/laravel blog

Step 2: Create Razorpay Account

To get started, it’s essential to sign up for a Razorpay account. Once you’ve completed the registration, obtaining your account key ID and key secret is a straightforward process.

To begin, you can create your Razorpay account by visiting this link: www.razorpay.com.

After a successful registration, proceed to the following link to acquire your ID and secret, as illustrated in the accompanying screenshot:

Access this page here: https://dashboard.razorpay.com/app/keys.

Following this, you can easily create an .env file and retrieve your account key ID and secret, as demonstrated below.

.env
RAZORPAY_KEY=rzp_test_XXXXXXXXX
RAZORPAY_SECRET=XXXXXXXXXXXXXXXX

Step 3: Install razorpay/razorpay Package

To utilize the Razorpay API effectively, it’s crucial to install the razorpay/razorpay Composer package. Follow these instructions to proceed:

Also Read: Model Events Example in LARAVEL 10

composer require razorpay/razorpay

Step 4: Create a Route

Now that we’ve established a route, it’s time to integrate it into the web.php file, allowing easy access to our example.

routes/web.php
<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\RazorpayPaymentController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('razorpay-payment', [RazorpayPaymentController::class, 'index']);
Route::post('razorpay-payment', [RazorpayPaymentController::class, 'store'])->name('razorpay.payment.store');

Step 5: Create a Controller

In this phase, we will craft the SMS sending functionality and establish the RazorpayPaymentController. To do so, we will introduce a fresh route to the web.php file, as illustrated here:

Also Read: How to use Ajax Autocomplete Search using JQUERY UI in laravel 10

app/Http/Controllers/RazorpayPaymentController.php
<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Razorpay\Api\Api;
use Session;
use Exception;
  
class RazorpayPaymentController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {        
        return view('razorpayView');
    }
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function store(Request $request)
    {
        $input = $request->all();
  
        $api = new Api(env('RAZORPAY_KEY'), env('RAZORPAY_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 Blade File

To include the blade file, you’ll need to create a file named ‘razorpayView.blade.php‘ and insert the provided code.

resources/views/razorpayView.blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>Laravel - Razorpay Payment Gateway Integration</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
    <div id="app">
        <main class="py-4">
            <div class="container">
                <div class="row">
                    <div class="col-md-6 offset-3 col-md-offset-6">
  
                        @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
  
                        @if($message = Session::get('success'))
                            <div class="alert alert-success alert-dismissible fade {{ Session::has('success') ? 'show' : '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
  
                        <div class="card card-default">
                            <div class="card-header">
                                Laravel - Razorpay Payment Gateway Integration
                            </div>
  
                            <div class="card-body text-center">
                                <form action="{{ route('razorpay.payment.store') }}" method="POST" >
                                    @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://www.itcodestuff.com/frontTheme/images/logo.png"
                                            data-prefill.name="name"
                                            data-prefill.email="email"
                                            data-theme.color="#ff7529">
                                    </script>
                                </form>
                            </div>
                        </div>
  
                    </div>
                </div>
            </div>
        </main>
    </div>
</body>
</html>

Access the URL effortlessly with the following command:

Also Read: how to use Flash Message using LARAVEL 10

Run Project:

php artisan serve
Open URL:
localhost:8000/razorpay-payment

Obtain a Razorpay testing card easily – Click Here to access it.

Discover the best in Laravel blog through our thoughtfully crafted content.

Leave a Comment