How to use PayUmoney Checkout or Payment method using PHP

Inthis post I am going to explain how to setup PayUmoney payment gateway in your web application. Integration of PayU gateway is a little confusing for the first time and also some of my readers request me for this tutorial. Most of the thing is already in PayU documentation I am just going to explain it in step so you will easily integrate without reading the whole document.

Index.php

<?php
session_start();

if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') == 0){
    //Request hash
    $contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';    
    if(strcasecmp($contentType, 'application/json') == 0){
        $data = json_decode(file_get_contents('php://input'));
        $hash=hash('sha512', $data->key.'|'.$data->txnid.'|'.$data->amount.'|'.$data->productinfo.'|'.$data->firstname.'|'.$data->email.'|||||'.$data->udf5.'||||||'.$data->salt);
        $json=array();
        $json['success'] = $hash;
        $_SESSION['salt'] = $data->salt;
        echo json_encode($json);
    
    }
    exit(0);
}
 
function getCallbackUrl()
{
    $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $uri = str_replace('/index.php','/',$_SERVER['REQUEST_URI']);
    return $protocol . $_SERVER['HTTP_HOST'] . $uri . 'response.php';
}

$action = 'https://secure.payu.in/_payment';

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PayUBiz PHP7 Kit</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

</head>
<style type="text/css">
    .main {
        margin-left:30px;
        font-family:Verdana, Geneva, sans-serif, serif;
    }
    .text {
        float:left;
        width:180px;
    }
    .dv {
        margin-bottom:5px;
    }
</style>
<body>
<div class="main">
    <div>
        <img src="images/logo.png" />
    </div>
    <div>
        <h3>PayUBiz PHP7 Kit</h3>
    </div>
    
    <form action="<?php echo $action; ?>" id="payment_form" method="post">
    <input type="hidden" id="udf5" name="udf5" value="PayUBiz_PHP7_Kit" />
    <input type="hidden" id="surl" name="surl" value="<?php echo getCallbackUrl(); ?>" />
    <input type="hidden" id="furl" name="furl" value="<?php echo getCallbackUrl(); ?>" />
    <input type="hidden" id="curl" name="curl" value="<?php echo getCallbackUrl(); ?>" />
    <div class="dv">
    <span class="text"><label>Merchant Key:</label></span>
    <span><input type="text" id="key" name="key" placeholder="Merchant Key" value="" /></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>Merchant Salt:</label></span>
    <span><input type="text" id="salt" name="salt" placeholder="Merchant Salt" value="" /></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>Transaction/Order ID:</label></span>
    <span><input type="text" id="txnid" name="txnid" placeholder="Transaction ID" value="<?php echo  "Txn" . rand(10000,99999999)?>" /></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>Amount:</label></span>
    <span><input type="text" id="amount" name="amount" placeholder="Amount" value="6.00" /></span>    
    </div>
    
    <div class="dv">
    <span class="text"><label>Product Info:</label></span>
    <span><input type="text" id="productinfo" name="productinfo" placeholder="Product Info" value="P01,P02" /></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>First Name:</label></span>
    <span><input type="text" id="firstname" name="firstname" placeholder="First Name" value="" /></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>Last Name:</label></span>
    <span><input type="text" id="Lastname" name="Lastname" placeholder="Last Name" value="" /></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>Zip Code:</label></span>
    <span><input type="text" id="Zipcode" name="Zipcode" placeholder="Zip Code" value="" /></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>Email ID:</label></span>
    <span><input type="text" id="email" name="email" placeholder="Email ID" value="" /></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>Mobile/Cell Number:</label></span>
    <span><input type="text" id="phone" name="phone" placeholder="Mobile/Cell Number" value="" /></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>Address1:</label></span>
    <span><input type="text" id="address1" name="address1" placeholder="Address1" value="" /></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>Address2:</label></span>
    <span><input type="text" id="address2" name="address2" placeholder="Address2" value="" /></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>City:</label></span>
    <span><input type="text" id="city" name="city" placeholder="City" value="" /></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>State:</label></span>
    <span><input type="text" id="state" name="state" placeholder="State" value="" /></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>Country:</label></span>
    <span><input type="text" id="country" name="country" placeholder="Country" value="" /></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>PG:</label></span>
    <span><input type="text" id="Pg" name="Pg" placeholder="PG" value="CC" /></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>Hash:</label></span>
    <span><input type="text" id="hash" name="hash" placeholder="Hash" value="" /></span>
    </div>
    
    
    <div><input type="button" id="btnsubmit" name="btnsubmit" value="Pay" onclick="frmsubmit(); return true;" /></div>
    </form>
