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.     /** @var OrderConverter */
  25.     private $orderConverter;
  26.     /** @var OrderFetcherInterface */
  27.     private $orderFetcher;
  28.     public function __construct(
  29.         OrderConverter $orderConverter,
  30.         OrderFetcherInterface $orderFetcher
  31.     ) {
  32.         $this->orderConverter $orderConverter;
  33.         $this->orderFetcher   $orderFetcher;
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             CheckoutConfirmPageLoadedEvent::class  => 'addCartData',
  39.             AccountEditOrderPageLoadedEvent::class => 'addCartData',
  40.         ];
  41.     }
  42.     /** @param AccountEditOrderPageLoadedEvent|CheckoutConfirmPageLoadedEvent $event */
  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 null|CheckoutCartPaymentData $extension */
  59.         $extension $page->getExtension(CheckoutCartPaymentData::EXTENSION_NAME);
  60.         if (null !== $extension && null !== $payoneData) {
  61.             $payoneData->assign([
  62.                 'workOrderId' => $extension->getWorkorderId(),
  63.                 'cartHash'    => $extension->getCartHash(),
  64.             ]);
  65.         }
  66.         $page->addExtension(CheckoutConfirmPaymentData::EXTENSION_NAME$payoneData);
  67.     }
  68.     /** @param AccountEditOrderPage|CheckoutConfirmPage $page */
  69.     private function hidePayonePaymentMethodsOnZeroAmountCart(Page $pageCart $cartSalesChannelContext $salesChannelContext): void
  70.     {
  71.         $totalAmount = (int) round($cart->getPrice()->getTotalPrice() * (10 ** $salesChannelContext->getCurrency()->getDecimalPrecision()));
  72.         if ($totalAmount 0) {
  73.             return;
  74.         }
  75.         $page->setPaymentMethods(
  76.             $page->getPaymentMethods()->filter(static function (PaymentMethodEntity $paymentMethod) {
  77.                 return mb_strpos($paymentMethod->getHandlerIdentifier(), PaymentMethodInstaller::HANDLER_IDENTIFIER_ROOT_NAMESPACE) === false;
  78.             })
  79.         );
  80.         $salesChannelContext->assign(['paymentMethods' => $page->getPaymentMethods()]);
  81.     }
  82.     private function convertCartFromOrder(OrderEntity $orderEntityContext $context): Cart
  83.     {
  84.         $order $this->orderFetcher->getOrderById($orderEntity->getId(), $context);
  85.         if (null === $order) {
  86.             throw new LogicException('could not find order via id');
  87.         }
  88.         return $this->orderConverter->convertToCart($order$context);
  89.     }
  90. }