How To Integrate Payumoney Payment Gateway In PHP

What is PayUMoney payment Gateway?

PayUMoney is a Digital Payment Services for Individual and registered Businesses in India which accepts multiple currencies like INR & USD and others. This is a Digital Payment Service through which you can collect payments from your customer through online like Online Ecommerce Store and from other Web Applications. Today I am going to explain How to integrate payumoney payment gateway in  PHP.

You can get those information from PayUMoney documentations as well but here i am explain with simple steps given below:

Step 1 :

First of all you have to create a PayUMoney Account. You can Create a Live or Test Account as per your need. If you are a developer then I suggest you create test account for development.

Live Account : https://payumoney.com
Test Account : https://test.payumoney.com

Step 2 :

You have to sign up over the website with your registered email address.

Step 3 :

Add Your Real Bank Account Details over Live Website or Dummy Bank account Details over Test account.

Step 4 :

You have to create Index.php file inside root directory and copy code to same file given below:
index.php

<?php  
define('MERCHANT_KEY', 'gtKFFx');
define('SALT', 'eCwWELxi');
define('PAYU_BASE_URL', 'https://test.payu.in');    //Testing url
//define('PAYU_BASE_URL', 'https://secure.payu.in');  //actual URL
define('SUCCESS_URL', 'success.php');  //have complete url
define('FAIL_URL', 'fail.php');    //add complete url 
$txnid = substr(hash('sha256', mt_rand() . microtime()), 0, 20);
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$totalCost = $_POST['totalCost'];
$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>

Step 5 :

You have to create another php file which will be success.php along with code given below. When the user pays with successful payment then he will redirect to this file along with order information.
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 = "GQs7yium";

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>";
}
?>

Step 6 :

You have to create another php file which will be fail.php . User will redirect to this page if payment fails.
fail.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="fGxoywOg8S";
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.". </h4>";
} 
?>

Step 7 :

Now all is Done . Now you have to open this url over your browser like https://example.com/index.php

if you want to Integrate PayU Money over Laravel then you can read my another Article which is How To Integrate Payumoney Payment Gateway In Laravel.

Also You can check PayU Official Documentation as well.

I hope you enjoyed this article. If you have any issues and queries then please comment below. Also need more informational articles then you can visit my website https://brotech4u.com for more updates and knowledge.

Tags : How to integrate payumoney payment gateway in  PHP