Skip to main content

Problems encoding Amazon Flexible Payments secret string in PHP


I am trying to use Amazon Payment Services, and they require me to do something like this:



Here is the complete signature so you can see I added the signature method:




$string_to_sign = "GET\n
authorize.payments-sandbox.amazon.com\n
cobranded-ui/actions/start?
SignatureMethod=HmacSHA256&SignatureVersion=2&callerKey=my_key&callerReference=YourCallerReference&paymentReason=donation&pipelineName=SingleUse&returnUrl=http%3A%2F%2Fyourwebsite.com%2Freturn.html&transactionAmount=4.0";



and then I encrypt it like below.




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



I do that, but then I get an error from them saying:




Caller Input Exception: The following input(s) are either invalid or absent:[signatureMethod]



Any idea what might be going wrong here?



Here is the entire code for this: (the variables are assigned values above)




<?php
$string_to_sign = 'GET
authorize.payments-sandbox.amazon.com/cobranded-ui/actions/startSignatureMethod=HmacSHA256&SignatureVersion=2&callerKey=AKIAJENBYSJCJX2IDWDQ&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;

//echo $amazon_request_sandbox; - use this if you want to see the resulting request and paste it into the browser

header('Location: '.$amazon_request_sandbox);
?>



Thanks!!


Source: Tips4all

Comments

  1. Check if you included &SignatureMethod=HmacSHA256 on the request

    This kind of errors has 3 basic natures:


    Missing Keys/Values
    Typos on Keys/Values
    Incorrect encoding or spaces on Keys/Values


    Hope that helps!

    Regards

    ReplyDelete
  2. Have you set your signature method? from the AWS documentation:


    You must set the SignatureMethod request parameter to either
    HmacSHA256 or HmacSHA1 to indicate which signing method you're using

    ReplyDelete
  3. I don't believe you need to base64 encode the hash (after all, it's already being urlencoded) -- try removing Base64_Encode.

    ReplyDelete
  4. Your $string_to_sign variable is missing a '?' between start and SignatureMethod for your encoded Signature.


    Signature version 2 is an enhanced signing method for both Amazon
    Simple Pay and Amazon Flexible Payments Service.

    For inbound requests (from your application to Amazon Payments), it
    uses the entire request URI as the basis for the signature, with
    encryption based on the unique security credentials for your account.

    For outbound requests (from Amazon Payments to your application),
    Amazon signs the response which you can verify using the
    VerifySignature API


    EDIT:

    As @Jonathan Spooner mentioned already and what I use is the function varifySignature() located in


    /amazon-fps-2010-08-28-php5-library/src/Amazon/FPS/Samples/Client.php


    which can be downloaded here. It also has an example as to how to use it in


    /amazon-fps-2010-08-28-php5-library/src/Amazon/FPS/Samples/VerifySignatureSample.php


    It makes the whole process much easier. It may be worth a shot...

    ReplyDelete
  5. Have you tried this

    base64_encode(hash_hmac('sha256', $Request, $AmazonSecretKey, true));

    Pass a boolean to pass it as a raw output.

    ReplyDelete
  6. You're most definitely missing the last parameter for hash_hmac which has to be set true to get RFC 2104-compliant HMAC signature:

    base64_encode(
    hash_hmac($hash, $data, $key, true)
    );


    And in the complete example you're missing new lines in $string_to_sign.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex