Error occurs during Cashfree payment gateway integration in Laravel

35 views Asked by At

Every attempt to integrate the Cashfree payment gateway into my Laravel application results in the same error.

[2024-03-26 17:19:44] local.DEBUG: Input data for payment initiation: {"orderId":"inv004","orderAmount":"0963462492","customerName":"Manuel Portillo","customerEmail":"[email protected]","customerPhone":"7365435505","returnUrl":"https://payment.remwhacktechnologies.com/payment/callback","version":"2021-05-21"} [2024-03-26 17:19:45] local.ERROR: Error initiating payment: 400 - {"message":"version value should be 2021-05-21 or 2022-01-01 or 2022-09-01 or 2023-08-01","code":"request_failed","type":"invalid_request_error"}

I have changed the version year and tried all possibilities, but I consistently receive the same error. The code below is from my controller, where I am initiating my payment

public function initiatePayment(Request $request)
{
    try {
        // Perform validation on $request->input() if necessary

        $clientId = 'TEST10152050eee78259b07eef2a86fc05025101'; // Your Cashfree client ID
        $clientSecret = 'cfsk_ma_test_303cb4db3e6ad084b222959b7014db1a_0161753d'; // Your Cashfree client secret

        $orderId = 'inv004'; // Generate a unique order ID
        $orderAmount = $request->input('amount');
        $customerName = $request->input('name');
        $customerEmail = $request->input('email');
        $customerPhone = $request->input('phone');
        $returnUrl = 'https://payment.remwhacktechnologies.com/payment/callback'; // Ensure the return URL is correct
        $version = '2021-05-21'; // Set the correct version parameter

        // Log input data for debugging
        Log::debug('Input data for payment initiation:', [
            'orderId' => $orderId,
            'orderAmount' => $orderAmount,
            'customerName' => $customerName,
            'customerEmail' => $customerEmail,
            'customerPhone' => $customerPhone,
            'returnUrl' => $returnUrl,
            'version' => $version,
        ]);

        $response = Http::withHeaders([
            'X-Client-Id' => $clientId,
            'X-Client-Secret' => $clientSecret,
            'Content-Type' => 'application/json',
            'Accept' => 'application/json',
        ])->post('https://sandbox.cashfree.com/pg/orders', [
            'orderId' => $orderId,
            'orderAmount' => $orderAmount,
            'customerName' => $customerName,
            'customerEmail' => $customerEmail,
            'customerPhone' => $customerPhone,
            'returnUrl' => $returnUrl,
            'version' => $version, // Include the version parameter
        ]);

        if ($response->successful()) {
            // Redirect user to payment gateway
            return redirect($response['paymentLink']);
        } else {
            // Log error response
            Log::error('Error initiating payment: '.$response->status().' - '.$response->body());

            // Handle unsuccessful response
            return response()->json(['error' => 'Error initiating payment']);
        }
    } catch (RequestException $e) {
        // Log exception
        Log::error('Error initiating payment: '.$e->getCode().' - '.$e->getMessage());

        // Handle request exception
        return response()->json(['error' => 'Error initiating payment']);
    }
}

I have attempted to use all available versions, including 2021-05-21, 2022-01-01, 2022-09-01, and 2023-08-01, but the error persists. As a newcomer to PHP and Laravel, I would greatly appreciate any help you can give me.

1

There are 1 answers

1
Prem On

This is deprecated method. Instead of this try cashfree latest php sdk i.e. https://github.com/cashfree/cashfree-pg-sdk-php

Install using composer

composer require cashfree/cashfree-pg

For your use case sample code is

public function initiatePayment(Request $request)
{
    Cashfree::$XClientId = "XXXXXXXXX_Client_ID";
    Cashfree::$XClientSecret = "XXXXXXX_Client_Secret_Key";
    Cashfree::$XEnvironment = Cashfree::$SANDBOX;
    $cashfree = new Cashfree();

    $x_api_version = "2022-09-01";
    $orderId = 'inv004'; // Generate a unique order ID
    $orderAmount = $request->input('amount');
    $customerName = $request->input('name');
    $customerEmail = $request->input('email');
    $customerId = $request->input('customerId');
    $customerPhone = $request->input('phone');
    $returnUrl = 'https://payment.remwhacktechnologies.com/payment/callback'; // Ensure the return URL is correct
    $create_orders_request = new CreateOrderRequest();
    $create_orders_request->setOrderId($orderId);
    $create_orders_request->setOrderAmount($orderAmount);
    $create_orders_request->setOrderCurrency("INR");
    $customer_details = new CustomerDetails();
    $customer_details->setCustomerName($customerName);
    $customer_details->setCustomerId($customerId);
    $customer_details->setCustomerEmail($customerEmail);
    $customer_details->setCustomerPhone($customerPhone);
    $create_orders_request->setCustomerDetails($customer_details);
    $order_meta = new OrderMeta();
    $order_meta->setReturnUrl($returnUrl);
    $create_orders_request->setOrderMeta($order_meta);

    try {
        $result = $cashfree->PGCreateOrder($x_api_version, $create_orders_request);
        dd($result);
    } catch (Exception $e) {
        echo 'Exception when calling PGCreateOrder: ', $e->getMessage(), PHP_EOL;
    }
}

Make sure you have imported all necessary dependency