Categories: eLearning

PayUMoney Payment Gateway Integration in PHP

Today, TechnoGripper India will give you steps for How to integrate PayUMoney Payment Gateway in PHP. Integration of PayUMoney Payment gateway is confusing for the first time and also some of my visitors request us for this tutorial. We are just trying to brief you in simple steps, Please have a look on this steps:

Step 1: Create PayUMoney Merchant Account at www.payumoney.com and follow the instructions over there to activate your PayUMoney account. Once the account is approved you can start integrating.

Step 2: Please find the code for PHP File to process the payment.

<?php
 $MERCHANT_KEY = "<Merchant Key>";
 $SALT = "<Salt>";
 $PAYU_BASE_URL = "https://secure.payu.in/_payment";
 $action = '';
 
 $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>Payment System with PayUMoney - By TechnoGripper India (www.technogripper.net)</h2>
    <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 ?>" />
      <input type="hidden" name="amount" value="50" />
   <input type="hidden" name="productinfo" value="Iphone 6C - 16GB" />
   <input type="hidden" name="surl" value="http://example.com/success.php"/> <!-- Success notification -->
   <input type="hidden" name="furl" value="http://example.com/failure.php"/> <!-- Failure notification -->
      <table>
        <tr>
          <td>Amount: </td>
          <td>$50</td>
          <td>First Name: </td>
          <td><input name="firstname" id="firstname" value="<?php echo (empty($posted['firstname'])) ? '' : $posted['firstname']; ?>" /></td>
        </tr>
        <tr>
          <td>Email: </td>
          <td><input name="email" id="email" value="<?php echo (empty($posted['email'])) ? '' : $posted['email']; ?>" /></td>
          <td>Phone: </td>
          <td><input name="phone" value="<?php echo (empty($posted['phone'])) ? '' : $posted['phone']; ?>" /></td>
        </tr>
        <tr>
          <td>Product: </td>
          <td colspan="3"> Iphone 6C - 16GB</td>
        </tr>
        <tr>
          <?php if(!$hash) { ?>
            <td colspan="4"><input type="submit" value="Submit" /></td>
          <?php } ?>
        </tr>
      </table>
    </form>
  </body>
</html>

In this code, you need to replace <Merchant Key> with your PayUMoney Merchant Key & <Salt> with your PaUMoney Salt key. You’ll get this in your PayUMoney Account.



Step 3:

Success Page: In the case of payment succeeded, the page will redirect to success.php page with $_POST method and success parameters. You can do the further process of success payment here.

<?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 "Sorry! Invalid Transaction. Please try again";
 
  } else {
    echo "<h4>Your Payment has been done. Your Transaction ID for this transaction is ".$txnid.".</h4>";
  }         
?>

Failure Page: In the case of payment failure, the page will redirect on failure.php page with post method and failure parameters. You can do the further process of failed payment here.

<?php
echo "<h1>Sorry! Your Payment Canceled</h1>";
?>

That’s all. Now Check the Payment Process.

If you’ll get any error, you can reach us at info@technogripper.net



View Comments

  • hello, thank you so much for the demo code.
    I have payment integration on the website but now i want to allow only credit and debit card payment mode.
    All other payment modes as like UPI payment mode, Net banking, and EMI has to disable in the website. Please can you help me with it? I want to disable the UPI payment mode urgently.
    Please anyone help me
    Please reply ASAP.

    • Hello,

      Thanks for investing time in a reading article on WEXT.in. Please share your contact or call at 9560850036, our lead developer. He will surely help you to get your bug fixed.

      Thanks & Regards
      WEXT.in

Recent Posts

Mistakes of Entrepreneurs: Deadly Mistakes of Entrepreneurs

The mistakes of entrepreneurs can be one of the costliest mistakes. Not only it can cost your business, but it…

4 days ago

Top Young Entrepreneurs from India, you must know about

Today, We'll featuring top young entrepreneurs from India, who have actually inspired entrepreneurs across the world. India has one of…

1 week ago

नए उद्यम विकास को प्रभावित करने वाले कारक।

भारत में उद्यमिता विकास  को प्राभावित करने वाले कारण निम्न हैं। जैसे : -  आर्थिक, सामाजिक और सांस्कृतिक , मनोवैज्ञानिक,…

1 month ago

उद्यमियों (एंटरप्रेन्योर) के लिए कौन सी योजना उपलबध है

भारत सरकार ने हमेशा से ही उद्योग के क्षेत्र को बढ़ावा देने की कोशिश की है. देश में उद्यमियों की…

2 months ago

उद्यमियों ( एंटरप्रेन्योर ) के लिए वैश्विक समस्या क्या है

किसी भी देश में आर्थिक विकास की गति को बढ़ावा देने तथा व्यावसायिक समस्याओं का समाधान करने में उद्यमी की…

2 months ago

Cocofit Has Raised INR 5 Crores From Shark Tank India

The shark tank India startup Cocofit has raised an unconventional amount of 5 rupees for the equity of 5% from…

2 months ago

REACH US

WEXT India Ventures

C-22, Sector 65, Noida, Uttar Pradesh – 201301

Corporate Queries:

Email: ping@wext.in
Mobile: +91 956 085 0036

Advertisement Queries:

Email: advertise@wext.in
Mobile: 956 085 0036