Hello, web developers! In this article, we'll see how to integrate a cashfree payment gateway in laravel 10. Cashfree provides a robust and secure solution for handling payments, and we're going to seamlessly integrate it into our Laravel 10 project.
Cashfree Payment Gateway accepts domestic and international payments. It also supports a wide range of payment options. You can easily collect payments from your customers using its pre-built checkout integration, or customize the payment flow as per your requirements.
You can easily collect payments using popular Payment Methods like Cards, UPI, net banking, Wallets, PayPal, EMI, and Pay Later options.
Laravel 10 Integrate Cashfree Payment Gateway
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>Laravel 10 Integrate Cashfree Payment Gateway - Vidvatek</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 - Vidvatek</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
You might also like:
- Read Also: Laravel 10 Livewire Create Select2 Dropdown
- Read Also: Laravel 10 REST API Authentication using Sanctum
- Read Also: How To Integrate Paypal Payment Gateway In Laravel 10
- Read Also: Laravel 10 and Vue 3 Razorpay Payment Gateway Integration