Hello developers! Today, we're about to embark on a journey to elevate our Laravel applications by integrating the Cashfree Payment Gateway. Cashfree provides a robust and secure solution for handling payments, and we're going to seamlessly integrate it into our Laravel 10 project.
So, let's see how to integrate cashfree payment gateway in laravel 10, laravel 10 integrate cashfree payment gateway, and payment gateway integration in laravel 8/9/10.
let's make payment integration a breeze! 💳🚀
First things first, head over to the Cashfree website and sign up for an account. Once you're in, navigate to the dashboard and create a new app to get your API key and secret.
Now, let's configure the Cashfree credentials. Open your .env
file and add the following:
CASHFREE_APP_ID=your_app_id
CASHFREE_SECRET_KEY=your_secret_key
CASHFREE_MODE=test # Change to 'live' for production
Replace your_app_id
and your_secret_key
with the credentials obtained from the Cashfree dashboard.
Generate a new controller using the Artisan command:
php artisan make:controller PaymentController
Now, open app/Http/Controllers/PaymentController.php
and add the following code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class PaymentController extends Controller
{
public function create(Request $request)
{
return view('payment-create');
}
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|min:3',
'email' => 'required',
'mobile' => 'required',
'amount' => 'required'
]);
$url = "https://sandbox.cashfree.com/pg/orders";
$headers = array(
"Content-Type: application/json",
"x-api-version: 2022-01-01",
"x-client-id: ".env('CASHFREE_API_KEY'),
"x-client-secret: ".env('CASHFREE_API_SECRET')
);
$data = json_encode([
'order_id' => 'order_'.rand(1111111111,9999999999),
'order_amount' => $validated['amount'],
"order_currency" => "INR",
"customer_details" => [
"customer_id" => 'customer_'.rand(111111111,999999999),
"customer_name" => $validated['name'],
"customer_email" => $validated['email'],
"customer_phone" => $validated['mobile'],
],
"order_meta" => [
"return_url" => 'http://127.0.0.1:8000/cashfree/payments/success/?order_id={order_id}&order_token={order_token}'
]
]);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$resp = curl_exec($curl);
curl_close($curl);
return redirect()->to(json_decode($resp)->payment_link);
}
public function success(Request $request)
{
info($request->all()); // PAYMENT STATUS RESPONSE
}
}
Open routes/web.php
and add the following routes:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PaymentController;
/*
|--------------------------------------------------------------------------
| 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('cashfree/payments/create', [PaymentController::class, 'create'])->name('callback');
Route::post('cashfree/payments/store', [PaymentController::class, 'store'])->name('store');
Route::any('cashfree/payments/success', [PaymentController::class, 'success'])->name('success');
Create a new view file resources/views/payment-create.blade.php
to display the payment status:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Integrating Cashfree Payment Gateway with Laravel 10 - Websolutionstuff</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
<div class="container mt-3">
<div class="row justify-content-center">
<div class="col-12 col-md-6 mb-3">
<div class="card text-dark bg-light mb-3">
<div class="card-header text-center">
<h3 class="card-title " style="display: inline-block;">Create New Payment - Websolutionstuff</h3>
</div>
<div class="card-body">
<form action="{{ route('store') }}" method="POST" name="laravel9-cashfree-integration">
@csrf
<div class="form-floating py-2">
<input type="text" class="form-control" name="name" id="name" value="{{ old('name') }}" placeholder="name">
<label for="name">Full Name</label>
</div>
<div class="form-floating py-2">
<input type="email" class="form-control" name="email" id="email" value="{{ old('email') }}" placeholder="email">
<label for="email">Email Address</label>
</div>
<div class="form-floating py-2">
<input type="text" class="form-control" name="mobile" id="mobile" value="{{ old('mobile') }}" placeholder="mobile">
<label for="mobile">Mobile Number</label>
</div>
<div class="form-floating py-2">
<input type="text" class="form-control" name="amount" id="amount" value="{{ old('amount') }}" placeholder="amount">
<label for="amount">Amount</label>
</div>
<button class="w-100 btn btn-lg btn-success" type="submit">Place Order</button>
</form>
@if ($errors->any())
<div class="alert alert-danger text-start" role="alert">
<strong>Opps!</strong> Something went wrong<br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Run the following command and test cashfree payment gateway integration in laravel 10.
php artisan serve
And there you have it! You've successfully integrated the Cashfree Payment Gateway into your Laravel 10 application.
You might also like:
In this article, we will see the google line chart example in laravel 8. Google charts use to visualize d...
Feb-24-2021
Hello, laravel web developer! In this article, we'll see how to image upload in laravel 11 vue 3. Here, we'...
May-27-2024
In today's digital age, the ability to facilitate online payments efficiently is crucial for businesses and develope...
Oct-09-2023
In this article, we will see laravel 9 one to one relationship example. Also, you can use one to one re...
Apr-01-2022