vendor/shopware/storefront/Page/Checkout/Confirm/CheckoutConfirmPageLoader.php line 70

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Page\Checkout\Confirm;
  3. use Shopware\Core\Checkout\Cart\Address\Error\AddressValidationError;
  4. use Shopware\Core\Checkout\Cart\Cart;
  5. use Shopware\Core\Checkout\Cart\CartException;
  6. use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
  7. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
  8. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  9. use Shopware\Core\Checkout\Payment\SalesChannel\AbstractPaymentMethodRoute;
  10. use Shopware\Core\Checkout\Shipping\SalesChannel\AbstractShippingMethodRoute;
  11. use Shopware\Core\Checkout\Shipping\ShippingMethodCollection;
  12. use Shopware\Core\Content\Product\State;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Core\Framework\Log\Package;
  16. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  17. use Shopware\Core\Framework\Validation\DataBag\DataBag;
  18. use Shopware\Core\Framework\Validation\DataValidationFactoryInterface;
  19. use Shopware\Core\Framework\Validation\DataValidator;
  20. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  21. use Shopware\Storefront\Checkout\Cart\SalesChannel\StorefrontCartFacade;
  22. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  23. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  24. use Symfony\Component\HttpFoundation\Request;
  25. #[Package('storefront')]
  26. class CheckoutConfirmPageLoader
  27. {
  28.     private EventDispatcherInterface $eventDispatcher;
  29.     private StorefrontCartFacade $cartService;
  30.     private AbstractShippingMethodRoute $shippingMethodRoute;
  31.     private AbstractPaymentMethodRoute $paymentMethodRoute;
  32.     private GenericPageLoaderInterface $genericPageLoader;
  33.     private DataValidationFactoryInterface $addressValidationFactory;
  34.     private DataValidator $validator;
  35.     /**
  36.      * @internal
  37.      */
  38.     public function __construct(
  39.         EventDispatcherInterface $eventDispatcher,
  40.         StorefrontCartFacade $cartService,
  41.         AbstractShippingMethodRoute $shippingMethodRoute,
  42.         AbstractPaymentMethodRoute $paymentMethodRoute,
  43.         GenericPageLoaderInterface $genericPageLoader,
  44.         DataValidationFactoryInterface $addressValidationFactory,
  45.         DataValidator $validator
  46.     ) {
  47.         $this->eventDispatcher $eventDispatcher;
  48.         $this->cartService $cartService;
  49.         $this->shippingMethodRoute $shippingMethodRoute;
  50.         $this->paymentMethodRoute $paymentMethodRoute;
  51.         $this->genericPageLoader $genericPageLoader;
  52.         $this->addressValidationFactory $addressValidationFactory;
  53.         $this->validator $validator;
  54.     }
  55.     /**
  56.      * @throws CustomerNotLoggedInException
  57.      * @throws InconsistentCriteriaIdsException
  58.      */
  59.     public function load(Request $requestSalesChannelContext $context): CheckoutConfirmPage
  60.     {
  61.         $page $this->genericPageLoader->load($request$context);
  62.         /** @var CheckoutConfirmPage $page */
  63.         $page CheckoutConfirmPage::createFrom($page);
  64.         if ($page->getMetaInformation()) {
  65.             $page->getMetaInformation()->setRobots('noindex,follow');
  66.         }
  67.         $page->setPaymentMethods($this->getPaymentMethods($context));
  68.         $page->setShippingMethods($this->getShippingMethods($context));
  69.         $cart $this->cartService->get($context->getToken(), $context);
  70.         $this->validateCustomerAddresses($cart$context);
  71.         $page->setCart($cart);
  72.         $page->setShowRevocation($cart->getLineItems()->hasLineItemWithState(State::IS_DOWNLOAD));
  73.         $page->setHideShippingAddress(!$cart->getLineItems()->hasLineItemWithState(State::IS_PHYSICAL));
  74.         $this->eventDispatcher->dispatch(
  75.             new CheckoutConfirmPageLoadedEvent($page$context$request)
  76.         );
  77.         return $page;
  78.     }
  79.     private function getPaymentMethods(SalesChannelContext $context): PaymentMethodCollection
  80.     {
  81.         $request = new Request();
  82.         $request->query->set('onlyAvailable''1');
  83.         return $this->paymentMethodRoute->load($request$context, new Criteria())->getPaymentMethods();
  84.     }
  85.     private function getShippingMethods(SalesChannelContext $context): ShippingMethodCollection
  86.     {
  87.         $request = new Request();
  88.         $request->query->set('onlyAvailable''1');
  89.         return $this->shippingMethodRoute->load($request$context, new Criteria())->getShippingMethods();
  90.     }
  91.     /**
  92.      * @throws CustomerNotLoggedInException
  93.      */
  94.     private function validateCustomerAddresses(Cart $cartSalesChannelContext $context): void
  95.     {
  96.         $customer $context->getCustomer();
  97.         if ($customer === null) {
  98.             throw CartException::customerNotLoggedIn();
  99.         }
  100.         $billingAddress $customer->getActiveBillingAddress();
  101.         $shippingAddress $customer->getActiveShippingAddress();
  102.         $this->validateBillingAddress($billingAddress$cart$context);
  103.         $this->validateShippingAddress($shippingAddress$billingAddress$cart$context);
  104.     }
  105.     private function validateBillingAddress(
  106.         ?CustomerAddressEntity $billingAddress,
  107.         Cart $cart,
  108.         SalesChannelContext $context
  109.     ): void {
  110.         $validation $this->addressValidationFactory->create($context);
  111.         $validationEvent = new BuildValidationEvent($validation, new DataBag(), $context->getContext());
  112.         $this->eventDispatcher->dispatch($validationEvent);
  113.         if ($billingAddress === null) {
  114.             return;
  115.         }
  116.         $violations $this->validator->getViolations($billingAddress->jsonSerialize(), $validation);
  117.         if ($violations->count() > 0) {
  118.             $cart->getErrors()->add(new AddressValidationError(true$violations));
  119.         }
  120.     }
  121.     private function validateShippingAddress(
  122.         ?CustomerAddressEntity $shippingAddress,
  123.         ?CustomerAddressEntity $billingAddress,
  124.         Cart $cart,
  125.         SalesChannelContext $context
  126.     ): void {
  127.         $validation $this->addressValidationFactory->create($context);
  128.         $validationEvent = new BuildValidationEvent($validation, new DataBag(), $context->getContext());
  129.         $this->eventDispatcher->dispatch($validationEvent);
  130.         if ($shippingAddress === null) {
  131.             return;
  132.         }
  133.         if ($billingAddress !== null && $shippingAddress->getId() === $billingAddress->getId()) {
  134.             return;
  135.         }
  136.         $violations $this->validator->getViolations($shippingAddress->jsonSerialize(), $validation);
  137.         if ($violations->count() > 0) {
  138.             $cart->getErrors()->add(new AddressValidationError(false$violations));
  139.         }
  140.     }
  141. }