Cashfree Payments

blog

Introduction

In the fast-paced world of online transactions, ensuring seamless and secure payment processing is essential. Cashfree, a prominent payment gateway, offers a straightforward method to receive payments via QR codes. This article will guide you through the steps of creating an order, generating a terminal transaction, and using webhooks for verifying payment requests in Cashfree.

Step 1: Creating an Order on Cashfree

An order in Cashfree represents the amount and currency you wish to collect. To create an order and obtain a cf_order_id, which is vital for initiating a transaction, use the Cashfree API endpoint:

Prerequisites

Before creating an order, you need to authorize your API requests using the x-client-id and x-client-secret provided by Cashfree.

Authorization

Ensure you have your x-client-id and x-client-secret ready. These credentials are necessary for API authentication and to ensure secure communication between your application and Cashfree.

Request Body and Headers

To create an order, include the following parameters in your request body:

  • order_id: A unique identifier for the order in your system.
  • order_amount: The total amount for the order, specified up to two decimal places.
  • order_currency: The currency for the order, defaulting to INR if left empty.
  • customer_details: Required customer details, which can be dummy data if your use case allows.

Additionally, the headers must include:

  • x-api-version: The API version to use, such as 2023-08-01.
  • x-request-id: A unique request identifier for troubleshooting.
  • x-idempotency-key: A unique key to prevent duplicate actions in case of retries.

Refer to the official Cashfree documentation for detailed information on request body parameters, headers, and response status codes.

Creating the Order

Once you have authorized the request and included all required parameters, you can make the API call to create an order. If the request is successful, the order will be generated, and you will receive a response with a status code of 200.

Here is an example in Node.js:


const axios = require('axios');
 
const createOrder = async () => {
  try {
    const response = await axios.post('https://sandbox.cashfree.com/pg/orders', {
      order_id: 'your_order_id',
      order_amount: 100.50,
      order_currency: 'INR',
      customer_details: {
        customer_id: '12345',
        customer_email: 'customer@example.com',
        customer_phone: '9876543210'
      }
    }, {
      headers: {
        'x-api-version': '2023-08-01',
        'x-client-id': 'your_client_id',
        'x-client-secret': 'your_client_secret',
        'x-request-id': 'unique_request_id',
        'x-idempotency-key': 'unique_idempotency_key'
      }
    });
 
    if (response.status === 200) {
      console.log('Order created successfully:', response.data);
      // Use the payment_sessions_id from response.data
    }
  } catch (error) {
    console.error('Error creating order:', error);
  }
};
 

Step 3: Scanning the QR Code and Making the Payment

Once the QR code is generated, the user can scan it with their mobile device to make the payment. The QR code will direct the user to the payment interface, where they can complete the transaction.

Verifying Payment Requests Using Webhooks in Cashfree

Introduction

In the realm of online payments, ensuring the security and authenticity of transactions is crucial. Cashfree, a leading payment gateway, offers a robust mechanism to verify payment requests through webhooks. This section explores how webhooks can be utilized to verify payment requests, ensuring secure and reliable transactions.

Understanding Webhooks

Webhooks are automated messages sent from one app to another when an event occurs. In the context of payment gateways like Cashfree, webhooks notify your server about various events such as payment success, failure, or refund initiation. This real-time communication helps automate and streamline the verification process.

Setting Up Webhooks in Cashfree

To set up webhooks in Cashfree, follow these steps:

  • To set up webhooks in Cashfree, follow these steps: Log in to your Cashfree account and go to the Webhook settings section.

  • Configure Webhook URL:Enter the URL of your server endpoint where you want to receive the Webhook notifications.

  • Select Events:Choose the events you want to be notified about, such as payment success or failure.

  • Save Settings:Save your settings to start receiving webhook notifications.

    Handling Webhook Notifications

    Cashfree sends a webhook notification to your specified URL when a payment request is made. Here’s how you can handle these notifications to verify the payment:

  • Receive the Webhook: Your server should have an endpoint to receive the POST request from Cashfree.

  • Parse the Payload: EExtract the data from the webhook payload. This typically includes details like transaction ID, payment status, and amount.

  • Verify the Signature: ECashfree provides a signature to ensure the webhook’s authenticity. Use this signature to verify the webhook by comparing it with the generated hash using your secret key.

  • Update Your Records: Based on the verification, update your records to reflect the payment status accurately.

Conclusion

Integrating Cashfree to receive payments via QR codes and verify them using webhooks provides a seamless and secure payment experience. Following these steps ensures that your payment processing system is efficient, reliable, and secure.

site-logo