Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exceptions #83

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 34 additions & 27 deletions AmazonPay/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@
require_once 'HttpCurl.php';
require_once 'ClientInterface.php';
require_once 'Regions.php';
require_once(__DIR__.'/Exception/ClientException.php');
require_once(__DIR__.'/Exception/ConfigException.php');
require_once(__DIR__.'/Exception/SignatureException.php');
if (!interface_exists('\Psr\Log\LoggerAwareInterface')) {
require_once(__DIR__.'/../Psr/Log/LoggerAwareInterface.php');
}

if (!interface_exists('\Psr\Log\LoggerInterface')) {
require_once(__DIR__.'/../Psr/Log/LoggerInterface.php');
}

use AmazonPay\Exception\ClientException;
use AmazonPay\Exception\ConfigException;
use AmazonPay\Exception\SignatureException;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -93,10 +100,10 @@ public function __construct($config = null)
if (is_array($configArray)) {
$this->checkConfigKeys($configArray);
} else {
throw new \Exception('$config is of the incorrect type ' . gettype($configArray) . ' and should be of the type array');
throw new ConfigException('$config is of the incorrect type ' . gettype($configArray) . ' and should be of the type array');
}
} else {
throw new \Exception('$config cannot be null.');
throw new ConfigException('$config cannot be null.');
}
}

Expand Down Expand Up @@ -135,11 +142,11 @@ private function checkIfFileExists($config)

if ($jsonError != 0) {
$errorMsg = "Error with message - content is not in json format" . $this->getErrorMessageForJsonError($jsonError) . " " . $configArray;
throw new \Exception($errorMsg);
throw new ConfigException($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);
throw new ConfigException($errorMsg);
}
return $configArray;
}
Expand All @@ -158,7 +165,7 @@ private function checkConfigKeys($config)
if (array_key_exists($key, $this->config)) {
$this->config[$key] = $value;
} else {
throw new \Exception('Key ' . $key . ' is either not part of the configuration or has incorrect Key name.
throw new ConfigException('Key ' . $key . ' is either not part of the configuration or has incorrect Key name.
check the config array key names to match your key names of your config array', 1);
}
}
Expand Down Expand Up @@ -201,7 +208,7 @@ public function setSandbox($value)
if (is_bool($value)) {
$this->config['sandbox'] = $value;
} else {
throw new \Exception('sandbox value ' . $value . ' is of type ' . gettype($value) . ' and should be a boolean value');
throw new ConfigException('sandbox value ' . $value . ' is of type ' . gettype($value) . ' and should be a boolean value');
}
}

Expand All @@ -214,7 +221,7 @@ public function setClientId($value)
if (!empty($value)) {
$this->config['client_id'] = $value;
} else {
throw new \Exception('setter value for client ID provided is empty');
throw new ConfigException('setter value for client ID provided is empty');
}
}

Expand All @@ -227,7 +234,7 @@ public function setAppId($value)
if (!empty($value)) {
$this->config['app_id'] = $value;
} else {
throw new \Exception('setter value for app ID provided is empty');
throw new ConfigException('setter value for app ID provided is empty');
}
}

Expand Down Expand Up @@ -272,7 +279,7 @@ public function __get($name)
if (array_key_exists(strtolower($name), $this->config)) {
return $this->config[strtolower($name)];
} else {
throw new \Exception('Key ' . $name . ' is either not a part of the configuration array config or the ' . $name . ' does not match the key name in the config array', 1);
throw new ConfigException('Key ' . $name . ' is either not a part of the configuration array config or the ' . $name . ' does not match the key name in the config array', 1);
}
}

Expand Down Expand Up @@ -328,7 +335,7 @@ public function getUserInfo($accessToken)
// As long as one of these matches, from a security perspective, we have done our due diligence
if (($data->aud != $this->config['client_id']) && ($data->app_id != $this->config['app_id'])) {
// The access token does not belong to us
throw new \Exception('The Access Token belongs to neither your Client ID nor App ID');
throw new ClientException('The Access Token belongs to neither your Client ID nor App ID');
}

