How to Integrate RazorPay Payment Gateway in Laravel 10 - Part 2

Category: Laravel
Updated on: 22 Dec, 2024
First, you have to install a fresh version of Laravel 10. You can go to the official documentation of Laravel:
You will see a section there: "Creating A Laravel Project". Here you will get your codes to install Laravel 10. I am going to write the commands you will need to install laravel. First, open your terminal where you want to install laravel on your computer. Then use these commands:
composer create-project laravel/laravel example-app
cd example-app
php artisan serve
You can run your application now using this url into your browser: http://127.0.0.1:8000
Now go to this location: resources > views > welcome.blade.php.

Change the codes of this page like this:
welcome.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Laravel - Razorpay Integration</title>
</head>
<body>
    <h2>Product: Laptop</h2>
    <h3>Price: ₹20</h3>
    <form action="{{ route('razorpay') }}" method="post">
        @csrf
        <script src="https://checkout.razorpay.com/v1/checkout.js"
        data-key = "{{ env('RAZORPAY_KEY_ID') }}"
        data-amount = "2000"
        data-buttontext = "Pay with Razorpay"
        data-image = "https://uxwing.com/wp-content/themes/uxwing/download/brands-and-social-media/razorpay-icon.png"
        data-notes.customer_name = "Morshedul Arefin"
        data-notes.customer_email = "codewitharefin@gmail.com"
        data-notes.product_name = "Laptop"
        data-notes.quantity = "1"
        data-prefill.name = "Morshedul Arefin"
        ></script>
    </form>
</body>
</html>
Go to this link to install a razorpay package in php:
Go to your terminal and run this command:
composer require "razorpay/razorpay:2.*"
Create a migration file using this command:
php artisan make:migration create_payments_table
A migration file will be created in this location: "database > migrations > 2023_11_23_093629_create_payments_table.php"

Now write the following code here:
2023_11_25_094343_create_payments_table.php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('payments', function (Blueprint $table) {
            $table->id();
            $table->string('payment_id');
            $table->string('product_name');
            $table->string('quantity');
            $table->string('amount');
            $table->string('currency');
            $table->string('customer_name');
            $table->string('customer_email');
            $table->string('payment_status');
            $table->string('payment_method');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('payments');
    }
};
Write this command to create the "payments" table.
php artisan migrate
You have to create a model now. The model name will be the singular form of the table name. So it will be "Payment". Run this command in the terminal to create this model:
php artisan make:model Payment
I am going to create a controller now. So run this command in terminal:
php artisan make:controller RazorpayController
Modify the "route > web.php" file as this:
web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\RazorpayController;

Route::get('/', function () {
    return view('welcome');
});

Route::post('razorpay', [RazorpayController::class, 'razorpay'])->name('razorpay');
Route::get('success', [RazorpayController::class, 'success'])->name('success');
Route::get('cancel', [RazorpayController::class, 'cancel'])->name('cancel');
In previous section, we got the key id and key secret. See this:
Key ID: rzp_test_LtILAoYF0sW1mM
Key Secret: dAdKJkzK0mHYO6nokbjxgoIx
Now you will have to use these values in your application. So, add the following lines in the bottom of your .env file:
RAZORPAY_KEY_ID=rzp_test_LtILAoYF0sW1mM
RAZORPAY_KEY_SECRET=dAdKJkzK0mHYO6nokbjxgoIx
RazorpayController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Payment;
use Razorpay\Api\Api;

class RazorpayController extends Controller
{
    public function razorpay(Request $request)
    {
        //dd($request->all());
        if(isset($request->razorpay_payment_id) && $request->razorpay_payment_id != '')
        {
            $api = new Api(env('RAZORPAY_KEY_ID'), env('RAZORPAY_KEY_SECRET'));
            $payment = $api->payment->fetch($request->razorpay_payment_id);
            //dd($payment);
            $response = $payment->capture(array('amount'=>$payment->amount));
            //dd($response);

            $payment = new Payment();
            $payment->payment_id = $response['id'];
            $payment->product_name = $response['notes']['product_name'];
            $payment->quantity = $response['notes']['quantity'];
            $payment->amount = $response['amount']/100;
            $payment->currency = $response['currency'];
            $payment->customer_name = $response['notes']['customer_name'];
            $payment->customer_email = $response['notes']['customer_email'];
            $payment->payment_status = $response['status'];
            $payment->payment_method = 'Razorpay';
            $payment->save();

            return redirect()->route('success');

        } else {
            return redirect()->route('cancel');
        }
        
    }
    public function success()
    {
        return "Payment successful";
    }
    public function cancel()
    {
        return "Payment failed";
    }   
}
See Full Tutorial on YouTube
If you want to see the full functionality in live, you can see my video on youtube.

Download Source Code

Please give your email address. We will send you the source code on your email.