2012-01-17 74 views
2

我正在尝试使用亚马逊灵活付款的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); 
?> 

这似乎是跟随他们的指示,但我不能让过去的错误。

谢谢!

+0

我不确定你是否看到我的回答,但我想知道它是否有帮助。 – Funktr0n 2012-05-15 00:44:32

回答

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

7
<?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. 如果您的任何参数中都有/有空格,并且您尝试在除PHP的最新(5.4)版本之外的其他所有版本中使用html_build_query(),则会给出以“+”标记为特征的编码方案而不是“亚马逊”似乎喜欢的“%20”。上面的代码通过在整个参数数组上实现rawurlencode()来处理这个问题。

  3. 参数的排序在签名的构造中是非常重要的。键(不是值)需要以不区分大小写的字母顺序。同样值得注意的是,尽管文档中提到的API,符号(&)和equals(=)必须存在于创建签名的查询字符串中。

例:

查询字符串签名:callerKey = 1111111111111 & CURRENCYCODE = USD & signatureVersion = 2

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

在包含在PHP SDK(2010-8-28)中的示例代码,文件“CBUISingleUsePipelineSample.php”中的“paymentReason”属性列为s“HarryPotter 1-5 DVD套装”。由于此属性在其中包含空格,因此当您尝试访问生成的链接时,它会引发永久令人讨厌的“无效签名”错误,因为使用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”标志。

最后事情最后...

这是我在计算器上的第一篇文章,所以如果我打破了协议,不要杀我。希望能帮助到你!