Cashfree Payment Gateway Integration in PHP – Step-by-Step Guide

If you are building a website and want to accept online payments, it is extremely important to integrate a reliable and completely secure payment gateway. Cashfree is one of the easiest and fastest payment gateways to integrate into your website, especially if you are using PHP.

In this Papayacoders.com blog, you will learn how to easily integrate the Cashfree payment gateway into PHP.

What is Cashfree?

Cashfree is one of the popular Indian payment gateways that allows businesses to accept online payments via UPI, credit/debit cards, net banking, wallets, etc. It supports instant settlement and offers developer-friendly APIs.

Read also:-

What You Need Before Integration

Before starting, make sure you have:

  1. A Cashfree account – Sign up here
  2. PHP installed (can use XAMPP, WAMP or live server)A website or localhost project
  3. Basic understanding of HTML and PHP
  4. Cashfree API keys (App ID and Secret Key)

Step-by-Step Guide to Integrate Cashfree in PHP

Step 1: Get Cashfree API Keys

  • Login to your Cashfree Dashboard
  • Go to API Keys
  • Copy your App ID and Secret Key
  • Use the Test Key for sandbox environment

Step 2: Create HTML Payment Form

htmlCopyEdit<form method="post" action="pay.php">
<input type="text" name="name" placeholder="Enter Name" required><br>
<input type="email" name="email" placeholder="Enter Email" required><br>
<input type="number" name="amount" placeholder="Enter Amount" required><br>
<button type="submit">Pay Now</button>
</form>

Step 3: Create pay.php File in PHP

phpCopyEdit<?php
$appId = "Your_APP_ID";
$secretKey = "Your_SECRET_KEY";

// Customer data
$name = $_POST['name'];
$email = $_POST['email'];
$amount = $_POST['amount'];
$orderId = "Order" . rand(1111, 9999); // Unique order ID
$returnUrl = "http://yourwebsite.com/response.php"; // Callback page

$data = array(
"appId" => $appId,
"orderId" => $orderId,
"orderAmount" => $amount,
"orderCurrency" => "INR",
"orderNote" => "Test Payment",
"customerName" => $name,
"customerEmail" => $email,
"customerPhone" => "9999999999",
"returnUrl" => $returnUrl,
"notifyUrl" => $returnUrl
);

// Generate signature
ksort($data);
$signatureData = "";
foreach ($data as $key => $value) {
$signatureData .= "$key$value";
}
$signature = hash_hmac('sha256', $signatureData, $secretKey, false);
$data["signature"] = $signature;

// Auto redirect to Cashfree payment page
echo '<form id="redirectForm" method="post" action="https://sandbox.cashfree.com/pg/checkout/post/">';
foreach ($data as $key => $value) {
echo '<input type="hidden" name="'.$key.'" value="'.$value.'">';
}
echo '</form>';
echo '<script>document.getElementById("redirectForm").submit();</script>';
?>

Step 4: Handle the Response in response.php

phpCopyEdit<?php
if ($_POST) {
echo "<h3>Payment Response</h3>";
echo "Order ID: " . $_POST['orderId'] . "<br>";
echo "Payment Status: " . $_POST['txStatus'] . "<br>";
echo "Amount: " . $_POST['orderAmount'] . "<br>";
echo "Reference ID: " . $_POST['referenceId'] . "<br>";
}
?>

Test Your Integration

Make sure to use the sandbox link (https://sandbox.cashfree.com/pg/checkout/post/while testing. After everything works fine, switch to the live environment and use the live keys and live URL.

Important Notes

  • Always store API keys securely
  • Use https in production
  • Always verify the signature in the response for added security
  • Use the Cashfree documentation for the latest updates

Final Words:-

If you follow all the steps correctly, it becomes very easy to integrate Cashfree with PHP, and you can easily accept secure and fast payments with just a few lines of code. Whether you are selling a product or a service, Cashfree helps you receive payments easily.

You can give your personal opinion in the comments, follow Papayacoders’ official page on Instagram, where more than 10k people take advantage of digital marketing every day

Share this post :