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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PayonePayment\EventListener;
  4. use PayonePayment\Components\Helper\OrderFetcherInterface;
  5. use PayonePayment\Storefront\Struct\CheckoutCartPaymentData;
  6. use PayonePayment\Storefront\Struct\CheckoutConfirmPaymentData;
  7. use Shopware\Core\Checkout\Cart\Cart;
  8. use Shopware\Core\Checkout\Cart\Order\OrderConverter;
  9. use Shopware\Core\Checkout\Order\OrderEntity;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  12. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  13. use Shopware\Storefront\Page\PageLoadedEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Process\Exception\LogicException;
  16. class CheckoutConfirmCartDataEventListener implements EventSubscriberInterface
  17. {
  18.     /** @var OrderConverter */
  19.     private $orderConverter;
  20.     /** @var OrderFetcherInterface */
  21.     private $orderFetcher;
  22.     public function __construct(
  23.         OrderConverter $orderConverter,
  24.         OrderFetcherInterface $orderFetcher
  25.     ) {
  26.         $this->orderConverter $orderConverter;
  27.         $this->orderFetcher   $orderFetcher;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             CheckoutConfirmPageLoadedEvent::class  => 'addCartData',
  33.             AccountEditOrderPageLoadedEvent::class => 'addCartData',
  34.         ];
  35.     }
  36.     public function addCartData(PageLoadedEvent $event): void
  37.     {
  38.         $page $event->getPage();
  39.         if ($event instanceof CheckoutConfirmPageLoadedEvent) {
  40.             $cart $event->getPage()->getCart();
  41.         } elseif ($event instanceof AccountEditOrderPageLoadedEvent) {
  42.             $order $event->getPage()->getOrder();
  43.             $cart  $this->convertCartFromOrder($order$event->getContext());
  44.         } else {
  45.             return;
  46.         }
  47.         if ($cart->hasExtension(CheckoutCartPaymentData::EXTENSION_NAME)) {
  48.             $payoneData $cart->getExtension(CheckoutCartPaymentData::EXTENSION_NAME);
  49.         } else {
  50.             $payoneData = new CheckoutConfirmPaymentData();
  51.         }
  52.         /** @var null|CheckoutCartPaymentData $extension */
  53.         $extension $page->getExtension(CheckoutCartPaymentData::EXTENSION_NAME);
  54.         if (null !== $extension && null !== $payoneData) {
  55.             $payoneData->assign([
  56.                 'workOrderId' => $extension->getWorkorderId(),
  57.                 'cartHash'    => $extension->getCartHash(),
  58.             ]);
  59.         }
  60.         $page->addExtension(CheckoutConfirmPaymentData::EXTENSION_NAME$payoneData);
  61.     }
  62.     private function convertCartFromOrder(OrderEntity $orderEntityContext $context): Cart
  63.     {
  64.         $order $this->orderFetcher->getOrderById($orderEntity->getId(), $context);
  65.         if (null === $order) {
  66.             throw new LogicException('could not find order via id');
  67.         }
  68.         return $this->orderConverter->convertToCart($order$context);
  69.     }
  70. }