Skip to content

Instantly share code, notes, and snippets.

@islandskater43
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save islandskater43/3f20972d21a24a04b342 to your computer and use it in GitHub Desktop.
Save islandskater43/3f20972d21a24a04b342 to your computer and use it in GitHub Desktop.
Authorize Script for Pay with Amazon
<?php
// include Pay with Amazon SDK - available here: https://github.com/amzn/login-and-pay-with-amazon-sdk-php/
require('Client.php');
// set config values
$config = array('merchant_id' => '',
'access_key' => '',
'secret_key' => '',
'client_id' => '',
'region' => 'us',
'sandbox' => true );
$pwaClient = new \PayWithAmazon\Client($config);
// pass in the Amazon Order Reference Id, the dollar amount to authorize and Capture, and the PWA Client
authorize('S01-9639729-4675159',5.00, $pwaClient);
function authorize($amazonOrderReferenceId, $amount, $client) {
// Authorize
$requestParameters = array();
$requestParameters['amazon_order_reference_id'] = $amazonOrderReferenceId;
// a cross-account unique id for this authorization call
$requestParameters['authorization_reference_id'] = $amazonOrderReferenceId . "-" . substr(md5($amazonOrderReferenceId . microtime()),0,4);
$requestParameters['authorization_amount'] = $amount;
$requestParameters['currency_code'] = 'USD';
$requestParameters['transaction_timeout'] = 0;
$requestParameters['capture_now'] = true;
//$requestParameters['seller_authorization_note'] = '{"SandboxSimulation": {"State":"Declined", "ReasonCode":"TransactionTimedOut"}}';
$response = $client->authorize($requestParameters);
debugCallOutput("authorize",$requestParameters,$response);
$authJson = json_decode($response->toJson());
$state = $authJson->AuthorizeResult->AuthorizationDetails->AuthorizationStatus->State;
$reasonCode = $authJson->AuthorizeResult->AuthorizationDetails->AuthorizationStatus->ReasonCode;
if($state == 'Closed' && $reasonCode == 'MaxCapturesProcessed') {
print "Authorization Successful\n\n";
} else if($state == 'Declined' && $reasonCode == 'InvalidPaymentMethod') {
print "Authorization UNSUCCESSFUL - Contact customer and ask them to update their Credit Card at payments.amazon.com, then try again.\n\n";
} else if($state == 'Declined' && ( $reasonCode == 'TransactionTimedOut' || $reasonCode == 'ProcessingFailure' ) ) {
print "Authorization UNSUCCESSFUL - Wait a few hours and try this authorization again.\n\n";
} else if($state == 'Declined' && $reasonCode == 'AmazonRejected') {
print "Transaction was rejected by Amazon. Please reach out to the customer and request alternate payment.\n\n";
} else {
print "Authorization was unsuccessful. Try again later. If this persists, reach out to the customer and request alternate payment.\n\n";
}
}
function debugCallOutput ($operation,$request,$response) {
print $operation . "\n\n";
print "Input Values:\n".print_r($request,true)."\n\n";
print "Response:\n" . $response->toJson() . "\n\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment