Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Explore the validation process for paytm in php

Setting Callback URL

Integrating Paytm with PHP involves a series of steps, including setting up a Paytm account, obtaining API credentials, and implementing the necessary code in your PHP application. Here's a step-by-step example:

Step 1: Create a Paytm Merchant Account
Visit the Paytm website and sign up for a merchant account.
Complete the necessary documentation and verification process.
Step 2: Obtain API Credentials
Log in to your Paytm Merchant account.
Navigate to the "Developer" section and obtain your Merchant ID, Merchant Key, Website, and Industry type.
Step 3: Download Paytm PHP Kit
Download the Paytm PHP Kit from the Paytm GitHub repository.
Extract the downloaded files to your project directory.
Step 4: Set Configuration Parameters
Open the paytm/PaytmKit/lib/config_paytm.php file and set the following parameters:

<?php
$PAYTM_MERCHANT_KEY = "your_merchant_key";
$PAYTM_MERCHANT_MID = "your_merchant_id";
$PAYTM_MERCHANT_WEBSITE = "your_website";
$PAYTM_CHANNEL_ID = "WEB";
$PAYTM_INDUSTRY_TYPE_ID = "Retail";
?>
Enter fullscreen mode Exit fullscreen mode

Step 5: Create a Transaction Request
In your PHP code, create a transaction request and redirect the user to the Paytm payment gateway. Here's a simplified example:

<?php
require_once("paytm/PaytmKit/lib/config_paytm.php");
require_once("paytm/PaytmKit/lib/encdec_paytm.php");

// Set transaction parameters
$orderId = "ORDER" . time();
$amount = "10.00"; // Set your desired amount

// Create an array with transaction parameters
$paramList = array(
    "MID" => $PAYTM_MERCHANT_MID,
    "ORDER_ID" => $orderId,
    "CUST_ID" => "CUST" . time(),
    "TXN_AMOUNT" => $amount,
    "CHANNEL_ID" => $PAYTM_CHANNEL_ID,
    "WEBSITE" => $PAYTM_MERCHANT_WEBSITE,
    "INDUSTRY_TYPE_ID" => $PAYTM_INDUSTRY_TYPE_ID,
    "CALLBACK_URL" => "your_callback_url",
);

// Generate checksum
$checkSum = getChecksumFromArray($paramList, $PAYTM_MERCHANT_KEY);

// Add checksum to the parameters
$paramList["CHECKSUMHASH"] = $checkSum;

// Redirect the user to the Paytm payment page
?>
<html>
<head>
    <title>Paytm Payment Gateway Integration</title>
</head>
<body>
    <form method="post" action="https://securegw.paytm.in/theia/processTransaction" name="paytm_form">
        <?php
        foreach ($paramList as $name => $value) {
            echo '<input type="hidden" name="' . $name . '" value="' . $value . '">';
        }
        ?>
        <input type="submit" value="Proceed to Paytm">
    </form>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Step 6: Handle Callback
Once the payment is complete, Paytm will send a callback to the specified CALLBACK_URL. You need to handle this callback to update the payment status and perform any necessary actions in your application.

Practical Example

Setting Callback URL:

$paramList["CALLBACK_URL"] = "https://www.wizbrand.com/payment/pgResponse.php?ORDER_ID=$ORDER_ID&TXN_AMOUNT=$TXN_AMOUNT&admin_id=$admin_id&user_name=$user_name&create_date=$create_date&EMAIL=$EMAIL&created_date=$created_date";
Enter fullscreen mode Exit fullscreen mode

This line constructs a callback URL by concatenating various parameters like ORDER_ID, TXN_AMOUNT, admin_id, etc., to the base URL "https://www.wizbrand.com/payment/pgResponse.php".
Checking Valid Checksum:
Step 7: Handle Callback in Your PHP Code
In your callback processing script (specified in the CALLBACK_URL), validate the callback status and update your database or perform any necessary actions:

<?php
require_once("paytm/PaytmKit/lib/config_paytm.php");
require_once("paytm/PaytmKit/lib/encdec_paytm.php");

// Verify the checksum
$paytmChecksum = isset($_POST["CHECKSUMHASH"]) ? $_POST["CHECKSUMHASH"] : "";
$isValidChecksum = verifychecksum_e($_POST, $PAYTM_MERCHANT_KEY, $paytmChecksum);

// Validate the callback status
if ($isValidChecksum == "TRUE" && $_POST["STATUS"] == "TXN_SUCCESS") {
    // Payment is successful, update your database or perform necessary actions
    $orderId = $_POST["ORDERID"];
    $transactionId = $_POST["TXNID"];
    $amount = $_POST["TXNAMOUNT"];

    // Perform actions like updating order status, sending email notifications, etc.
    // Example: Update order status in your database
    // $updateOrderQuery = "UPDATE orders SET status = 'completed' WHERE order_id = '$orderId'";
    // Execute the query as per your database structure and requirements

    // Example: Send email notification
    // mail($customerEmail, "Payment Success", "Thank you for your purchase!");

    echo "Payment successful. Order ID: $orderId, Transaction ID: $transactionId, Amount: $amount";
} else {
    // Payment failed or checksum mismatch, handle accordingly
    $orderId = isset($_POST["ORDERID"]) ? $_POST["ORDERID"] : "Unknown";
    echo "Payment failed for Order ID: $orderId";
}
?>
Enter fullscreen mode Exit fullscreen mode

