クラス
State
Charge object stateソース ソース
ファイル: src/Models/Charge/State.php
class State extends ErrorStore implements JsonSerializable, TransactionState
{
const AUTHORIZATION_INITIATED = 'AuthorizationInitiated';
const AUTHORIZED = 'Authorized';
const CAPTURE_INITIATED = 'CaptureInitiated';
const CAPTURED = 'Captured';
const CANCELED = 'Canceled';
const DECLINED = 'Declined';
const STATE_TO_LOG_ACTION = [
self::AUTHORIZATION_INITIATED => Log::ACTION_ASYNC_AUTHORIZE,
self::AUTHORIZED => Log::ACTION_AUTHORIZE,
self::CAPTURE_INITIATED => Log::ACTION_ASYNC_CAPTURE_PAYMENTS,
self::CAPTURED => Log::ACTION_CAPTURE_PAYMENTS,
];
/**
* Map of constant names and their corresponding values
*
* @var string[]
*/
public static $stateMap = [
'AUTHORIZATION_INITIATED' => self::AUTHORIZATION_INITIATED,
'AUTHORIZED' => self::AUTHORIZED,
'CAPTURE_INITIATED' => self::CAPTURE_INITIATED,
'CAPTURED' => self::CAPTURED,
'CANCELED' => self::CANCELED,
'DECLINED' => self::DECLINED,
];
/**
* State to CSS class map
*
* @var string[]
*/
private $cssClassMap = [
self::AUTHORIZATION_INITIATED => 'card-auth-init',
self::AUTHORIZED => 'card-auth',
self::CAPTURE_INITIATED => 'card-gathering-init',
self::CAPTURED => 'card-gathering',
self::CANCELED => 'card-delete',
self::DECLINED => 'card-declined',
];
/**
* `statusDetails`'s `state` key value
*
* @var string
*/
private $state;
/**
* `statusDetails`'s `reasonCode` key value
*
* @var string|null
*/
private $reasonCode;
/**
* `statusDetails`'s `reasonDescription` key value
*
* @var string|null
*/
private $reasonDescription;
/**
* Charge Permission ID
*
* @var string|null
*/
private $chargePermissionId;
/**
* Initializes a Charge `State` object
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param string $state
* @param string|null $reasonCode
* @param string|null $reasonDescription
* @return void
*/
public function __construct($state, $reasonCode = null, $reasonDescription = null) {
$this->state = $state;
$this->reasonCode = $reasonCode;
$this->reasonDescription = $reasonDescription;
parent::__construct();
$this->populate();
if ($this->reasonCode !== null) {
if (isset($this->getErrorCodeMap()[$reasonCode])) {
$error = $this->getErrorCodeMap()[$reasonCode];
$this->reasonDescription = is_callable($error->message) ? $this->reasonDescription : $error->message;
}
}
}
/**
* Setter for `chargePermissionId`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param string $chargePermissionId
* @return void
*/
public function setChargePermissionId($chargePermissionId) {
$this->chargePermissionId = $chargePermissionId;
}
/**
* Getter for `chargePermissionId`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string|null
*/
public function getTransactionId() {
return $this->chargePermissionId;
}
/**
* Returns human-readable text representation of current state
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string
*/
public function getDisplayText() {
$text = '';
switch ($this->state) {
case self::AUTHORIZATION_INITIATED:
$text = __('Authorization Initiated', 'wcexaap');
break;
case self::AUTHORIZED:
$text = __('Authorized', 'wcexaap');
break;
case self::CAPTURE_INITIATED:
$text = __('Capture Initiated', 'wcexaap');
break;
case self::CAPTURED:
$text = __('Captured', 'wcexaap');
break;
case self::CANCELED:
$text = __('Canceled', 'wcexaap');
break;
case self::DECLINED:
$text = __('Declined', 'wcexaap');
break;
}
return $text;
}
/**
* Returns CSS class for the current state
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string
*/
public function getCssClass() {
return isset($this->cssClassMap[$this->state]) ? $this->cssClassMap[$this->state] : '';
}
/**
* Returns `GenericError` instance created from a `Canceled` state.
*
* This method returns an `UNKNOWN_ERROR` `GenericError` instance if the state
* is not canceled.
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return GenericError
*/
public function getErrorResponseFromCanceledState() {
$error = $this->getErrorResponse(self::UNKNOWN_ERROR);
if ($this->state !== self::CANCELED) {
return $error;
}
return $this->getErrorResponse($this->reasonCode, [$this->reasonDescription]);
}
/**
* Returns `GenericError` instance created from a `Declined` state.
*
* This method returns an `UNKNOWN_ERROR` `GenericError` instance if the state
* is not declined.
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return GenericError
*/
public function getErrorResponseFromDeclinedState() {
$error = $this->getErrorResponse(self::UNKNOWN_ERROR);
if ($this->state !== self::DECLINED) {
return $error;
}
return $this->getErrorResponse($this->reasonCode, [$this->reasonDescription]);
}
/**
* Returns `true` if state is 'AuthorizationInitiated', `false` otherwise
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return bool
*/
public function isAuthorizationInitiated() {
return $this->state === self::AUTHORIZATION_INITIATED;
}
/**
* Returns `true` if state is 'Authorized', `false` otherwise
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return bool
*/
public function isAuthorized() {
return $this->state === self::AUTHORIZED;
}
/**
* Returns `true` if state is 'CaptureInitiated', `false` otherwise
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return bool
*/
public function isCaptureInitiated() {
return $this->state === self::CAPTURE_INITIATED;
}
/**
* Returns `true` if state is 'Captured', `false` otherwise
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return bool
*/
public function isCaptured() {
return $this->state === self::CAPTURED;
}
/**
* Returns `true` if state is 'Canceled', `false` otherwise
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return bool
*/
public function isCanceled() {
return $this->state === self::CANCELED;
}
/**
* Returns `true` if state is 'Declined', `false` otherwise
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return bool
*/
public function isDeclined() {
return $this->state === self::DECLINED;
}
/**
* Returns `true` if the state cannot be modified, `false` otherwise
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return bool
*/
public function isUnmodifiable() {
return $this->state === self::CANCELED || $this->state === self::AUTHORIZATION_INITIATED;
}
/**
* Getter for `state`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string
*/
public function getState() {
return $this->state;
}
/**
* Getter for `reasonCode`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string|null
*/
public function getReasonCode() {
return $this->reasonCode;
}
/**
* Getter for `reasonDescription`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string|null
*/
public function getReasonDescription() {
return $this->reasonDescription;
}
/**
* Getter for `cssClassMap`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string[]
*/
public function getCssClassMap() {
return $this->cssClassMap;
}
/**
* Returns JSON object when `json_encode` is invoked on an instance of this class
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return array
*/
public function jsonSerialize() {
return [
'state' => $this->state,
'reasonCode' => $this->reasonCode,
'reasonDescription' => $this->reasonDescription,
];
}
/**
* `Charge` specific errors for a `Canceled` or `Declined` state
*
* Note that there are certain `reasonCodes` that do not necessarily indicate that
* an error occured (ie. `MerchantCanceled`, `ChargePermissionCanceled`, etc.).
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return void
* @throws InvalidArgumentException Thrown if the `errorcode` is already added.
*/
public function populate() {
// Canceled
$this->addError(new GenericError(
CanceledReasonCodes::EXPIRED_UNUSED,
'EXPIRED_UNUSED',
400,
function ($message) {
return $message;
},
__('Funds have not been captured within 30 days. The charge has expired.', 'wcexaap')
));
$this->addError(new GenericError(
CanceledReasonCodes::AMAZON_CANCELED,
'AMAZON_CANCELED',
503,
function ($message) {
return $message;
},
__('Amazon canceled the charge.', 'wcexaap')
));
$this->addError(new GenericError(
CanceledReasonCodes::MERCHANT_CANCELED,
'MERCHANT_CANCELED',
400,
function ($message) {
return $message;
},
__('The charge has been canceled.', 'wcexaap')
));
$this->addError(new GenericError(
CanceledReasonCodes::CHARGE_PERMISSION_CANCELED,
'CHARGE_PERMISSION_CANCELED',
400,
function ($message) {
return $message;
},
__('The charge has been canceled.', 'wcexaap')
));
$this->addError(new GenericError(
CanceledReasonCodes::BUYER_CANCELED,
'BUYER_CANCELED',
400,
function ($message) {
return $message;
},
__('The charge was canceled', 'wcexaap')
));
// Declined
$this->addError(new GenericError(
DeclinedReasonCodes::SOFT_DECLINED,
'SOFT_DECLINED',
403,
function ($message) {
return $message;
},
__('The charge has been declined.', 'wcexaap')
));
$this->addError(new GenericError(
DeclinedReasonCodes::HARD_DECLINED,
'HARD_DECLINED',
403,
function ($message) {
return $message;
},
__('The charge has been declined.', 'wcexaap')
));
$this->addError(new GenericError(
DeclinedReasonCodes::AMAZON_REJECTED,
'AMAZON_REJECTED',
403,
function ($message) {
return $message;
},
__('The charge was declined by Amazon.', 'wcexaap')
));
$this->addError(new GenericError(
DeclinedReasonCodes::PROCESSING_FAILURE,
'PROCESSING_FAILURE',
500,
function ($message) {
return $message;
},
__('Amazon was unable to process the charge due to an internal error.', 'wcexaap')
));
$this->addError(new GenericError(
DeclinedReasonCodes::TRANSACTION_TIMED_OUT,
'TRANSACTION_TIMED_OUT',
503,
function ($message) {
return $message;
},
__('The transaction timed out.', 'wcexaap')
));
}
}
- __construct — Initializes a Charge `State` object
- getCssClass — Returns CSS class for the current state
- getCssClassMap — Getter for `cssClassMap`
- getDisplayText — Returns human-readable text representation of current state
- getErrorResponseFromCanceledState — Returns `GenericError` instance created from a `Canceled` state.
- getErrorResponseFromDeclinedState — Returns `GenericError` instance created from a `Declined` state.
- getReasonCode — Getter for `reasonCode`
- getReasonDescription — Getter for `reasonDescription`
- getState — Getter for `state`
- getTransactionId — Getter for `chargePermissionId`
- isAuthorizationInitiated — Returns `true` if state is 'AuthorizationInitiated', `false` otherwise
- isAuthorized — Returns `true` if state is 'Authorized', `false` otherwise
- isCanceled — Returns `true` if state is 'Canceled', `false` otherwise
- isCaptured — Returns `true` if state is 'Captured', `false` otherwise
- isCaptureInitiated — Returns `true` if state is 'CaptureInitiated', `false` otherwise
- isDeclined — Returns `true` if state is 'Declined', `false` otherwise
- isUnmodifiable — Returns `true` if the state cannot be modified, `false` otherwise
- jsonSerialize — Returns JSON object when `json_encode` is invoked on an instance of this class
- populate — `Charge` specific errors for a `Canceled` or `Declined` state
- setChargePermissionId — Setter for `chargePermissionId`