diff --git a/.gitignore b/.gitignore index 7579f74..5a2d43a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor composer.lock + diff --git a/AmazonPay/Client.php b/AmazonPay/Client.php index dddb72b..5169a7e 100644 --- a/AmazonPay/Client.php +++ b/AmazonPay/Client.php @@ -23,7 +23,7 @@ class Client implements ClientInterface, LoggerAwareInterface { - const SDK_VERSION = '3.1.0'; + const SDK_VERSION = '3.2.0'; const MWS_VERSION = '2013-01-01'; const MAX_ERROR_RETRY = 3; @@ -50,7 +50,8 @@ class Client implements ClientInterface, LoggerAwareInterface 'proxy_password' => null, 'client_id' => null, 'app_id' => null, - 'handle_throttle' => true + 'handle_throttle' => true, + 'override_service_url' => null ); private $modePath = null; @@ -67,11 +68,11 @@ class Client implements ClientInterface, LoggerAwareInterface // Boolean variable to check if the API call was a success public $success = false; + /* Takes user configuration array from the user as input * Takes JSON file path with configuration information as input * Validates the user configuration array against existing config array */ - public function __construct($config = null) { $this->getRegionUrls(); @@ -99,55 +100,55 @@ public function __construct($config = null) } } + public function setLogger(LoggerInterface $logger = null) { $this->logger = $logger; } - /* Helper function to log data within the Client */ + /* Helper function to log data within the Client */ private function logMessage($message) { if ($this->logger) { $this->logger->debug($message); } } + /* Get the Region specific properties from the Regions class.*/ - private function getRegionUrls() { - $regionObject = new Regions(); - $this->mwsServiceUrls = $regionObject->mwsServiceUrls; - $this->regionMappings = $regionObject->regionMappings; - $this->profileEndpointUrls = $regionObject->profileEndpointUrls; + $regionObject = new Regions(); + $this->mwsServiceUrls = $regionObject->mwsServiceUrls; + $this->regionMappings = $regionObject->regionMappings; + $this->profileEndpointUrls = $regionObject->profileEndpointUrls; } - /* checkIfFileExists - check if the JSON file exists in the path provided */ + /* checkIfFileExists - check if the JSON file exists in the path provided */ private function checkIfFileExists($config) { - if(file_exists($config)) - { - $jsonString = file_get_contents($config); - $configArray = json_decode($jsonString, true); + if (file_exists($config)) { + $jsonString = file_get_contents($config); + $configArray = json_decode($jsonString, true); - $jsonError = json_last_error(); + $jsonError = json_last_error(); - if ($jsonError != 0) { - $errorMsg = "Error with message - content is not in json format" . $this->getErrorMessageForJsonError($jsonError) . " " . $configArray; - throw new \Exception($errorMsg); + if ($jsonError != 0) { + $errorMsg = "Error with message - content is not in json format" . $this->getErrorMessageForJsonError($jsonError) . " " . $configArray; + throw new \Exception($errorMsg); + } + } else { + $errorMsg ='$config is not a Json File path or the Json File was not found in the path provided'; + throw new \Exception($errorMsg); } - } else { - $errorMsg ='$config is not a Json File path or the Json File was not found in the path provided'; - throw new \Exception($errorMsg); - } - return $configArray; + return $configArray; } + /* Checks if the keys of the input configuration matches the keys in the config array * if they match the values are taken else throws exception * strict case match is not performed */ - private function checkConfigKeys($config) { $config = array_change_key_case($config, CASE_LOWER); @@ -163,13 +164,13 @@ private function checkConfigKeys($config) } } + /* Convert a json error code to a descriptive error message * * @param int $jsonError message code * * @return string error message */ - private function getErrorMessageForJsonError($jsonError) { switch ($jsonError) { @@ -191,10 +192,10 @@ private function getErrorMessageForJsonError($jsonError) } } + /* Setter for sandbox * Sets the Boolean value for config['sandbox'] variable */ - public function setSandbox($value) { if (is_bool($value)) { @@ -204,10 +205,10 @@ public function setSandbox($value) } } + /* Setter for config['client_id'] * Sets the value for config['client_id'] variable */ - public function setClientId($value) { if (!empty($value)) { @@ -217,10 +218,10 @@ public function setClientId($value) } } + /* Setter for config['app_id'] * Sets the value for config['app_id'] variable */ - public function setAppId($value) { if (!empty($value)) { @@ -230,6 +231,7 @@ public function setAppId($value) } } + /* Setter for Proxy * input $proxy [array] * @param $proxy['proxy_user_host'] - hostname for the proxy @@ -237,11 +239,10 @@ public function setAppId($value) * @param $proxy['proxy_user_name'] - if your proxy required a username * @param $proxy['proxy_user_password'] - if your proxy required a password */ - public function setProxy($proxy) { - if (!empty($proxy['proxy_user_host'])) - $this->config['proxy_host'] = $proxy['proxy_user_host']; + if (!empty($proxy['proxy_user_host'])) + $this->config['proxy_host'] = $proxy['proxy_user_host']; if (!empty($proxy['proxy_user_port'])) $this->config['proxy_port'] = $proxy['proxy_user_port']; @@ -253,19 +254,19 @@ public function setProxy($proxy) $this->config['proxy_password'] = $proxy['proxy_user_password']; } + /* Setter for $mwsServiceUrl * Set the URL to which the post request has to be made for unit testing */ - public function setMwsServiceUrl($url) { - $this->mwsServiceUrl = $url; + $this->mwsServiceUrl = $url; } + /* Getter * Gets the value for the key if the key exists in config */ - public function __get($name) { if (array_key_exists(strtolower($name), $this->config)) { @@ -275,37 +276,35 @@ public function __get($name) } } + /* Getter for parameters string * Gets the value for the parameters string for unit testing */ - public function getParameters() { - return trim($this->parameters); + return trim($this->parameters); } + /* Trim the input Array key values */ - private function trimArray($array) { - foreach ($array as $key => $value) - { + foreach ($array as $key => $value) { // Do not attemp to trim array variables, boolean variables, or the proxy password // Trimming a boolean value (as a string) may not produce the expected output, so pass it through as-is - if (!is_array($value) && !is_bool($value) && $key!=='proxy_password') - { + if (!is_array($value) && !is_bool($value) && $key !== 'proxy_password') { $array[$key] = trim($value); } } return $array; } + /* GetUserInfo convenience function - Returns user's profile information from Amazon using the access token returned by the Button widget. * * @see http://login.amazon.com/website Step 4 * @param $accessToken [String] */ - public function getUserInfo($accessToken) { // Get the correct Profile Endpoint URL based off the country/region provided in the config['region'] @@ -344,11 +343,11 @@ public function getUserInfo($accessToken) return $userInfo; } + /* setParametersAndPost - sets the parameters array with non empty values from the requestParameters array sent to API calls. * If Provider Credit Details is present, values are set by setProviderCreditDetails * If Provider Credit Reversal Details is present, values are set by setProviderCreditDetails */ - private function setParametersAndPost($parameters, $fieldMappings, $requestParameters) { /* For loop to take all the non empty parameters in the $requestParameters and add it into the $parameters array, @@ -362,10 +361,14 @@ private function setParametersAndPost($parameters, $fieldMappings, $requestParam } // Ensure that no unexpected type coercions have happened - if ($param === 'capture_now' || $param === 'confirm_now' || $param === 'inherit_shipping_address') { + if ($param === 'capture_now' || $param === 'confirm_now' || $param === 'inherit_shipping_address' || $param === 'request_payment_authorization') { if (!is_bool($value)) { throw new \Exception($param . ' value ' . $value . ' is of type ' . gettype($value) . ' and should be a boolean value'); } + } elseif ($param === 'provider_credit_details' || $param === 'provider_credit_reversal_details' || $param === 'order_item_categories') { + if (!is_array($value)) { + throw new \Exception($param . ' value ' . $value . ' is of type ' . gettype($value) . ' and should be an array value'); + } } // When checking for non-empty values, consider any boolean as non-empty @@ -377,6 +380,8 @@ private function setParametersAndPost($parameters, $fieldMappings, $requestParam $parameters = $this->setProviderCreditDetails($parameters, $value); } elseif ($param === 'provider_credit_reversal_details') { $parameters = $this->setProviderCreditReversalDetails($parameters, $value); + } elseif ($param === 'order_item_categories') { + $parameters = $this->setOrderItemCategories($parameters, $value); } } else { @@ -391,8 +396,8 @@ private function setParametersAndPost($parameters, $fieldMappings, $requestParam return $responseObject; } - /* calculateSignatureAndPost - convert the Parameters array to string and curl POST the parameters to MWS */ + /* calculateSignatureAndPost - convert the Parameters array to string and curl POST the parameters to MWS */ private function calculateSignatureAndPost($parameters) { // Call the signature and Post function to perform the actions. Returns XML in array format @@ -406,6 +411,7 @@ private function calculateSignatureAndPost($parameters) return $responseObject; } + /* If merchant_id is not set via the requestParameters array then it's taken from the config array * * Set the platform_id if set in the config['platform_id'] array @@ -413,21 +419,20 @@ private function calculateSignatureAndPost($parameters) * If currency_code is set in the $requestParameters and it exists in the $fieldMappings array, strtoupper it * else take the value from config array if set */ - private function setDefaultValues($parameters, $fieldMappings, $requestParameters) { if (empty($requestParameters['merchant_id'])) $parameters['SellerId'] = $this->config['merchant_id']; if (array_key_exists('platform_id', $fieldMappings)) { - if (empty($requestParameters['platform_id']) && !empty($this->config['platform_id'])) - $parameters[$fieldMappings['platform_id']] = $this->config['platform_id']; - } - + if (empty($requestParameters['platform_id']) && !empty($this->config['platform_id'])) + $parameters[$fieldMappings['platform_id']] = $this->config['platform_id']; + } if (array_key_exists('currency_code', $fieldMappings)) { if (!empty($requestParameters['currency_code'])) { - $parameters[$fieldMappings['currency_code']] = strtoupper($requestParameters['currency_code']); - } else { + $parameters[$fieldMappings['currency_code']] = strtoupper($requestParameters['currency_code']); + } else if (!(array_key_exists('Action', $parameters) && $parameters['Action'] === 'SetOrderAttributes')) { + // Only supply a default CurrencyCode parameter if not using SetOrderAttributes API $parameters[$fieldMappings['currency_code']] = strtoupper($this->config['currency_code']); } } @@ -435,16 +440,33 @@ private function setDefaultValues($parameters, $fieldMappings, $requestParameter return $parameters; } + + /* setOrderItemCategories - helper function used by SetOrderAttributes API to set + * one or more Order Item Categories + */ + private function setOrderItemCategories($parameters, $categories) + { + $categoryIndex = 0; + $categoryString = 'OrderAttributes.SellerOrderAttributes.OrderItemCategories.OrderItemCategory.'; + + foreach ($categories as $value) { + $categoryIndex = $categoryIndex + 1; + $parameters[$categoryString . $categoryIndex] = $value; + } + + return $parameters; + } + + /* setProviderCreditDetails - sets the provider credit details sent via the Capture or Authorize API calls * @param provider_id - [String] * @param credit_amount - [String] * @optional currency_code - [String] */ - private function setProviderCreditDetails($parameters, $providerCreditInfo) { - $providerIndex = 0; - $providerString = 'ProviderCreditList.member.'; + $providerIndex = 0; + $providerString = 'ProviderCreditList.member.'; $fieldMappings = array( 'provider_id' => 'ProviderId', @@ -452,67 +474,62 @@ private function setProviderCreditDetails($parameters, $providerCreditInfo) 'currency_code' => 'CreditAmount.CurrencyCode' ); - foreach ($providerCreditInfo as $key => $value) - { - $value = array_change_key_case($value, CASE_LOWER); - $providerIndex = $providerIndex + 1; + foreach ($providerCreditInfo as $key => $value) { + $value = array_change_key_case($value, CASE_LOWER); + $providerIndex = $providerIndex + 1; - foreach ($value as $param => $val) - { - if (array_key_exists($param, $fieldMappings) && trim($val)!='') { - $parameters[$providerString.$providerIndex. '.' .$fieldMappings[$param]] = $val; - } - } + foreach ($value as $param => $val) { + if (array_key_exists($param, $fieldMappings) && trim($val)!='') { + $parameters[$providerString.$providerIndex. '.' .$fieldMappings[$param]] = $val; + } + } - // If currency code is not entered take it from the config array - if(empty($parameters[$providerString.$providerIndex. '.' .$fieldMappings['currency_code']])) - { - $parameters[$providerString.$providerIndex. '.' .$fieldMappings['currency_code']] = strtoupper($this->config['currency_code']); + // If currency code is not entered take it from the config array + if (empty($parameters[$providerString.$providerIndex. '.' .$fieldMappings['currency_code']])) { + $parameters[$providerString.$providerIndex. '.' .$fieldMappings['currency_code']] = strtoupper($this->config['currency_code']); + } } - } - return $parameters; + return $parameters; } + /* setProviderCreditReversalDetails - sets the reverse provider credit details sent via the Refund API call. * @param provider_id - [String] * @param credit_amount - [String] * @optional currency_code - [String] */ - private function setProviderCreditReversalDetails($parameters, $providerCreditInfo) { - $providerIndex = 0; - $providerString = 'ProviderCreditReversalList.member.'; + $providerIndex = 0; + $providerString = 'ProviderCreditReversalList.member.'; $fieldMappings = array( 'provider_id' => 'ProviderId', - 'credit_reversal_amount' => 'CreditReversalAmount.Amount', - 'currency_code' => 'CreditReversalAmount.CurrencyCode' + 'credit_reversal_amount' => 'CreditReversalAmount.Amount', + 'currency_code' => 'CreditReversalAmount.CurrencyCode' ); - foreach ($providerCreditInfo as $key => $value) - { - $value = array_change_key_case($value, CASE_LOWER); - $providerIndex = $providerIndex + 1; + foreach ($providerCreditInfo as $key => $value) { + $value = array_change_key_case($value, CASE_LOWER); + $providerIndex = $providerIndex + 1; - foreach ($value as $param => $val) - { - if (array_key_exists($param, $fieldMappings) && trim($val)!='') { - $parameters[$providerString.$providerIndex. '.' .$fieldMappings[$param]] = $val; - } - } + foreach ($value as $param => $val) { + if (array_key_exists($param, $fieldMappings) && trim($val)!='') { + $parameters[$providerString.$providerIndex. '.' .$fieldMappings[$param]] = $val; + } + } - // If currency code is not entered take it from the config array - if(empty($parameters[$providerString.$providerIndex. '.' .$fieldMappings['currency_code']])) - { - $parameters[$providerString.$providerIndex. '.' .$fieldMappings['currency_code']] = strtoupper($this->config['currency_code']); + // If currency code is not entered take it from the config array + if (empty($parameters[$providerString.$providerIndex. '.' .$fieldMappings['currency_code']])) { + $parameters[$providerString.$providerIndex. '.' .$fieldMappings['currency_code']] = strtoupper($this->config['currency_code']); + } } - } - return $parameters; + return $parameters; } + /* GetOrderReferenceDetails API call - Returns details about the Order Reference object and its current state. * @see https://pay.amazon.com/developer/documentation/apireference/201751970 * @@ -525,7 +542,6 @@ private function setProviderCreditReversalDetails($parameters, $providerCreditIn * You cannot pass both address_consent_token and access_token in * the same call or you will encounter a 400/"AmbiguousToken" error */ - public function getOrderReferenceDetails($requestParameters = array()) { @@ -544,6 +560,7 @@ public function getOrderReferenceDetails($requestParameters = array()) return ($responseObject); } + /* SetOrderReferenceDetails API call - Sets order reference details such as the order total and a description for the order. * @see https://pay.amazon.com/developer/documentation/apireference/201751960 * @@ -556,9 +573,9 @@ public function getOrderReferenceDetails($requestParameters = array()) * @optional requestParameters['seller_order_id'] - [String] * @optional requestParameters['store_name'] - [String] * @optional requestParameters['custom_information'] - [String] + * @optional requestParameters['request_payment_authorization'] - [Boolean] * @optional requestParameters['mws_auth_token'] - [String] */ - public function setOrderReferenceDetails($requestParameters = array()) { $parameters = array(); @@ -566,23 +583,69 @@ public function setOrderReferenceDetails($requestParameters = array()) $requestParameters = array_change_key_case($requestParameters, CASE_LOWER); $fieldMappings = array( - 'merchant_id' => 'SellerId', - 'amazon_order_reference_id' => 'AmazonOrderReferenceId', - 'amount' => 'OrderReferenceAttributes.OrderTotal.Amount', - 'currency_code' => 'OrderReferenceAttributes.OrderTotal.CurrencyCode', - 'platform_id' => 'OrderReferenceAttributes.PlatformId', - 'seller_note' => 'OrderReferenceAttributes.SellerNote', - 'seller_order_id' => 'OrderReferenceAttributes.SellerOrderAttributes.SellerOrderId', - 'store_name' => 'OrderReferenceAttributes.SellerOrderAttributes.StoreName', - 'custom_information' => 'OrderReferenceAttributes.SellerOrderAttributes.CustomInformation', - 'mws_auth_token' => 'MWSAuthToken' + 'merchant_id' => 'SellerId', + 'amazon_order_reference_id' => 'AmazonOrderReferenceId', + 'amount' => 'OrderReferenceAttributes.OrderTotal.Amount', + 'currency_code' => 'OrderReferenceAttributes.OrderTotal.CurrencyCode', + 'platform_id' => 'OrderReferenceAttributes.PlatformId', + 'seller_note' => 'OrderReferenceAttributes.SellerNote', + 'seller_order_id' => 'OrderReferenceAttributes.SellerOrderAttributes.SellerOrderId', + 'store_name' => 'OrderReferenceAttributes.SellerOrderAttributes.StoreName', + 'custom_information' => 'OrderReferenceAttributes.SellerOrderAttributes.CustomInformation', + 'request_payment_authorization' => 'OrderReferenceAttributes.RequestPaymentAuthorization', + 'mws_auth_token' => 'MWSAuthToken' ); $responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters); + return ($responseObject); + } - return ($responseObject); + + /* SetOrderAttributes API call - Sets order reference details such as the order total and a description for the order. + * + * @param requestParameters['merchant_id'] - [String] + * @param requestParameters['amazon_order_reference_id'] - [String] + * @optional requestParameters['amount'] - [String] + * @optional requestParameters['currency_code'] - [String] + * @optional requestParameters['platform_id'] - [String] + * @optional requestParameters['seller_note'] - [String] + * @optional requestParameters['seller_order_id'] - [String] + * @optional requestParameters['store_name'] - [String] + * @optional requestParameters['custom_information'] - [String] + * @optional requestParameters['request_payment_authorization'] - [Boolean] + * @optional requestParameters['payment_service_provider_id'] - [String] + * @optional requestParameters['payment_service_provider_order_id'] - [String] + * @optional requestParameters['order_item_categories'] - [array()] + * @optional requestParameters['mws_auth_token'] - [String] + */ + public function setOrderAttributes($requestParameters = array()) + { + $parameters = array(); + $parameters['Action'] = 'SetOrderAttributes'; + $requestParameters = array_change_key_case($requestParameters, CASE_LOWER); + + $fieldMappings = array( + 'merchant_id' => 'SellerId', + 'amazon_order_reference_id' => 'AmazonOrderReferenceId', + 'amount' => 'OrderAttributes.OrderTotal.Amount', + 'currency_code' => 'OrderAttributes.OrderTotal.CurrencyCode', + 'platform_id' => 'OrderAttributes.PlatformId', + 'seller_note' => 'OrderAttributes.SellerNote', + 'seller_order_id' => 'OrderAttributes.SellerOrderAttributes.SellerOrderId', + 'store_name' => 'OrderAttributes.SellerOrderAttributes.StoreName', + 'custom_information' => 'OrderAttributes.SellerOrderAttributes.CustomInformation', + 'request_payment_authorization' => 'OrderAttributes.RequestPaymentAuthorization', + 'payment_service_provider_id' => 'OrderAttributes.PaymentServiceProviderAttributes.PaymentServiceProviderId', + 'payment_service_provider_order_id' => 'OrderAttributes.PaymentServiceProviderAttributes.PaymentServiceProviderOrderId', + 'order_item_categories' => array(), + 'mws_auth_token' => 'MWSAuthToken' + ); + + $responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters); + return ($responseObject); } + /* ConfirmOrderReference API call - Confirms that the order reference is free of constraints and all required information has been set on the order reference. * @see https://pay.amazon.com/developer/documentation/apireference/201751980 @@ -590,7 +653,6 @@ public function setOrderReferenceDetails($requestParameters = array()) * @param requestParameters['amazon_order_reference_id'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function confirmOrderReference($requestParameters = array()) { $parameters = array(); @@ -604,10 +666,10 @@ public function confirmOrderReference($requestParameters = array()) ); $responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters); - return ($responseObject); } + /* CancelOrderReference API call - Cancels a previously confirmed order reference. * @see https://pay.amazon.com/developer/documentation/apireference/201751990 * @@ -616,7 +678,6 @@ public function confirmOrderReference($requestParameters = array()) * @optional requestParameters['cancelation_reason'] [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function cancelOrderReference($requestParameters = array()) { $parameters = array(); @@ -624,17 +685,18 @@ public function cancelOrderReference($requestParameters = array()) $requestParameters = array_change_key_case($requestParameters, CASE_LOWER); $fieldMappings = array( - 'merchant_id' => 'SellerId', + 'merchant_id' => 'SellerId', 'amazon_order_reference_id' => 'AmazonOrderReferenceId', - 'cancelation_reason' => 'CancelationReason', - 'mws_auth_token' => 'MWSAuthToken' + 'cancelation_reason' => 'CancelationReason', + 'mws_auth_token' => 'MWSAuthToken' ); $responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters); - return ($responseObject); + return ($responseObject); } + /* CloseOrderReference API call - Confirms that an order reference has been fulfilled (fully or partially) * and that you do not expect to create any new authorizations on this order reference. * @see https://pay.amazon.com/developer/documentation/apireference/201752000 @@ -644,7 +706,6 @@ public function cancelOrderReference($requestParameters = array()) * @optional requestParameters['closure_reason'] [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function closeOrderReference($requestParameters = array()) { $parameters = array(); @@ -652,10 +713,10 @@ public function closeOrderReference($requestParameters = array()) $requestParameters = array_change_key_case($requestParameters, CASE_LOWER); $fieldMappings = array( - 'merchant_id' => 'SellerId', + 'merchant_id' => 'SellerId', 'amazon_order_reference_id' => 'AmazonOrderReferenceId', - 'closure_reason' => 'ClosureReason', - 'mws_auth_token' => 'MWSAuthToken' + 'closure_reason' => 'ClosureReason', + 'mws_auth_token' => 'MWSAuthToken' ); $responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters); @@ -663,6 +724,7 @@ public function closeOrderReference($requestParameters = array()) return ($responseObject); } + /* CloseAuthorization API call - Closes an authorization. * @see https://pay.amazon.com/developer/documentation/apireference/201752070 * @@ -671,7 +733,6 @@ public function closeOrderReference($requestParameters = array()) * @optional requestParameters['closure_reason'] [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function closeAuthorization($requestParameters = array()) { $parameters = array(); @@ -698,7 +759,7 @@ public function closeAuthorization($requestParameters = array()) * @param requestParameters['authorization_amount'] [String] * @param requestParameters['currency_code'] - [String] * @param requestParameters['authorization_reference_id'] [String] - * @optional requestParameters['capture_now'] [String] + * @optional requestParameters['capture_now'] [Boolean] * @optional requestParameters['provider_credit_details'] - [array (array())] * @optional requestParameters['seller_authorization_note'] [String] * @optional requestParameters['transaction_timeout'] [String] - Defaults to 1440 minutes @@ -706,6 +767,7 @@ public function closeAuthorization($requestParameters = array()) * @optional requestParameters['mws_auth_token'] - [String] */ + public function authorize($requestParameters = array()) { $parameters = array(); @@ -731,6 +793,7 @@ public function authorize($requestParameters = array()) return ($responseObject); } + /* GetAuthorizationDetails API call - Returns the status of a particular authorization and the total amount captured on the authorization. * @see https://pay.amazon.com/developer/documentation/apireference/201752030 * @@ -738,7 +801,6 @@ public function authorize($requestParameters = array()) * @param requestParameters['amazon_authorization_id'] [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function getAuthorizationDetails($requestParameters = array()) { $parameters = array(); @@ -746,13 +808,12 @@ public function getAuthorizationDetails($requestParameters = array()) $requestParameters = array_change_key_case($requestParameters, CASE_LOWER); $fieldMappings = array( - 'merchant_id' => 'SellerId', - 'amazon_authorization_id' => 'AmazonAuthorizationId', - 'mws_auth_token' => 'MWSAuthToken' + 'merchant_id' => 'SellerId', + 'amazon_authorization_id' => 'AmazonAuthorizationId', + 'mws_auth_token' => 'MWSAuthToken' ); $responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters); - return ($responseObject); } @@ -769,7 +830,6 @@ public function getAuthorizationDetails($requestParameters = array()) * @optional requestParameters['soft_descriptor'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function capture($requestParameters = array()) { $parameters = array(); @@ -777,22 +837,22 @@ public function capture($requestParameters = array()) $requestParameters = array_change_key_case($requestParameters, CASE_LOWER); $fieldMappings = array( - 'merchant_id' => 'SellerId', - 'amazon_authorization_id' => 'AmazonAuthorizationId', - 'capture_amount' => 'CaptureAmount.Amount', - 'currency_code' => 'CaptureAmount.CurrencyCode', - 'capture_reference_id' => 'CaptureReferenceId', - 'provider_credit_details' => array(), + 'merchant_id' => 'SellerId', + 'amazon_authorization_id' => 'AmazonAuthorizationId', + 'capture_amount' => 'CaptureAmount.Amount', + 'currency_code' => 'CaptureAmount.CurrencyCode', + 'capture_reference_id' => 'CaptureReferenceId', + 'provider_credit_details' => array(), 'seller_capture_note' => 'SellerCaptureNote', 'soft_descriptor' => 'SoftDescriptor', - 'mws_auth_token' => 'MWSAuthToken' + 'mws_auth_token' => 'MWSAuthToken' ); $responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters); - - return ($responseObject); + return ($responseObject); } + /* GetCaptureDetails API call - Returns the status of a particular capture and the total amount refunded on the capture. * @see https://pay.amazon.com/developer/documentation/apireference/201752060 * @@ -800,7 +860,6 @@ public function capture($requestParameters = array()) * @param requestParameters['amazon_capture_id'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function getCaptureDetails($requestParameters = array()) { $parameters = array(); @@ -808,16 +867,16 @@ public function getCaptureDetails($requestParameters = array()) $requestParameters = array_change_key_case($requestParameters, CASE_LOWER); $fieldMappings = array( - 'merchant_id' => 'SellerId', + 'merchant_id' => 'SellerId', 'amazon_capture_id' => 'AmazonCaptureId', - 'mws_auth_token' => 'MWSAuthToken' + 'mws_auth_token' => 'MWSAuthToken' ); $responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters); - return ($responseObject); } + /* Refund API call - Refunds a previously captured amount. * @see https://pay.amazon.com/developer/documentation/apireference/201752080 * @@ -831,7 +890,6 @@ public function getCaptureDetails($requestParameters = array()) * @optional requestParameters['soft_descriptor'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function refund($requestParameters = array()) { $parameters = array(); @@ -839,22 +897,22 @@ public function refund($requestParameters = array()) $requestParameters = array_change_key_case($requestParameters, CASE_LOWER); $fieldMappings = array( - 'merchant_id' => 'SellerId', - 'amazon_capture_id' => 'AmazonCaptureId', - 'refund_reference_id' => 'RefundReferenceId', - 'refund_amount' => 'RefundAmount.Amount', - 'currency_code' => 'RefundAmount.CurrencyCode', - 'provider_credit_reversal_details' => array(), - 'seller_refund_note' => 'SellerRefundNote', - 'soft_descriptor' => 'SoftDescriptor', - 'mws_auth_token' => 'MWSAuthToken' + 'merchant_id' => 'SellerId', + 'amazon_capture_id' => 'AmazonCaptureId', + 'refund_reference_id' => 'RefundReferenceId', + 'refund_amount' => 'RefundAmount.Amount', + 'currency_code' => 'RefundAmount.CurrencyCode', + 'provider_credit_reversal_details' => array(), + 'seller_refund_note' => 'SellerRefundNote', + 'soft_descriptor' => 'SoftDescriptor', + 'mws_auth_token' => 'MWSAuthToken' ); $responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters); - return ($responseObject); } + /* GetRefundDetails API call - Returns the status of a particular refund. * @see https://pay.amazon.com/developer/documentation/apireference/201752100 * @@ -862,7 +920,6 @@ public function refund($requestParameters = array()) * @param requestParameters['amazon_refund_id'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function getRefundDetails($requestParameters = array()) { $parameters = array(); @@ -880,6 +937,7 @@ public function getRefundDetails($requestParameters = array()) return ($responseObject); } + /* GetServiceStatus API Call - Returns the operational status of the OffAmazonPayments API section * @see https://pay.amazon.com/developer/documentation/apireference/201752110 * @@ -890,7 +948,6 @@ public function getRefundDetails($requestParameters = array()) * @param requestParameters['merchant_id'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function getServiceStatus($requestParameters = array()) { $parameters = array(); @@ -904,9 +961,10 @@ public function getServiceStatus($requestParameters = array()) $responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters); - return ($responseObject); + return ($responseObject); } + /* CreateOrderReferenceForId API Call - Creates an order reference for the given object * @see https://pay.amazon.com/developer/documentation/apireference/201751670 * @@ -922,7 +980,6 @@ public function getServiceStatus($requestParameters = array()) * @optional requestParameters['custom_information'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function createOrderReferenceForId($requestParameters = array()) { $parameters = array(); @@ -946,10 +1003,10 @@ public function createOrderReferenceForId($requestParameters = array()) ); $responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters); - return ($responseObject); } + /* GetBillingAgreementDetails API Call - Returns details about the Billing Agreement object and its current state. * @see https://pay.amazon.com/developer/documentation/apireference/201751690 * @@ -962,7 +1019,6 @@ public function createOrderReferenceForId($requestParameters = array()) * You cannot pass both address_consent_token and access_token in * the same call or you will encounter a 400/"AmbiguousToken" error */ - public function getBillingAgreementDetails($requestParameters = array()) { $parameters = array(); @@ -970,18 +1026,18 @@ public function getBillingAgreementDetails($requestParameters = array()) $requestParameters = array_change_key_case($requestParameters, CASE_LOWER); $fieldMappings = array( - 'merchant_id' => 'SellerId', - 'amazon_billing_agreement_id' => 'AmazonBillingAgreementId', - 'address_consent_token' => 'AddressConsentToken', - 'access_token' => 'AccessToken', - 'mws_auth_token' => 'MWSAuthToken' + 'merchant_id' => 'SellerId', + 'amazon_billing_agreement_id' => 'AmazonBillingAgreementId', + 'address_consent_token' => 'AddressConsentToken', + 'access_token' => 'AccessToken', + 'mws_auth_token' => 'MWSAuthToken' ); $responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters); - - return ($responseObject); + return ($responseObject); } + /* SetBillingAgreementDetails API call - Sets Billing Agreement details such as a description of the agreement and other information about the seller. * @see https://pay.amazon.com/developer/documentation/apireference/201751700 * @@ -996,7 +1052,6 @@ public function getBillingAgreementDetails($requestParameters = array()) * @optional requestParameters['custom_information'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function setBillingAgreementDetails($requestParameters = array()) { $parameters = array(); @@ -1004,21 +1059,21 @@ public function setBillingAgreementDetails($requestParameters = array()) $requestParameters = array_change_key_case($requestParameters, CASE_LOWER); $fieldMappings = array( - 'merchant_id' => 'SellerId', + 'merchant_id' => 'SellerId', 'amazon_billing_agreement_id' => 'AmazonBillingAgreementId', - 'platform_id' => 'BillingAgreementAttributes.PlatformId', - 'seller_note' => 'BillingAgreementAttributes.SellerNote', + 'platform_id' => 'BillingAgreementAttributes.PlatformId', + 'seller_note' => 'BillingAgreementAttributes.SellerNote', 'seller_billing_agreement_id' => 'BillingAgreementAttributes.SellerBillingAgreementAttributes.SellerBillingAgreementId', - 'custom_information' => 'BillingAgreementAttributes.SellerBillingAgreementAttributes.CustomInformation', - 'store_name' => 'BillingAgreementAttributes.SellerBillingAgreementAttributes.StoreName', - 'mws_auth_token' => 'MWSAuthToken' + 'custom_information' => 'BillingAgreementAttributes.SellerBillingAgreementAttributes.CustomInformation', + 'store_name' => 'BillingAgreementAttributes.SellerBillingAgreementAttributes.StoreName', + 'mws_auth_token' => 'MWSAuthToken' ); $responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters); - return ($responseObject); } + /* ConfirmBillingAgreement API Call - Confirms that the Billing Agreement is free of constraints and all required information has been set on the Billing Agreement. * @see https://pay.amazon.com/developer/documentation/apireference/201751710 * @@ -1026,7 +1081,6 @@ public function setBillingAgreementDetails($requestParameters = array()) * @param requestParameters['amazon_billing_agreement_id'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function confirmBillingAgreement($requestParameters = array()) { $parameters = array(); @@ -1034,16 +1088,16 @@ public function confirmBillingAgreement($requestParameters = array()) $requestParameters = array_change_key_case($requestParameters, CASE_LOWER); $fieldMappings = array( - 'merchant_id' => 'SellerId', + 'merchant_id' => 'SellerId', 'amazon_billing_agreement_id' => 'AmazonBillingAgreementId', - 'mws_auth_token' => 'MWSAuthToken' + 'mws_auth_token' => 'MWSAuthToken' ); $responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters); - return ($responseObject); } + /* ValidateBillignAgreement API Call - Validates the status of the Billing Agreement object and the payment method associated with it. * @see https://pay.amazon.com/developer/documentation/apireference/201751720 * @@ -1051,7 +1105,6 @@ public function confirmBillingAgreement($requestParameters = array()) * @param requestParameters['amazon_billing_agreement_id'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function validateBillingAgreement($requestParameters = array()) { $parameters = array(); @@ -1059,16 +1112,16 @@ public function validateBillingAgreement($requestParameters = array()) $requestParameters = array_change_key_case($requestParameters, CASE_LOWER); $fieldMappings = array( - 'merchant_id' => 'SellerId', + 'merchant_id' => 'SellerId', 'amazon_billing_agreement_id' => 'AmazonBillingAgreementId', - 'mws_auth_token' => 'MWSAuthToken' + 'mws_auth_token' => 'MWSAuthToken' ); $responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters); - return ($responseObject); } + /* AuthorizeOnBillingAgreement API call - Reserves a specified amount against the payment method(s) stored in the Billing Agreement. * @see https://pay.amazon.com/developer/documentation/apireference/201751940 * @@ -1079,7 +1132,7 @@ public function validateBillingAgreement($requestParameters = array()) * @param requestParameters['currency_code'] - [String] * @optional requestParameters['seller_authorization_note'] [String] * @optional requestParameters['transaction_timeout'] - Defaults to 1440 minutes - * @optional requestParameters['capture_now'] [String] + * @optional requestParameters['capture_now'] [Boolean] * @optional requestParameters['soft_descriptor'] - - [String] * @optional requestParameters['seller_note'] - [String] * @optional requestParameters['platform_id'] - [String] @@ -1089,7 +1142,6 @@ public function validateBillingAgreement($requestParameters = array()) * @optional requestParameters['inherit_shipping_address'] [Boolean] - Defaults to true * @optional requestParameters['mws_auth_token'] - [String] */ - public function authorizeOnBillingAgreement($requestParameters = array()) { $parameters = array(); @@ -1116,10 +1168,10 @@ public function authorizeOnBillingAgreement($requestParameters = array()) ); $responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters); - - return ($responseObject); + return ($responseObject); } + /* CloseBillingAgreement API Call - Returns details about the Billing Agreement object and its current state. * @see https://pay.amazon.com/developer/documentation/apireference/201751950 * @@ -1128,7 +1180,6 @@ public function authorizeOnBillingAgreement($requestParameters = array()) * @optional requestParameters['closure_reason'] [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function closeBillingAgreement($requestParameters = array()) { $parameters = array(); @@ -1136,17 +1187,17 @@ public function closeBillingAgreement($requestParameters = array()) $requestParameters = array_change_key_case($requestParameters, CASE_LOWER); $fieldMappings = array( - 'merchant_id' => 'SellerId', + 'merchant_id' => 'SellerId', 'amazon_billing_agreement_id' => 'AmazonBillingAgreementId', - 'closure_reason' => 'ClosureReason', - 'mws_auth_token' => 'MWSAuthToken' + 'closure_reason' => 'ClosureReason', + 'mws_auth_token' => 'MWSAuthToken' ); $responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters); - return ($responseObject); } + /* charge convenience method * Performs the API calls * 1. SetOrderReferenceDetails / SetBillingAgreementDetails @@ -1169,7 +1220,6 @@ public function closeBillingAgreement($requestParameters = array()) * @optional requestParameters['charge_order_id'] - [String] : Custom Order ID provided * @optional requestParameters['mws_auth_token'] - [String] */ - public function charge($requestParameters = array()) { $requestParameters = array_change_key_case($requestParameters, CASE_LOWER); @@ -1225,126 +1275,128 @@ public function charge($requestParameters = array()) { return $response; } - /* makeChargeCalls - makes API calls based off the charge type (OrderReference or BillingAgreement) */ + /* makeChargeCalls - makes API calls based off the charge type (OrderReference or BillingAgreement) */ private function makeChargeCalls($chargeType, $setParameters, $confirmParameters, $authorizeParameters) { - switch ($chargeType) { + switch ($chargeType) { - case 'OrderReference': + case 'OrderReference': - // Get the Order Reference details and feed the response object to the ResponseParser - $responseObj = $this->getOrderReferenceDetails($setParameters); + // Get the Order Reference details and feed the response object to the ResponseParser + $responseObj = $this->getOrderReferenceDetails($setParameters); - // Call the function getOrderReferenceDetailsStatus in ResponseParser.php providing it the XML response - // $oroStatus is an array containing the State of the Order Reference ID - $oroStatus = $responseObj->getOrderReferenceDetailsStatus($responseObj->toXml()); + // Call the function getOrderReferenceDetailsStatus in ResponseParser.php providing it the XML response + // $oroStatus is an array containing the State of the Order Reference ID + $oroStatus = $responseObj->getOrderReferenceDetailsStatus($responseObj->toXml()); - if ($oroStatus['State'] === 'Draft') { - $response = $this->setOrderReferenceDetails($setParameters); - if ($this->success) { - $this->confirmOrderReference($confirmParameters); + if ($oroStatus['State'] === 'Draft') { + $response = $this->setOrderReferenceDetails($setParameters); + if ($this->success) { + $this->confirmOrderReference($confirmParameters); + } } - } - $responseObj = $this->getOrderReferenceDetails($setParameters); + $responseObj = $this->getOrderReferenceDetails($setParameters); - // Check the Order Reference Status again before making the Authorization. - $oroStatus = $responseObj->getOrderReferenceDetailsStatus($responseObj->toXml()); + // Check the Order Reference Status again before making the Authorization. + $oroStatus = $responseObj->getOrderReferenceDetailsStatus($responseObj->toXml()); - if ($oroStatus['State'] === 'Open') { - if ($this->success) { - $response = $this->authorize($authorizeParameters); + if ($oroStatus['State'] === 'Open') { + if ($this->success) { + $response = $this->authorize($authorizeParameters); + } } - } - if ($oroStatus['State'] != 'Open' && $oroStatus['State'] != 'Draft') { - throw new \Exception('The Order Reference is in the ' . $oroStatus['State'] . " State. It should be in the Draft or Open State"); - } + if ($oroStatus['State'] != 'Open' && $oroStatus['State'] != 'Draft') { + throw new \Exception('The Order Reference is in the ' . $oroStatus['State'] . " State. It should be in the Draft or Open State"); + } - return $response; + return $response; - case 'BillingAgreement': + case 'BillingAgreement': - // Get the Billing Agreement details and feed the response object to the ResponseParser + // Get the Billing Agreement details and feed the response object to the ResponseParser - $responseObj = $this->getBillingAgreementDetails($setParameters); + $responseObj = $this->getBillingAgreementDetails($setParameters); - // Call the function getBillingAgreementDetailsStatus in ResponseParser.php providing it the XML response - // $baStatus is an array containing the State of the Billing Agreement - $baStatus = $responseObj->getBillingAgreementDetailsStatus($responseObj->toXml()); + // Call the function getBillingAgreementDetailsStatus in ResponseParser.php providing it the XML response + // $baStatus is an array containing the State of the Billing Agreement + $baStatus = $responseObj->getBillingAgreementDetailsStatus($responseObj->toXml()); - if ($baStatus['State'] === 'Draft') { + if ($baStatus['State'] === 'Draft') { $response = $this->setBillingAgreementDetails($setParameters); if ($this->success) { $response = $this->confirmBillingAgreement($confirmParameters); } - } + } - // Check the Billing Agreement status again before making the Authorization. - $responseObj = $this->getBillingAgreementDetails($setParameters); - $baStatus = $responseObj->getBillingAgreementDetailsStatus($responseObj->toXml()); + // Check the Billing Agreement status again before making the Authorization. + $responseObj = $this->getBillingAgreementDetails($setParameters); + $baStatus = $responseObj->getBillingAgreementDetailsStatus($responseObj->toXml()); - if ($this->success && $baStatus['State'] === 'Open') { - $response = $this->authorizeOnBillingAgreement($authorizeParameters); - } + if ($this->success && $baStatus['State'] === 'Open') { + $response = $this->authorizeOnBillingAgreement($authorizeParameters); + } - if ($baStatus['State'] != 'Open' && $baStatus['State'] != 'Draft') { - throw new \Exception('The Billing Agreement is in the ' . $baStatus['State'] . " State. It should be in the Draft or Open State"); - } + if ($baStatus['State'] != 'Open' && $baStatus['State'] != 'Draft') { + throw new \Exception('The Billing Agreement is in the ' . $baStatus['State'] . " State. It should be in the Draft or Open State"); + } - return $response; + return $response; + + default: + throw new \Exception('Invalid Charge Type'); } } + /* GetProviderCreditDetails API Call - Get the details of the Provider Credit. * * @param requestParameters['merchant_id'] - [String] * @param requestParameters['amazon_provider_credit_id'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function getProviderCreditDetails($requestParameters = array()) { - $parameters = array(); + $parameters = array(); $parameters['Action'] = 'GetProviderCreditDetails'; $requestParameters = array_change_key_case($requestParameters, CASE_LOWER); $fieldMappings = array( - 'merchant_id' => 'SellerId', + 'merchant_id' => 'SellerId', 'amazon_provider_credit_id' => 'AmazonProviderCreditId', - 'mws_auth_token' => 'MWSAuthToken' + 'mws_auth_token' => 'MWSAuthToken' ); $responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters); - return ($responseObject); } + /* GetProviderCreditReversalDetails API Call - Get details of the Provider Credit Reversal. * * @param requestParameters['merchant_id'] - [String] * @param requestParameters['amazon_provider_credit_reversal_id'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function getProviderCreditReversalDetails($requestParameters = array()) { - $parameters = array(); + $parameters = array(); $parameters['Action'] = 'GetProviderCreditReversalDetails'; $requestParameters = array_change_key_case($requestParameters, CASE_LOWER); $fieldMappings = array( - 'merchant_id' => 'SellerId', + 'merchant_id' => 'SellerId', 'amazon_provider_credit_reversal_id' => 'AmazonProviderCreditReversalId', - 'mws_auth_token' => 'MWSAuthToken' + 'mws_auth_token' => 'MWSAuthToken' ); $responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters); - return ($responseObject); } + /* ReverseProviderCredit API Call - Reverse the Provider Credit. * * @param requestParameters['merchant_id'] - [String] @@ -1355,28 +1407,27 @@ public function getProviderCreditReversalDetails($requestParameters = array()) * @optional requestParameters['credit_reversal_note'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function reverseProviderCredit($requestParameters = array()) { - $parameters = array(); + $parameters = array(); $parameters['Action'] = 'ReverseProviderCredit'; $requestParameters = array_change_key_case($requestParameters, CASE_LOWER); $fieldMappings = array( - 'merchant_id' => 'SellerId', + 'merchant_id' => 'SellerId', 'amazon_provider_credit_id' => 'AmazonProviderCreditId', - 'credit_reversal_reference_id' => 'CreditReversalReferenceId', - 'credit_reversal_amount' => 'CreditReversalAmount.Amount', - 'currency_code' => 'CreditReversalAmount.CurrencyCode', - 'credit_reversal_note' => 'CreditReversalNote', - 'mws_auth_token' => 'MWSAuthToken' + 'credit_reversal_reference_id' => 'CreditReversalReferenceId', + 'credit_reversal_amount' => 'CreditReversalAmount.Amount', + 'currency_code' => 'CreditReversalAmount.CurrencyCode', + 'credit_reversal_note' => 'CreditReversalNote', + 'mws_auth_token' => 'MWSAuthToken' ); $responseObject = $this->setParametersAndPost($parameters, $fieldMappings, $requestParameters); - return ($responseObject); } + /* Create an Array of required parameters, sort them * Calculate signature and invoke the POST to the MWS Service URL * @@ -1386,12 +1437,11 @@ public function reverseProviderCredit($requestParameters = array()) * @param Timestamp [String] * @param Signature [String] */ - private function calculateSignatureAndParametersToString($parameters = array()) { foreach ($parameters as $key => $value) { // Ensure that no unexpected type coercions have happened - if ($key === 'CaptureNow' || $key === 'ConfirmNow' || $key === 'InheritShippingAddress') { + if ($key === 'CaptureNow' || $key === 'ConfirmNow' || $key === 'InheritShippingAddress' || $key === 'RequestPaymentAuthorization') { if (!is_bool($value)) { throw new \Exception($key . ' value ' . $value . ' is of type ' . gettype($value) . ' and should be a boolean value'); } @@ -1421,6 +1471,7 @@ private function calculateSignatureAndParametersToString($parameters = array()) return $parameters; } + /* Computes RFC 2104-compliant HMAC signature for request parameters * Implements AWS Signature, as per following spec: * @@ -1450,7 +1501,6 @@ private function calculateSignatureAndParametersToString($parameters = array()) * Pairs of parameter and values are separated by the '&' character (ASCII code 38). * */ - private function signParameters(array $parameters) { $signatureVersion = $parameters['SignatureVersion']; @@ -1467,11 +1517,11 @@ private function signParameters(array $parameters) return $this->sign($stringToSign, $algorithm); } + /* Calculate String to Sign for SignatureVersion 2 * @param array $parameters request parameters * @return String to Sign */ - private function calculateStringToSignV2(array $parameters) { $data = 'POST'; @@ -1487,8 +1537,8 @@ private function calculateStringToSignV2(array $parameters) return $data; } - /* Convert paremeters to Url encoded query string */ + /* Convert paremeters to Url encoded query string */ private function getParametersAsString(array $parameters) { $queryParameters = array(); @@ -1500,13 +1550,14 @@ private function getParametersAsString(array $parameters) } + private function urlEncode($value) { return str_replace('%7E', '~', rawurlencode($value)); } - /* Computes RFC 2104-compliant HMAC signature */ + /* Computes RFC 2104-compliant HMAC signature */ private function sign($data, $algorithm) { if ($algorithm === 'HmacSHA1') { @@ -1520,26 +1571,26 @@ private function sign($data, $algorithm) return base64_encode(hash_hmac($hash, $data, $this->config['secret_key'], true)); } - /* Formats date as ISO 8601 timestamp */ + /* Formats date as ISO 8601 timestamp */ private function getFormattedTimestamp() { return gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time()); } + /* invokePost takes the parameters and invokes the httpPost function to POST the parameters * Exponential retries on error 500 and 503 * The response from the POST is an XML which is converted to Array */ - private function invokePost($parameters) { $response = array(); $statusCode = 200; $this->success = false; - // Submit the request and read response body - try { + // Submit the request and read response body + try { $shouldRetry = true; $retries = 0; do { @@ -1580,13 +1631,13 @@ private function invokePost($parameters) return $response; } + /* Exponential sleep on failed request * Up to three retries will occur if first reqest fails * after 1.0 second, 2.2 seconds, and finally 7.0 seconds * @param retries current retry * @throws Exception if maximum number of retries has been reached */ - private function pauseOnRetry($retries, $status) { if ($retries <= self::MAX_ERROR_RETRY) { @@ -1601,8 +1652,8 @@ private function pauseOnRetry($retries, $status) } } - /* Create MWS service URL and the Endpoint path */ + /* Create MWS service URL and the Endpoint path */ private function createServiceUrl() { $this->modePath = strtolower($this->config['sandbox']) ? 'OffAmazonPayments_Sandbox' : 'OffAmazonPayments'; @@ -1610,7 +1661,13 @@ private function createServiceUrl() if (!empty($this->config['region'])) { $region = strtolower($this->config['region']); if (array_key_exists($region, $this->regionMappings)) { - $this->mwsEndpointUrl = $this->mwsServiceUrls[$this->regionMappings[$region]]; + + if (!is_null($this->config['override_service_url'])) { + $this->mwsEndpointUrl = preg_replace("(https?://)", "", $this->config['override_service_url']); + } else { + $this->mwsEndpointUrl = $this->mwsServiceUrls[$this->regionMappings[$region]]; + } + $this->mwsServiceUrl = 'https://' . $this->mwsEndpointUrl . '/' . $this->modePath . '/' . self::MWS_VERSION; $this->mwsEndpointPath = '/' . $this->modePath . '/' . self::MWS_VERSION; } else { @@ -1621,28 +1678,29 @@ private function createServiceUrl() } } - /* Based on the config['region'] and config['sandbox'] values get the user profile URL */ + /* Based on the config['region'] and config['sandbox'] values get the user profile URL */ private function profileEndpointUrl() { - $profileEnvt = strtolower($this->config['sandbox']) ? "api.sandbox" : "api"; + $profileEnvt = strtolower($this->config['sandbox']) ? "api.sandbox" : "api"; if (!empty($this->config['region'])) { $region = strtolower($this->config['region']); - if (array_key_exists($region, $this->regionMappings) ) { + if (array_key_exists($region, $this->regionMappings) ) { $this->profileEndpoint = 'https://' . $profileEnvt . '.' . $this->profileEndpointUrls[$region]; - }else{ - throw new \Exception($region . ' is not a valid region'); - } - } else { + } else { + throw new \Exception($region . ' is not a valid region'); + } + } else { throw new \Exception("config['region'] is a required parameter and is not set"); } } - /* Create the User Agent Header sent with the POST request */ - private function constructUserAgentHeader() + /* Create the User Agent Header sent with the POST request */ + /* Protected because of PSP module usaged */ + protected function constructUserAgentHeader() { $this->userAgent = 'amazon-pay-sdk-php/' . self::SDK_VERSION . ' ('; @@ -1665,12 +1723,12 @@ private function constructUserAgentHeader() $this->userAgent .= ')'; } + /* Collapse multiple whitespace characters into a single ' ' and backslash escape '\', * and '/' characters from a string. * @param $s * @return string */ - private function quoteApplicationName($s) { $quotedString = preg_replace('/ {2,}|\s/', ' ', $s); @@ -1679,13 +1737,13 @@ private function quoteApplicationName($s) return $quotedString; } + /* Collapse multiple whitespace characters into a single ' ' and backslash escape '\', * and '(' characters from a string. * * @param $s * @return string */ - private function quoteApplicationVersion($s) { $quotedString = preg_replace('/ {2,}|\s/', ' ', $s); @@ -1744,4 +1802,5 @@ public static function getSignature($stringToSign, $secretKey) { return base64_encode(hash_hmac('sha256', $stringToSign, $secretKey, true)); } + } diff --git a/AmazonPay/ClientInterface.php b/AmazonPay/ClientInterface.php index 75c39fd..6c04fc2 100644 --- a/AmazonPay/ClientInterface.php +++ b/AmazonPay/ClientInterface.php @@ -8,21 +8,21 @@ interface ClientInterface /* Setter for sandbox * Sets the boolean value for config['sandbox'] variable */ - public function setSandbox($value); + /* Setter for config['client_id'] * Sets the value for config['client_id'] variable */ - public function setClientId($value); + /* Setter for config['app_id'] * Sets the value for config['app_id'] variable */ - public function setAppId($value); + /* Setter for Proxy * input $proxy [array] * @param $proxy['proxy_user_host'] - hostname for the proxy @@ -30,34 +30,34 @@ public function setAppId($value); * @param $proxy['proxy_user_name'] - if your proxy required a username * @param $proxy['proxy_user_password'] - if your proxy required a passowrd */ - public function setProxy($proxy); + /* Setter for $_mwsServiceUrl * Set the URL to which the post request has to be made for unit testing */ - public function setMwsServiceUrl($url); + /* Getter * Gets the value for the key if the key exists in config */ - public function __get($name); + /* Getter for parameters string * Gets the value for the parameters string for unit testing */ - public function getParameters(); + /* GetUserInfo convenience funtion - Returns user's profile information from Amazon using the access token returned by the Button widget. * * @param $access_token [String] */ - public function getUserInfo($access_token); + /* GetOrderReferenceDetails API call - Returns details about the Order Reference object and its current state. * @see https://pay.amazon.com/developer/documentation/apireference/201751970 * @@ -66,9 +66,9 @@ public function getUserInfo($access_token); * @optional requestParameters['address_consent_token'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function getOrderReferenceDetails($requestParameters = array()); + /* SetOrderReferenceDetails API call - Sets order reference details such as the order total and a description for the order. * @see https://pay.amazon.com/developer/documentation/apireference/201751960 * @@ -81,11 +81,34 @@ public function getOrderReferenceDetails($requestParameters = array()); * @optional requestParameters['seller_order_id'] - [String] * @optional requestParameters['store_name'] - [String] * @optional requestParameters['custom_information'] - [String] + * @optional requestParameters['request_payment_authorization'] - [Boolean] * @optional requestParameters['mws_auth_token'] - [String] */ - public function setOrderReferenceDetails($requestParameters = array()); + + /* SetOrderAttributes API call - Sets order reference attributes such as the order total and a description for the order. + * Works same as SetOrderReferenceDetails, but includes additional PSP-related attributes and can also be called after + * the ORO has been confirmed. Only some values can be changed the ORO has been confirmed. See API documentation. + * + * @param requestParameters['merchant_id'] - [String] + * @param requestParameters['amazon_order_reference_id'] - [String] + * @optional requestParameters['amount'] - [String] + * @optional requestParameters['currency_code'] - [String] + * @optional requestParameters['platform_id'] - [String] + * @optional requestParameters['seller_note'] - [String] + * @optional requestParameters['seller_order_id'] - [String] + * @optional requestParameters['store_name'] - [String] + * @optional requestParameters['custom_information'] - [String] + * @optional requestParameters['request_payment_authorization'] - [Boolean] + * @optional requestParameters['payment_service_provider_id'] - [String] + * @optional requestParameters['payment_service_provider_order_id'] - [String] + * @optional requestParameters['order_item_categories'] - [array()] + * @optional requestParameters['mws_auth_token'] - [String] + */ + public function setOrderAttributes($requestParameters = array()); + + /* ConfirmOrderReferenceDetails API call - Confirms that the order reference is free of constraints and all required information has been set on the order reference. * @see https://pay.amazon.com/developer/documentation/apireference/201751980 * @@ -93,9 +116,9 @@ public function setOrderReferenceDetails($requestParameters = array()); * @param requestParameters['amazon_order_reference_id'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function confirmOrderReference($requestParameters = array()); + /* CancelOrderReferenceDetails API call - Cancels a previously confirmed order reference. * @see https://pay.amazon.com/developer/documentation/apireference/201751990 * @@ -104,9 +127,9 @@ public function confirmOrderReference($requestParameters = array()); * @optional requestParameters['cancelation_reason'] [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function cancelOrderReference($requestParameters = array()); + /* CloseOrderReference API call - Confirms that an order reference has been fulfilled (fully or partially) * and that you do not expect to create any new authorizations on this order reference. * @see https://pay.amazon.com/developer/documentation/apireference/201752000 @@ -116,9 +139,9 @@ public function cancelOrderReference($requestParameters = array()); * @optional requestParameters['closure_reason'] [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function closeOrderReference($requestParameters = array()); + /* CloseAuthorization API call - Closes an authorization. * @see https://pay.amazon.com/developer/documentation/apireference/201752070 * @@ -127,9 +150,9 @@ public function closeOrderReference($requestParameters = array()); * @optional requestParameters['closure_reason'] [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function closeAuthorization($requestParameters = array()); + /* Authorize API call - Reserves a specified amount against the payment method(s) stored in the order reference. * @see https://pay.amazon.com/developer/documentation/apireference/201752010 * @@ -145,9 +168,9 @@ public function closeAuthorization($requestParameters = array()); * @optional requestParameters['soft_descriptor'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function authorize($requestParameters = array()); + /* GetAuthorizationDetails API call - Returns the status of a particular authorization and the total amount captured on the authorization. * @see https://pay.amazon.com/developer/documentation/apireference/201752030 * @@ -155,9 +178,9 @@ public function authorize($requestParameters = array()); * @param requestParameters['amazon_authorization_id'] [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function getAuthorizationDetails($requestParameters = array()); + /* Capture API call - Captures funds from an authorized payment instrument. * @see https://pay.amazon.com/developer/documentation/apireference/201752040 * @@ -171,9 +194,9 @@ public function getAuthorizationDetails($requestParameters = array()); * @optional requestParameters['soft_descriptor'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function capture($requestParameters = array()); + /* GetCaptureDetails API call - Returns the status of a particular capture and the total amount refunded on the capture. * @see https://pay.amazon.com/developer/documentation/apireference/201752060 * @@ -181,9 +204,9 @@ public function capture($requestParameters = array()); * @param requestParameters['amazon_capture_id'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function getCaptureDetails($requestParameters = array()); + /* Refund API call - Refunds a previously captured amount. * @see https://pay.amazon.com/developer/documentation/apireference/201752080 * @@ -197,9 +220,9 @@ public function getCaptureDetails($requestParameters = array()); * @optional requestParameters['soft_descriptor'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function refund($requestParameters = array()); + /* GetRefundDetails API call - Returns the status of a particular refund. * @see https://pay.amazon.com/developer/documentation/apireference/201752100 * @@ -207,9 +230,9 @@ public function refund($requestParameters = array()); * @param requestParameters['amazon_refund_id'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function getRefundDetails($requestParameters = array()); + /* GetServiceStatus API Call - Returns the operational status of the OffAmazonPayments API section * @see https://pay.amazon.com/developer/documentation/apireference/201752110 * @@ -220,9 +243,9 @@ public function getRefundDetails($requestParameters = array()); * @param requestParameters['merchant_id'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function getServiceStatus($requestParameters = array()); + /* CreateOrderReferenceForId API Call - Creates an order reference for the given object * @see https://pay.amazon.com/developer/documentation/apireference/201751670 * @@ -238,9 +261,9 @@ public function getServiceStatus($requestParameters = array()); * @optional requestParameters['custom_information'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function createOrderReferenceForId($requestParameters = array()); + /* GetBillingAgreementDetails API Call - Returns details about the Billing Agreement object and its current state. * @see https://pay.amazon.com/developer/documentation/apireference/201751690 * @@ -248,9 +271,9 @@ public function createOrderReferenceForId($requestParameters = array()); * @param requestParameters['amazon_billing_agreement_id'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function getBillingAgreementDetails($requestParameters = array()); + /* SetBillingAgreementDetails API call - Sets Billing Agreement details such as a description of the agreement and other information about the seller. * @see https://pay.amazon.com/developer/documentation/apireference/201751700 * @@ -265,9 +288,9 @@ public function getBillingAgreementDetails($requestParameters = array()); * @optional requestParameters['custom_information'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function setBillingAgreementDetails($requestParameters = array()); + /* ConfirmBillingAgreement API Call - Confirms that the Billing Agreement is free of constraints and all required information has been set on the Billing Agreement. * @see https://pay.amazon.com/developer/documentation/apireference/201751710 * @@ -275,9 +298,9 @@ public function setBillingAgreementDetails($requestParameters = array()); * @param requestParameters['amazon_billing_agreement_id'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function confirmBillingAgreement($requestParameters = array()); + /* ValidateBillingAgreement API Call - Validates the status of the Billing Agreement object and the payment method associated with it. * @see https://pay.amazon.com/developer/documentation/apireference/201751720 * @@ -285,9 +308,9 @@ public function confirmBillingAgreement($requestParameters = array()); * @param requestParameters['amazon_billing_agreement_id'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function validateBillingAgreement($requestParameters = array()); + /* AuthorizeOnBillingAgreement API call - Reserves a specified amount against the payment method(s) stored in the Billing Agreement. * @see https://pay.amazon.com/developer/documentation/apireference/201751940 * @@ -308,9 +331,9 @@ public function validateBillingAgreement($requestParameters = array()); * @optional requestParameters['inherit_shipping_address'] [Boolean] - Defaults to true * @optional requestParameters['mws_auth_token'] - [String] */ - public function authorizeOnBillingAgreement($requestParameters = array()); + /* CloseBillingAgreement API Call - Returns details about the Billing Agreement object and its current state. * @see https://pay.amazon.com/developer/documentation/apireference/201751950 * @@ -319,9 +342,9 @@ public function authorizeOnBillingAgreement($requestParameters = array()); * @optional requestParameters['closure_reason'] [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function closeBillingAgreement($requestParameters = array()); + /* charge convenience method * Performs the API calls * 1. SetOrderReferenceDetails / SetBillingAgreementDetails @@ -344,27 +367,27 @@ public function closeBillingAgreement($requestParameters = array()); * @optional requestParameters['charge_order_id'] - [String] : Custom Order ID provided * @optional requestParameters['mws_auth_token'] - [String] */ - public function charge($requestParameters = array()); + /* GetProviderCreditDetails API Call - Get the details of the Provider Credit. * * @param requestParameters['merchant_id'] - [String] * @param requestParameters['amazon_provider_credit_id'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function getProviderCreditDetails($requestParameters = array()); + /* GetProviderCreditReversalDetails API Call - Get details of the Provider Credit Reversal. * * @param requestParameters['merchant_id'] - [String] * @param requestParameters['amazon_provider_credit_reversal_id'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function getProviderCreditReversalDetails($requestParameters = array()); + /* ReverseProviderCredit API Call - Reverse the Provider Credit. * * @param requestParameters['merchant_id'] - [String] @@ -375,6 +398,5 @@ public function getProviderCreditReversalDetails($requestParameters = array()); * @optional requestParameters['credit_reversal_note'] - [String] * @optional requestParameters['mws_auth_token'] - [String] */ - public function reverseProviderCredit($requestParameters = array()); } diff --git a/AmazonPay/HttpCurl.php b/AmazonPay/HttpCurl.php index 400d160..df281b2 100644 --- a/AmazonPay/HttpCurl.php +++ b/AmazonPay/HttpCurl.php @@ -13,7 +13,7 @@ class HttpCurl implements HttpCurlInterface private $header = false; private $accessToken = null; private $curlResponseInfo = null; - private $headerArray = []; + private $headerArray = array(); /* Takes user configuration array as input * Takes configuration for API call or IPN config @@ -47,7 +47,7 @@ public function setAccessToken($accesstoken) * config['proxy_password'] */ - private function commonCurlParams($url,$userAgent) + protected function commonCurlParams($url,$userAgent) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); @@ -111,8 +111,8 @@ public function httpGet($url, $userAgent = null) } /* Execute Curl request */ - - private function execute($ch) + /* Protected because will be used by PSP module */ + protected function execute($ch) { $response = ''; diff --git a/CHANGES.txt b/CHANGES.txt index e00ffb9..2c5591e 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,3 +1,6 @@ +3.2.0 - November 2017 +- Add support for new SetOrderAttributes API call (available for use in 2018) + 3.1.0 - May 2017 - Fix getUserInfo call (bearer token) issue impacted by 3.0.0's Curl fix - app_id can be passed in to Client constructor as optional configuration parameter diff --git a/README.md b/README.md index f530155..c3a2c13 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Support for PHP 5.3 and 5.4 is being deprecated. The SDK will work in these olde ## Documentation -* Integration steps can be found below: +Integration steps can be found below: * [US](https://pay.amazon.com/us/developer/documentation) * [UK](https://pay.amazon.com/uk/developer/documentation) * [DE](https://pay.amazon.com/de/developer/documentation) @@ -24,12 +24,11 @@ Support for PHP 5.3 and 5.4 is being deprecated. The SDK will work in these olde ## Sample -* View the sample integration demo [here](https://amzn.github.io/amazon-pay-sdk-samples/) +* View the [Amazon Pay SDK samples](https://amzn.github.io/amazon-pay-sdk-samples/) ## Quick Start -Instantiating the client: -Client Takes in parameters in the following format +The client takes in parameters in the following format: 1. Associative array 2. Path to the JSON file containing configuration information. diff --git a/composer.json b/composer.json index 2e078a6..2c4e487 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "amzn/amazon-pay-sdk-php", "type": "library", "description": "Amazon Pay SDK (PHP)", - "version": "3.1.0", + "version": "3.2.0", "keywords": [ "amazon", "pay", diff --git a/dist/amazon-pay.phar b/dist/amazon-pay.phar index 29ea602..e102782 100644 Binary files a/dist/amazon-pay.phar and b/dist/amazon-pay.phar differ diff --git a/tst/unit/ClientTest.php b/tst/unit/ClientTest.php index e080262..49e8141 100644 --- a/tst/unit/ClientTest.php +++ b/tst/unit/ClientTest.php @@ -79,6 +79,10 @@ public function testConfigArray() $this->assertRegExp('/is either not part of the configuration or has incorrect Key name./i', strval($expected)); } + // Test passing in override service URL for MWS API endpoint + $client = new Client(array('override_service_url' => 'https://over.ride')); + $this->assertEquals('https://over.ride', $client->__get('override_service_url')); + // Test passing in an empty array to construtor try { $configParams = array(); @@ -179,16 +183,17 @@ public function testSetOrderReferenceDetails() { $client = new Client($this->configParams); $fieldMappings = array( - 'Merchant_Id' => 'SellerId', - 'amazon_order_reference_id' => 'AmazonOrderReferenceId', - 'amount' => 'OrderReferenceAttributes.OrderTotal.Amount', - 'currency_code' => 'OrderReferenceAttributes.OrderTotal.CurrencyCode', - 'platform_id' => 'OrderReferenceAttributes.PlatformId', - 'seller_note' => 'OrderReferenceAttributes.SellerNote', - 'seller_order_id' => 'OrderReferenceAttributes.SellerOrderAttributes.SellerOrderId', - 'store_name' => 'OrderReferenceAttributes.SellerOrderAttributes.StoreName', - 'custom_information' => 'OrderReferenceAttributes.SellerOrderAttributes.CustomInformation', - 'mws_auth_token' => 'MWSAuthToken' + 'Merchant_Id' => 'SellerId', + 'amazon_order_reference_id' => 'AmazonOrderReferenceId', + 'amount' => 'OrderReferenceAttributes.OrderTotal.Amount', + 'currency_code' => 'OrderReferenceAttributes.OrderTotal.CurrencyCode', + 'platform_id' => 'OrderReferenceAttributes.PlatformId', + 'seller_note' => 'OrderReferenceAttributes.SellerNote', + 'seller_order_id' => 'OrderReferenceAttributes.SellerOrderAttributes.SellerOrderId', + 'store_name' => 'OrderReferenceAttributes.SellerOrderAttributes.StoreName', + 'custom_information' => 'OrderReferenceAttributes.SellerOrderAttributes.CustomInformation', + 'request_payment_authorization' => 'OrderReferenceAttributes.RequestPaymentAuthorization', + 'mws_auth_token' => 'MWSAuthToken' ); $action = 'SetOrderReferenceDetails'; @@ -206,6 +211,80 @@ public function testSetOrderReferenceDetails() $this->assertEquals($apiParametersString, $expectedStringParams); } + public function testSetOrderAttributesBeforeConfirm() + { + $client = new Client($this->configParams); + $fieldMappings = array( + 'merchant_id' => 'SellerId', + 'amazon_order_reference_id' => 'AmazonOrderReferenceId', + 'amount' => 'OrderAttributes.OrderTotal.Amount', + 'currency_code' => 'OrderAttributes.OrderTotal.CurrencyCode', + 'platform_id' => 'OrderAttributes.PlatformId', + 'seller_note' => 'OrderAttributes.SellerNote', + 'seller_order_id' => 'OrderAttributes.SellerOrderAttributes.SellerOrderId', + 'store_name' => 'OrderAttributes.SellerOrderAttributes.StoreName', + 'custom_information' => 'OrderAttributes.SellerOrderAttributes.CustomInformation', + 'request_payment_authorization' => 'OrderAttributes.RequestPaymentAuthorization', + 'payment_service_provider_id' => 'OrderAttributes.PaymentServiceProviderAttributes.PaymentServiceProviderId', + 'payment_service_provider_order_id' => 'OrderAttributes.PaymentServiceProviderAttributes.PaymentServiceProviderOrderId', + 'order_item_categories' => array(), + 'mws_auth_token' => 'MWSAuthToken' + ); + + $action = 'SetOrderAttributes'; + + $parameters = $this->setParametersAndPost($fieldMappings, $action); + $expectedParameters = $parameters['expectedParameters']; + $expectedParameters['OrderAttributes.SellerOrderAttributes.OrderItemCategories.OrderItemCategory.1'] = 'Antiques'; + $expectedParameters['OrderAttributes.SellerOrderAttributes.OrderItemCategories.OrderItemCategory.2'] = 'Electronics'; + $apiCallParams = $parameters['apiCallParams']; + + $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters); + + $response = $client->setOrderAttributes($apiCallParams); + + $apiParametersString = $client->getParameters(); + + $this->assertEquals($apiParametersString, $expectedStringParams); + } + + + /* Call is same as BeforeConfirm call except the amount and currency_code fields are omitted */ + public function testSetOrderAttributesAfterConfirm() + { + $client = new Client($this->configParams); + $fieldMappings = array( + 'merchant_id' => 'SellerId', + 'amazon_order_reference_id' => 'AmazonOrderReferenceId', + 'platform_id' => 'OrderAttributes.PlatformId', + 'seller_note' => 'OrderAttributes.SellerNote', + 'seller_order_id' => 'OrderAttributes.SellerOrderAttributes.SellerOrderId', + 'store_name' => 'OrderAttributes.SellerOrderAttributes.StoreName', + 'custom_information' => 'OrderAttributes.SellerOrderAttributes.CustomInformation', + 'request_payment_authorization' => 'OrderAttributes.RequestPaymentAuthorization', + 'payment_service_provider_id' => 'OrderAttributes.PaymentServiceProviderAttributes.PaymentServiceProviderId', + 'payment_service_provider_order_id' => 'OrderAttributes.PaymentServiceProviderAttributes.PaymentServiceProviderOrderId', + 'order_item_categories' => array(), + 'mws_auth_token' => 'MWSAuthToken' + ); + + $action = 'SetOrderAttributes'; + + $parameters = $this->setParametersAndPost($fieldMappings, $action); + $expectedParameters = $parameters['expectedParameters']; + $expectedParameters['OrderAttributes.SellerOrderAttributes.OrderItemCategories.OrderItemCategory.1'] = 'Antiques'; + $expectedParameters['OrderAttributes.SellerOrderAttributes.OrderItemCategories.OrderItemCategory.2'] = 'Electronics'; + $apiCallParams = $parameters['apiCallParams']; + + $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters); + + $response = $client->setOrderAttributes($apiCallParams); + + $apiParametersString = $client->getParameters(); + + $this->assertEquals($apiParametersString, $expectedStringParams); + } + public function testConfirmOrderReference() { $client = new Client($this->configParams); @@ -835,12 +914,15 @@ private function setParametersAndPost($fieldMappings, $action) $expectedParameters['Action'] = $action; foreach ($fieldMappings as $parm => $value) { - if ($parm === 'capture_now' || $parm === 'confirm_now' || $parm === 'inherit_shipping_address') { - $expectedParameters[$value] = false; - $apiCallParams[$parm] = false; + if ($parm === 'capture_now' || $parm === 'confirm_now' || $parm === 'inherit_shipping_address' || $parm === 'request_payment_authorization') { + $expectedParameters[$value] = true; + $apiCallParams[$parm] = true; + } elseif ($parm === 'order_item_categories') { + $apiCallParams[$parm] = array('Antiques', 'Electronics'); } elseif (!isset($expectedParameters[$value])) { - $expectedParameters[$value] = 'test'; - $apiCallParams[$parm] = 'test'; + $unique_id = uniqid(); + $expectedParameters[$value] = $unique_id; + $apiCallParams[$parm] = $unique_id; } }