2

我已经使用 Amazon Login and Pay Express 成功付款(基于此处的演示:https ://github.com/amzn/pay-with-amazon-express-demo/tree/master/php )。

但是,当用户成功购买时,他们会被重定向回我的网站返回成功 url,其中包含一堆描述他们购买的东西的参数和亚马逊提供的请求的签名。

我尝试了下面给出的代码来计算从亚马逊返回的验证签名的原始签名,但此代码不会生成与亚马逊在返回 url 中发送的内容匹配的签名。

ExpressSuccess.php

<?php
echo ("<pre>");
print_r($_GET);
echo ("</pre>");

/* begin signature validation */
$merchantId  = "************"; // SellerID
$accessKey   = "*****************"; // MWS Access Key
$secretKey   = "***********************"; // MWS Secret Key
$lwaClientId = "***********************"; // Login With Amazon Client ID

/* Add http:// or https:// before your Return URL
 * The webpage of your site where the buyer should be redirected to after the payment is made
 * In this example you can link it to the Result.php, which checks for the success or failure of the payment
 * and routes it to the appropriate URL defined
 */
$returnURL   = "http://localhost/demo/pay-with-amazon/ExpressSuccess.php";
$cancelReturnURL = "http://localhost/demo/pay-with-amazon/ExpressCancel.php";

$signatureReturned = $_GET['signature'];
$parameters = $_GET;
unset($parameters['signature']);

if(isset($parameters['sellerOrderId'])) {
    $parameters['sellerOrderId'] = rawurlencode($parameters['sellerOrderId']);
}
uksort($parameters, 'strcmp');

$parseUrl = parse_url($returnURL);    
$stringToSign = "GET\n" . $parseUrl['host'] . "\n" . $parseUrl['path'] . "\n";

foreach ($parameters as $key => $value) {
    $queryParameters[] = $key . '=' . str_replace('%7E', '~', rawurlencode($value));
}
$stringToSign .= implode('&', $queryParameters);

$signatureCalculated = base64_encode(hash_hmac("sha256", $stringToSign, $secretKey, true));
$signatureCalculated = str_replace('%7E', '~', rawurlencode($signatureCalculated));

if ($signatureReturned == $signatureCalculated) {
    echo "Signature was successfully validated.";
} else {
    echo "Signature does not match.";
}
?>

如果有人知道我在哪里做错了,请告诉我。

谢谢 !!

4

0 回答 0