custom/plugins/DatoElectronicInvoicingSw6/src/Storefront/Controller/CheckoutController.php line 53

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace SmartDato\DatoElectronicInvoicing\Storefront\Controller;
  3. use Shopware\Core\Checkout\Cart\Exception\CartTokenNotFoundException;
  4. use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
  5. use Shopware\Core\Checkout\Cart\Exception\OrderNotFoundException;
  6. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
  7. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  8. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  9. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Storefront\Controller\CheckoutController as CheckoutControllerInner;
  12. use SmartDato\DatoElectronicInvoicing\EntityManager\Customer\CustomerAddressEntityManager;
  13. use SmartDato\DatoElectronicInvoicing\Service\PluginConfig\PluginConfigService;
  14. use SmartDato\DatoElectronicInvoicing\Validation\Constraint\MainValidator;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Shopware\Core\Framework\Routing\Annotation\Since;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. /**
  20.  * @RouteScope(scopes={"storefront"})
  21.  */
  22. class CheckoutController extends StorefrontBaseController
  23. {
  24.     /** @var CheckoutControllerInner */
  25.     private $checkoutControllerInner;
  26.     /** @var PluginConfigService */
  27.     private $pluginConfigService;
  28.     /**
  29.      * CheckoutController constructor.
  30.      *
  31.      * @param CheckoutControllerInner $checkoutControllerInner
  32.      * @param PluginConfigService $pluginConfigService
  33.      */
  34.     public function __construct(
  35.         $checkoutControllerInner,
  36.         PluginConfigService $pluginConfigService
  37.     )
  38.     {
  39.         $this->checkoutControllerInner $checkoutControllerInner;
  40.         $this->pluginConfigService $pluginConfigService;
  41.     }
  42.     /**
  43.      * @Since("6.0.0.0")
  44.      * @Route("/checkout/cart", name="frontend.checkout.cart.page", options={"seo"="false"}, methods={"GET"})
  45.      */
  46.     public function cartPage(Request $requestSalesChannelContext $context): Response
  47.     {
  48.         return $this->checkoutControllerInner->cartPage($request$context);
  49.     }
  50.     /**
  51.      * @Since("6.0.0.0")
  52.      * @Route("/checkout/confirm", name="frontend.checkout.confirm.page", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
  53.      */
  54.     public function confirmPage(Request $requestSalesChannelContext $context): Response
  55.     {
  56.         return $this->checkoutControllerInner->confirmPage($request$context);
  57.     }
  58.     /**
  59.      * @Since("6.0.0.0")
  60.      * @Route("/checkout/finish", name="frontend.checkout.finish.page", options={"seo"="false"}, methods={"GET"})
  61.      *
  62.      * @throws CustomerNotLoggedInException
  63.      * @throws MissingRequestParameterException
  64.      * @throws OrderNotFoundException
  65.      */
  66.     public function finishPage(Request $requestSalesChannelContext $contextRequestDataBag $dataBag): Response
  67.     {
  68.         return $this->checkoutControllerInner->finishPage($request$context$dataBag);
  69.     }
  70.     /**
  71.      * @Since("6.0.0.0")
  72.      * @Route("/checkout/order", name="frontend.checkout.finish.order", options={"seo"="false"}, methods={"POST"})
  73.      */
  74.     public function order(RequestDataBag $dataSalesChannelContext $contextRequest $request): Response
  75.     {
  76.         $this->addFiscalCodeAndInvoicingFieldsValidationDefinitions($data$context);
  77.         return $this->checkoutControllerInner->order($data$context$request);
  78.     }
  79.     /**
  80.      * @Since("6.0.0.0")
  81.      * @Route("/widgets/checkout/info", name="frontend.checkout.info", methods={"GET"}, defaults={"XmlHttpRequest"=true})
  82.      *
  83.      * @throws CartTokenNotFoundException
  84.      */
  85.     public function info(Request $requestSalesChannelContext $context): Response
  86.     {
  87.         return $this->checkoutControllerInner->info($request$context);
  88.     }
  89.     /**
  90.      * @Since("6.0.0.0")
  91.      * @Route("/checkout/offcanvas", name="frontend.cart.offcanvas", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
  92.      *
  93.      * @throws CartTokenNotFoundException
  94.      */
  95.     public function offcanvas(Request $requestSalesChannelContext $context): Response
  96.     {
  97.         return $this->checkoutControllerInner->offcanvas($request$context);
  98.     }
  99.     /**
  100.      * Add fiscal code and invoicing fields validation definitions to validate order
  101.      *
  102.      * @param RequestDataBag $data
  103.      * @param SalesChannelContext $context
  104.      */
  105.     private function addFiscalCodeAndInvoicingFieldsValidationDefinitions(RequestDataBag $dataSalesChannelContext $context): void
  106.     {
  107.         if ($context->getCustomer() === null) {
  108.             return;
  109.         }
  110.         $billingAddress $context->getCustomer()->getActiveBillingAddress();
  111.         if ($billingAddress === null) {
  112.             return;
  113.         }
  114.         $validationConfig $this->pluginConfigService->getValidationConfig($context->getSalesChannel()->getId());
  115.         if (!$validationConfig->active) {
  116.             return;
  117.         }
  118.         $this->setFiscalCodeAndInvoicingFieldsDataToValidate($data$billingAddress);
  119.         MainValidator::createConstraintsAndAddToContext(
  120.             $context->getContext(),
  121.             CustomerAddressEntityManager::getCustomerTypeFromEntity($billingAddress),
  122.             $billingAddress->getCountryId(),
  123.             $validationConfig
  124.         );
  125.     }
  126.     /**
  127.      * Set fiscal code and invoicing fields data to validate these data
  128.      *
  129.      * @param RequestDataBag $data
  130.      * @param CustomerAddressEntity $billingAddress
  131.      */
  132.     private function setFiscalCodeAndInvoicingFieldsDataToValidate(RequestDataBag $dataCustomerAddressEntity $billingAddress): void
  133.     {
  134.         $data->set(
  135.             MainValidator::DATO_FISCAL_CODE,
  136.             (string)CustomerAddressEntityManager::getFiscalCodeFromEntity($billingAddress)
  137.         );
  138.         $data->set(
  139.             MainValidator::DATO_ELECTRONIC_INVOICING_FIELDS,
  140.             CustomerAddressEntityManager::getInvoicingFieldsFromEntity($billingAddress)
  141.         );
  142.     }
  143. }