custom/plugins/PayonePayment/src/EventListener/CheckoutConfirmTemplateEventListener.php line 26

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PayonePayment\EventListener;
  4. use PayonePayment\Installer\CustomFieldInstaller;
  5. use PayonePayment\Storefront\Struct\CheckoutCartPaymentData;
  6. use PayonePayment\Storefront\Struct\CheckoutConfirmPaymentData;
  7. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  8. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  9. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  10. use Shopware\Storefront\Page\PageLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class CheckoutConfirmTemplateEventListener implements EventSubscriberInterface
  13. {
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             CheckoutConfirmPageLoadedEvent::class  => 'addPayonePageData',
  18.             AccountEditOrderPageLoadedEvent::class => 'addPayonePageData',
  19.         ];
  20.     }
  21.     public function addPayonePageData(PageLoadedEvent $event): void
  22.     {
  23.         $page    $event->getPage();
  24.         $context $event->getSalesChannelContext();
  25.         if (!$this->isPayonePayment($context->getPaymentMethod())) {
  26.             return;
  27.         }
  28.         $template $this->getTemplateFromPaymentMethod($context->getPaymentMethod());
  29.         if ($page->hasExtension(CheckoutCartPaymentData::EXTENSION_NAME)) {
  30.             $payoneData $page->getExtension(CheckoutCartPaymentData::EXTENSION_NAME);
  31.         } else {
  32.             $payoneData = new CheckoutConfirmPaymentData();
  33.         }
  34.         if (null !== $payoneData) {
  35.             $payoneData->assign([
  36.                 'template' => $template,
  37.             ]);
  38.         }
  39.         $page->addExtension(CheckoutConfirmPaymentData::EXTENSION_NAME$payoneData);
  40.     }
  41.     private function getTemplateFromPaymentMethod(PaymentMethodEntity $paymentMethod): ?string
  42.     {
  43.         $customFields $paymentMethod->getCustomFields();
  44.         if (!empty($customFields[CustomFieldInstaller::TEMPLATE])) {
  45.             return $customFields[CustomFieldInstaller::TEMPLATE];
  46.         }
  47.         return null;
  48.     }
  49.     private function isPayonePayment(PaymentMethodEntity $paymentMethod): bool
  50.     {
  51.         $customFields $paymentMethod->getCustomFields();
  52.         if (empty($customFields[CustomFieldInstaller::IS_PAYONE])) {
  53.             return false;
  54.         }
  55.         if (!$customFields[CustomFieldInstaller::IS_PAYONE]) {
  56.             return false;
  57.         }
  58.         return true;
  59.     }
  60. }