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
How to Validate Reactive Form in Angular 17
How to Validate Reactive Form...

Hello developer, In this article, we'll see how to validate a reactive form in angular 17. Reactive forms...

Read More

Mar-27-2024

How To Get Current Date And Time In React JS
How To Get Current Date And Ti...

In this article, we will see how to get the current date and time in react js. You can get the current date and tim...

Read More

Sep-02-2022

How to File Upload in Laravel 11 Livewire
How to File Upload in Laravel...

Hello, laravel web developers! In this article, we'll see how to file upload in laravel 11 Livewire. Here, we'll...

Read More

Jun-10-2024

How to Create Apexcharts Line Chart in Laravel 11
How to Create Apexcharts Line...

Hello developers! In this article, we'll see how to create apexcharts line chart in laravel 11. ApexCharts is a...

Read More

Apr-22-2024