Skip to content
Merged
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
51 changes: 47 additions & 4 deletions controllers/front/AbstractOpcJsonFrontController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use PrestaShop\Module\PsOnePageCheckout\Analytics\Analytics;
use PrestaShop\Module\PsOnePageCheckout\Translation\ModuleTranslation;

abstract class Ps_OnepagecheckoutAbstractOpcJsonFrontController extends ModuleFrontController
{
/** @var bool */
Expand All @@ -15,12 +18,29 @@ public function initContent()
/**
* @return array<string,mixed>
*/
abstract protected function handleOpcRequest(): array;
protected function handleOpcRequest(): array
{
if (!$this->isOpcAvailable()) {
return $this->buildTechnicalErrorResponse();
}

try {
return $this->handleAvailableOpcRequest();
} catch (Throwable $exception) {
return $this->handleRuntimeException($exception);
}
}

/**
* @return array<string,mixed>
*/
abstract protected function handleAvailableOpcRequest(): array;

protected function isOpcAvailable(): bool
{
return $this->module instanceof Ps_Onepagecheckout
&& $this->module->isOnePageCheckoutEnabled();
assert($this->module instanceof Ps_Onepagecheckout);

return $this->module->isOnePageCheckoutEnabled();
}

/**
Expand All @@ -32,7 +52,7 @@ protected function buildTechnicalErrorResponse(): array
'success' => false,
'errors' => [
'' => [
$this->trans('One-page checkout is currently unavailable.', [], 'Shop.Notifications.Error'),
$this->trans('One-page checkout is currently unavailable.', [], ModuleTranslation::SHOP_DOMAIN),
],
],
];
Expand All @@ -46,6 +66,29 @@ protected function getTechnicalErrorResponseExtra(): array
return [];
}

/**
* @return array<string,mixed>
*/
protected function handleRuntimeException(Throwable $exception): array
{
PrestaShopLogger::addLog(
sprintf('ps_onepagecheckout runtime exception: %s', $exception->getMessage()),
3,
null,
'Module',
(int) $this->module->id,
true
);

Analytics::trackOpcCriticalError(
'unknown',
(bool) Configuration::get('PS_GUEST_CHECKOUT_ENABLED') ? 'yes' : 'no',
(string) $this->module->version
);

return $this->buildTechnicalErrorResponse();
}

/**
* @param array<string,mixed> $response
*/
Expand Down
74 changes: 29 additions & 45 deletions controllers/front/addresseslist.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,37 @@ class Ps_OnepagecheckoutAddressesListModuleFrontController extends Ps_Onepageche
/**
* @return array<string,mixed>
*/
protected function handleOpcRequest(): array
protected function handleAvailableOpcRequest(): array
{
if (!$this->isOpcAvailable()) {
return $this->buildTechnicalErrorResponse();
$handler = new OnePageCheckoutAddressesListHandler(
$this->context,
$this->module->getTranslator(),
new CheckoutCustomerContextResolver($this->context)
);
$response = $handler->handle();
if (empty($response['success'])) {
return $response;
}

try {
$handler = new OnePageCheckoutAddressesListHandler(
$this->context,
new CheckoutCustomerContextResolver($this->context)
);
$response = $handler->handle();
if (empty($response['success'])) {
return $response;
}

return [
'success' => true,
'address_count' => (int) ($response['address_count'] ?? 0),
'delivery_html' => $this->render(
'checkout/_partials/one-page-checkout/address-list',
[
'customer' => $response['customer'] ?? [],
'prefix' => '',
'selected_address' => (int) ($response['selected_delivery_address'] ?? 0),
]
),
'billing_html' => $this->render(
'checkout/_partials/one-page-checkout/address-list',
[
'customer' => $response['customer'] ?? [],
'prefix' => 'invoice_',
'selected_address' => (int) ($response['selected_invoice_address'] ?? 0),
]
),
];
} catch (Throwable $exception) {
PrestaShopLogger::addLog(
sprintf('ps_onepagecheckout addressesList runtime exception: %s', $exception->getMessage()),
3,
null,
'Module',
(int) $this->module->id,
true
);

return $this->buildTechnicalErrorResponse();
}
return [
'success' => true,
'address_count' => (int) ($response['address_count'] ?? 0),
'delivery_html' => $this->render(
'checkout/_partials/one-page-checkout/address-list',
[
'customer' => $response['customer'] ?? [],
'prefix' => '',
'selected_address' => (int) ($response['selected_delivery_address'] ?? 0),
]
),
'billing_html' => $this->render(
'checkout/_partials/one-page-checkout/address-list',
[
'customer' => $response['customer'] ?? [],
'prefix' => 'invoice_',
'selected_address' => (int) ($response['selected_invoice_address'] ?? 0),
]
),
];
}
}
48 changes: 11 additions & 37 deletions controllers/front/addressform.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* AJAX endpoint for module-owned OPC address form refresh.
*/

use PrestaShop\Module\PsOnePageCheckout\Analytics\Analytics;
use PrestaShop\Module\PsOnePageCheckout\Checkout\Ajax\CheckoutCustomerContextResolver;
use PrestaShop\Module\PsOnePageCheckout\Checkout\Ajax\OnePageCheckoutAddressFormHandler;
use PrestaShop\Module\PsOnePageCheckout\Form\OnePageCheckoutFormFactory;
Expand All @@ -16,43 +15,18 @@ class Ps_OnepagecheckoutAddressFormModuleFrontController extends Ps_Onepagecheck
/**
* @return array<string,mixed>
*/
protected function handleOpcRequest(): array
protected function handleAvailableOpcRequest(): array
{
if (!$this->isOpcAvailable()) {
return $this->buildTechnicalErrorResponse();
}

try {
$opcFormFactory = $this->getOpcFormFactory();
$handler = $this->createAddressFormHandler($opcFormFactory);
$templateVariables = $handler->getTemplateVariables(Tools::getAllValues());

return [
'addresses_section' => $this->render(
'checkout/_partials/one-page-checkout/addresses-section',
$templateVariables
),
];
} catch (Throwable $exception) {
PrestaShopLogger::addLog(
sprintf('ps_onepagecheckout addressForm runtime exception: %s', $exception->getMessage()),
3,
null,
'Module',
(int) $this->module->id,
true
);

if ($this->module instanceof Ps_Onepagecheckout) {
Analytics::trackOpcCriticalError(
'unknown',
(bool) Configuration::get('PS_GUEST_CHECKOUT_ENABLED') ? 'yes' : 'no',
(string) $this->module->version
);
}

return $this->buildTechnicalErrorResponse();
}
$opcFormFactory = $this->getOpcFormFactory();
$handler = $this->createAddressFormHandler($opcFormFactory);
$templateVariables = $handler->getTemplateVariables(Tools::getAllValues());

return [
'addresses_section' => $this->render(
'checkout/_partials/one-page-checkout/addresses-section',
$templateVariables
),
];
}

protected function getOpcFormFactory(): OnePageCheckoutFormFactory
Expand Down
6 changes: 1 addition & 5 deletions controllers/front/carriers.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ class Ps_OnepagecheckoutCarriersModuleFrontController extends Ps_Onepagecheckout
/**
* @return array<string,mixed>
*/
protected function handleOpcRequest(): array
protected function handleAvailableOpcRequest(): array
{
if (!$this->isOpcAvailable()) {
return $this->buildTechnicalErrorResponse();
}

$handler = new OnePageCheckoutCarriersHandler($this->context, $this->module->getTranslator());
$response = $handler->handle(Tools::getAllValues());

Expand Down
6 changes: 1 addition & 5 deletions controllers/front/carttotals.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ class Ps_OnepagecheckoutCartTotalsModuleFrontController extends Ps_Onepagechecko
/**
* @return array<string,mixed>
*/
protected function handleOpcRequest(): array
protected function handleAvailableOpcRequest(): array
{
if (!$this->isOpcAvailable()) {
return $this->buildTechnicalErrorResponse();
}

$cartPresenterHelper = new CartPresenterHelper($this->context);
$cartPreview = $cartPresenterHelper->presentCart();

Expand Down
6 changes: 1 addition & 5 deletions controllers/front/deleteaddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ class Ps_OnepagecheckoutDeleteAddressModuleFrontController extends Ps_Onepageche
/**
* @return array<string,mixed>
*/
protected function handleOpcRequest(): array
protected function handleAvailableOpcRequest(): array
{
if (!$this->isOpcAvailable()) {
return $this->buildTechnicalErrorResponse();
}

$handler = new OnePageCheckoutDeleteAddressHandler(
$this->context,
$this->module->getTranslator(),
Expand Down
33 changes: 33 additions & 0 deletions controllers/front/giftwrapping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use PrestaShop\Module\PsOnePageCheckout\Checkout\Ajax\OrderOptions\OnePageCheckoutGiftWrappingHandler;

require_once __DIR__ . '/AbstractOpcJsonFrontController.php';

class Ps_OnepagecheckoutGiftwrappingModuleFrontController extends Ps_OnepagecheckoutAbstractOpcJsonFrontController
{
/**
* @return array<string,mixed>
*/
protected function handleAvailableOpcRequest(): array
{
$handler = new OnePageCheckoutGiftWrappingHandler($this->context, $this->module->getTranslator());
$result = $handler->handle(Tools::getAllValues());

if (empty($result['success']) || !isset($result['cart'])) {
return $result;
}

return [
'success' => true,
'preview' => $this->render(
'checkout/_partials/cart-summary',
[
'cart' => $result['cart'],
'static_token' => Tools::getToken(false),
]
),
'totals' => $result['totals'],
];
}
}
34 changes: 4 additions & 30 deletions controllers/front/guestinit.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* AJAX endpoint for module-owned OPC guest initialization.
*/

use PrestaShop\Module\PsOnePageCheckout\Analytics\Analytics;
use PrestaShop\Module\PsOnePageCheckout\Checkout\Ajax\OnePageCheckoutGuestInitHandler;
use PrestaShop\Module\PsOnePageCheckout\Form\OnePageCheckoutFormFactory;

Expand All @@ -15,37 +14,12 @@ class Ps_OnepagecheckoutGuestInitModuleFrontController extends Ps_Onepagecheckou
/**
* @return array<string,mixed>
*/
protected function handleOpcRequest(): array
protected function handleAvailableOpcRequest(): array
{
if (!$this->isOpcAvailable()) {
return $this->buildTechnicalErrorResponse();
}
$opcFormFactory = $this->getOpcFormFactory();
$handler = $this->createGuestInitHandler($opcFormFactory);

try {
$opcFormFactory = $this->getOpcFormFactory();
$handler = $this->createGuestInitHandler($opcFormFactory);

return $handler->handle(Tools::getAllValues());
} catch (Throwable $exception) {
PrestaShopLogger::addLog(
sprintf('ps_onepagecheckout guestInit runtime exception: %s', $exception->getMessage()),
3,
null,
'Module',
(int) $this->module->id,
true
);

if ($this->module instanceof Ps_Onepagecheckout) {
Analytics::trackOpcCriticalError(
'unknown',
(bool) Configuration::get('PS_GUEST_CHECKOUT_ENABLED') ? 'yes' : 'no',
(string) $this->module->version
);
}

return $this->buildTechnicalErrorResponse();
}
return $handler->handle(Tools::getAllValues());
}

protected function getOpcFormFactory(): OnePageCheckoutFormFactory
Expand Down
Loading
Loading