PayUMoney集成 - 如何计算哈希以与响应进行比较?

时间:2014-10-28 07:11:55

标签: php hash payment-gateway payumoney

Hash请求生成Post

    $hashSequence = "key|txnid|amount|productinfo|firstname|email|udf1|"
                    ."udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10";
    $hashVarsSeq  = explode('|', $hashSequence);
    $hashString   = '';  
    foreach ($hashVarsSeq as $hashVar) {
        $hashString .= isset($payObject['params'][$hashVar]) ? $payObject['params'][$hashVar] : '';
        $hashString .= '|';
    }
    $hashString .= $salt;
    //generate hash
    $hash = strtolower(hash('sha512', $hashString));

成功生成响应Hash

$retHashSeq = $salt.'|'.$status.'||||||||'.$udf3.'|'.$udf2.'|'.$udf1.'|'.$email.'|||'.$amount.'|'.$txnid.'|'.$key;
$hash = hash("sha512", $retHashSeq);

但生成的HashHash服务器返回的PayU不匹配。 可能是什么问题呢??任何帮助,将不胜感激。

4 个答案:

答案 0 :(得分:1)

您似乎正在尝试重新实现PayU REST API。 我找不到对当前版本的REST API中$hashSequence模式的任何引用。

您是否考虑过使用official SDK

答案 1 :(得分:0)

此代码适用于服务器端的android hashcodegeneration

   <?php

$key=$_POST["key"];

$salt="xxxxx"; #your payumoney salt
$txnId=$_POST["txnid"];
$amount=$_POST["amount"];
$productName=$_POST["productInfo"];
$firstName=$_POST["firstName"];
$email=$_POST["email"];
$udf1=$_POST["udf1"];
$udf2=$_POST["udf2"];
$udf3=$_POST["udf3"];
$udf4=$_POST["udf4"];
$udf5=$_POST["udf5"];

$payhash_str = $key . '|' . checkNull($txnId) . '|' .checkNull($amount)  . '|' .checkNull($productName)  . '|' . checkNull($firstName) . '|' . checkNull($email) . '|' . checkNull($udf1) . '|' . checkNull($udf2) . '|' . checkNull($udf3) . '|' . checkNull($udf4) . '|' . checkNull($udf5) . '|' . $salt;


function checkNull($value) {
            if ($value == null) {
                  return '';
            } else {
                  return $value;
            }
      }


$hash = strtolower(hash('sha512', $payhash_str));
$arr['result'] = $hash;
$arr['status']=0;
$arr['errorCode']=null;
$arr['responseCode']=null;
$arr['hashtest']=$payhash_str;
$output=$arr;


echo json_encode($output);

?>

答案 2 :(得分:0)

我知道很晚才回答这个问题,但这个答案可能有助于未来的搜索者。只需从官方网站下载最新的PayUMoney工具包,并将SALT密钥也放在success.php页面中。

这是我最近的成功.php

<?php
include'config/db.php'; // Your database connection file if needed
$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=""; // PLACE YOUR SALT KEY HERE

// Salt should be same Post Request
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 = strtolower(hash('sha512', $retHashSeq)); // NOTE: THIS PART IN YOUR KIT MAY HAVE AN ERROR. THERE YOU MIGHT GET $hash_string instead of $retHashSeq. JUST REPLACE $hash_string with $retHashSeq.

if($hash != $posted_hash){
  // Transaction completed but is Invalid as Hash Values are not Matching. Notify Admin.
  //header('Location: fail.php');
  //exit();
}else{
  // Transaction is Valid. Process orders here.
  //header('Location: thanks.php');
  //exit();
}
?>

答案 3 :(得分:0)

PayUMoney C#API中的请求和响应中的哈希计算

hashSequence =

键| txnid |金额|产品信息|名字|电子邮件| udf1 | udf2 | udf3 | udf4 | udf5 ||||||盐;

$ hash = hash(“ sha512”,$ hashSequence);

PayUMoney仪表板上有盐的地方。

注意:即使商人没有在输入请求中传递任何udf字段,在计算hashSequence时也将使用空白的udf字段。

对于响应哈希,变量序列与付款请求哈希相反。另外,在salt和udf1之间添加了状态变量

序列

hashSequence = salt |状态|||||| udf5 | udf4 | udf3 | udf2 | udf1 |电子邮件|名字|产品信息|金额| txnid |密钥;

$ hash = hash(“ sha512”,$ hashSequence);

PayUMoney仪表板上有盐的地方。

以下是用于响应哈希计算的示例代码:-

                bool isCheckSum = false;
                var strhash = Request.Form["hash"];
                var strstatus = Request.Form["status"];
                var strfirstname = Request.Form["firstname"];
                var stramount = Request.Form["amount"];
                var strtxnid = Request.Form["txnid"];
                var strkey = Request.Form["key"];
                var strproductinfo = Request.Form["productinfo"];
                var stremail = Request.Form["email"];
                var stradditionalCharges = Request.Form["additionalCharges"];
                string strudf1 = Request.Form["udf1"];
                string strudf2 = Request.Form["udf2"];
                string strudf3 = Request.Form["udf3"];
                string strudf4 = Request.Form["udf4"];
                string strudf5 = Request.Form["udf5"];                   

                System.Security.Cryptography.SHA512Managed sha512 = new System.Security.Cryptography.SHA512Managed();
                byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(strSALT + "|" + strstatus + "||||||" + strudf5 + "|" + strudf4 + "|" + strudf3 + "|" + strudf2 + "|" + strudf1 + "|" + stremail + "|" + strfirstname + "|" + strproductinfo + "|" + stramount + "|" + strtxnid + "|" + strkey);
                byte[] hashBytes = sha512.ComputeHash(inputBytes);
                byte[] hashValue;
                string hex = "";
                hashValue = sha512.ComputeHash(inputBytes);
                foreach (byte x in hashValue)
                {
                    hex += String.Format("{0:x2}", x);
                }

                if(strhash == hex)
                {
                    isCheckSum = true;
                }