Program:- [Project File] PayuMoney PHP Scripts
[Project File] PayuMoney PHP Scripts
Index.php
<?php $test_key = "enter your test key here"; $SALT = "enter your salt"; // Merchant Key and Salt as provided by Payu. $PAYU_BASE_URL = "https://sandboxsecure.payu.in"; // For Sandbox Mode //$PAYU_BASE_URL = "https://secure.payu.in"; // For Production Mode $action = ''; $posted = array(); if(!empty($_POST)) { //print_r($_POST); foreach($_POST as $key => $value) { $posted[$key] = $value; } } $formError = 0; if(empty($posted['txnid'])) { // Generate random transaction id $txnid = substr(hash('sha256', mt_rand() . microtime()), 0, 20); } else { $txnid = $posted['txnid']; } $hash = ''; // Hash Sequence $hashSequence = "key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10"; if(empty($posted['hash']) && sizeof($posted) > 0) { if( empty($posted['key']) || empty($posted['txnid']) || empty($posted['amount']) || empty($posted['firstname']) || empty($posted['email']) || empty($posted['phone']) || empty($posted['productinfo']) || empty($posted['surl']) || empty($posted['furl']) || empty($posted['service_provider']) ) { $formError = 1; } else { //$posted['productinfo'] = json_encode(json_decode('[{"name":"tutionfee","description":"","value":"500","isRequired":"false"},{"name":"developmentfee","description":"monthly tution fee","value":"1500","isRequired":"false"}]')); $hashVarsSeq = explode('|', $hashSequence); $hash_string = ''; foreach($hashVarsSeq as $hash_var) { $hash_string .= isset($posted[$hash_var]) ? $posted[$hash_var] : ''; $hash_string .= '|'; } $hash_string .= $SALT; $hash = strtolower(hash('sha512', $hash_string)); $action = $PAYU_BASE_URL . '/_payment'; } } elseif(!empty($posted['hash'])) { $hash = $posted['hash']; $action = $PAYU_BASE_URL . '/_payment'; } ?> <html> <head> <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%3E%0A%20%20%20%20var%20hash%20%3D%20'%3C%3Fphp%20echo%20%24hash%20%3F%3E'%3B%0A%20%20%20%20function%20submitPayuForm()%20%7B%0A%20%20%20%20%20%20if(hash%20%3D%3D%20'')%20%7B%0A%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20var%20payuForm%20%3D%20document.forms.payuForm%3B%0A%20%20%20%20%20%20payuForm.submit()%3B%0A%20%20%20%20%7D%0A%20%20%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<script>" title="<script>" /> </head> <body onload="submitPayuForm()"> <h2>PayU Form</h2> <br/> <?php if($formError) { ?> <span style="color:red">Please fill all mandatory fields.</span> <br/> <br/> <?php } ?> <form action="<?php echo $action; ?>" method="post" name="payuForm"> <input type="hidden" name="key" value="<?php echo $test_key ?>" /> <input type="hidden" name="hash" value="<?php echo $hash ?>"/> <input type="hidden" name="txnid" value="<?php echo $txnid ?>" /> <input class="form-control" type="hidden" name="surl" value="http://localhost/HA/textlocal/success.php" size="64" /> <input class="form-control" type="hidden" name="furl" value="http://localhost/HA/textlocal/failure.php" size="64" /> <input class="form-control" type="hidden" name="service_provider" value="payu_paisa" size="64" /> <label>First Name: </label> <input class="form-control" name="firstname" id="firstname" value="<?php echo (empty($posted['firstname'])) ? '' : $posted['firstname']; ?>" /> <label>Amount: </label> <input class="form-control" name="amount" value="<?php echo (empty($posted['amount'])) ? '' : $posted['amount'] ?>" /> <label>Email: </label> <input class="form-control" name="email" id="email" value="<?php echo (empty($posted['email'])) ? '' : $posted['email']; ?>" /> <label>Phone: </label> <input class="form-control" name="phone" value="<?php echo (empty($posted['phone'])) ? '' : $posted['phone']; ?>" /> <label>Product Info: </label> <input class="form-control" type='text' name="productinfo" value="<?php echo (empty($posted['productinfo'])) ? '' : $posted['productinfo'] ?>" > <?php if(!$hash) { ?> <input class="form-control btn btn-success mt-2" type="submit" value="Submit" /> <?php } ?> </tr> </table> </form> </body> </html>
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=""; // 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=""; // 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>"; } ?>
Leave a Reply