</div>
<script type="text/javascript"><!--
function frmsubmit()
{
    document.getElementById("salt").remove(); //Salt should not be passed over internet for extra security
    document.getElementById("payment_form").submit();    
    return true;
}
//-->
</script>
<script type="text/javascript"><!--
$('#payment_form').bind('keyup blur', function(){
    $.ajax({
          url: 'index.php',
          type: 'post',
          data: JSON.stringify({
            key: $('#key').val(),
            salt: $('#salt').val(),
            txnid: $('#txnid').val(),
            amount: $('#amount').val(),
            productinfo: $('#productinfo').val(),
            firstname: $('#firstname').val(),
            email: $('#email').val(),            
            udf5: $('#udf5').val()
          }),
          contentType: "application/json",
          dataType: 'json',
          success: function(json) {
            if (json['success']) {    
                $('#hash').val(json['success']);
            }
          }
        });
});
//-->
</script>
</body>
</html>
   

 

Response.php

 

<?php
session_start();
$postdata = $_POST;
$msg = '';
$salt = '9rH5kuC8';
// $_SESSION['salt']; //Salt should not be passed over internet for extra security

if (isset($postdata ['key'])) {
    $key                =   $postdata['key'];
    $txnid                 =     $postdata['txnid'];
    $amount              =     $postdata['amount'];
    $productInfo          =     $postdata['productinfo'];
    $firstname            =     $postdata['firstname'];
    $email                =    $postdata['email'];
    $udf5                =   $postdata['udf5'];    
    $status                =     $postdata['status'];
    $resphash            =     $postdata['hash'];
    //Calculate response hash to verify    
    $keyString               =      $key.'|'.$txnid.'|'.$amount.'|'.$productInfo.'|'.$firstname.'|'.$email.'|||||'.$udf5.'|||||';
    $keyArray               =     explode("|",$keyString);
    $reverseKeyArray     =     array_reverse($keyArray);
    $reverseKeyString    =    implode("|",$reverseKeyArray);
    $CalcHashString     =     strtolower(hash('sha512', $salt.'|'.$status.'|'.$reverseKeyString));
    
    
    if ($status == 'success'  && $resphash == $CalcHashString) {
        $msg = "Transaction Successful and Hash Verified...";
        //Do success order processing here...
        // if success then show that information, you have successfully purchased the prodcuct

 


    }
    else {
        //tampered or failed show message -> unable to buy product
        $msg = "Payment failed for Hash not verified...";

 

    }
}
else exit(0);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PayUBiz PHP7 Kit</title>
</head>
<style type="text/css">
    .main {
        margin-left:30px;
        font-family:Verdana, Geneva, sans-serif, serif;
    }
    .text {
        float:left;
        width:180px;
    }
    .dv {
        margin-bottom:5px;
    }
</style>
<body>
<div class="main">
    <div>
        <img src="images/logo.png" />
    </div>
    <div>
        <h3>PHP7 PayUBiz Response</h3>
    </div>
    
    <div class="dv">
    <span class="text"><label>Merchant Key:</label></span>
    <span><?php echo $key; ?></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>Merchant Salt:</label></span>
    <span><?php echo $salt; ?></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>Transaction/Order ID:</label></span>
    <span><?php echo $txnid; ?></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>Amount:</label></span>
    <span><?php echo $amount; ?></span>    
    </div>
    
    <div class="dv">
    <span class="text"><label>Product Info:</label></span>
    <span><?php echo $productInfo; ?></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>First Name:</label></span>
    <span><?php echo $firstname; ?></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>Email ID:</label></span>
    <span><?php echo $email; ?></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>Hash:</label></span>
    <span><?php echo $resphash; ?></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>Transaction Status:</label></span>
    <span><?php echo $status; ?></span>
    </div>
    
    <div class="dv">
    <span class="text"><label>Message:</label></span>
    <span><?php echo $msg; ?></span>
    </div>
    
    <br />
    <br />
    <div class="dv">
    <span class="text"><label><a href="./">New Order</a></label></span>    
    </div>
    
</div>
</body>
</html>
   

Click on the below link to download the source code

 

How to use PayUmoney Checkout or Payment method using PHP

 

For more clarity watch video


Please Share this course

It Looks like you have completed your course!! We have created Online series based on the above content. You can give a test and check your skill.




Be a Part of CollegeSpike by giving your precious comment here and help us in improving our content.