万事达卡将md5迁移到SHA-256 HMAC php

时间:2016-10-31 18:29:38

标签: php payment gateway

我有一个来自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 个答案:

答案 0 :(得分:1)

请尝试https://github.com/kareem3d/merchant-sample-code

我在此示例代码中只遇到一个问题, 在https://github.com/kareem3d/merchant-sample-code/blob/master/functions.php中,您需要删除urlencode。

行:$ secureHash。= $ key。“=”。$ value。“&amp;”;

答案 1 :(得分:0)

400可能是无效的哈希

你要做的是ascii sort并加入'&amp;'将发出请求的vpc值对(减去vpc_SecureHash和vpc_SecureHashType)。不要对vpc_ReturnURL进行URL编码。将此字符串传递给hmac以创建vpc_SecureHashSecret。编码返回URL并构建您的请求,包括vpc_SecureHash和vpc_SecureHashType

答案 2 :(得分:0)

由于您在适应SHA256之后没有提供您的代码,我无法确定您的问题是什么。但我想这是使用urlencode()的问题,因为我遇到了同样的问题并通过这种方式修复了它。