Razorpay Payment Gateway Integration in Laravel

In this post we will learn razorpay payment gateway integration in laravel. i explain simply step by step razorpay integration in laravel. we weill explain laravel razorpay integration.

Razorpay payment gateway is available for india. razorpay provide lot’s of options such as credit card, debit card, phone pay, UPI, google pay, paytm payment option to do payment of user.

This post help for razorpay integration in laravel 6, 7, 8. also i will show razorpay payment integration in laravel.

Read also : Laravel Dynamic Dependent Select Box using jQuery Ajax

Step 1: Install Laravel

In this step we will install laravel for razorpay payment integration. So let’s following command to install laravel.

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

Step 2: Create Account in Razorpay

In this step we will create razorpay account and this account we will use for laravel razorpay integration. Create Account from here: www.razorpay.com.

Step 3 : Install Razorpay Package

In this step we will install razorpay/razorpay package in laravel for razorpay integration in laravel. Now following command and run in your terminal.

Read also : PayPal Integration in Laravel Example

composer require razorpay/razorpay

 

Step 4 : Add Key And Secret Key

In this step we will add key and secret key in .env file for razorpay api integration, you can find these key from setting menu of razor pay dashboard.

Read Also :  Laravel Migration Add Default Value Example

.env

RAZORPAY_KEY=rzp_test_XXXXXXXXX
RAZORPAY_SECRET=XXXXXXXXXXXXXXXX
Step 5 : Create Route
Route::get('razorpay', [RazorpayController::class, 'razorpay'])->name('razorpay');
Route::post('razorpaypayment', [RazorpayController::class, 'payment'])->name('payment');
Step 6 : Create Controller

In this step we well create RazorpayController in this location app/Http/Controllers.

<?php

namespace App\Http\Controllers;

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

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

    public function payment(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, your order will be despatched in the next 48 hours.');
        return redirect()->back();
    }
}
Step 7 : Create View

In this step we will create view file for form and output , we have save as index.blade.php file.

<!DOCTYPE html>
<html>
<head>
    <title>Razorpay payment gateway integration in laravel - web-tuts.com</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>Razorpay payment gateway integration in laravel - web-tuts.com </h3><br>
                <div class="panel-heading">
                    <h2>Pay With Razorpay</h2>

                <!-- <div class="panel-body text-center"> -->
                    <form action="{!!route('payment')!!}" method="POST" >                        
                        <script src="https://checkout.razorpay.com/v1/checkout.js"
                                data-key="{{ env('RAZORPAY_KEY') }}"
                                data-amount="1000"
                                data-buttontext="Pay 10 INR"
                                data-name="Webtuts"
                                data-description="Payment"
                                data-image="https://web-tuts.com/abc_content/uploads/2021/01/web-tuts.png"
                                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>

I hope you understand of laravel razorpay payment integration and it can help you..

Read Also :  Laravel Resize Image Before Upload