Skip to content

Commit

Permalink
Amazon Pay API SDK (PHP) 2.5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
akshitaWaldia committed Mar 6, 2023
1 parent 80d0c7a commit 5b38287
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 56 deletions.
37 changes: 16 additions & 21 deletions Amazon/Pay/API/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class Client implements ClientInterface
{
const SDK_VERSION = '2.5.1';
const SDK_VERSION = '2.5.2';
const HASH_ALGORITHM = 'sha256';
const API_VERSION = 'v2';

Expand Down Expand Up @@ -94,7 +94,7 @@ private function createServiceUrl()
} else {
$apiEndpointUrl = $this->apiServiceUrls[$this->regionMappings[$region]];
}
if($this->isEnvSpecificPublicKeyId($this->config['public_key_id'])){
if($this->isEnvSpecificPublicKeyId()){
return 'https://' . $apiEndpointUrl . '/';
}
return 'https://' . $apiEndpointUrl . '/' . $modePath . '/';
Expand All @@ -107,7 +107,7 @@ private function createServiceUrl()
}

// Method used to validate whether PublicKeyId starts with prefix LIVE or SANDBOX
private function isEnvSpecificPublicKeyId($publicKeyId) {
private function isEnvSpecificPublicKeyId() {
return $this->startsWith($this->config['public_key_id'], 'LIVE') || $this->startsWith($this->config['public_key_id'], 'SANDBOX');
}

Expand Down Expand Up @@ -149,26 +149,26 @@ private function sortCanonicalArray($canonicalArray)
$sortedCanonicalArray = array();
foreach ($canonicalArray as $key => $val) {
if (is_array($val)) {
foreach ($this->subArrays($val, "$key") as $newKey => $subVal) {
$sortedCanonicalArray["$newKey"] = $subVal;
foreach ($this->subArrays($val, $key) as $newKey => $subVal) {
$sortedCanonicalArray[$newKey] = $subVal;
}
}
else if ((is_null($val)) || ($val === '')) {}
else {
$sortedCanonicalArray["$key"] = $val;
$sortedCanonicalArray[$key] = $val;
}
}
ksort($sortedCanonicalArray);

return $sortedCanonicalArray;
}

/* subArrays - helper function used to break out arays in an array */
private function subArrays($parameters, $catagory)
/* subArrays - helper function used to break out arrays in an array */
private function subArrays($parameters, $category)
{
$categoryIndex = 0;
$newParameters = array();
$categoryString = "$catagory.";
$categoryString = "$category.";
foreach ($parameters as $value) {
$categoryIndex++;
$newParameters[$categoryString . $categoryIndex] = $value;
Expand All @@ -186,7 +186,7 @@ private function createCanonicalQuery($requestParameters)
return $this->getParametersAsString($sortedRequestParameters);
}

/* Convert paremeters to Url encoded query string */
/* Convert parameters to Url encoded query string */
private function getParametersAsString(array $parameters)
{
$queryParameters = array();
Expand Down Expand Up @@ -220,7 +220,7 @@ private function getCanonicalHeaders($headers)
foreach ($headers as $key => $val) {
if ((is_null($val)) || ($val === '')) {}
else {
$sortedCanonicalArray[strtolower("$key")] = $val;
$sortedCanonicalArray[strtolower($key)] = $val;
}
}
ksort($sortedCanonicalArray);
Expand All @@ -238,9 +238,7 @@ private function getCanonicalHeadersNames($headers)
$parameters[] = $key;
}
ksort($parameters);
$headerNames = implode(';', $parameters);

return $headerNames;
return implode(';', $parameters);
}

/* getHost
Expand Down Expand Up @@ -302,7 +300,7 @@ private function collectSubVals($parameters)
}

/* stringFromArray - helper function used to check if parameters is an array.
* If it is array it returns all the values as a string
* If it is an array it returns all the values as a string
* Otherwise it returns parameters
*/
private function stringFromArray($parameters)
Expand All @@ -316,7 +314,7 @@ private function stringFromArray($parameters)
}