// Exchange the access token for user profile
Expand Down Expand Up @@ -363,11 +370,11 @@ 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' || $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');
throw new ClientException($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' || $param === 'notification_configuration_list') {
if (!is_array($value)) {
throw new \Exception($param . ' value ' . $value . ' is of type ' . gettype($value) . ' and should be an array value');
throw new ClientException($param . ' value ' . $value . ' is of type ' . gettype($value) . ' and should be an array value');
}
}

Expand Down Expand Up @@ -468,14 +475,14 @@ private function setNotificationConfigurationList($parameters, $configuration)
{
$configurationIndex = 0;
if (!is_array($configuration)) {
throw new \Exception('Notification Configuration List value ' . $configuration . ' is of type ' . gettype($configuration) . ' and should be an array value');
throw new ClientException('Notification Configuration List value ' . $configuration . ' is of type ' . gettype($configuration) . ' and should be an array value');
}
foreach ($configuration as $url => $events) {
$configurationIndex = $configurationIndex + 1;
$parameters['NotificationConfigurationList.NotificationConfiguration.' . $configurationIndex . '.NotificationUrl'] = $url;
$eventIndex = 0;
if (!is_array($events)) {
throw new \Exception('Notification Configuration Events value ' . $events . ' is of type ' . gettype($events) . ' and should be an array value');
throw new ClientException('Notification Configuration Events value ' . $events . ' is of type ' . gettype($events) . ' and should be an array value');
}
foreach ($events as $event) {
$eventIndex = $eventIndex + 1;
Expand Down Expand Up @@ -1461,10 +1468,10 @@ public function charge($requestParameters = array()) {
$confirmParameters['amazon_billing_agreement_id'] = $requestParameters['amazon_reference_id'];
break;
default:
throw new \Exception('Invalid Amazon Reference ID');
throw new ClientException('Invalid Amazon Reference ID');
}
} else {
throw new \Exception('key amazon_order_reference_id or amazon_billing_agreement_id is null and is a required parameter');
throw new ClientException('key amazon_order_reference_id or amazon_billing_agreement_id is null and is a required parameter');
}

// Set the other parameters if the values are present
Expand Down Expand Up @@ -1519,7 +1526,7 @@ private function makeChargeCalls($chargeType, $setParameters, $confirmParameters
}

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");
throw new ClientException('The Order Reference is in the ' . $oroStatus['State'] . " State. It should be in the Draft or Open State");
}

return $response;
Expand Down Expand Up @@ -1550,13 +1557,13 @@ private function makeChargeCalls($chargeType, $setParameters, $confirmParameters
}

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");
throw new ClientException('The Billing Agreement is in the ' . $baStatus['State'] . " State. It should be in the Draft or Open State");
}

return $response;

default:
throw new \Exception('Invalid Charge Type');
throw new ClientException('Invalid Charge Type');
}
}

Expand Down Expand Up @@ -1653,7 +1660,7 @@ private function calculateSignatureAndParametersToString($parameters = array())
// Ensure that no unexpected type coercions have happened
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');
throw new SignatureException($key . ' value ' . $value . ' is of type ' . gettype($value) . ' and should be a boolean value');
}
}

Expand Down Expand Up @@ -1721,7 +1728,7 @@ private function signParameters(array $parameters)
$parameters['SignatureMethod'] = $algorithm;
$stringToSign = $this->calculateStringToSignV2($parameters);
} else {
throw new \Exception("Invalid Signature Version specified");
throw new SignatureException("Invalid Signature Version specified");
}

return $this->sign($stringToSign, $algorithm);
Expand Down Expand Up @@ -1775,7 +1782,7 @@ private function sign($data, $algorithm)
} else if ($algorithm === 'HmacSHA256') {
$hash = 'sha256';
} else {
throw new \Exception("Non-supported signing method specified");
throw new SignatureException("Non-supported signing method specified");
}

