How to Integrate Cashfree Payment Gateway in Laravel 10

Websolutionstuff | Feb-12-2024 | Categories : Laravel PHP

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.

Cashfree-Payment-Gateway

let's make payment integration a breeze! 💳🚀

Step 1: Sign Up on Cashfree

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.

 

Step 2: Configure Cashfree Credentials

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.

 

Step 3: Create Payment Controller

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
     }
}

 

Step 4: Set Up Routes

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');

 

Step 5: Create Payment Status View

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>

 

Step 6: Test Your Integration

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:

Recommended Post
Featured Post
Laravel 8 Eloquent orWhereHas Condition
Laravel 8 Eloquent orWhereHas...

In this example we will see laravel 8 eloquent orWhereHas() condition. In previous example we will learn about ...

Read More

Oct-15-2021

How To Upload Multiple Image In Laravel 9
How To Upload Multiple Image I...

 In this article, we will see how to upload multiple images in laravel 9. here, we will see the tutorial of multipl...

Read More

Mar-18-2022

How To File Upload In Angular 15 Example
How To File Upload In Angular...

As an Angular 15 developer, I understand the significance of incorporating file upload functionality into web applicatio...

Read More

Jun-19-2023

How To Install React In Laravel 9
How To Install React In Larave...

In this article, we will see how to install React in laravel 9. We will also install react with laravel 9 and...

Read More

Aug-15-2022