Files
2026-09-15 10:35:56 +08:00

44 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\CustomerVat\Controller\Vat;
use Esun\CustomerVat\Model\VatApplicationService;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Data\Form\FormKey\Validator;
class Save extends AbstractVat implements HttpPostActionInterface
{
public function __construct(
Context $context,
Session $customerSession,
private readonly Validator $formKeyValidator,
private readonly VatApplicationService $applications
) {
parent::__construct($context, $customerSession);
}
public function execute()
{
if (!$this->formKeyValidator->validate($this->getRequest())) {
$this->messageManager->addErrorMessage(__('Your session has expired. Please try again.'));
return $this->resultRedirectFactory->create()->setPath('*/*/index');
}
$id = (int)$this->getRequest()->getParam('id');
try {
$this->applications->saveForCustomer(
(int)$this->customerSession->getCustomerId(),
(array)$this->getRequest()->getParams(),
$id
);
$this->messageManager->addSuccessMessage(__('Your VAT number has been submitted for review.'));
return $this->resultRedirectFactory->create()->setPath('*/*/index');
} catch (\Throwable $exception) {
$this->messageManager->addErrorMessage($exception->getMessage());
return $this->resultRedirectFactory->create()->setPath('*/*/' . ($id ? 'edit' : 'new'), $id ? ['id' => $id] : []);
}
}
}