如何将变量分配给 hash_hmac sha256 字符串或 php 中的数据?

How to assign a variable to a hash_hmac sha256 string or data in php?

我正在使用万事达卡支付网关。 如果我硬编码 hash_hmac sha256.

的数据或字符串,一切正常

工作版本:

$vpcURL = 'https://migs.mastercard.com.au/vpcpay?';
$secret = strtoupper("MYSECRET CODE");

$data ="vpc_AccessCode=0E5AC9E6&vpc_Amount=1000&vpc_Command=pay&vpc_Locale=en&vpc_MerchTxnRef=TEST_TRN&vpc_Merchant=TESTSITE&vpc_OrderInfo=123&vpc_ReturnURL=https://www.examplesite.com/payment-confirmation/&vpc_Version=1";

$sha256_hmac = strtoupper(hash_hmac('sha256', $data, pack('H*', $secret)));
header("Location: " . $vpcURL . "&" . $data . "&vpc_SecureHash=" . $sha256_hmac."&vpc_SecureHashType=SHA256");

但我无法将硬编码值传递给 vpc_Amount 我从一个表格中获取金额,用户可以在其中输入他们想要的金额。

所以我得到的金额来自:

$totalAmount = $_POST['totalAmount'];

现在我想将这个 $totalAmount 传递给 $data。 所以我将 $data 更改为:

$data ="vpc_AccessCode=0E5AC9E6&vpc_Amount=$totalAmount&vpc_Command=pay&vpc_Locale=en&vpc_MerchTxnRef=TEST_TRN&vpc_Merchant=TESTSITE&vpc_OrderInfo=123&vpc_ReturnURL=https://www.examplesite.com/payment-confirmation/&vpc_Version=1";

当我使用它时,支付网关直接转到确认页面:https://www.examplesite.com/payment-confirmation/ 并且所有值都是空的。

我认为这是一个简单的语法错误..

我该如何解决这个问题? 如何正确地将 $totalAmount 传递给 $data

print_r ($data); gives this:

vpc_AccessCode=0E5AC9E6&vpc_Amount=58,258.00&vpc_Command=pay&vpc_Locale=en&vpc_MerchTxnRef=TEST_TRN&vpc_Merchant=TESTSITE&vpc_OrderInfo=123&vpc_ReturnURL=https://www.examplesite.com/payment-confirmation/?vpc_Version=1

更新 如果我将代码更新为

$real_integer_amount = filter_var($totalAmount, FILTER_SANITIZE_NUMBER_INT);

$data ="vpc_AccessCode=0E5BC9E7&vpc_Amount={$real_integer_amount}&vpc_Command=pay&vpc_Locale=en&vpc_MerchTxnRef=TEST_TRN&vpc_Merchant=TESTSITE&vpc_OrderInfo=123&vpc_ReturnURL=https://www.examplesite.com/payment-confirmation/?vpc_Version=1";

在确认页面显示实际金额,其他为空,但仍未进入支付网关,用户可以在其中输入他们的银行卡详细信息

我无法想象接收服务器想要在值中使用逗号。此外,您应该构建这样的查询字符串以避免未转义值的问题:

<?php
$vpcURL = 'https://migs.mastercard.com.au/vpcpay?';
$secret = strtoupper("MYSECRET CODE");
$totalAmount = str_replace(",", "", $_POST["totalAmount"]);

$data = [
    "vpc_AccessCode" => "0E5AC9E6",
    "vpc_Amount" => $totalAmount,
    "vpc_Command" => "pay",
    "vpc_Locale" => "en",
    "vpc_MerchTxnRef" => "TEST_TRN",
    "vpc_Merchant" => "TESTSITE",
    "vpc_OrderInfo" => "123",
    "vpc_ReturnURL" => "https://www.examplesite.com/payment-confirmation/",
    "vpc_Version" => "1",
];
$data = http_build_query($data);

$sha256_hmac = strtoupper(hash_hmac('sha256', $data, pack('H*', $secret)));
header("Location: " . $vpcURL . "&" . $data . "&vpc_SecureHash=" . $sha256_hmac."&vpc_SecureHashType=SHA256");

我最初发布的内容非常好..

如果我将 vpc_amount 更改为任何有效的(硬编码)值..

问题是当我将 $totalAmount 分配给 vpc_amount 时,变量 ($totalAmount) 包含小数点和千位分隔符。 这使得这个问题..

我只是想在将这个 totalAmount 传递给 data 之前清理变量以使其工作..

所以我将其更新为:

$real_integer_amount = filter_var($totalAmount, FILTER_SANITIZE_NUMBER_INT);

现在可以正常工作了..

所以最终的工作代码是:

$vpcURL = 'https://migs.mastercard.com.au/vpcpay?';
$secret = strtoupper("My Secret Code");

$real_integer_amount = filter_var($totalAmount, FILTER_SANITIZE_NUMBER_INT);

$data ="vpc_AccessCode=0E5AC9E6&vpc_Amount=$real_integer_amount&vpc_Command=pay&vpc_Locale=en&vpc_MerchTxnRef=TEST_TRN&vpc_Merchant=TESTSITE&vpc_OrderInfo=123&vpc_ReturnURL=https://www.trinitycollege.lk/payment-confirmation/&vpc_Version=1";

$sha256_hmac = strtoupper(hash_hmac('sha256', $data, pack('H*', $secret)));
header("Location: " . $vpcURL . "&" . $data . "&vpc_SecureHash=" . $sha256_hmac."&vpc_SecureHashType=SHA256");

@MagnusEriksson:感谢您的宝贵时间和建议。URL 编码对这个问题没有任何作用。

@pvg 没有任何拼写错误..如果我只是在 $data 中使用 {$totalAmount} 它不起作用..但这需要这:FILTER_SANITIZE_NUMBER_INT

@miken32谢谢你的回答。我试图通过用我的实际详细信息替换参数来使用您的代码。但它给了我这个错误“HTTP Status - 400 E5000: Cannot form a matching secure hash based on the merchant's request using either of the two merchant's secrets

我仔细检查了拼写 values/params

这可能对以后的其他人有帮助..