Dear Reader, let us we know about “PayUMoney Payment Gateway Integration in PHP“.
It’s very easy to integrate, you just need to follow very easy steps to integrate it into your web application.
1. First, you need to go https://test.payumoney.com and sign up for a merchant account.
2. At a time of sign up, you need to use your valid email address. As you are creating a test account, you can enter whatever details for the testing purpose.
3. 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 code.
Here is a complete code of PayUMoney Payment Gateway Integration:-
3 files are needed.
1. PayUMoney_form.php :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
<?php // Merchant key here as provided by Payu $merchant_key = "gtKFFx"; $salt = "eCwWELxi"; $payu_base_url = "https://test.payu.in"; // For Test environment $action = ''; $currentDir = 'http://localhost/WebPreparations/payment/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> <script> var hash = '<?php echo $hash ?>'; function submitPayuForm() { if(hash == '') { return; } var payuForm = document.forms.payuForm; payuForm.submit(); } </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 $merchant_key ?>" /> <input type="hidden" name="hash" value="<?php echo $hash ?>"/> <input type="hidden" name="txnid" value="<?php echo $txnid ?>" /> <table> <tr> <td><b>Mandatory Parameters</b></td> </tr> <tr> <td>Amount <span class="mand">*</span>: </td> <td><input name="amount" type="number" value="<?php echo (empty($posted['amount'])) ? '' : $posted['amount'] ?>" /></td> <td>First Name <span class="mand">*</span>: </td> <td><input type="text" name="firstname" id="firstname" value="<?php echo (empty($posted['firstname'])) ? '' : $posted['firstname']; ?>" /></td> </tr> <tr> <td>Email <span class="mand">*</span>: </td> <td><input type="email" name="email" id="email" value="<?php echo (empty($posted['email'])) ? '' : $posted['email']; ?>" /></td> <td>Phone <span class="mand">*</span>: </td> <td><input type="text" name="phone" value="<?php echo (empty($posted['phone'])) ? '' : $posted['phone']; ?>" /></td> </tr> <tr> <td>Product Info <span class="mand">*</span>: </td> <td colspan="3"><textarea name="productinfo"><?php echo (empty($posted['productinfo'])) ? '' : $posted['productinfo'] ?></textarea></td> </tr> <tr> <td>Success URL <span class="mand">*</span>: </td> <td colspan="3"><input type="text" name="surl" value="<?php echo (empty($posted['surl'])) ? $currentDir.'success.php' : $posted['surl'] ?>" size="64" /></td> </tr> <tr> <td>Failure URL <span class="mand">*</span>: </td> <td colspan="3"><input type="text" name="furl" value="<?php echo (empty($posted['furl'])) ? $currentDir.'failure.php' : $posted['furl'] ?>" size="64" /></td> </tr> <tr> <td colspan="3"><input type="hidden" name="service_provider" value="" size="64" /></td> </tr> <tr> <td><b>Optional Parameters</b></td> </tr> <tr> <td>Last Name: </td> <td><input type="text" name="lastname" id="lastname" value="<?php echo (empty($posted['lastname'])) ? '' : $posted['lastname']; ?>" /></td> <td>Cancel URI: </td> <td><input type="text" name="curl" value="" /></td> </tr> <tr> <td>Address1: </td> <td><input type="text" name="address1" value="<?php echo (empty($posted['address1'])) ? '' : $posted['address1']; ?>" /></td> <td>Address2: </td> <td><input type="text" name="address2" value="<?php echo (empty($posted['address2'])) ? '' : $posted['address2']; ?>" /></td> </tr> <tr> <td>City: </td> <td><input type="text" name="city" value="<?php echo (empty($posted['city'])) ? '' : $posted['city']; ?>" /></td> <td>State: </td> <td><input type="text" name="state" value="<?php echo (empty($posted['state'])) ? '' : $posted['state']; ?>" /></td> </tr> <tr> <td>Country: </td> <td><input type="text" name="country" value="<?php echo (empty($posted['country'])) ? '' : $posted['country']; ?>" /></td> <td>Zipcode: </td> <td><input type="text" name="zipcode" value="<?php echo (empty($posted['zipcode'])) ? '' : $posted['zipcode']; ?>" /></td> </tr> <tr> <td>UDF1: </td> <td><input type="text" name="udf1" value="<?php echo (empty($posted['udf1'])) ? '' : $posted['udf1']; ?>" /></td> <td>UDF2: </td> <td><input type="text" name="udf2" value="<?php echo (empty($posted['udf2'])) ? '' : $posted['udf2']; ?>" /></td> </tr> <tr> <td>UDF3: </td> <td><input type="text" name="udf3" value="<?php echo (empty($posted['udf3'])) ? '' : $posted['udf3']; ?>" /></td> <td>UDF4: </td> <td><input type="text" name="udf4" value="<?php echo (empty($posted['udf4'])) ? '' : $posted['udf4']; ?>" /></td> </tr> <tr> <td>UDF5: </td> <td><input type="text" name="udf5" value="<?php echo (empty($posted['udf5'])) ? '' : $posted['udf5']; ?>" /></td> <td>PG: </td> <td><input type="text" name="pg" value="<?php echo (empty($posted['pg'])) ? '' : $posted['pg']; ?>" /></td> </tr> <tr> <?php if(!$hash) { ?> <td colspan="4"><input type="submit" value="Submit" /></td> <?php } ?> </tr> </table> </form> </body> </html> |
2. success.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?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>"; } ?> |
3. failure.php
1 2 3 4 5 6 7 |
<?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>"; }?> |
You can download all codes in zip format from here at single click payumoney_webpreparations.tar
Testing Credentials are :-
Card Type : Visa
Card Name : Test
Card Number : 4012001037141112
Expiry Date : 05/20
CVV : 123
Card Type : Master
Card Name : Test
Card Number : 5123456789012346
Expiry Date : 05/20
CVV : 123
Congratulations you have successfully learned “PayUMoney Payment Gateway Integration in PHP”, if you like this post and was helpful for you then share this post on social media and if you have any query then please contact us or comment below, Thanks