Amazon Pay Integration
Please note that the Amazon Pay API SDK can only be used for API calls to the pay-api.amazon.com|eu|jp endpoint.
If you need to make an Amazon Pay API call that uses the mws.amazonservices.com|jp or mws-eu.amazonservices.com endpoint, then you will need to use the original Amazon Pay SDK (PHP).
Use composer to install the latest release of the SDK and its dependencies:
composer require amzn/amazon-pay-api-sdk-php
Verify the installation with the following test script:
<?php
include 'vendor/autoload.php';
echo "SDK_VERSION=" . AmazonPayAPIClient::SDK_VERSION . "n";
?>
MWS access keys, MWS secret keys, and MWS authorization tokens from previous MWS integrations cannot be used with this SDK.
You will need to generate your own public/private key pair to make API calls with this SDK.
In Windows 10 this can be done with ssh-keygen commands:
ssh-keygen -t rsa -b 2048 -f private.pem
ssh-keygen -f private.pem -e -m PKCS8 > public.pub
In Linux or macOS this can be done using openssl commands:
openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -pubout > public.pub
The first command above generates a private key and the second line uses the private key to generate a public key.
To associate the key with your account, follow the instructions here to Get your Public Key ID.
Namespace for this package is AmazonPayAPI so that there are no conflicts with the original Amazon Pay MWS SDK's that use the AmazonPay namespace.
$amazonpay_config = array(
'public_key_id' => 'ABC123DEF456XYZ', // RSA Public Key ID (this is not the Merchant or Seller ID)
'private_key' => 'keys/private.pem', // Path to RSA Private Key (or a string representation)
'sandbox' => true, // true (Sandbox) or false (Production) boolean
'region' => 'us', // Must be one of: 'us', 'eu', 'jp'
'algorithm' => 'AMZN-PAY-RSASSA-PSS-V2' //Amazon Signing Algorithm, Optional: uses AMZN-PAY-RSASSA-PSS if not specified
'integrator_id' => 'AXXXXXXXXXXXXX', // (optional) Solution Provider Platform Id in Amz UID Format
'integrator_version' => '1.2.3', // (optional) Solution Provider Plugin Version in Semantic Versioning Format
'platform_version' => '0.0.4' // (optional) Solution Provider Platform Version in Semantic Versioning Format
);
If you have created environment specific keys (i.e Public Key Starts with LIVE or SANDBOX) in Seller Central, then use those PublicKeyId & PrivateKey. In this case, there is no need to pass the Sandbox parameter to the ApiConfiguration.
$amazonpay_config = array(
'public_key_id' => 'MY_PUBLIC_KEY_ID', // LIVE-XXXXX or SANDBOX-XXXXX
'private_key' => 'keys/private.pem', // Path to RSA Private Key (or a string representation)
'region' => 'us', // Must be one of: 'us', 'eu', 'jp'
'algorithm' => 'AMZN-PAY-RSASSA-PSS-V2' //Amazon Signing Algorithm, Optional: uses AMZN-PAY-RSASSA-PSS if not specified
);
If you have want to enable proxy support, you can set it in the $amazonpay_config in the following way:
$amazonpay_config = array(
'public_key_id' => 'ABC123DEF456XYZ', // RSA Public Key ID (this is not the Merchant or Seller ID)
'private_key' => 'keys/private.pem', // Path to RSA Private Key (or a string representation)
'sandbox' => true, // true (Sandbox) or false (Production) boolean
'region' => 'us', // Must be one of: 'us', 'eu', 'jp'
'algorithm' => 'AMZN-PAY-RSASSA-PSS-V2', //Amazon Signing Algorithm, Optional: uses AMZN-PAY-RSASSA-PSS if not specified
'integrator_id' => 'AXXXXXXXXXXXXX', // (optional) Solution Provider Platform Id in Amz UID Format
'integrator_version' => '1.2.3', // (optional) Solution Provider Plugin Version in Semantic Versioning Format
'platform_version' => '0.0.4', // (optional) Solution Provider Platform Version in Semantic Versioning Format
'proxy' => [
'host' => 'proxy_host',
'port' => 'proxy_port',
'username' => 'proxy_username',
'password' => 'proxy_password',
]
);
The pay-api.amazon.com|eu|jp endpoint uses versioning to allow future updates. The major version of this SDK will stay aligned with the API version of the endpoint.
If you have downloaded version 1.x.y of this SDK, $version in below examples would be "v1". 2.x.y would be "v2", etc.
Make use of the built-in convenience functions to easily make API calls. Scroll down further to see example code snippets.
When using the convenience functions, the request payload will be signed using the provided private key, and a HTTPS request is made to the correct regional endpoint. In the event of request throttling, the HTTPS call will be attempted up to three times using an exponential backoff approach.
Use this API to provide shipment tracking information to Amazon Pay so that Amazon Pay can notify buyers on Alexa when shipments are out for delivery and when they are delivered. Please refer to the Delivery Trackers API documentation for additional information.
Please note that your solution provider account must have a pre-existing relationship (valid and active MWS authorization token) with the merchant account in order to use this function.
API Integration Guide
The $headers field is not optional for create/POST calls below because it requires, at a minimum, the x-amz-pay-idempotency-key header:
$headers = array('x-amz-pay-idempotency-key' => uniqid());
Please contact your Amazon Pay Account Manager before using the In-Store API calls in a Production environment to obtain a copy of the In-Store Integration Guide.
Four quick steps are needed to make an API call:
Step 1. Construct a Client (using the previously defined Config Array).
$client = new AmazonPayAPIClient($amazonpay_config);
Step 2. Generate the payload.
$payload = '{"scanData":"UKhrmatMeKdlfY6b","scanReferenceId":"0b8fb271-2ae2-49a5-b35d7","merchantCOE":"US","ledgerCurrency":"USD","chargeTotal":{"currencyCode":"USD","amount":"2.00"},"metadata":{"merchantNote":"Merchant Name","communicationContext":{"merchantStoreName":"Store Name","merchantOrderId":"789123"}}}';
Step 3. Execute the call.
$result = $client->instoreMerchantScan($payload);
Step 4. Check the result.
The $result will be an array with the following keys:
The first two items (status, response) are critical. The remaining items are useful in troubleshooting situations.
To parse the response in PHP, you can use the PHP json_decode() function:
$response = json_decode($result['response'], true);
$id = $response['chargePermissionId'];
If you are a Solution Provider and need to make an API call on behalf of a different merchant account, you will need to pass along an extra authentication token parameter into the API call.
$headers = array('x-amz-pay-authtoken' => 'other_merchant_super_secret_token');
$result = $client->instoreMerchantScan($payload, $headers);
An alternate way to do Step 2 would be to use PHP arrays and programmatically generate the JSON payload:
$payload = array(
'scanData' => 'UKhrmatMeKdlfY6b',
'scanReferenceId' => uniqid(),
'merchantCOE' => 'US',
'ledgerCurrency' => 'USD',
'chargeTotal' => array(
'currencyCode' => 'USD',
'amount' => '2.00'
),
'metadata' => array(
'merchantNote' => 'Merchant Name',
'communicationContext' => array(
'merchantStoreName' => 'Store Name',
'merchantOrderId' => '789123'
)
)
);
$payload = json_encode($payload);
<?php
include 'vendor/autoload.php';
$amazonpay_config = array(
'public_key_id' => 'MY_PUBLIC_KEY_ID',
'private_key' => 'keys/private.pem',
'region' => 'US',
'sandbox' => false,
'algorithm' => 'AMZN-PAY-RSASSA-PSS-V2',
);
$payload = array(
'amazonOrderReferenceId' => 'P01-0000000-0000000',
'deliveryDetails' => array(array(
'trackingNumber' => '01234567890',
'carrierCode' => 'FEDEX'
))
);
try {
$client = new AmazonPayAPIClient($amazonpay_config);
$result = $client->deliveryTrackers($payload);
if ($result['status'] === 200) {
// success
echo $result['response'] . "n";
} else {
// check the error
echo 'status=' . $result['status'] . '; response=' . $result['response'] . "n";
}
} catch (Exception $e) {
// handle the exception
echo $e . "n";
}
?>
<?php
session_start();
include 'vendor/autoload.php';
$amazonpay_config = array(
'public_key_id' => 'MY_PUBLIC_KEY_ID',
'private_key' => 'keys/private.pem',
'region' => 'US',
'sandbox' => true,
'algorithm' => 'AMZN-PAY-RSASSA-PSS-V2',
'integrator_id' => 'AXXXXXXXXXXXXX', // (optional) Solution Provider Platform Id in Amz UID Format
'integrator_version' => '1.2.3', // (optional) Solution Provider Plugin Version in Semantic Versioning Format
'platform_version' => '0.0.4' // (optional) Solution Provider Platform Version in Semantic Versioning Format
);
$payload = array(
'webCheckoutDetails' => array(
'checkoutReviewReturnUrl' => 'https://localhost/store/checkout_review',
'checkoutResultReturnUrl' => 'https://localhost/store/checkout_result'
),
'storeId' => 'amzn1.application-oa2-client.000000000000000000000000000000000'
);
$headers = array('x-amz-pay-Idempotency-Key' => uniqid());
try {
$client = new AmazonPayAPIClient($amazonpay_config);
$result = $client->createCheckoutSession($payload, $headers);
header("Content-type:application/json; charset=utf-8");
echo $result['response'];
if ($result['status'] !== 201) {
http_response_code(500);
}
} catch (Exception $e) {
// handle the exception
echo $e . "n";
http_response_code(500);
}
?>
<?php
include 'vendor/autoload.php';
$amazonpay_config = array(
'public_key_id' => 'YOUR_PUBLIC_KEY_ID',
'private_key' => 'keys/private.pem', // Path to RSA Private Key (or a string representation)
'region' => 'YOUR_REGION_CODE',
'sandbox' => true,
'algorithm' => 'AMZN-PAY-RSASSA-PSS-V2',
'integrator_id' => 'AXXXXXXXXXXXXX', // (optional) Solution Provider Platform Id in Amz UID Format
'integrator_version' => '1.2.3', // (optional) Solution Provider Plugin Version in Semantic Versioning Format
'platform_version' => '0.0.4' // (optional) Solution Provider Platform Version in Semantic Versioning Format
);
$payload = array(
'webCheckoutDetails' => array(
'checkoutReviewReturnUrl' => 'https://localhost/store/checkout_review',
'checkoutResultReturnUrl' => 'https://localhost/store/checkout_result'
),
'storeId' => 'amzn1.application-oa2-client.000000000000000000000000000000000'
);
$headers = array('x-amz-pay-Idempotency-Key' => uniqid());
try {
$client = new AmazonPayAPIClient($amazonpay_config);
$result = $client->createCheckoutSession($payload, $headers);
if ($result['status'] === 201) {
// created
$response = json_decode($result['response'], true);
$checkoutSessionId = $response['checkoutSessionId'];
echo "checkoutSessionId=$checkoutSessionIdn";
} else {
// check the error
echo 'status=' . $result['status'] . '; response=' . $result['response'] . "n";
}
} catch (Exception $e) {
// handle the exception
echo $e . "n";
}
?>
<?php
include 'vendor/autoload.php';
$amazonpay_config = array(
'public_key_id' => 'YOUR_PUBLIC_KEY_ID',
'private_key' => 'keys/private.pem', // Path to RSA Private Key (or a string representation)
'region' => 'YOUR_REGION_CODE',
'sandbox' => true,
'algorithm' => 'AMZN-PAY-RSASSA-PSS-V2',
'integrator_id' => 'AXXXXXXXXXXXXX', // (optional) Solution Provider Platform Id in Amz UID Format
'integrator_version' => '1.2.3', // (optional) Solution Provider Plugin Version in Semantic Versioning Format
'platform_version' => '0.0.4' // (optional) Solution Provider Platform Version in Semantic Versioning Format
);
try {
$checkoutSessionId = '00000000-0000-0000-0000-000000000000';
$client = new AmazonPayAPIClient($amazonpay_config);
$result = $client->getCheckoutSession($checkoutSessionId);
if ($result['status'] === 200) {
$response = json_decode($result['response'], true);
$checkoutSessionState = $response['statusDetails']['state'];
$chargeId = $response['chargeId'];
$chargePermissionId = $response['chargePermissionId'];
// NOTE: Once Checkout Session moves to a "Completed" state, buyer and shipping
// details must be obtained from the getCharges() function call instead
$buyerName = $response['buyer']['name'];
$buyerEmail = $response['buyer']['email'];
$shipName = $response['shippingAddress']['name'];
$shipAddrLine1 = $response['shippingAddress']['addressLine1'];
$shipCity = $response['shippingAddress']['city'];
$shipState = $response['shippingAddress']['stateOrRegion'];
$shipZip = $response['shippingAddress']['postalCode'];
$shipCounty = $response['shippingAddress']['countryCode'];
echo "checkoutSessionState=$checkoutSessionStaten";
echo "chargeId=$chargeId; chargePermissionId=$chargePermissionIdn";
echo "buyer=$buyerName ($buyerEmail)n";
echo "shipName=$shipNamen";
echo "address=$shipAddrLine1; $shipCity $shipState $shipZip ($shipCounty)n";
} else {
// check the error
echo 'status=' . $result['status'] . '; response=' . $result['response'] . "n";
}
} catch (Exception $e) {
// handle the exception
echo $e . "n";
}
?>
<?php
include 'vendor/autoload.php';
$amazonpay_config = array(
'public_key_id' => 'YOUR_PUBLIC_KEY_ID',
'private_key' => 'keys/private.pem', // Path to RSA Private Key (or a string representation)
'region' => 'YOUR_REGION_CODE',
'sandbox' => true,
'algorithm' => 'AMZN-PAY-RSASSA-PSS-V2',
'integrator_id' => 'AXXXXXXXXXXXXX', // (optional) Solution Provider Platform Id in Amz UID Format
'integrator_version' => '1.2.3', // (optional) Solution Provider Plugin Version in Semantic Versioning Format
'platform_version' => '0.0.4' // (optional) Solution Provider Platform Version in Semantic Versioning Format
);
$payload = array(
'paymentDetails' =>