We need only 3 PHP files to achieve the same.
index.php:
<?php
define('MERCHANT_KEY', 'Your Key');
define('SALT', 'Your Salt Code');
define('PAYU_BASE_URL', 'https://sandboxsecure.payu.in'); //Testing url
//define('PAYU_BASE_URL', 'https://secure.payu.in'); //actual URL
define('SUCCESS_URL', 'Redirect URL After Success'); //add success url
define('FAIL_URL', 'Redirect URL After Failure'); //add failure url
$txnid = substr(hash('sha256', mt_rand() . microtime()), 0, 20);
$email = "User Mail Id";
$mobile = "User Mobile No";
$firstName = "User First Name";
$lastName = "User Last Name";
$totalCost = "Enter The Amount User Needs to Pay";
$hash='';
//Below is the required format need to hash it and send it across payumoney page. UDF means User Define Fields.
//$hashSequence = "key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10";
$hash_string = MERCHANT_KEY."|".$txnid."|".$totalCost."|"."productinfo|".$firstName."|".$email."|||||||||||".SALT;
$hash = strtolower(hash('sha512', $hash_string));
$action = PAYU_BASE_URL . '/_payment';
?>
<form action="<?php echo $action; ?>" method="post" name="payuForm" id="payuForm" style="display: none">
<input type="hidden" name="key" value="<?php echo MERCHANT_KEY ?>" />
<input type="hidden" name="hash" value="<?php echo $hash ?>"/>
<input type="hidden" name="txnid" value="<?php echo $txnid ?>" />
<input name="amount" type="number" value="<?php echo $totalCost; ?>" />
<input type="text" name="firstname" id="firstname" value="<?php echo $firstName; ?>" />
<input type="email" name="email" id="email" value="<?php echo $email; ?>" />
<input type="text" name="phone" value="<?php echo $mobile; ?>" />
<textarea name="productinfo"><?php echo "productinfo"; ?></textarea>
<input type="text" name="surl" value="<?php echo SUCCESS_URL; ?>" />
<input type="text" name="furl" value="<?php echo FAIL_URL?>"/>
<input type="text" name="service_provider" value="payu_paisa"/>
<input type="text" name="lastname" id="lastname" value="<?php echo $lastName ?>" />
</form>
<script type="text/javascript">
var payuForm = document.forms.payuForm;
payuForm.submit();
</script>
success.php:
<?php
$status=$_POST["status"];
$firstname=$_POST["firstname"];
$amount=$_POST["amount"];
$txnid=$_POST["txnid"];
$posted_hash=$_POST["hash"];
$key=$_POST["key"];
$productinfo=$_POST["productinfo"];
$email=$_POST["email"];
$salt="Your Salt Code";
// Salt should be same Post Request
If (isset($_POST["additionalCharges"])) {
$additionalCharges=$_POST["additionalCharges"];
$retHashSeq = $additionalCharges.'|'.$salt.'|'.$status.'|||||||||||'.$email.'|'.$firstname.'|'.$productinfo.'|'.$amount.'|'.$txnid.'|'.$key;
} else {
$retHashSeq = $salt.'|'.$status.'|||||||||||'.$email.'|'.$firstname.'|'.$productinfo.'|'.$amount.'|'.$txnid.'|'.$key;
}
$hash = hash("sha512", $retHashSeq);
if ($hash != $posted_hash) {
echo "Invalid Transaction. Please try again";
} else {
echo "<h3>Thank You. Your order status is ". $status .".</h3>";
echo "<h4>Your Transaction ID for this transaction is ".$txnid.".</h4>";
echo "<h4>We have received a payment of Rs. " . $amount . ". Your order will soon be shipped.</h4>";
}
?>
failure.php:
<?php
$status=$_POST["status"];
$firstname=$_POST["firstname"];
$amount=$_POST["amount"];
$txnid=$_POST["txnid"];
$posted_hash=$_POST["hash"];
$key=$_POST["key"];
$productinfo=$_POST["productinfo"];
$email=$_POST["email"];
$salt="Your Salt Code";
// Salt should be same Post Request
If (isset($_POST["additionalCharges"])) {
$additionalCharges=$_POST["additionalCharges"];
$retHashSeq = $additionalCharges.'|'.$salt.'|'.$status.'|||||||||||'.$email.'|'.$firstname.'|'.$productinfo.'|'.$amount.'|'.$txnid.'|'.$key;
} else {
$retHashSeq = $salt.'|'.$status.'|||||||||||'.$email.'|'.$firstname.'|'.$productinfo.'|'.$amount.'|'.$txnid.'|'.$key;
}
$hash = hash("sha512", $retHashSeq);
if ($hash != $posted_hash) {
echo "Invalid Transaction. Please try again";
} else {
echo "<h3>Your order status is ". $status .".</h3>";
echo "<h4>Your transaction id for this transaction is ".$txnid.". You may try making the payment by clicking the link below.</h4>";
}
?>