View on GitHub

Amazon Pay SDK Samples

Amazon Pay Simple Checkout

Select Shipping and Payment Method

Select your billing address and payment method from the widgets below.

Notice in the URL above there are several parameters available. The 'access_token' should be saved in order to obtain address line one and two of the shipping address associated with the payment method.

This is known as the address consent token. It is passed to the GetOrderReferenceDetails API call to retrieve information about the order reference Id that is generated by the widgets.

If you see a error message in the widgets you will need to start over. This usually indicates that your session has expired.

Code

Create the widget containers.

<div id="addressBookWidgetDiv" style="width:400px; height:240px;"></div>
<div id="walletWidgetDiv" style="width:400px; height:240px;"></div>

Add script to set the CLIENT_ID just as you did for displaying the Amazon Pay button.

<script type='text/javascript'>
    window.onAmazonLoginReady = function () {
        amazon.Login.setClientId('CLIENT_ID');
    };
</script>

Copy/paste the javascript code below to import the widgets.js. This must be pasted after you set the client Id above.

USA
<script type='text/javascript' src='https://static-na.payments-amazon.com/OffAmazonPayments/us/sandbox/js/Widgets.js'></script>
UK
<script type='text/javascript' src='https://static-eu.payments-amazon.com/OffAmazonPayments/uk/sandbox/js/Widgets.js'></script>
DE
<script type='text/javascript' src='https://static-eu.payments-amazon.com/OffAmazonPayments/uk/sandbox/js/Widgets.js'></script>
JP
<script type='text/javascript' src='https://origin-na.ssl-images-amazon.com/images/G/09/EP/offAmazonPayments/sandbox/prod/lpa/js/Widgets.js'></script>

Add the widgets code. You can use the onOrderReferenceCreate method to obtain information about the order reference Id.

<script type="text/javascript">
    new OffAmazonPayments.Widgets.AddressBook({
        sellerId: 'MERCHANT_ID',
        onOrderReferenceCreate: function (orderReference) {
           orderReferenceId = orderReference.getAmazonOrderReferenceId();
        },
        onAddressSelect: function () {
            // do stuff here like recalculate tax and/or shipping
        },
        design: {
            designMode: 'responsive'
        },
        onError: function (error) {
            // your error handling code
        }
    }).bind("addressBookWidgetDiv");

    new OffAmazonPayments.Widgets.Wallet({
        sellerId: 'MERCHANT_ID',
        onPaymentSelect: function () {
        },
        design: {
            designMode: 'responsive'
        },
        onError: function (error) {
            // your error handling code
        }
    }).bind("walletWidgetDiv");
</script>

These are the backend API calls to SetOrderReferenceDetails and GetOrderReferenceDetails. These API calls set the order total to 175.00 and gets the order reference details. The Amazon Order Reference Id is created when the widgets load. (see the 'orderReferenceId' variable in the javascript above)

The Amazon Pay client will be initialized here and used throughout the rest of the sample.

from amazon_pay.client import AmazonPayClient

client = AmazonPayClient(
    mws_access_key='ACCESS_KEY',
    mws_secret_key='SECRET_KEY'
    merchant_id='MERCHANT_ID',
    sandbox=True,
    region='na',
    currency_code='USD')

order_reference_id = 'AMAZON_ORDER_REFERENCE_ID'

response = client.set_order_reference_details(
    amazon_order_reference_id=order_reference_id,
    order_total='175.00')

if response.success:
    response = client.get_order_reference_details(
        amazon_order_reference_id=order_reference_id,
        address_consent_token=session['access_token'])

pretty = json.dumps(
    json.loads(
        response.to_json()),
    indent=4)
return pretty
namespace AmazonPay;

session_start();

require_once '/AmazonPay/Client.php';

$config =array( 'merchant_id'   => 'MERCHANT_ID',
                'access_key'    => 'ACCESS_KEY',
                'secret_key'    => 'SECRET_KEY',
                'client_id'     => 'CLIENT_ID',
                'currency_code' => 'usd',
                'region'        => 'us',
                'sandbox'   => true);

// Instantiate the client object with the configuration
$client = new Client($config);
$requestParameters = array();

// Create the parameters array to set the order
$requestParameters['amazon_order_reference_id'] = 'AMAZON_ORDER_REFERENCE_ID';
$requestParameters['amount']            = '175.00';
$requestParameters['currency_code']     = 'USD';
$requestParameters['seller_note']   = 'Love this sample';
$requestParameters['seller_order_id']   = '123456-TestOrder-123456';
$requestParameters['store_name']        = 'Saurons collectibles in Mordor';

// Set the Order details by making the SetOrderReferenceDetails API call
$response = $client->SetOrderReferenceDetails($requestParameters);

// If the API call was a success Get the Order Details by making the GetOrderReferenceDetails API call
if($client->success)
{
    $requestParameters['address_consent_token'] = null;
    $response = $client->GetOrderReferenceDetails($requestParameters);
}
// Pretty print the Json and then echo it for the Ajax success to take in
$json = json_decode($response->toJson());
echo json_encode($json, JSON_PRETTY_PRINT);
require 'amazon_pay'

access_key = 'ACCESS_KEY'
secret_key = 'SECRET_KEY'
merchant_id = 'MERCHANT_ID'

client = AmazonPay::Client.new(
    access_key,
    secret_key,
    merchant_id,
    region: :na, # default: :na
    sandbox: true, # default: false
    currency_code: :usd) # default: :usd

amazon_order_reference_id = 'AMAZON_ORDER_REFERENCE_ID'
amount = '175.00'

response = client.set_order_reference_details(
    amazon_order_reference_id,
    amount)

if response.success
    response = client.get_order_reference_details(
        amazon_order_reference_id,
        address_consent_token: access_token)
end

response_hash = Hash.from_xml(response.body)
return response_hash

This is the response from the previous API call.


{
    "GetOrderReferenceDetailsResponse": {
        "ResponseMetadata": {
            "RequestId": "9816e385-9d90-4c09-a798-3942f7b6d347"
        },
        "GetOrderReferenceDetailsResult": {
            "OrderReferenceDetails": {
                "Destination": {
                    "DestinationType": "Physical",
                    "PhysicalDestination": {
                        "Phone": "800-000-0000",
                        "CountryCode": "US",
                        "City": "New York",
                        "StateOrRegion": "NY",
                        "AddressLine1": "Happy Happy Toys",
                        "Name": "Tom Thompson",
                        "PostalCode": "10016",
                        "AddressLine2": "45 Big Apple Way"
                    }
                },
                "Buyer": {
                    "Name": "Tom",
                    "Email": "tomt@example.org"
                },
                "IdList": null,
                "SellerOrderAttributes": null,
                "OrderTotal": {
                    "CurrencyCode": "USD",
                    "Amount": "175.00"
                },
                "ExpirationTimestamp": "2015-10-31T20:45:51.747Z",
                "CreationTimestamp": "2015-05-04T20:45:51.747Z",
                "AmazonOrderReferenceId": "S01-5046426-7112299",
                "ReleaseEnvironment": "Sandbox",
                "OrderReferenceStatus": {
                    "State": "Draft"
                }
            }
        }
    }
}