亚马逊灵活付款例外:来电者输入例外:无效签名

时间:2012-01-17 14:13:55

标签: php amazon-web-services amazon-pay

我正在尝试使用PHP API进行亚马逊的灵活付款。

以下是我发送付款请求的PHP代码段:

<?php

$string_to_sign = 'GET
authorize.payments-sandbox.amazon.com
/cobranded-ui/actions/start
SignatureMethod=HmacSHA256&SignatureVersion=2&callerKey=my_access_key&callerReference=YourCallerReference&paymentReason=donation&pipelineName=SingleUse&returnUrl=http%3A%2F%2Fproblemio.com&transactionAmount=4.0';

$encoded_string_to_sign = URLEncode(Base64_Encode(hash_hmac('sha256', $string_to_sign, 'my_secret_key')));

$amazon_request_sandbox = 'https://authorize.payments-sandbox.amazon.com/cobranded-ui/actions/start?SignatureVersion=2&returnUrl='.$return_url.'&paymentReason='.$payment_reason.'&callerReference=YourCallerReference&callerKey='.$my_access_key_id.'&transactionAmount=4.0&pipelineName=SingleUse&SignatureMethod=HmacSHA256&Signature='.$encoded_string_to_sign;

// When it goes to the url, it gets the invalid signature error
header('Location: '.$amazon_request_sandbox); 
?>

这似乎遵循了他们的指示,但我无法克服这个错误。

谢谢!

2 个答案:

答案 0 :(得分:8)

<?php

$method = 'GET';
$host   = 'authorize.payments-sandbox.amazon.com';
$path   = '/cobranded-ui/actions/start';

$params = array( 
    'signatureMethod'  => 'HmacSHA256',
    'signatureVersion' => '2',
    'currencyCode'     => 'USD',
    'callerKey' => 'Your_Key_ID',
    'callerReference'  => 'YourCallerReference',
    'paymentReason'    => 'donation',
    'pipelineName'     => 'SingleUse',
    'returnUrl'        => 'http://yourcallback.com',
    'transactionAmount'=> '5',
    'version'          => '2009-01-09',
);


$params = array_map('rawurlencode', $params);


$paramStringArray = array();

foreach($params as $key => $value){

    $paramStringArray[] = $key . '=' . $value;

}




$paramString = implode('&', $paramStringArray);

$string_to_sign = $method . "\n"
        . $host . "\n"
        . $path  . "\n"
        . $paramString;


$signature = base64_encode(hash_hmac(
    'sha256',
    $string_to_sign,
    'Your_Super_Secret_Key',
    true
));



$amazon_request_sandbox = "https://{$host}{$path}?" . $paramString .
    '&signature=' . rawurlencode($signature);

header('Location: '.$amazon_request_sandbox); 

?>

好的......使用下面代码中的结构,我终于通过上面的代码想出了这一切。在形成签名/ URL时要记录三件事情......

  1. 似乎参数“transactionAmount”对于有效的联合品牌UI管道是必需的,即使没有具体指示暗示该问题。

  2. 如果你的任何参数中有空格,并且你试图在最新的(5.4)版本的PHP中使用html_build_query(),那么你会得到一个以“+”为特色的编码方案标记为空格而不是“%20”,这是亚马逊似乎喜欢的。我上面的代码通过在整个参数数组上实现rawurlencode()来解决这个问题。

  3. 参数的排序在签名的构造中是至关重要的。键(不是值)需要不区分大小写的字母顺序。值得注意的是,尽管文档对API有所说明,但在创建签名的查询字符串时,&符号(&amp;)和等号(=)都必须存在。

  4.   

    例如:

      签名的查询字符串:callerKey = 1111111111111&amp; currencyCode = USD&amp; signatureVersion = 2

    我注意到的其他一些事情......

    在PHP SDK(2010-8-28)附带的示例代码中,文件“CBUISingleUsePipelineSample.php”中的“paymentReason”属性列为“HarryPotter 1-5 DVD set”。由于此属性中包含空格,因此当您尝试访问生成的链接时会抛出令人讨厌的“无效签名”错误,因为html_build_query()用于生成URL的查询字符串。要解决此问题,请打开“CBUIPipeline.php”,并在constructUrl()方法中查找以下行...

    $queryString = http_build_query($parameters, '', '&');
    

    将其替换为:

    $queryString = str_replace('+', '%20', http_build_query($parameters, '', '&'));
    

    这将解决旧版PHP(<5.4)的空间编码问题。使用最新版本,您可以设置“enc_type”标志。

    最后的事情......

    这是我在StackOverflow上的第一篇文章,所以如果我破坏了协议,不要杀了我。希望它有所帮助!

答案 1 :(得分:0)

试试这段代码:

<?php

$method = 'GET';
$host   = 'authorize.payments-sandbox.amazon.com';
$path   = '/cobranded-ui/actions/start';

$params = array(
    'SignatureMethod'  => 'HmacSHA256'
    'SignatureVersion' => 2,
    'callerKey'        => 'my_access_key',
    'callerReference'  => 'YourCallerReference',
    'paymentReason'    => 'donation',
    'pipelineName'     => 'SingleUse',
    'returnUrl'        => 'http://problemio.com&transactionAmount=4.0',
);

$string_to_sign = $method . "\n"
    . $host . "\n"
    . $path . "\n"
    . http_build_query($params);

$signature = base64_encode(hash_hmac(
    'sha256',
    $string_to_sign,
    'my_secret_key'
));

$params['Signature'] = $signature;

$amazon_request_sandbox = "https://{$host}{$path}?" . http_build_query($params);

header('Location: ' . $amazon_request_sandbox);

所以我做了一些改变:

  • PHP的http_build_query()来构建查询字符串(确保正确的编码)
  • 尝试重复使用你的变量而不是重复努力(更容易发现错误等)。
  • 明确\n - 您的编辑可能已输入\r\r\n

HTH

相关问题