PayUMoney Payment Gateway Integration using PHP, in this post we are going to learn how to integrate payment gateway in your mobile application or in your website. Payment gateway let your customer to pay online product amount for selected product from your website or app. PayUMoney is one of the best payment gateway from India. You can easily integrate this payment gateway in your Website or App after that your customer pay through Credit Cards, Debit Cards, Net Banking and more. So lets start integration Using PHP
You can also check
Create Merchant Account
First you need to go https://test.payumoney.com and sign up as a merchant account. For creating merchant account you need to use your valid email address. As you are creating test account, you can enter whatever details for the testing purpose.
Once you’re done with your test merchant account live, you will get your Merchant Id, Merchant Key, and Merchant Salt to use in your.
So lets start with creating user information with product information in which i’m going to use Bootstrap
User Interface Design
index.php
<html> <head> <title>TechJunkgigs</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script> var hash = '<?php echo $hash ?>'; function submitPayuForm() { if(hash == '') { return; } var payuForm = document.forms.payuForm; payuForm.submit(); } </script> </head> <body onload="submitPayuForm()"> <div class = "jumbotron"> <h2 align = "center">PayU Form</h2> </div> <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 $merchant_key ?>" /> <input type="hidden" name="hash" value="<?php echo $hash ?>"/> <input type="hidden" name="txnid" value="<?php echo $txnid ?>" /> <div class = "container"> <div class = "row"> <div class="col-sm-3">Amount <span class="mand">*</span>: </div> <div class="col-sm-3"><input name="amount" type="number" class="form-control" value="<?php echo (empty($posted['amount'])) ? '' : $posted['amount'] ?>" /></div> <div class="col-sm-3">First Name <span class="mand">*</span>: </div> <div class="col-sm-3"><input type="text" name="firstname" class="form-control" id="firstname" value="<?php echo (empty($posted['firstname'])) ? '' : $posted['firstname']; ?>" /></div> </div><br/> <div class = "row"> <div class="col-sm-3">Email <span class="mand">*</span>: </div> <div class="col-sm-3"><input type="email" name="email" class="form-control" id="email" value="<?php echo (empty($posted['email'])) ? '' : $posted['email']; ?>" /></div> <div class="col-sm-3">Phone <span class="mand">*</span>: </div> <div class="col-sm-3"><input type="text" name="phone" class="form-control" value="<?php echo (empty($posted['phone'])) ? '' : $posted['phone']; ?>" /></div> </div> <br/> <br/> <div class = "row"> <div class="col-sm-3">Product Info <span class="mand">*</span>: </div> <div class="col-sm-2"><textarea name="productinfo" class="form-control"><?php echo (empty($posted['productinfo'])) ? '' : $posted['productinfo'] ?></textarea></div> </div> <br/> <br/> <input type="hidden" name="surl" value="<?php echo (empty($posted['surl'])) ? $currentDir.'success.php' : $posted['surl'] ?>" size="64" /> <input type="hidden" name="furl" value="<?php echo (empty($posted['furl'])) ? $currentDir.'failure.php' : $posted['furl'] ?>" size="64" /> <input type="hidden" name="service_provider" value="" size="64" /> <?php if(!$hash) { ?> <input type="submit" class="btn btn-primary" value="Submit" /> <?php } ?> </div> </div> </form> </body> </html>
with this code we have our User interacting design
PHP code for executing salt key and merchant key
<?php $merchant_key = "<Merchant Key>"; $salt = "<salt>"; $payu_base_url = "https://test.payu.in"; // For Test environment $action = ''; $currentDir = 'http://localhost/payumoney/'; $posted = array(); if(!empty($_POST)) { foreach($_POST as $key => $value) { $posted[$key] = $value; } } $formError = 0; if(empty($posted['txnid'])) { $txnid = substr(hash('sha256', mt_rand() . microtime()), 0, 20); } else { $txnid = $posted['txnid']; } $hash = ''; $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']) ){ $formError = 1; } else { $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'; } ?>
Whole code for Index.php
<?php $merchant_key = "<Merchant Key>"; $salt = "<Salt>"; $payu_base_url = "https://test.payu.in"; // For Test environment $action = ''; $currentDir = 'http://localhost/payumoney/'; $posted = array(); if(!empty($_POST)) { foreach($_POST as $key => $value) { $posted[$key] = $value; } } $formError = 0; if(empty($posted['txnid'])) { $txnid = substr(hash('sha256', mt_rand() . microtime()), 0, 20); } else { $txnid = $posted['txnid']; } $hash = ''; $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']) ){ $formError = 1; } else { $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> <title>TechJunkgigs</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script> var hash = '<?php echo $hash ?>'; function submitPayuForm() { if(hash == '') { return; } var payuForm = document.forms.payuForm; payuForm.submit(); } </script> </head> <body onload="submitPayuForm()"> <div class = "jumbotron"> <h2 align = "center">PayU Form</h2> </div> <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 $merchant_key ?>" /> <input type="hidden" name="hash" value="<?php echo $hash ?>"/> <input type="hidden" name="txnid" value="<?php echo $txnid ?>" /> <div class = "container"> <div class = "row"> <div class="col-sm-3">Amount <span class="mand">*</span>: </div> <div class="col-sm-3"><input name="amount" type="number" class="form-control" value="<?php echo (empty($posted['amount'])) ? '' : $posted['amount'] ?>" /></div> <div class="col-sm-3">First Name <span class="mand">*</span>: </div> <div class="col-sm-3"><input type="text" name="firstname" class="form-control" id="firstname" value="<?php echo (empty($posted['firstname'])) ? '' : $posted['firstname']; ?>" /></div> </div><br/> <div class = "row"> <div class="col-sm-3">Email <span class="mand">*</span>: </div> <div class="col-sm-3"><input type="email" name="email" class="form-control" id="email" value="<?php echo (empty($posted['email'])) ? '' : $posted['email']; ?>" /></div> <div class="col-sm-3">Phone <span class="mand">*</span>: </div> <div class="col-sm-3"><input type="text" name="phone" class="form-control" value="<?php echo (empty($posted['phone'])) ? '' : $posted['phone']; ?>" /></div> </div> <br/> <br/> <div class = "row"> <div class="col-sm-3">Product Info <span class="mand">*</span>: </div> <div class="col-sm-2"><textarea name="productinfo" class="form-control"><?php echo (empty($posted['productinfo'])) ? '' : $posted['productinfo'] ?></textarea></div> </div> <br/> <br/> <input type="hidden" name="surl" value="<?php echo (empty($posted['surl'])) ? $currentDir.'success.php' : $posted['surl'] ?>" size="64" /> <input type="hidden" name="furl" value="<?php echo (empty($posted['furl'])) ? $currentDir.'failure.php' : $posted['furl'] ?>" size="64" /> <input type="hidden" name="service_provider" value="" size="64" /> <?php if(!$hash) { ?> <input type="submit" class="btn btn-primary" value="Submit" /> <?php } ?> </div> </div> </form> </body> </html>
When we submit the form there are two case occur – Failure or Success the transaction.
i’m implementing both success and failure transaction page.
Creating Transaction Page
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 ="eCwWELxi"; 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 = "eCwWELxi"; // Your salt 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>"; } ?>
Redirected Page for Payment through you debit card, credit card or Net banking
I hope this tutorial helped you to learn PayUMoney Payment Gateway Integration using PHP . To get the latest news and updates follow us on twitter & facebook, subscribe to our YouTube channel. If you have any query then please let us know by using comment.
Mohammad Arif says
nice Artical Good Work
Jamaley Hussain says
Hi Mohammad Arif,
I’m glad to know that. Thanks for stopping and commenting here
Keep visiting.