有人能回答为什么我总是得到"HMAC验证失败“的错误响应吗?
我的代码:
$response_purchase_JSON = $payeezy - > purchase(array(
"amount" => "420",
"card_number" => "4012000033330026",
"card_type" => "VISA",
"card_holder_name" => "Test Account",
"card_cvv" => "675",
"card_expiry" => "1119",
"merchant_ref" => "Transaction",
"currency_code" => "USD",
));
print_r($response_purchase_JSON);发布于 2014-12-24 08:39:44
您需要构造HMAC值。查看文档,请访问:https://developer.payeezy.com/content/hmac-validation-failure
此外,您的JSON有效负载也不正确。
正确有效负载(test.json)示例:
{
"transaction_type": "authorize",
"method": "credit_card",
"amount": "420",
"currency_code": "USD",
"credit_card": {
"type": "visa",
"cardholder_name": "Test Account",
"card_number": "4012000033330026",
"exp_date": "1119",
"cvv": "675"
}
}下面也是示例PHP代码:
<?php
$serviceURL = 'https://api-cert.payeezy.com/v1/transactions';
$apikey = 'yourapikey';
$token = 'yourapitoken';
$apisecret = 'yourapisecret';
list($usec, $sec) = explode(" ", microtime());
$timestamp = round(((float)$usec + (float)$sec) * 1000);
$timestamp = $timestamp - 5000;
$nonce = rand();
echo 'Timestamp is: '. $timestamp."\n";
$reqbody = file_get_contents('./test.json', true);
echo 'Request body: '.$reqbody."\n";
$summarize = "";
$summarize .= $apikey;
$summarize .= $nonce;
$summarize .= $timestamp;
$summarize .= $token;
$summarize .= $reqbody;
$hmac = hash_hmac('SHA256', $summarize, $apisecret);
echo "Hmac is: ".$hmac."\n";
$hmac_enc = base64_encode($hmac);
$curl = curl_init($serviceURL);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $reqbody);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_VERBOSE, true);
$headers = array(
'Content-type: application/json',
"Authorization: ".$hmac_enc,
"apikey: ".$apikey,
"token: ".$token,
"timestamp: ".$timestamp,
"nonce: ".$nonce,
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $serviceURL failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
echo "Response is: ".$response."\n";
echo "JSON response is: ".$json_response."\n";
?> 发布于 2016-05-04 13:10:58
以下是HMAC验证失败的常见原因:
HTTP头中的
如果你在本地机器上测试支付,并且你认为你已经正确地遵循了所有的步骤,然后也得到了同样的错误,那么尝试在服务器上运行代码
https://stackoverflow.com/questions/27386764
复制相似问题