Hello Artisan,
In this Razorpay integration Laravel example tutorial, I will show you step by step that how we can integrate the Razorpay payment gateway in the Laravel 8 application. This tutorial will work in Laravel 5, 6, 7, 8 applications. So from this Razorpay payment gateway integration in Laravel tutorial, you will learn it from scratch.
By following this tutorial, we can easily integrate Razorpay payment gateway in Laravel 5, laravel 6, Laravel 7, and Laravel 8 applications. You know that Razorpay payment gateway is a complete package for online payment in India. So if you don't know how to integrate this Razorpay payment gateway then this example is for you.
Step 1: Download Laravel
As we are going to start from scratch, so we need a fresh Laravel application. So download a fresh Laravel application.
composer create-project --prefer-dist laravel/laravel blog
Step 2: Create Razorpay Account
First, you need to create an account on Razorpay. then you can easily get the account key id and key secret. Create an Account from here: www.razorpay.com.
After registering successfully. you need to go below link and get id and secret as below from this link https://dashboard.razorpay.com/app/keys.
Now update your .env like that:
.env
RAZORPAY_KEY=rzp_test_XXXXXXXXX
RAZORPAY_SECRET=XXXXXXXXXXXXXXXX
Step 3: Install razorpay/razorpay Package
Having completed the above step, we need to install razorpay/razorpay
composer package to use Razorpay API. so let's run bellow command:
composer require razorpay/razorpay
Step 4: Create Route
Now we will create one route for calling our example, so let's add a new route to the web.php
file as bellow:
routes/web.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 Controller
In this step, we will create a controller and write payment logic like below:
app/Http/Controllers/RazorpayPaymentController.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 Blade File
All are almost set to go. Just now we need to add a blade file. so let's create razorpayView.blade.php
file and put bellow code:
resources/views/razorpayView.blade.php
Visit this link and you can get a testing card for Razorpay from here: Click Here
Hope this Razorpay payment gateway integration tutorial will help you.
#laravel #laravel-8x