Files
ibanpro/controllers/front/validation.php
2025-08-28 21:44:55 +03:00

140 lines
5.2 KiB
PHP

<?php
declare(strict_types=1);
// Ensure this file is not directly accessed
if (!defined('_PS_VERSION_')) {
exit;
}
/**
* @property \IbanPro $module
*/
class IbanProValidationModuleFrontController extends ModuleFrontController
{
/**
* Processes the incoming POST request for payment validation.
* Determines the payment method and delegates to the appropriate handler.
*
* @see FrontController::postProcess()
*/
public function postProcess(): void
{
// Set the template to render the form for redirection
if (
!Validate::isLoadedObject($this->context->cart) ||
!Validate::isLoadedObject($this->context->customer) ||
!Validate::isLoadedObject($this->context->currency) ||
!$this->module->active // Ensure the module is active
) {
PrestaShopLogger::addLog(
'banProValidation: Initial validation failed. Cart ID: ' . (int)$this->context->cart->id .
' Customer ID: ' . (int)$this->context->customer->id . ' Module Active: ' . (int)$this->module->active,
3
);
Tools::redirect('index.php?controller=order&step=1');
return;
}
$this->processOrderCreation();
}
/**
* Processes the online payment flow.
* This method orchestrates the steps for creating an order and redirecting to LiqPay.
*/
protected function processOrderCreation(): void
{
$this->validateCartAndModuleAccess();
$order = $this->createOrder();
Tools::redirect($this->context->link->getPageLink('order-confirmation', true, $this->context->language->id, [
'id_cart' => (int)$this->context->cart->id,
'id_module' => (int)$this->module->id,
'id_order' => (int)$order->id,
'key' => $this->context->customer->secure_key,
]));
}
/**
* Validates the cart and checks if the payment module is authorized for the current cart.
* Redirects to the order page if validation fails.
*/
protected function validateCartAndModuleAccess(): void
{
// Check cart validity (redundant if init is robust, but good for defensive programming)
if (
(int)$this->context->cart->id_customer === 0 ||
(int)$this->context->cart->id_address_delivery === 0 ||
(int)$this->context->cart->id_address_invoice === 0 ||
(int)$this->context->cart->id === 0 // Check for valid cart ID (not null or 0)
) {
PrestaShopLogger::addLog(
'IbanProValidation: Invalid cart details during validation. Cart ID: ' . (int)$this->context->cart->id,
3
);
Tools::redirect('index.php?controller=order&step=1');
}
// Check that this payment option is still available
$authorized = false;
foreach (Module::getPaymentModules() as $module) {
if ($module['name'] === $this->module->name) {
$authorized = true;
break;
}
}
if (!$authorized) {
PrestaShopLogger::addLog(
'IbanProValidation: Payment method is not authorized for cart ' . (int)$this->context->cart->id,
3
);
// Using die() with l() is acceptable for payment module authorization errors.
die($this->module->l('This payment method is not available.', 'validation'));
}
}
/**
* Creates a new PrestaShop order for the current cart.
*
* @return \Order The newly created order object.
* @throws PrestaShopException If order creation fails.
*/
protected function createOrder(): Order
{
try {
// Validate and create the order
$this->module->validateOrder(
(int)$this->context->cart->id,
Configuration::get('IBANTRANSFER_OS_CREATION'), // Use the appropriate pending status for your module
(float)$this->context->cart->getOrderTotal(true, Cart::BOTH),
$this->module->paymentMethodName, // Payment method name for order history
null, // Message
null, // Extra vars
(int)$this->context->currency->id,
false, // Don't convert currency
$this->context->customer->secure_key
);
// After validateOrder, the module's currentOrder property holds the new order ID
$order = new Order((int)$this->module->currentOrder);
if (!Validate::isLoadedObject($order)) {
throw new PrestaShopException('Failed to load the newly created order.');
}
return $order;
} catch (Throwable $e) {
PrestaShopLogger::addLog(
'IbanProValidation: Order creation failed for cart ' . (int)$this->context->cart->id . '. Error: ' . $e->getMessage() . ' Trace: ' . $e->getTraceAsString(),
4
);
Tools::redirect('index.php?controller=order&step=1'); // Redirect to order page on failure
// In a real scenario, you might want to display a more user-friendly error or log to the user.
}
}
}