/* Create the User Agent Header sent with the POST request */
/* Protected because of PSP module usaged */
/* Protected because of PSP module usage */
protected function constructUserAgentHeader()
{
return 'amazon-pay-api-sdk-php/' . self::SDK_VERSION . ' ('
Expand Down Expand Up @@ -472,7 +470,7 @@ public function apiCall($method, $urlFragment, $payload, $headers = null, $query
if (is_array($payload)) {

// json_encode will fail if non-UTF-8 encodings are present, need to convert them to UTF-8
array_walk_recursive($payload, function (&$item, $key) {
array_walk_recursive($payload, function (&$item) {
if (is_string($item) && mb_detect_encoding($item, 'UTF-8', true) === false) {
$item = utf8_encode($item);
}
Expand Down Expand Up @@ -503,8 +501,7 @@ public function apiCall($method, $urlFragment, $payload, $headers = null, $query
}

$httpCurlRequest = new HttpCurl(isset($this->config['proxy']) ? $this->config['proxy'] : []);
$response = $httpCurlRequest->invokeCurl($method, $url, $payload, $postSignedHeaders);
return $response;
return $httpCurlRequest->invokeCurl($method, $url, $payload, $postSignedHeaders);
}

/* Setter for sandbox
Expand Down Expand Up @@ -649,5 +646,3 @@ public function getRefund($refundId, $headers = null)
}

}
?>

19 changes: 9 additions & 10 deletions Amazon/Pay/API/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function getCheckoutSession($checkoutSessionId, $headers = null);
*
* Update the Checkout Session with transaction details. You can keep updating the Checkout Session,
* as long as it’s in an Open state. Once all mandatory parameters have been set, the Checkout Session object
* will respond with an unique amazonPayRedirectUrl that you will use to redirect the buyer to complete checkout.
* will respond with a unique amazonPayRedirectUrl that you will use to redirect the buyer to complete checkout.
*
* @param checkoutSessionId - [String] Checkout Session identifier
* @param payload - [String in JSON format] or [array]
Expand All @@ -84,7 +84,7 @@ public function completeCheckoutSession($checkoutSessionId, $payload, $headers =
* You can also use this operation to retrieve buyer details and their shipping address after a successful checkout.
* You can only retrieve details for 30 days from the time the Charge Permission was created.
*
* @param chargePermissionId - [String] - Charge Permission identifer
* @param chargePermissionId - [String] - Charge Permission identifier
* @optional headers - [array] - optional x-amz-pay-authtoken
*/
public function getChargePermission($chargePermissionId, $headers = null);
Expand All @@ -94,7 +94,7 @@ public function getChargePermission($chargePermissionId, $headers = null);
*
* Update external merchant metadata such as: buyer note, store name, and external reference ID.
*
* @param chargePermissionId - [String] - Charge Permission identifer
* @param chargePermissionId - [String] - Charge Permission identifier
* @param payload - [String in JSON format] or [array]
* @optional headers - [array] - optional x-amz-pay-authtoken
*/
Expand All @@ -106,7 +106,7 @@ public function updateChargePermission($chargePermissionId, $payload, $headers =
* Moves the Charge Permission to a Closed state. No future charges can be made and
* pending charges will be canceled if you set cancelPendingCharges to true.
*
* @param chargePermissionId - [String] - Charge Permission identifer
* @param chargePermissionId - [String] - Charge Permission identifier
* @param payload - [String in JSON format] or [array]
* @optional headers - [array] - optional x-amz-pay-authtoken
*/
Expand All @@ -131,7 +131,7 @@ public function createCharge($payload, $headers);
* Get Charge details such as charge amount and authorization state.
* Use this operation to determine if authorization or capture was successful.
*
* @param chargeId - [String] - Charge identifer
* @param chargeId - [String] - Charge identifier
* @optional headers - [array] - optional x-amz-pay-authtoken
*/
public function getCharge($chargeId, $headers = null);
Expand All @@ -145,7 +145,7 @@ public function getCharge($chargeId, $headers = null);
* more than 7 days after authorization. See asynchronous processing for more information.
* An unsuccessful Charge will move to a Declined state if payment was declined.
*
* @param chargeId - [String] - Charge identifer
* @param chargeId - [String] - Charge identifier
* @param payload - [String in JSON format] or [array]
* @param headers - [array] - requires x-amz-pay-Idempotency-Key header; optional x-amz-pay-authtoken
*/
Expand All @@ -157,7 +157,7 @@ public function captureCharge($chargeId, $payload, $headers);
* Moves Charge to Canceled state and releases any authorized payments.
* You can call this operation until Capture is initiated while Charge is in an AuthorizationInitiated or Authorized state.
*
* @param chargeId - [String] - Charge identifer
* @param chargeId - [String] - Charge identifier
* @param payload - [String in JSON format] or [array]
* @optional headers - [array] - optional x-amz-pay-authtoken
*/
Expand All @@ -182,7 +182,7 @@ public function createRefund($payload, $headers);
*
* Get refund details.
*
* @param refundId - [String] - Refund identifer
* @param refundId - [String] - Refund identifier
* @optional headers - [array] - optional x-amz-pay-authtoken
*/
public function getRefund($refundId, $headers = null);
Expand All @@ -207,7 +207,7 @@ public function instoreMerchantScan($payload, $headers = null);
public function instoreCharge($payload, $headers = null);


/* In-Store refund API call - Peforms Refund on a Charge ID
/* In-Store refund API call - Performs Refund on a Charge ID
*
* @param payload - [String in JSON format] or [multi-dimension array with key and values]
* @optional headers - [indexed array of string key-value pairs]
Expand Down Expand Up @@ -259,4 +259,3 @@ public function createSignature($http_request_method, $request_uri, $request_par
public function apiCall($method, $urlFragment, $payload, $headers = null);

}
?>
32 changes: 9 additions & 23 deletions Amazon/Pay/API/HttpCurl.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ private function commonCurlParams($url)
}


/* Send using curl
*/
/* Send using curl */
private function httpSend($method, $url, $payload, $postSignedHeaders)
{
// Ensure we never send the "Expect: 100-continue" header by adding
Expand All @@ -70,15 +69,12 @@ private function httpSend($method, $url, $payload, $postSignedHeaders)
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, $postSignedHeaders);

$response = $this->execute($ch);
return $response;
return $this->execute($ch);
}

/* Execute Curl request */
private function execute($ch)
{
$response = '';

$response = curl_exec($ch);
if ($response === false) {
$error_msg = "Unable to send request, underlying exception of " . curl_error($ch);
Expand Down Expand Up @@ -123,19 +119,11 @@ public function invokeCurl($method, $url, $payload, $postSignedHeaders)
);

$statusCode = $response['status'];
if ($statusCode === 200) {
$shouldRetry = false;
} elseif ($statusCode === 429 || $statusCode === 500 || $statusCode === 502 || $statusCode === 503 || $statusCode === 504) {

$shouldRetry = true;
if ($shouldRetry) {
$this->pauseOnRetry(++$retries, $response);
if ($retries > self::MAX_ERROR_RETRY) {
$shouldRetry = false;
}
}
} else {
$shouldRetry = false;
$shouldRetry = false;
$retryCodes = array(408, 429, 500, 502, 503, 504);
if (in_array($statusCode, $retryCodes)) {
$this->pauseOnRetry(++$retries);
$shouldRetry = $retries <= self::MAX_ERROR_RETRY;
}
} catch (\Exception $e) {
throw $e;
Expand All @@ -149,12 +137,12 @@ public function invokeCurl($method, $url, $payload, $postSignedHeaders)
}

/* Exponential sleep on failed request
* Up to three retries will occur if first reqest fails
* Up to three retries will occur if first request 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, $response)
private function pauseOnRetry($retries)
{
if ($retries <= self::MAX_ERROR_RETRY) {
// PHP delays are in microseconds (1 million microsecond = 1 sec)
Expand All @@ -171,5 +159,3 @@ private function useProxy() {
}

}

?>
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### Version 2.5.2 - March 2023
* Added Error Code 408 to API retry logic
* Corrected Typos & refactored codes

### Version 2.5.1 - January 2023
* Applied bug fix for 2.5.0 - please use 2.5.1 if facing issues with region in 2.5.0

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "amzn/amazon-pay-api-sdk-php",
"type": "library",
"description": "Amazon Pay API SDK (PHP)",
"version": "2.5.1",
"version": "2.5.2",
"keywords": [
"amazon",
"pay",
Expand Down
1 change: 0 additions & 1 deletion tests/unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,3 @@ private function verifyUnifiedEndpoint($region, $publicKeyId, $expectedURL) {
}

}
?>

0 comments on commit 5b38287

Please sign in to comment.