クラス
Checkout
ソース ソース
ファイル: src/Checkout.php
class Checkout extends CheckoutHooks
{
/**
* `GenericError` instance if checkout failed, `null` otherwise
*
* @var GenericError|null
*/
private $error = null;
/**
* `Get Checkout Session` JSON decoded response object
*
* @var array|null
*/
private $checkoutResponse = null;
/**
* `Get Charge` JSON decoded response object
*
* @var array|null
*/
private $chargeResponse = null;
/**
* Handles a redirect from the `amazonPayRedirectUrl` returned by Amazon
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \usc_e_shop $usces
* @return void
*/
public function handleCheckoutResult() {
global $usces;
$sessionId = $_REQUEST['amazonCheckoutSessionId'];
$result = (new API\CheckoutSession\Complete($this->module))->post($sessionId);
if ($result instanceof GenericError) {
$this->error = $result;
$this->setToErrorPage();
return;
}
$this->checkoutResponse = json_decode($result['response'], true);
$details = $this->checkoutResponse['statusDetails'];
$checkoutSessionState = new Models\CheckoutSession\State(
$details['state'],
$details['reasonCode'],
$details['reasonDescription']
);
if ($checkoutSessionState->isCanceled()) {
$this->error = $checkoutSessionState->getErrorResponseFromCanceledState();
$this->setToErrorPage();
return;
}
// deferred authorization
if ($checkoutSessionState->isOpen()) {
$res = $usces->order_processing();
if (strtolower($res) === 'ordercompletion') {
$this->setToCompletionPage();
} else {
$this->setToErrorPage();
}
return;
}
if ($checkoutSessionState->isCompleted()) {
$getChargeRes = (new API\Charge\Get($this->module))->get($this->checkoutResponse['chargeId']);
if ($getChargeRes instanceof GenericError) {
$this->error = $getChargeRes;
$this->setToErrorPage();
return;
}
$this->chargeResponse = json_decode($getChargeRes['response'], true);
$res = $usces->order_processing();
if (strtolower($res) === 'ordercompletion') {
$this->setToCompletionPage();
} else {
$this->setToErrorPage();
}
return;
}
$this->error = $this->module->errors->getErrorResponse(Errors\GenericErrorStore::UNKNOWN_ERROR);
$this->setToErrorPage();
}
/**
* Sets `$usces->page` to 'error' and adds redirect action
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \usc_e_shop $usces
* @return void
*/
public function setToErrorPage() {
global $usces;
$usces->page = 'error';
add_filter('yoast-ga-push-after-pageview', 'usces_trackPageview_error');
add_action('the_post', [$usces, 'action_cartFilter']);
add_action('template_redirect', [$usces, 'template_redirect']);
}
/**
* Sets `$usces->page` to 'ordercompletion' and adds redirect action
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \usc_e_shop $usces
* @return void
*/
public function setToCompletionPage() {
global $usces;
$usces->page = 'ordercompletion';
add_filter('yoast-ga-push-after-pageview', 'usces_trackPageview_ordercompletion');
add_action('the_post', [$usces, 'action_cartFilter']);
add_action('template_redirect', [$usces, 'template_redirect']);
}
/**
* Creates error HTML for error page
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param string $html
* @return string
*/
protected function errorPageMessage($html) {
if ($this->error === null) {
return $html;
}
return (new Views\CartErrorPage\CartErrorPage())->getErrorPageHtml($html, $this->error);
}
/**
* Saves AmazonPay order meta data
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param array $args {
* Welcart `$_SESSION` variables
*
* @type array $cart
* @type array $entry
* @type int $order_id
* @type int $member_id
* @type array $payments
* @type string $charging_type
* @type array $results
* }
* @return void
*/
protected function registerOrderData($args) {
if ($this->error === null) {
$order = (new API\WelcartEntries())->getOrder();
$chargeState = null;
if ($this->chargeResponse !== null) {
$chargeState = new Models\Charge\State(
$this->chargeResponse['statusDetails']['state'],
$this->chargeResponse['statusDetails']['reasonCode'],
$this->chargeResponse['statusDetails']['reasonDescription']
);
}
OrderMeta::saveOrderMetaDataFromCheckoutSession(
$this->checkoutResponse,
(int)$args['order_id'],
$order['total_full_price'],
usces_crcode('return'),
$chargeState
);
}
}
}
- errorPageMessage — Creates error HTML for error page
- handleCheckoutResult — Handles a redirect from the `amazonPayRedirectUrl` returned by Amazon
- registerOrderData — Saves AmazonPay order meta data
- setToCompletionPage — Sets `$usces->page` to 'ordercompletion' and adds redirect action
- setToErrorPage — Sets `$usces->page` to 'error' and adds redirect action