Integrate Easebuzz payment gateway in PHP

The Easebuzz Pvt ltd payment gateway allows merchants to collect payments online using various payment methods such as credit cards, debit cards, UPI and many more while keeping customer details encrypted and secure. Easebuzz is a completely API integrated online payment gateway platform. You can integrate your being website or mobile app with the Easebuzz payment gateway.

How to Integrate Easebuzz Payment Gateway

Integrating Easebuzz Payment gateway in PHP is a very easy task. Here are some of the steps that are needed to be followed in order to integrate the Easebuzz Payment gateway in PHP.

See also

  1. Integrate PayUMoney Payment Gateway in PHP
  2. Razorpay Payment Gateway Integration in PHP
  3. Cashfree Payments Gateway Integration in PHP
  4. Instamojo payment gateway integration in PHP
  5. Paypal Payment Gateway Integration in PHP Step by Step
  6. Paytm Payment Gateway Integration in PHP Step by Step

Easebuzz log or Sign-up

First of all go the Easebuzz website https://auth.easebuzz.in/easebuzz/login and sign up for an account. After sign up, log in to your dashboard and get your test credentials MERCHANT_KEY and SALT_KEY.

Install the Easebuzz PHP SDK

Easebuzz provides an official PHP SDK to simplify the integration process. You can download it from GitHub. For the latest instructions, check out Easebuzz GitHub repository https://docs.easebuzz.in/docs/payment-gateway/ex7xej43lwl4g-php

File Structure

Create Database and Table by SQL query

We need to create database and table, so here I created webscodex database payment_transaction & products table. payment_transaction table holds the records which will be payment success or failed. You can simply create table as following SQL query.

