クラス
Complete
ソース ソース
ファイル: src/API/CheckoutSession/Complete.php
class Complete extends ErrorStore
{
const CHECKOUT_SESSION_CANCELED = 'CheckoutSessionCanceled';
const CURRENCY_MISMATCH = 'CurrencyMismatch';
const TRANSACTION_AMOUNT_EXCEEDED = 'TransactionAmountExceeded';
const RESOURCE_NOT_FOUND = 'ResourceNotFound';
const AMOUNT_MISMATCH = 'AmountMismatch';
const INVALID_CHECKOUT_SESSION_STATUS = 'InvalidCheckoutSessionStatus';
const INVALID_CHARGE_STATUS = 'InvalidChargeStatus';
const SOFT_DECLINED = 'SoftDeclined';
const HARD_DECLINED = 'HardDeclined';
const PAYMENT_METHOD_NOT_ALLOWED = 'PaymentMethodNotAllowed';
const AMAZON_REJECTED = 'AmazonRejected';
const MFA_NOT_COMPLETED = 'MFANotCompleted';
const TRANSACTION_TIMED_OUT = 'TransactionTimedOut';
const PROCESSING_FAILURE = 'ProcessingFailure';
/**
* AmazonPay module object
*
* @var AmazonPay
*/
private $module;
/**
* 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();
$this->populate();
}
/**
* Completes a checkout session
*
* @see https://amazonpaycheckoutintegrationguide.s3.amazonaws.com/amazon-pay-api-v2/checkout-session.html#complete-checkout-session
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param string $checkoutSessionId
* @return array
* @throws InvalidArgumentException Thrown by `getAmazonClient`.
*/
public function post($checkoutSessionId) {
$order = (new API\WelcartEntries())->getOrder();
$payload = [
'chargeAmount' => [
'amount' => $order['total_full_price'],
'currencyCode' => usces_crcode('return'),
],
];
$headers = ['x-amz-pay-Idempotency-Key' => uniqid()];
try {
$client = $this->module->getAmazonClient();
$result = $client->completeCheckoutSession($checkoutSessionId, $payload, $headers);
if ($this->module->errors->hasError($result)) {
return $this->module->errors->getAmzErrorResponse($result, $this);
}
} catch (\Exception $e) {
return $this->module->errors->getErrorResponse(
GenericErrorStore::AMAZON_PAY_SDK_CLIENT_EXCEPTION,
[$e->getMessage()]
);
}
return $result;
}
/**
* Populates error store
*
* @see https://amazonpaycheckoutintegrationguide.s3.amazonaws.com/amazon-pay-api-v2/checkout-session.html#error-codes-3
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return void
* @throws InvalidArgumentException Thrown if duplicate errorcodes exist.
*/
public function populate() {
$actingLogger = new ActingLogger($this->module);
$defaultMessage = __('A processing error occured. Please try again.', 'wcexaap');
$this->addError((new GenericError(
self::CHECKOUT_SESSION_CANCELED,
$this->getConstantNameByValue(self::CHECKOUT_SESSION_CANCELED),
400,
function ($message) {
return $message;
},
__('The payment method may have been declined. Please choose a different payment method and try again.', 'wcexaap'),
__('Checkout was unsuccessful because the buyer cancelled the transaction or payment was declined', 'wcexaap')
))->setLogger($actingLogger));
$this->addError((new GenericError(
self::CURRENCY_MISMATCH,
$this->getConstantNameByValue(self::CURRENCY_MISMATCH),
400,
function ($message) {
return $message;
},
$defaultMessage,
__('The currency code provided in the request does not match the currency set during checkout', 'wcexaap')
))->setLogger($actingLogger));
$this->addError((new GenericError(
self::TRANSACTION_AMOUNT_EXCEEDED,
$this->getConstantNameByValue(self::TRANSACTION_AMOUNT_EXCEEDED),
400,
function ($message) {
return $message;
},
__('Transaction amount exceeded. Please close all other tabs for this website and try again.', 'wcexaap'),
__('Youve exceeded the maximum charge amount allowed for the Checkout Session', 'wcexaap')
))->setLogger($actingLogger));
$this->addError((new GenericError(
self::RESOURCE_NOT_FOUND,
$this->getConstantNameByValue(self::RESOURCE_NOT_FOUND),
404,
function ($message) {
return $message;
},
__('The checkout session has expired. Please try again.'),
__('Checkout Session details are permanently deleted after 30 days. Any request on the Checkout Session will return this error code', 'wcexaap')
))->setLogger($actingLogger));
$this->addError((new GenericError(
self::AMOUNT_MISMATCH,
$this->getConstantNameByValue(self::AMOUNT_MISMATCH),
409,
function ($message) {
return $message;
},
__('The total price of your purchase has changed unexpectedly. Please close all other tabs for this website and try again.', 'wcexaap'),
__('Mismatch between Checkout Session object chargeAmount and the request chargeAmount', 'wcexaap')
))->setLogger($actingLogger));
$this->addError((new GenericError(
self::INVALID_CHECKOUT_SESSION_STATUS,
$this->getConstantNameByValue(self::INVALID_CHECKOUT_SESSION_STATUS),
422,
function ($message) {
return $message;
},
$defaultMessage,
__('You tried to call an operation on a Checkout Session that is in a state where that operation is not allowed', 'wcexaap')
))->setLogger($actingLogger));
$this->addError((new GenericError(
self::INVALID_CHARGE_STATUS,
$this->getConstantNameByValue(self::INVALID_CHARGE_STATUS),
422,
function ($message) {
return $message;
},
$defaultMessage,
__('You tried to call an operation on a Charge that is in a state where that operation is not allowed or you tried to Complete Checkout Session on a Checkout Session with paymentIntent set to AuthorizeWithCapture and canHandlePendingAuthorization set to true', 'wcexaap')
))->setLogger($actingLogger));
$this->addError((new GenericError(
self::SOFT_DECLINED,
$this->getConstantNameByValue(self::SOFT_DECLINED),
422,
function ($message) {
return $message;
},
__('Your payment method was declined. Please choose a different payment method and try again.', 'wcexaap'),
__('Charge was soft declined. Retry attempts may or may not be successful. If repeated retry attempts are unsuccessful, contact the buyer and have them choose a different payment instrument', 'wcexaap')
))->setLogger($actingLogger));
$this->addError((new GenericError(
self::HARD_DECLINED,
$this->getConstantNameByValue(self::HARD_DECLINED),
422,
function ($message) {
return $message;
},
__('Your payment method was declined. Please choose a different payment method and try again.', 'wcexaap'),
__('Charge was hard declined. Retry attemps will not succeed. Please contact the buyer and have them choose a different payment instrument', 'wcexaap')
))->setLogger($actingLogger));
$this->addError((new GenericError(
self::PAYMENT_METHOD_NOT_ALLOWED,
$this->getConstantNameByValue(self::PAYMENT_METHOD_NOT_ALLOWED),
422,
function ($message) {
return $message;
},
__('Your payment method was declined. Please choose a different payment method and try again.', 'wcexaap'),
__('The payment method selected by the buyer is not allowed for this charge', 'wcexaap')
))->setLogger($actingLogger));
$this->addError((new GenericError(
self::AMAZON_REJECTED,
$this->getConstantNameByValue(self::AMAZON_REJECTED),
422,
function ($message) {
return $message;
},
__('Your payment method was declined. Please choose a different payment method and try again.', 'wcexaap'),
__('Charge was declined by Amazon. The associated Charge Permission will also be canceled', 'wcexaap')
))->setLogger($actingLogger));
$this->addError((new GenericError(
self::MFA_NOT_COMPLETED,
$this->getConstantNameByValue(self::MFA_NOT_COMPLETED),
422,
function ($message) {
return $message;
},
$defaultMessage,
__('Multi-factor authentication (MFA) is required to be complete by the buyer for this transaction to process', 'wcexaap')
))->setLogger($actingLogger));
$this->addError((new GenericError(
self::TRANSACTION_TIMED_OUT,
$this->getConstantNameByValue(self::TRANSACTION_TIMED_OUT),
422,
function ($message) {
return $message;
},
__('The transaction timed-out. Please try again.', 'wcexaap'),
__('The Charge was declined because Amazon did not have enough time to process the authorization.', 'wcexaap')
))->setLogger($actingLogger));
$this->addError((new GenericError(
self::PROCESSING_FAILURE,
$this->getConstantNameByValue(self::PROCESSING_FAILURE),
500,
function ($message) {
return $message;
},
__('Amazon could not process the transaction.', 'wcexaap'),
__('Amazon could not process the Charge because of an internal processing error. You should retry the charge only if the Charge Permission is in the Chargeable state', 'wcexaap')
))->setLogger($actingLogger));
}
}
- __construct — Sets `$module` member var with dependency injection.
- populate — Populates error store
- post — Completes a checkout session