万事达卡MIGS从MD5转换为SHA-256 HMAC PHP

5

我有一个来自MIGS的网关,但他们将MD5更改为SHA-256 HMAC,我该如何在我的代码中进行更改?我尝试了很多次,但是一直收到错误400,我认为我的代码存在一些问题。

现有代码:

<?php
$db1 = new ps_DB();
$q = "SELECT country_2_code FROM #__vm_country WHERE     country_3_code='".$user->country."' ORDER BY country_2_code ASC";
$db1->query($q);


$url = "https://migs.mastercard.com.au/vpcpay";

$SECURE_SECRET = MIGS_SS;
$vpcURL = $url . "?";
$md5HashData = $SECURE_SECRET;

$tax_total = $db->f("order_tax") + $db->f("order_shipping_tax");
$discount_total = $db->f("coupon_discount") + $db->f("order_discount");

 if( MIGS_TEST == 1)
  $amt123 = MIGS_TESTMODEAMT*100;
 else
  $amt123 =   round(($db->f("order_total")+$tax_total-$discount_total)*100,2);

    $post_variables = Array(
   "vpc_Version" => "1",
    "vpc_Command" => "pay",
    "vpc_AccessCode" => MIGS_ACCESSCODE,
    "vpc_MerchTxnRef" => $db->f("order_id").'_'.$db->f("order_number"),
    "vpc_Merchant" => MIGS_MID,
    "vpc_OrderInfo" =>  $VM_LANG->_('PHPSHOP_ORDER_PRINT_PO_NUMBER')."_".   $db->f("order_id"),
   "vpc_Amount" => $amt123, 
   "vpc_Locale" => 'en',
   "vpc_ReturnURL" => SECUREURL ."index.php?option=com_virtuemart&  page=checkout.migs&order_id=".$db->f("order_id")

    );
    ksort ($post_variables);

    if( $page == "checkout.thankyou" ) {
    $query_string = "?";
    foreach( $post_variables as $name => $value ) {
    $query_string .= urlencode($name). "=" . urlencode($value) ."&";
    //$vpcURL .= urlencode($name). "=" . urlencode($value) ."&";
    $md5HashData .= $value;
     }

    if (strlen($SECURE_SECRET) > 0) {
    $query_string .=  "vpc_SecureHash=" . strtoupper(md5($md5HashData));
    //$vpcURL .= "vpc_SecureHash=" . strtoupper(md5($md5HashData));
    }

    //die(  $url.' pppppppp '.$query_string);

   vmRedirect( $url . $query_string );
   } else {
   echo '<form action="'.$url.'" method="post" target="_blank">';
  echo '<input type="image" name="submit" src="https://www.paypal.com/en_US  /i/btn/x-click-but6.gif" alt="Click to pay with PayPal - it is fast, free and secure!" />';

    foreach( $post_variables as $name => $value ) {
   echo '<input type="hidden" name="'.$name.'"    value="'.htmlspecialchars($value).'" />';
    }
   echo '</form>';

    }
  ?>

我从Migs那里获得了新代码。

    foreach($_POST as $key => $value) {
     // create the hash input and URL leaving out any fields that have no  value
    if (strlen($value) > 0) {
    ?>
         <input type="hidden" name="<?php echo($key); ?>"  value="<?php    echo($value); ?>"/><br>
  <?php             
 if ((strlen($value) > 0) && ((substr($key, 0,4)=="vpc_") ||       (substr($key,0,5) =="user_"))) {
     $hashinput .= $key . "=" . $value . "&";
    }
    }

   }
   $hashinput = rtrim($hashinput, "&");
    ?>      
         <!-- attach SecureHash -->
                        <input type="hidden" name="vpc_SecureHash"  value="<?php echo(strtoupper(hash_hmac('SHA256',
  $hashinput, pack('H*',$securesecret)))); ?>"/>
    <input type="hidden" name="vpc_SecureHashType" value="SHA256">

我该如何在我的代码中使用它?使用基于MD5的代码时工作正常,但当我将其转换为SHA时,在到达网关后会出现400错误,由于安全问题,我已删除MIGS秘密代码。

3个回答

1

0

由于您没有提供您的代码如何适应SHA256,我无法确定您的问题。但我猜测问题出在使用urlencode()上,因为我曾遇到过同样的问题,并通过这种方式解决了它。


0

400 可能是无效的哈希值

你正在尝试对将构成请求的 vpc 值对进行 ASCII 排序并使用“&”连接(减去 vpc_SecureHash 和 vpc_SecureHashType)。暂时不要对 vpc_ReturnURL 进行 URL 编码。将此字符串传递给 hmac 以创建 vpc_SecureHashSecret。编码返回 URL 并构建您的请求,包括 vpc_SecureHash 和 vpc_SecureHashType。


1
你能否在这里写出正确的代码,我应该在哪里进行更改? - Sr33raj

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接