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.
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:
Before creating an order, you need to authorize your API requests using the x-client-id and x-client-secret provided by Cashfree.
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.
To create an order, include the following parameters in your request body:
Additionally, the headers must include:
Refer to the official Cashfree documentation for detailed information on request body parameters, headers, and response status codes.
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);
}
};
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.
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.
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.
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.
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.
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.