custom/plugins/PayonePayment/src/EventListener/CheckoutConfirmCartDataEventListener.php line 51

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PayonePayment\EventListener;
  4. use PayonePayment\Components\Helper\OrderFetcherInterface;
  5. use PayonePayment\Installer\PaymentMethodInstaller;
  6. use PayonePayment\Storefront\Struct\CheckoutCartPaymentData;
  7. use PayonePayment\Storefront\Struct\CheckoutConfirmPaymentData;
  8. use Shopware\Core\Checkout\Cart\Cart;
  9. use Shopware\Core\Checkout\Cart\Order\OrderConverter;
  10. use Shopware\Core\Checkout\Order\OrderEntity;
  11. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  12. use Shopware\Core\Framework\Context;
  13. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  14. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPage;
  15. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  16. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPage;
  17. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  18. use Shopware\Storefront\Page\Page;
  19. use Shopware\Storefront\Page\PageLoadedEvent;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Symfony\Component\Process\Exception\LogicException;
  22. class CheckoutConfirmCartDataEventListener implements EventSubscriberInterface
  23. {
  24.     private OrderConverter $orderConverter;
  25.     private OrderFetcherInterface $orderFetcher;
  26.     public function __construct(
  27.         OrderConverter $orderConverter,
  28.         OrderFetcherInterface $orderFetcher
  29.     ) {
  30.         $this->orderConverter $orderConverter;
  31.         $this->orderFetcher $orderFetcher;
  32.     }
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             CheckoutConfirmPageLoadedEvent::class => 'addCartData',
  37.             AccountEditOrderPageLoadedEvent::class => 'addCartData',
  38.         ];
  39.     }
  40.     /**
  41.      * @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $event
  42.      */
  43.     public function addCartData(PageLoadedEvent $event): void
  44.     {
  45.         $page $event->getPage();
  46.         if ($event instanceof CheckoutConfirmPageLoadedEvent) {
  47.             $cart $event->getPage()->getCart();
  48.         } else {
  49.             $order $event->getPage()->getOrder();
  50.             $cart $this->convertCartFromOrder($order$event->getContext());
  51.         }
  52.         $this->hidePayonePaymentMethodsOnZeroAmountCart($page$cart$event->getSalesChannelContext());
  53.         if ($cart->hasExtension(CheckoutCartPaymentData::EXTENSION_NAME)) {
  54.             $payoneData $cart->getExtension(CheckoutCartPaymentData::EXTENSION_NAME);
  55.         } else {
  56.             $payoneData = new CheckoutConfirmPaymentData();
  57.         }
  58.         /** @var CheckoutCartPaymentData|null $extension */
  59.         $extension $page->getExtension(CheckoutCartPaymentData::EXTENSION_NAME);
  60.         if ($extension !== null && $payoneData !== null) {
  61.             $payoneData->assign([
  62.                 'workOrderId' => $extension->getWorkorderId(),
  63.                 'cartHash' => $extension->getCartHash(),
  64.             ]);
  65.         }
  66.         $page->addExtension(CheckoutConfirmPaymentData::EXTENSION_NAME$payoneData);
  67.     }
  68.     /**
  69.      * @param AccountEditOrderPage|CheckoutConfirmPage $page
  70.      */
  71.     private function hidePayonePaymentMethodsOnZeroAmountCart(Page $pageCart $cartSalesChannelContext $salesChannelContext): void
  72.     {
  73.         $totalAmount = (int) round($cart->getPrice()->getTotalPrice() * (10 ** $salesChannelContext->getCurrency()->getDecimalPrecision()));
  74.         if ($totalAmount 0) {
  75.             return;
  76.         }
  77.         $page->setPaymentMethods(
  78.             $page->getPaymentMethods()->filter(static function (PaymentMethodEntity $paymentMethod) {
  79.                 return mb_strpos($paymentMethod->getHandlerIdentifier(), PaymentMethodInstaller::HANDLER_IDENTIFIER_ROOT_NAMESPACE) === false;
  80.             })
  81.         );
  82.         $salesChannelContext->assign(['paymentMethods' => $page->getPaymentMethods()]);
  83.     }
  84.     private function convertCartFromOrder(OrderEntity $orderEntityContext $context): Cart
  85.     {
  86.         $order $this->orderFetcher->getOrderById($orderEntity->getId(), $context);
  87.         if ($order === null) {
  88.             throw new LogicException('could not find order via id');
  89.         }
  90.         return $this->orderConverter->convertToCart($order$context);
  91.     }
  92. }