-- Database: `webscodex`
--
-- --------------------------------------------------------
--
-- Table structure for table `products`
--
CREATE TABLE `products` (
  `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `product_name` varchar(100) NOT NULL,
  `image` varchar(100) NOT NULL,
  `description` text NOT NULL,
  `price` int(50) NOT NULL,
  `status` tinyint(4) NOT NULL,
  `created` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Table structure for table `payment_transaction`
--
CREATE TABLE `payment_transaction` (
  `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `order_id` varchar(100) NOT NULL,
  `product_summary` varchar(100) NOT NULL,
  `full_name` varchar(100) NOT NULL,
  `mobile_number` varchar(100) NOT NULL,
  `email` varchar(50) DEFAULT NULL,
  `amount` float(10,2) NOT NULL,
  `currency` varchar(100) NOT NULL,
  `status` varchar(100) NOT NULL,
  `txns_id` varchar(100) NOT NULL,
  `txns_date` datetime NOT NULL,
  `address` varchar(50) NOT NULL,
  `pincode` varchar(50) NOT NULL,
  `city` varchar(50) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

Create Database Configuration

In this step, we require to create database configuration file, here we will set database name, server, username and password. So let’s create config.php file on your root directory and put bellow code:

config.php

<?php
   
   // Database configuration    
   define('DBSERVER', 'localhost');
   define('DBUSERNAME', 'root');
   define('DBPASSWORD', '');
   define('DBNAME', 'webscodex');
  
   // Create database connection 
   $con = new mysqli(DBSERVER, DBUSERNAME, DBPASSWORD, DBNAME);
    
   // Check connection 
   if ($con->connect_error) { 
       die("Connection failed: " . $con->connect_error); 
   }
   
?>

Create Add to cart or Buy Product Page

Create Product list or add to card list in our project for e-commerce feature then buy a product using Easebuzz payment gateway. We have created an index.php file for product payment form.

index.php

<!DOCTYPE html>
<html lang="">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Easebuzz Payment Gateway Integration in PHP Step by Step</title>
        <!-- Bootstrap CSS -->
        <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
    </head>
<body >
    <div class="container" style="background:#e6e6e6; padding:20px;">
        <div class="py-5 text-center">
            <h2> Products List (Easebuzz Payment Gateway Integration) </h2>
        </div>
        <div class="row">
            <?php
                // Database configuration    
                require "inc/config.php";

                $sql = "SELECT * FROM products WHERE status = '1' order by id DESC";
                $query = $con->query($sql);
                if ($query->num_rows > 0) {
                    while ($row = $query->fetch_assoc()) {

            ?>
            <div class="col-md-4">
                <div class="card" style="height: 543px;">
                    <img src="images/<?php echo $row['image']?>" style="width:348px; height: 250px;">
                    <div class="card-body">
                        <h5 class="card-title"><?php echo $row['product_name']?></h5>
                        <p class="card-text"><?php echo $row['description']?></p>
                        <a href="checkout.php?product_id=<?php echo $row['id']?>" class="btn btn-sm btn-warning text-white">Buy Now</a> 
                        <b><span style="float: right;">₹<?php echo $row['price']?></span></b>
                    </div>
                </div>
            </div>
            <?php } } ?>
        </div>
    </div>
</body>
</html>

Create HTML Checkout Form

Create a HTML checkout form on your website with the required fields so that it can be passed to the Easebuzz Payment to retrieve the order id and other information needed for the payment step. An example HTML form field is shown below. Name of the item, quantity, amount, item description, item address, email, and so on.

checkout.php

<?php
    
    // Database configuration  
    require "inc/config.php";
  
    if (!empty($_GET['product_id']) && isset($_GET['product_id'])) {
        $pid = $_GET['product_id'];
    }

    $sql = "SELECT * FROM products WHERE id = $pid";
    $query = $con->query($sql);

    if ($count = $query->num_rows > 0) {

        $row = $query->fetch_assoc();
    }

?>

<!DOCTYPE html>
<html lang="">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Easebuzz Payment Gateway Integration in PHP Step by Step</title>
        <!-- Bootstrap CSS -->
        <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
    </head>
<body>
<div class="container" style="background: #f2f2f2; padding-bottom:20px; border: 1px solid #d9d9d9; border-radius: 5px;">
    <div class="py-5 text-center">
        <h2> Easebuzz Payment Gateway Integration Checkout</h2>
        <p class="lead">This Checkout page using Easebuzz Payment Gateway for Testing purpose </p>
    </div>
    <form action="easebuzz.php" method="POST">
        <div class="row">
            <div class="col-md-8">
                <h4>Billing address</h4>
                <div class="card p-3">
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="firstName">Full Name </label>
                            <input type="text" class="form-control" name="full_name" placeholder="Full Name" required="">
                        </div>
                        <div class="col-md-6 mb-3">
                            <label for="mobile">Mobile Number</label>                        
                            <input type="text" class="form-control" name="mobile_number" placeholder="Mobile Number" required="">
                        </div>
                    </div>
                    <div class="mb-3">
                        <label for="email">Email <span class="text-muted">(Optional)</span></label>
                        <input type="email" class="form-control" name="email" placeholder="Email">
                    </div>
                    <div class="mb-3">
                        <label for="address">Flat, House no. Area, Street, Sector, Village</label>
                        <input type="text" class="form-control" name="address" placeholder="Full Address" required="">
                    </div>
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="city">Town/City</label>
                            <input type="text" class="form-control" name="city" placeholder="Town/City">
                        </div>
                        <div class="col-md-6 mb-3">
                            <label for="pincode">Pincode</label>
                            <input type="text" class="form-control" name="pincode" placeholder="6 digits [0-9] Pin code" required="">
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <h4 class="d-flex justify-content-between align-items-center">
                    <span>Order Summary</span>
                    <span class="badge badge-secondary badge-pill"><?php echo $count; ?></span>
                </h4>
                <ul class="list-group mb-3 sticky-top">
                    <li class="list-group-item d-flex justify-content-between lh-condensed">
                        <div class="product-list">
                            <img src="images/<?php echo $row['image']?>" style="width:100px; height: 100px;">
                            <h6><?php echo $row['product_name']?></h6>
                            <small class="text-muted"><?php echo $row['description']?></small>
                        </div>
                        <span class="text-muted">₹<?php echo $row['price']?></span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between">
                        <strong> Order Total: </strong>
                        <strong>₹<?php echo $row['price']?></strong>
                        <input type="hidden" name="amount" value="<?php echo $row['price']?>" />
                        <input type="hidden" name="product_summary" value="<?php echo $row['product_name']?>" />
                    </li>
                </ul>
                <div class="form-group">
                    <input type="submit" name="check_out" class="btn btn-warning text-white btn-block" value="Continue to Checkout">
                </div>
            </div>
        </div>
    </form>
</div>
</body>
</html>

Easebuzz Payment Request

easebuzz.php

<?php

    if (isset($_POST['check_out'])) {

        // Database configuration  
        include_once "inc/config.php";

        // Include Easebuss library 
        include_once('easebuzz-lib/easebuzz_payment_gateway.php');

        $MERCHANT_KEY = "";           // Marchant key
        $SALT = "";                   // Salt key
        $ENV = "test";                // set enviroment name

        $easebuzzObj = new Easebuzz($MERCHANT_KEY, $SALT, $ENV);
        
         // Use date default function to India time zore for save txns date
        date_default_timezone_set("Asia/Kolkata");

        // Generate txns Id by using mt_rand function
        $transactionId = substr(str_shuffle(md5(time())),0, 15);

        $customerName  = $_POST['full_name'];
        $customerEmail = $_POST['email'];
        $customerPhone = $_POST['mobile_number'];
        $description = $_POST['product_summary'];
        $address = $_POST['address'];
        $city = $_POST['city'];
        $pincode = $_POST['pincode'];
        $amount = $_POST['amount'];
        $orderId  = "WC".mt_rand(11111, 99999);
        $currency = "INR";
        $txnsDate = date('Y-m-d H:i:s');
        $payment_status = "Pending";
        
        //Insert transaction data into the database 
        $query = "INSERT INTO payment_transaction (order_id, product_summary, full_name, mobile_number, email, amount, currency, status, txns_id, txns_date, address, pincode, city) 
        VALUES ('$orderId', '$description', '$customerName', '$customerPhone', '$customerEmail', '$amount', '$currency', '$payment_status', '$transactionId', '$txnsDate', '$address', '$pincode', '$city')";
        
        $con->query($query);
       
        $postData = array ( 
            "txnid" => $transactionId, 
            "amount" => $amount.".0", 
            "firstname" => $customerName, 
            "email" => $customerEmail, 
            "phone" => $customerPhone, 
            "productinfo" => $description, 
            "surl" => "http://localhost/BlogPost/Easebuzz-Payment-gateway-in-php/success.php", 
            "furl" => "http://localhost/BlogPost/Easebuzz-Payment-gateway-in-php/failed.php", 
            "udf1" => "aaaa", 
            "udf2" => "aaaa", 
            "udf3" => "aaaa", 
            "udf4" => "aaaa", 
            "udf5" => "aaaa", 
            "address1" => $address, 
            "address2" => "",
            "city" => $city, 
            "state" => $city, 
            "country" => "INDIA", 
            "zipcode" => $pincode
        );
        
        $easebuzzObj->initiatePaymentAPI($postData);
     
    }
?>
Integrate Easebuzz payment gateway in PHP

Easebuzz Success Payment Status

success.php

<!DOCTYPE html>
<html lang="">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Easebuzz Payment Gateway Integration in PHP</title>
        <!-- Bootstrap CSS -->
        <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
    </head>
<body>

<?php
    // Database configuration  
    require_once "inc/config.php";

    $txnsId = $_POST["txnid"];

    /* update payment status by txnsId */
    $query = "UPDATE payment_transaction SET status='success' WHERE txns_id = '$txnsId'";
    if($con->query($query)){
        echo "<h3 class='text-success'>Your Payment has been Successful</h3>";
    }
?>
</body>
</html>

Easebuzz Cancel or Failed Payment status

failed.php

<!DOCTYPE html>
<html lang="">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Easebuzz Payment Gateway Integration in PHP</title>
        <!-- Bootstrap CSS -->
        <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
    </head>
<body>

<?php
    // Database configuration  
    require_once "inc/config.php";

    $txnsId = $_POST["txnid"];

    /* update payment status by txnsId */
    $query = "UPDATE payment_transaction SET status='failed' WHERE txns_id = '$txnsId'";
    if($con->query($query)){
        echo "<h3 class='text-danger'>Your Transaction has been Failed</h3>";
    }
?>

</body>
</html>

Conclusion

In this tutorial, I have explain the process of How you can integrate Easebuzz Payment gateway in PHP application. I have explain the very simple way to Easebuzz payment integrate. You can extend this to more complex uses like online shops etc.

If you have any suggestions or problems about this tutorial, please comment on the  form below.😊

Integrate Easebuzz payment gateway in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *