title: How to match the returned hash with the posted hash in payumoney payment gateway (JSP module)?
body: <p>I have to integrate payumoney payment gateway into my organization's website. Since our application has been developed using JSP so we choose to use the JSP kit provided by payumoney. The problem I am getting during test is that the posted hash and the returned hash do not match. I am providing you both the codes one for sending the digital order file(payuform.jsp) and the digital response file (success_payumoney.jsp) here for your kind debugging where I am wrong. Please excuse me for giving the whole code.</p>
  
  <p>payuform.jsp</p>
  
  <pre><code>            &lt;%@ page import="java.util.*" %&gt;
              &lt;%@ page import="java.security.*" %&gt;
  
              &lt;%!
              public boolean empty(String s)
                  {
                      if(s== null || s.trim().equals(""))
                          return true;
                      else
                          return false;
                  }
              %&gt;
              &lt;%!
                  public String hashCal(String type,String str){
                      byte[] hashseq=str.getBytes();
                      StringBuffer hexString = new StringBuffer();
                      try{
                      MessageDigest algorithm = MessageDigest.getInstance(type);
                      algorithm.reset();
                      algorithm.update(hashseq);
                      byte messageDigest[] = algorithm.digest();
  
  
  
                      for (int i=0;i&lt;messageDigest.length;i++) {
                          String hex=Integer.toHexString(0xFF &amp; messageDigest[i]);
                          if(hex.length()==1) hexString.append("0");
                          hexString.append(hex);
                      }
  
                      }catch(NoSuchAlgorithmException nsae){ }
  
                      return hexString.toString();
  
                  }
              %&gt;
              &lt;%  
                  String merchant_key="JBZaLc";
                  String salt="GQs7yium";
                  String action1 ="";
                  String base_url="https://test.payu.in";
                  int error=0;
                  String hashString="";
  
  
  
                  Enumeration paramNames = request.getParameterNames();
                  Map&lt;String,String&gt; params= new HashMap&lt;String,String&gt;();
                      while(paramNames.hasMoreElements()) 
                  {
                          String paramName = (String)paramNames.nextElement();
  
                          String paramValue = request.getParameter(paramName);
  
                      params.put(paramName,paramValue);
                  }
                  String txnid ="";
                  if(empty(params.get("txnid"))){
                      Random rand = new Random();
                      String rndm = Integer.toString(rand.nextInt())+(System.currentTimeMillis() / 1000L);
                      txnid=hashCal("SHA-256",rndm).substring(0,20);
                  }
                  else
                      txnid=params.get("txnid");
                      /*udf2 = txnid;*/
                  String txn="abcd";
                  String hash="";
                  String hashSequence = "key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10";
                  if(empty(params.get("hash")) &amp;&amp; params.size()&gt;0)
                  {
                      if( empty(params.get("key"))
                          || empty(params.get("txnid"))
                          || empty(params.get("amount"))
                          || empty(params.get("firstname"))
                          || empty(params.get("email"))
                          || empty(params.get("phone"))
                          || empty(params.get("productinfo"))
                          || empty(params.get("surl"))
                          || empty(params.get("furl"))
                          || empty(params.get("service_provider"))
                      )
  
                          error=1;
                      else{
                          String[] hashVarSeq=hashSequence.split("\\|");
  
                          for(String part : hashVarSeq)
                          {
                              hashString= (empty(params.get(part)))?hashString.concat(""):hashString.concat(params.get(part));
                              hashString=hashString.concat("|");
                          }
                          hashString=hashString.concat(salt);
  
  
                           hash=hashCal("SHA-512",hashString);
                          action1=base_url.concat("/_payment");
                      }
                  }
                  else if(!empty(params.get("hash")))
                  {
                      hash=params.get("hash");
                      action1=base_url.concat("/_payment");
                  }
              %&gt;
  
              &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
              &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
              &lt;head&gt;
              &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
              &lt;title&gt;-----&lt;/title&gt;
              &lt;!-- // Stylesheets // --&gt;
              &lt;link rel="stylesheet" href="../css/style.css" type="text/css"&gt;
              &lt;script type="text/javascript" src="../js/ajax.js"&gt;&lt;/script&gt;
              &lt;script type="text/javascript" src="../js/service.js"&gt;&lt;/script&gt;
  
              &lt;!-- bootstrap framework files --&gt;
              &lt;link rel="stylesheet" href="../../bootstrap/3.3.4/css/bootstrap.min.css"&gt;
              &lt;link rel="stylesheet" href="../../bootstrap/3.3.4/css/bootstrap-theme.min.css"&gt;
              &lt;script src="../../ajax/libs/jquery/1.11.2/jquery.min.js"&gt;&lt;/script&gt;
              &lt;script src="../../bootstrap/3.3.4/js/bootstrap.min.js"&gt;&lt;/script&gt;
              &lt;script&gt;
              var hash='&lt;%= hash %&gt;';
              function submitPayuForm() {
  
                  if (hash == '')
                      return;
  
                    var payuForm = document.forms.payuForm;
                    payuForm.submit();
                  }
              &lt;/script&gt;
              &lt;/head&gt;
              &lt;body onLoad="submitPayuForm();"&gt;
              &lt;div style="border:#999999 1px solid; margin:20px auto; width:700px;"&gt;
                  &lt;div class="container"&gt;
                  &lt;div class="row"&gt;
                      &lt;div class="col-md-8"&gt;
                          &lt;form action="&lt;%= action1 %&gt;" method="post" name="payuForm" class="form-horizontal"&gt;
                          &lt;input type="hidden" name="key" value="&lt;%= merchant_key %&gt;" /&gt;
                                &lt;input type="hidden" name="hash" value="&lt;%= hash %&gt;"/&gt;
                                &lt;input type="hidden" name="txnid" value="&lt;%= txnid %&gt;" /&gt;
                               &lt;!-- &lt;input type="hidden" name="udf2" value="&lt;%//= txnid %&gt;" /&gt;--&gt;
                                &lt;input type="hidden" name="service_provider" value="payu_paisa" /&gt;
                  &lt;h1&gt;Payu Money Payment Gateway&lt;/h1&gt;           
                  &lt;span style="color:#FF0000; font-weight:bold; font-style:italic;"&gt;All Fields are Mandatory&lt;/span&gt;
                  &lt;br /&gt;&lt;br /&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="Amount" class="control-label col-xs-2"&gt;Amount:&lt;/label&gt;
                      &lt;div class="col-xs-8"&gt;
                      &lt;input name="amount" value="&lt;%= (empty(params.get("amount"))) ? "" : params.get("amount") %&gt;" class="form-control" /&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
  
                  &lt;div class="form-group"&gt;
                      &lt;label for="FirstName" class="control-label col-xs-2"&gt;First Name:&lt;/label&gt;
                      &lt;div class="col-xs-8"&gt;
                      &lt;input name="firstname" id="firstname" value="&lt;%= (empty(params.get("firstname"))) ? "" : params.get("firstname") %&gt;" class="form-control" /&gt;
                      &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class="form-group"&gt;
                          &lt;label for="Email" class="control-label col-xs-2"&gt;Email:&lt;/label&gt;
                          &lt;div class="col-xs-8"&gt;&lt;input name="email" id="email" value="&lt;%= (empty(params.get("email"))) ? "" : params.get("email") %&gt;" class="form-control" /&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class="form-group"&gt;
                      &lt;label for="Phone" class="control-label col-xs-2"&gt;Phone:&lt;/label&gt;
                      &lt;div class="col-xs-8"&gt;
                      &lt;input name="phone" value="&lt;%= (empty(params.get("phone"))) ? "" : params.get("phone") %&gt;" class="form-control" /&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class="form-group"&gt;
                      &lt;label for="productinfo" class="control-label col-xs-2"&gt;Product Info:&lt;/label&gt;
                      &lt;div class="col-xs-8"&gt;
                      &lt;input name="productinfo" value="&lt;%= (empty(params.get("productinfo"))) ? "" : params.get("productinfo") %&gt;" size="64" class="form-control" /&gt;
                      &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class="form-group"&gt;
                      &lt;label for="FirstName" class="control-label col-xs-2"&gt;Success URI:&lt;/label&gt;
                      &lt;div class="col-xs-8"&gt;
                      &lt;input name="surl" value="&lt;%= (empty(params.get("surl"))) ? "" : params.get("surl") %&gt;" size="64" class="form-control" /&gt;
                      &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class="form-group"&gt;
                      &lt;label for="FirstName" class="control-label col-xs-2"&gt;Failure URI:&lt;/label&gt;
                      &lt;div class="col-xs-8"&gt;
                      &lt;input name="furl" value="&lt;%= (empty(params.get("furl"))) ? "" : params.get("furl") %&gt;" size="64" class="form-control" /&gt;
                      &lt;/div&gt;
                      &lt;/div&gt;
                            &lt;% if(empty(hash)){ %&gt;
                          &lt;div class="form-group" align="center"&gt;
                          &lt;input type="submit" value="Submit" class="btn btn-success btn-lg"/&gt;
                          &lt;/div&gt;
                          &lt;% } %&gt;
                              &lt;/form&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                &lt;/div&gt;
              &lt;/body&gt;
              &lt;/html&gt;
  </code></pre>
  
  <p>success_payumoney.jsp</p>
  
  <pre><code>            &lt;%@ page import="java.util.*" %&gt;
              &lt;%@ page import="java.security.*" %&gt;
  
              &lt;%!
              public boolean empty(String s)
                  {
                      if(s== null || s.trim().equals(""))
                          return true;
                      else
                          return false;
                  }
              %&gt;
              &lt;%!
                  public String hashCal(String type,String str){
                      byte[] hashseq=str.getBytes();
                      StringBuffer hexString = new StringBuffer();
                      try{
                      MessageDigest algorithm = MessageDigest.getInstance(type);
                      algorithm.reset();
                      algorithm.update(hashseq);
                      byte messageDigest[] = algorithm.digest();
  
  
  
                      for (int i=0;i&lt;messageDigest.length;i++) {
                          String hex=Integer.toHexString(0xFF &amp; messageDigest[i]);
                          if(hex.length()==1) hexString.append("0");
                          hexString.append(hex);
                      }
  
                      }catch(NoSuchAlgorithmException nsae){ }
  
                      return hexString.toString();
  
                  }
              %&gt;
              &lt;%
              String status = request.getParameter("status");
              String firstname = request.getParameter("firstname");
              String amount = request.getParameter("amount");
              String txnid = request.getParameter("txnid");
              String posted_hash = request.getParameter("hash");
              String key = request.getParameter("key");
              String productinfo = request.getParameter("productinfo");
              String email = request.getParameter("email");
              String salt = "GQs7yium";
  
              Enumeration paramNames = request.getParameterNames();
                  Map&lt;String,String&gt; params= new HashMap&lt;String,String&gt;();
                      while(paramNames.hasMoreElements()) 
                  {
                          String paramName = (String)paramNames.nextElement();
  
                          String paramValue = request.getParameter(paramName);
  
                      params.put(paramName,paramValue);
                  }
  
              String retHashSeq = "salt|status|udf10|udf9|udf8|udf7|udf6|udf5|udf4|udf3|udf2|udf1|email|firstname|productinfo|amount|txnid|key";
  
                      String[] hashVarSeq=retHashSeq.split("\\|");
  
                          for(String part : hashVarSeq)
                          {
                              retHashSeq= (empty(params.get(part)))?retHashSeq.concat(""):retHashSeq.concat(params.get(part));
                              retHashSeq=retHashSeq.concat("|");
                          }
  
                     String hash = hashCal("SHA-512", retHashSeq);
  
                     if (hash != posted_hash) {
                          out.print(hash+"&lt;br/&gt;");
                          out.print(posted_hash+"&lt;br/&gt;");
                         out.print("Invalid Transaction. Please try again");
                         }
                     else {
  
                        out.print("&lt;h3&gt;Thank You. Your order status is "+status+"&lt;/h3&gt;");
                        out.print("&lt;h4&gt;Your Transaction ID for this transaction is "+txnid+"&lt;/h4&gt;");
                        out.print("&lt;h4&gt;We have received a payment of Rs. "+amount+"Your order will soon be shipped.&lt;/h4&gt;");
  
              }         
              %&gt;
  </code></pre>