custom/plugins/BilliePaymentSW6/src/Components/Checkout/Subscriber/CheckoutSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * Copyright (c) Billie GmbH
  5.  *
  6.  * For the full copyright and license information, please view the LICENSE
  7.  * file that was distributed with this source code.
  8.  */
  9. namespace Billie\BilliePayment\Components\Checkout\Subscriber;
  10. use Billie\BilliePayment\Components\Checkout\Service\WidgetService;
  11. use Billie\BilliePayment\Components\PaymentMethod\Util\MethodHelper;
  12. use RuntimeException;
  13. use Shopware\Core\Framework\Struct\ArrayStruct;
  14. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  15. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  16. use Shopware\Storefront\Page\PageLoadedEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class CheckoutSubscriber implements EventSubscriberInterface
  19. {
  20.     private WidgetService $widgetService;
  21.     public function __construct(WidgetService $widgetService)
  22.     {
  23.         $this->widgetService $widgetService;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             CheckoutConfirmPageLoadedEvent::class => ['addWidgetData'310],
  29.             AccountEditOrderPageLoadedEvent::class => ['addWidgetData'310],
  30.         ];
  31.     }
  32.     public function addWidgetData(PageLoadedEvent $event): void
  33.     {
  34.         if (!$event instanceof CheckoutConfirmPageLoadedEvent && !$event instanceof AccountEditOrderPageLoadedEvent) {
  35.             throw new RuntimeException('method ' self::class . '::' __METHOD__ ' does not supports a parameter of type' get_class($event));
  36.         }
  37.         $paymentMethod $event->getSalesChannelContext()->getPaymentMethod();
  38.         if (MethodHelper::isBilliePayment($paymentMethod) && $event->getPage()->getPaymentMethods()->has($paymentMethod->getId())) {
  39.             if ($event instanceof CheckoutConfirmPageLoadedEvent) {
  40.                 $widgetData $this->widgetService->getWidgetDataBySalesChannelContext($event->getSalesChannelContext());
  41.             } elseif ($event instanceof AccountEditOrderPageLoadedEvent) {
  42.                 $widgetData $this->widgetService->getWidgetDataByOrder($event->getPage()->getOrder(), $event->getSalesChannelContext());
  43.             } else {
  44.                 throw new RuntimeException('invalid event: ' gettype($event));
  45.             }
  46.             if ($widgetData instanceof ArrayStruct) {
  47.                 /** @var ArrayStruct $extension */
  48.                 $extension $event->getPage()->getExtension('billie_payment') ?? new ArrayStruct();
  49.                 $extension->set('widget'$widgetData->all());
  50.                 $event->getPage()->addExtension('billie_payment'$extension);
  51.             }
  52.         }
  53.     }
  54. }