In this example, the script verifies the checksum and checks if the transaction status is "TXN_SUCCESS." If the payment is successful, you can update your database, send email notifications, or perform any other actions based on your application's requirements.

if($isValidChecksum == "TRUE") {
Enter fullscreen mode Exit fullscreen mode

This conditional statement checks if the variable $isValidChecksum is equal to the string "TRUE".
Processing Successful Transaction:

if ($_POST["STATUS"] == "TXN_SUCCESS") {
    // Retrieve values from the URL parameters
    $ORDER_ID = $_GET['ORDER_ID'];
    $TXN_AMOUNT = $_GET['TXN_AMOUNT'];
    $admin_id = $_GET['admin_id'];
    $user_name = $_GET['user_name'];
    $create_date = $_GET['create_date'];
    $EMAIL = $_GET['EMAIL'];
    $created_date = $_GET['created_date'];

    // Construct redirect URL
    $redirect_url = "https://www.wizbrand.com/api/v1/j/view-laravel/$ORDER_ID/$TXN_AMOUNT/$admin_id/$user_name/$create_date/$EMAIL/$created_date";

    // Redirect to the constructed URL
    header("Location: $redirect_url");
}
Enter fullscreen mode Exit fullscreen mode

If the transaction status ($_POST["STATUS"]) is "TXN_SUCCESS," it extracts values from the URL parameters and constructs a redirect URL. The user is then redirected to this URL.
Handling Unsuccessful Transaction:

else {
    // Display a modal indicating payment failure
    echo '...';  // HTML and JavaScript code for the modal
}
Enter fullscreen mode Exit fullscreen mode

If the transaction status is not "TXN_SUCCESS," it displays a modal with a message indicating that the payment has failed.
Looping Through POST Parameters:

if (isset($_POST) && count($_POST)>0 )
{ 
    foreach($_POST as $paramName => $paramValue) {
        // Process each POST parameter
    }
}
Enter fullscreen mode Exit fullscreen mode

If there are POST parameters, it loops through them and processes each one.

Please note that the provided code includes some HTML and JavaScript for displaying a modal in case of payment failure. The modal is shown with an error message, and there is a link to the cart page. Additionally, there's JavaScript code for handling the modal display and closure.

FULL CODE

$orderid = $_POST['ORDERID'];

$paytmChecksum = "";
$paramList = array();
$isValidChecksum = "FALSE";
$paramList = $_POST;
$paytmChecksum = isset($_POST["CHECKSUMHASH"]) ? $_POST["CHECKSUMHASH"] : ""; //Sent by Paytm pg
$isValidChecksum = verifychecksum_e($paramList, PAYTM_MERCHANT_KEY, $paytmChecksum); //will return TRUE or FALSE string.

if($isValidChecksum == "TRUE") {
    //echo "<b>Checksum matched and following are the transaction details:</b>" . "<br/>";
    if ($_POST["STATUS"] == "TXN_SUCCESS") {
        //update payment status
        $ORDER_ID = $_GET['ORDER_ID'];
$TXN_AMOUNT = $_GET['TXN_AMOUNT'];
$admin_id = $_GET['admin_id'];
$user_name = $_GET['user_name'];
$create_date = $_GET['create_date'];
$EMAIL = $_GET['EMAIL'];
$created_date = $_GET['created_date'];
$redirect_url = "https://www.wizbrand.com/api/v1/j/view-laravel/$ORDER_ID/$TXN_AMOUNT/$admin_id/$user_name/$create_date/$EMAIL/$created_date";

header("Location: $redirect_url");
    }
    else {
        echo '
        <div id="reject-influencer" class="modal fade" role="dialog">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header" style="background-color:#d64a34;">
                                    <button type="button" style="color:white;" class="close"  data-dismiss="modal"> &times;</button>
                                    <br>                                    
                                </div>
                                <center> <div class="modal-body" style="background-color:#deb195a6;">
                                <div id="success-message" class="success-message">
                                <i class="fas fa-times-circle fa-3x" style="color: red;"></i>
                        <h5  class="text-center" style="margin-top: 10px; color: red;">Failed</h5>
                        <h5 id="rejectinfluencer" class="text-center" style="margin-top: 10px; color: green;">Your payment has failed</h5>

                        <!-- Button with link -->
                        <a href="https://www.wizbrand.com/cart" class="btn btn-primary">Go to cart</a>
                    </div>
                                </div></center>
                            </div>
                        </div>
                    </div>
        <script>
            // Show the modal
            var modal = document.getElementById("myModal");
            modal.style.display = "block";

            // Close the modal when clicking the close button
            var closeBtn = document.getElementsByClassName("close")[0];
            closeBtn.onclick = function() {
                modal.style.display = "none";
            }

            // Close the modal when clicking outside of it
            window.onclick = function(event) {
                if (event.target == modal) {
                    modal.style.display = "none";
                }
            }
        </script>';
    }

    if (isset($_POST) && count($_POST)>0 )
    { 
        foreach($_POST as $paramName => $paramValue) {
                //echo "<br/>" . $paramName . " = " . $paramValue;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Paytm process

Top comments (0)