クラス
GenericErrorStore
ソース ソース
ファイル: src/Errors/GenericErrorStore.php
class GenericErrorStore extends ErrorStore
{
/**
* AmazonPay module object
*
* @var AmazonPay
*/
private $module;
// Welcart Errors
const USCES_DELIVERY_METHOD_INVALID = 'WelcartDeliveryMethodInvalid';
const USCES_CART_IS_EMPTY = 'WelcartCartIsEmpty';
const USCES_ITEM_OUT_OF_STOCK = 'WelcartItemOutOfStock';
const USCES_CUSTOM_FIELDS_ERR = 'WelcartCustomFieldsError';
const USCES_DELIVERY_PREF_INVALID = 'WelcartDeliveryPrefInvalid';
const USCES_COUNTRY_NOT_TARGET_MARGET = 'WelcartCountryNotTargetMarket';
const USCES_GIFT_SHIPPING_CUSTOMER_ADDRESS_INVALID = 'WelcartGiftShippingCustomerAddressInvalid';
// Custom errors
const AMAZON_PAY_ORDER_NOT_FOUND = 'WcexaapAmazonPayOrderNotFound';
// Amazon Generic Errors
const INVALID_HEADER_VALUE = 'InvalidHeaderValue';
const INVALID_PARAMETER_VALUE = 'InvalidParameterValue';
const INVALID_REQUEST_FORMAT = 'InvalidRequestFormat';
const INVALID_REQUEST = 'InvalidRequest';
const MISSING_HEADER_VALUE = 'MissingHeaderValue';
const MISSING_PARAMETER_VALUE = 'MissingParameterValue';
const UNRECOGNIZED_FIELD = 'UnrecognizedField';
const INVALID_SANDBOX_SIMULATION_SPECIFIED = 'InvalidSandboxSimulationSpecified';
const DUPLICATE_IDEMPOTENCY_KEY = 'DuplicateIdempotencyKey';
const CURRENCY_MISMATCH = 'CurrencyMismatch';
const UNAUTHORIZED_ACCESS = 'UnauthorizedAccess';
const INVALID_ACCOUNT_STATUS = 'InvalidAccountStatus';
const INVALID_REQUEST_SIGNATURE = 'InvalidRequestSignature';
const RESOURCE_NOT_FOUND = 'ResourceNotFound';
const UNSUPPORTED_OPERATION = 'UnsupportedOperation';
const REQUEST_NOT_SUPPORTED = 'RequestNotSupported';
const TOO_MANY_REQUESTS = 'TooManyRequests';
const AMAZON_INTERNAL_SERVER_ERROR = 'InternalServerError';
const SERVICE_UNAVAILABLE = 'ServiceUnavailable';
// Exceptions
const AMAZON_PAY_SDK_CLIENT_EXCEPTION = 'AmazonPaySDKClientException';
/**
* Sets `$module` member var with dependency injection.
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param AmazonPay $module
* @return void
*/
public function __construct(AmazonPay $module) {
$this->module = $module;
parent::__construct();
}
/**
* Declares errors
*
* @see https://amazonpaycheckoutintegrationguide.s3.amazonaws.com/amazon-pay-api-v2/error-handling.html
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return void
*/
public function populate() {
$genericLogger = new GenericLogger($this->module);
$defaultErrors = $this->getErrorCodeMap();
$defaultErrorMessage = $defaultErrors[ErrorStore::INTERNAL_SERVER_ERROR]->message;
// Welcart errors
$this->addError(new GenericError(
self::USCES_DELIVERY_METHOD_INVALID,
$this->getConstantNameByValue(self::USCES_DELIVERY_METHOD_INVALID),
400,
function ($message) {
return $message;
},
function ($message) {
return $message;
}
));
$this->addError(new GenericError(
self::USCES_CART_IS_EMPTY,
$this->getConstantNameByValue(self::USCES_CART_IS_EMPTY),
404,
__('Your session has expired. Please re-add the item(s) to your cart and try again.', 'wcexaap'),
__('Your session has expired. Please re-add the item(s) to your cart and try again.', 'wcexaap')
));
$this->addError(new GenericError(
self::USCES_ITEM_OUT_OF_STOCK,
$this->getConstantNameByValue(self::USCES_ITEM_OUT_OF_STOCK),
404,
function ($message) {
return $message;
},
function ($message) {
return $message;
}
));
$this->addError(new GenericError(
self::USCES_CUSTOM_FIELDS_ERR,
$this->getConstantNameByValue(self::USCES_CUSTOM_FIELDS_ERR),
400,
function ($message) {
return $message;
},
function ($message) {
return $message;
}
));
$this->addError(new GenericError(
self::USCES_GIFT_SHIPPING_CUSTOMER_ADDRESS_INVALID,
$this->getConstantNameByValue(self::USCES_GIFT_SHIPPING_CUSTOMER_ADDRESS_INVALID),
400,
function ($message) {
return $message;
},
function ($message) {
return $message;
}
));
$this->addError(new GenericError(
self::USCES_DELIVERY_PREF_INVALID,
$this->getConstantNameByValue(self::USCES_DELIVERY_PREF_INVALID),
400,
__('The prefecture is not valid.', 'wcexaap'),
__('The prefecture is not valid.', 'wcexaap')
));
$this->addError(new GenericError(
self::USCES_COUNTRY_NOT_TARGET_MARGET,
$this->getConstantNameByValue(self::USCES_COUNTRY_NOT_TARGET_MARGET),
400,
__('International shipping is unavailable for the country you selected.', 'wcexaap'),
__('International shipping is unavailable for the country you selected.', 'wcexaap')
));
// Custom errors
$this->addError(new GenericError(
self::AMAZON_PAY_ORDER_NOT_FOUND,
$this->getConstantNameByValue(self::AMAZON_PAY_ORDER_NOT_FOUND),
404,
function ($orderId) {
// translators: the order ID
return sprintf(__('Order ID %d is not an Amazon Pay order', 'wcexaap'), $orderId);
},
__('Amazon Pay order not found', 'wcexaap')
));
// Amazon Generic errors
$this->addError((new GenericError(
self::INVALID_PARAMETER_VALUE,
$this->getConstantNameByValue(self::INVALID_PARAMETER_VALUE),
400,
function ($message) {
return $message;
},
$defaultErrorMessage
))->setLogger($genericLogger));
$this->addError((new GenericError(
self::INVALID_HEADER_VALUE,
$this->getConstantNameByValue(self::INVALID_HEADER_VALUE),
400,
function ($message) {
return $message;
},
$defaultErrorMessage
))->setLogger($genericLogger));
$this->addError((new GenericError(
self::INVALID_REQUEST_FORMAT,
$this->getConstantNameByValue(self::INVALID_REQUEST_FORMAT),
400,
function ($message) {
return $message;
},
$defaultErrorMessage
))->setLogger($genericLogger));
$this->addError((new GenericError(
self::INVALID_REQUEST,
$this->getConstantNameByValue(self::INVALID_REQUEST),
400,
function ($message) {
return $message;
},
$defaultErrorMessage
))->setLogger($genericLogger));
$this->addError((new GenericError(
self::MISSING_HEADER_VALUE,
$this->getConstantNameByValue(self::MISSING_HEADER_VALUE),
400,
function ($message) {
return $message;
},
$defaultErrorMessage
))->setLogger($genericLogger));
$this->addError((new GenericError(
self::MISSING_PARAMETER_VALUE,
$this->getConstantNameByValue(self::MISSING_PARAMETER_VALUE),
400,
function ($message) {
return $message;
},
$defaultErrorMessage
))->setLogger($genericLogger));
$this->addError((new GenericError(
self::UNRECOGNIZED_FIELD,
$this->getConstantNameByValue(self::UNRECOGNIZED_FIELD),
400,
function ($message) {
return $message;
},
$defaultErrorMessage
))->setLogger($genericLogger));
$this->addError(new GenericError(
self::INVALID_SANDBOX_SIMULATION_SPECIFIED,
$this->getConstantNameByValue(self::INVALID_SANDBOX_SIMULATION_SPECIFIED),
400,
function ($message) {
return $message;
},
$defaultErrorMessage
));
$this->addError((new GenericError(
self::DUPLICATE_IDEMPOTENCY_KEY,
$this->getConstantNameByValue(self::DUPLICATE_IDEMPOTENCY_KEY),
400,
function ($message) {
return $message;
},
$defaultErrorMessage
))->setLogger($genericLogger));
$this->addError((new GenericError(
self::CURRENCY_MISMATCH,
$this->getConstantNameByValue(self::CURRENCY_MISMATCH),
400,
function ($message) {
return $message;
},
$defaultErrorMessage
))->setLogger($genericLogger));
$this->addError((new GenericError(
self::UNAUTHORIZED_ACCESS,
$this->getConstantNameByValue(self::UNAUTHORIZED_ACCESS),
401,
function ($message) {
return $message;
},
$defaultErrorMessage
))->setLogger($genericLogger));
$this->addError((new GenericError(
self::INVALID_ACCOUNT_STATUS,
$this->getConstantNameByValue(self::INVALID_ACCOUNT_STATUS),
403,
function ($message) {
return $message;
},
$defaultErrorMessage
))->setLogger($genericLogger));
$this->addError((new GenericError(
self::INVALID_REQUEST_SIGNATURE,
$this->getConstantNameByValue(self::INVALID_REQUEST_SIGNATURE),
403,
function ($message) {
return $message;
},
$defaultErrorMessage
))->setLogger($genericLogger));
$this->addError((new GenericError(
self::RESOURCE_NOT_FOUND,
$this->getConstantNameByValue(self::RESOURCE_NOT_FOUND),
404,
function ($message) {
return $message;
},
$defaultErrorMessage
))->setLogger($genericLogger));
$this->addError((new GenericError(
self::UNSUPPORTED_OPERATION,
$this->getConstantNameByValue(self::UNSUPPORTED_OPERATION),
405,
function ($message) {
return $message;
},
$defaultErrorMessage
))->setLogger($genericLogger));
$this->addError((new GenericError(
self::REQUEST_NOT_SUPPORTED,
$this->getConstantNameByValue(self::REQUEST_NOT_SUPPORTED),
405,
function ($message) {
return $message;
},
$defaultErrorMessage
))->setLogger($genericLogger));
$this->addError((new GenericError(
self::TOO_MANY_REQUESTS,
$this->getConstantNameByValue(self::TOO_MANY_REQUESTS),
429,
function ($message) {
return $message;
},
$defaultErrorMessage
))->setLogger($genericLogger));
$this->addError((new GenericError(
self::AMAZON_INTERNAL_SERVER_ERROR,
$this->getConstantNameByValue(self::AMAZON_INTERNAL_SERVER_ERROR),
429,
function ($message) {
return $message;
},
$defaultErrorMessage
))->setLogger($genericLogger));
$this->addError((new GenericError(
self::SERVICE_UNAVAILABLE,
$this->getConstantNameByValue(self::SERVICE_UNAVAILABLE),
503,
function ($message) {
return $message;
},
__('Amazon is experiencing technical difficulties. Please try again', 'wcexaap')
))->setLogger($genericLogger));
$this->addError((new GenericError(
self::AMAZON_PAY_SDK_CLIENT_EXCEPTION,
$this->getConstantNameByValue(self::AMAZON_PAY_SDK_CLIENT_EXCEPTION),
400,
function ($message) {
return $message;
},
__('There is a problem with your Amazon Pay merchant settings. Please check your settings and try again.', 'wcexaap')
))->setLogger($genericLogger));
}
/**
* Parses Amazon API result and returns true if an error exists
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param array $result
* @return bool
*/
public function hasError(array $result) {
$status = (int)$result['status'];
if ($status >= 400) {
return true;
}
return false;
}
/**
* Returns `AmazonGenericError` object created from an Amazon API error
*
* If `$contextapi` is provided and an error response is defined for `$contextapi`, it
* is returned. If `$contextapi` is not provided, a generic error lookup will take place.
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param array $result
* @param ErrorStore|null $contextapi object that extends `ErrorStore` and provides
* errors specific to the API
* @return AmazonGenericError
*/
public function getAmzErrorResponse(array $result, ErrorStore $contextapi = null) {
$status = (int)$result['status'];
$body = json_decode($result['response'], true);
$reasonCode = $body['reasonCode'];
$debugMessage = $body['message'];
$error = null;
if ($status >= 400) {
if ($contextapi !== null) {
$apierrmap = $contextapi->getErrorCodeMap();
if (isset($apierrmap[$reasonCode])) {
$error = $contextapi->getErrorResponse($reasonCode, [$debugMessage]);
return new AmazonGenericError(
$error->errorcode,
$error->errorname,
$error->httpcode,
$error->debugmsg,
$error->message,
$error->adminmsg,
$error->logger,
$result
);
}
}
$error = $this->getErrorResponse($reasonCode, [$debugMessage]);
}
if ($error !== null) {
return new AmazonGenericError(
$error->errorcode,
$error->errorname,
$error->httpcode,
$error->debugmsg,
$error->message,
$error->adminmsg,
$error->logger,
$result
);
}
$defaultErrors = $this->getErrorCodeMap();
$unknownError = $defaultErrors[ErrorStore::UNKNOWN_ERROR];
return new AmazonGenericError(
$unknownError->errorcode,
$unknownError->errorname,
$unknownError->httpcode,
$unknownError->debugmsg,
$unknownError->message,
$unknownError->adminmsg,
$unknownError->logger,
$result
);
}
}
- __construct — Sets `$module` member var with dependency injection.
- getAmzErrorResponse — Returns `AmazonGenericError` object created from an Amazon API error
- hasError — Parses Amazon API result and returns true if an error exists
- populate — Declares errors