return base64_encode(hash_hmac($hash, $data, $this->config['secret_key'], true));
Expand Down Expand Up @@ -1858,7 +1865,7 @@ private function pauseOnRetry($retries, $status)
$delay = (int) (pow(4, $retries) * 100000) + 600000;
usleep($delay);
} else {
throw new \Exception('Error Code: '. $status.PHP_EOL.'Maximum number of retry attempts - '. $retries .' reached');
throw new ClientException('Error Code: '. $status.PHP_EOL.'Maximum number of retry attempts - '. $retries .' reached');
}
}

Expand All @@ -1881,10 +1888,10 @@ private function createServiceUrl()
$this->mwsServiceUrl = 'https://' . $this->mwsEndpointUrl . '/' . $this->modePath . '/' . self::MWS_VERSION;
$this->mwsEndpointPath = '/' . $this->modePath . '/' . self::MWS_VERSION;
} else {
throw new \Exception($region . ' is not a valid region');
throw new ClientException($region . ' is not a valid region');
}
} else {
throw new \Exception("config['region'] is a required parameter and is not set");
throw new ClientException("config['region'] is a required parameter and is not set");
}
}

Expand All @@ -1900,10 +1907,10 @@ private function profileEndpointUrl()
if (array_key_exists($region, $this->regionMappings) ) {
$this->profileEndpoint = 'https://' . $profileEnvt . '.' . $this->profileEndpointUrls[$region];
} else {
throw new \Exception($region . ' is not a valid region');
throw new ConfigException($region . ' is not a valid region');
}
} else {
throw new \Exception("config['region'] is a required parameter and is not set");
throw new ConfigException("config['region'] is a required parameter and is not set");
}
}

Expand Down
9 changes: 9 additions & 0 deletions AmazonPay/Exception/ClientException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace AmazonPay\Exception;

use Exception;

class ClientException extends Exception
{
}
9 changes: 9 additions & 0 deletions AmazonPay/Exception/ConfigException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace AmazonPay\Exception;

use Exception;

class ConfigException extends Exception
{
}
9 changes: 9 additions & 0 deletions AmazonPay/Exception/CurlException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace AmazonPay\Exception;

use Exception;

class CurlException extends Exception
{
}
9 changes: 9 additions & 0 deletions AmazonPay/Exception/HttpCurlException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace AmazonPay\Exception;

use Exception;

class HttpCurlException extends Exception
{
}
9 changes: 9 additions & 0 deletions AmazonPay/Exception/SignatureException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace AmazonPay\Exception;

use Exception;

class SignatureException extends Exception
{
}
9 changes: 9 additions & 0 deletions AmazonPay/Exception/SnsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace AmazonPay\Exception;

use Exception;

class SnsException extends Exception
{
}
7 changes: 7 additions & 0 deletions AmazonPay/Exception/SnsHeaderException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace AmazonPay\Exception;

class SnsHeaderException extends SnsException
{
}
7 changes: 7 additions & 0 deletions AmazonPay/Exception/SnsMessageException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace AmazonPay\Exception;

class SnsMessageException extends SnsException
{
}
7 changes: 7 additions & 0 deletions AmazonPay/Exception/SnsSignatureException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace AmazonPay\Exception;

class SnsSignatureException extends SnsException
{
}
5 changes: 4 additions & 1 deletion AmazonPay/HttpCurl.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* Handles Curl POST function for all requests
*/

use AmazonPay\Exception\CurlException;
use AmazonPay\Exception\HttpCurlException;

require_once 'HttpCurlInterface.php';

class HttpCurl implements HttpCurlInterface
Expand Down Expand Up @@ -123,7 +126,7 @@ protected function execute($ch)
if ($response === false) {
$error_msg = "Unable to post request, underlying exception of " . curl_error($ch);
curl_close($ch);
throw new \Exception($error_msg);
throw new HttpCurlException($error_msg, 0, new CurlException(curl_error($ch), curl_errno($ch)));
} else {
$this->curlResponseInfo = curl_getinfo($ch);
}
Expand Down
Loading