How to integration cashfree payment gateway in codeigntier 3 - deesoft service

How to integration cashfree payment gateway in codeigntier 3

Deepak Tailor Image
Deepak Tailor - Feb 12 2023
How to integration cashfree payment gateway in codeigntier 3

Here's a general outline of the steps to integrate Cashfree payment gateway in CodeIgniter 3:

1. Create an account on the Cashfree Developer Dashboard to obtain your API keys (Test and Production).

2. Download and install CodeIgniter 3.

3. Create a new controller to handle the payment process. You can call it Payment.

4. In the controller, create a method to initiate the payment process, for example, initiatePayment.

5. In the initiatePayment method, use the Cashfree API to generate a payment request by sending the required parameters such as amount, orderId, and customer details to the Cashfree server.

6. On the Cashfree server, the payment request will be processed and a unique payment URL will be generated.

7. Return the payment URL to the client and redirect the user to the payment page by calling the redirect method of CodeIgniter.

8. After successful payment, Cashfree will notify your application through a callback URL. You need to implement a method in the controller to handle the callback request.

9. In the callback method, use the Cashfree API to verify the transaction status and update your database accordingly.

Here's sample code for some of the steps mentioned above:

class Payment extends CI_Controller {

  public function initiatePayment() {
    // Generate payment request using Cashfree API
    // ...

    // Redirect to payment page
    redirect($payment_url);
  }

  public function callback() {
    // Verify transaction status using Cashfree API
    // ...

    // Update database
    // ...
  }
}
class Payment extends CI_Controller {

  public function initiatePayment() {
    // Get payment details from form or database
    $amount = 100;
    $orderId = "order_".time();
    $customerName = "John Doe";
    $customerEmail = "john.doe@example.com";
    $customerPhone = "9876543210";
    $callbackUrl = base_url("payment/callback");

    // Generate payment request using Cashfree API
    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://test.cashfree.com/api/v2/order/create",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => "{\n\t\"appId\":\"YOUR_APP_ID\",\n\t\"orderId\":\"$orderId\",\n\t\"orderAmount\":$amount,\n\t\"orderCurrency\":\"INR\",\n\t\"orderNote\":\"Test order\",\n\t\"customerName\":\"$customerName\",\n\t\"customerPhone\":\"$customerPhone\",\n\t\"customerEmail\":\"$customerEmail\",\n\t\"returnUrl\":\"$callbackUrl\",\n\t\"notifyUrl\":\"$callbackUrl\"\n}",
      CURLOPT_HTTPHEADER => array(
        "Content-Type: application/json",
        "x-client-id: YOUR_CLIENT_ID",
        "x-client-secret: YOUR_CLIENT_SECRET"
      ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);

    if ($err) {
      // Handle error
    } else {
      $response = json_decode($response);
      $payment_url = $response->paymentUrl;
      // Redirect to payment page
      redirect($payment_url);
    }
  }

  public function callback() {
    // Get the order id from the request
    $orderId = $this->input->post("orderId");

    // Verify transaction status using Cashfree API
    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://test.cashfree.com/api/v2/order/details",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_
      FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => "{\n\t\"appId\":\"YOUR_APP_ID\",\n\t\"orderId\":\"$orderId\"\n}",
      CURLOPT_HTTPHEADER => array(
        "Content-Type: application/json",
        "x-client-id: YOUR_CLIENT_ID",
        "x-client-secret: YOUR_CLIENT_SECRET"
      ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);

    if ($err) {
      // Handle error
    } else {
      $response = json_decode($response);
      $status = $response->status;
      // Update database based on transaction status
      if ($status === "SUCCESS") {
        // Update database to mark payment as successful
      } else {
        // Update database to mark payment as failed
      }
    }
  }
}

Note: Replace YOUR_APP_ID, YOUR_CLIENT_ID, and YOUR_CLIENT_SECRET with your actual API keys obtained from the Cashfree Developer Dashboard. Also, make sure to use the appropriate API endpoint (test or production) based on your testing or live environment.

Deepak Tailor Image
Deepak Tailor

My name is Deepak tailor as a fullstack developer. I have been in the IT industry (PHP, Nodejs, flutter) for the last 5 years. For professional and customize web development & app development, you can send inquiry on our email.
----
You can contact him at deepaktailor10@yahoo.in