custom/plugins/PolPaymentPayolutionSW6/src/EventListener/CheckoutConfirmTemplateEventListener.php line 78

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PolPaymentPayolutionSW6\EventListener;
  4. use PolPaymentPayolutionSW6\Component\CartHashGenerator\CartHashGeneratorInterface;
  5. use PolPaymentPayolutionSW6\Component\CartProvider\CartProviderInterface;
  6. use PolPaymentPayolutionSW6\Component\Configuration\ConfigurationServiceInterface;
  7. use PolPaymentPayolutionSW6\Component\Validator\PaymentMethodValidator\PaymentMethodValidatorInterface;
  8. use PolPaymentPayolutionSW6\Struct\CheckoutConfirmData;
  9. use Shopware\Core\Checkout\Order\OrderEntity;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Core\System\Language\LanguageEntity;
  14. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  15. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPage;
  16. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  17. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPage;
  18. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  19. use Shopware\Storefront\Page\Page;
  20. use Shopware\Storefront\Page\PageLoadedEvent;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. class CheckoutConfirmTemplateEventListener implements EventSubscriberInterface
  23. {
  24.     private const FALLBACK_LOCALE 'en';
  25.     private const ISO_NL 'NL';
  26.     /**
  27.      * @var PaymentMethodValidatorInterface
  28.      */
  29.     private $paymentMethodValidator;
  30.     /**
  31.      * @var CartProviderInterface
  32.      */
  33.     private $cartProvider;
  34.     /**
  35.      * @var CartHashGeneratorInterface
  36.      */
  37.     private $cartHashGenerator;
  38.     /**
  39.      * @var ConfigurationServiceInterface
  40.      */
  41.     private $configurationService;
  42.     /**
  43.      * @var EntityRepositoryInterface
  44.      */
  45.     private $languageRepository;
  46.     public function __construct(
  47.         PaymentMethodValidatorInterface $paymentMethodValidator,
  48.         CartProviderInterface $cartProvider,
  49.         CartHashGeneratorInterface $cartHashGenerator,
  50.         ConfigurationServiceInterface $configurationService,
  51.         EntityRepositoryInterface $languageRepository
  52.     ) {
  53.         $this->paymentMethodValidator $paymentMethodValidator;
  54.         $this->cartProvider $cartProvider;
  55.         $this->cartHashGenerator $cartHashGenerator;
  56.         $this->configurationService $configurationService;
  57.         $this->languageRepository $languageRepository;
  58.     }
  59.     public static function getSubscribedEvents(): array
  60.     {
  61.         return [
  62.             CheckoutConfirmPageLoadedEvent::class => 'addTemplateDataForPayment',
  63.             AccountEditOrderPageLoadedEvent::class => 'addTemplateDataForPayment',
  64.         ];
  65.     }
  66.     public function addTemplateDataForPayment(PageLoadedEvent $event)
  67.     {
  68.         /** @var AccountEditOrderPage|CheckoutConfirmPage $page */
  69.         $page $event->getPage();
  70.         $salesChannelContext $event->getSalesChannelContext();
  71.         if (!$this->paymentMethodValidator->isPayolutionPaymentMethod($salesChannelContext->getPaymentMethod())) {
  72.             return;
  73.         }
  74.         if ($page->hasExtension(CheckoutConfirmData::EXTENSION_NAME)) {
  75.             $checkoutConfirmData $page->getExtension(CheckoutConfirmData::EXTENSION_NAME);
  76.         } else {
  77.             $checkoutConfirmData = new CheckoutConfirmData();
  78.         }
  79.         $customFields $salesChannelContext->getPaymentMethod()->getCustomFields();
  80.         $currentBirthday $salesChannelContext->getCustomer()->getBirthday();
  81.         $currentBillingAddress $salesChannelContext->getCustomer()->getActiveBillingAddress();
  82.         $existingOrder $this->getOrderForCurrentPage($page);
  83.         $checkoutConfirmData->assign([
  84.             'template' => $customFields['payolution_template'] ?? null,
  85.             'mandateDocumentUrl' => CheckoutConfirmData::MANDATE_DOCUMENT_URL,
  86.             'privacyPolicyUrl' => $this->getPrivacyPolicyUrl($salesChannelContext),
  87.             'cartHash' => $this->getCartHashForCurrentPage($page$salesChannelContext),
  88.             'orderId' => $existingOrder !== null $existingOrder->getId() : null,
  89.             'currentCompanyName' => $currentBillingAddress->getCompany(),
  90.             //'currentVatId' => $currentBillingAddress->getVatId(),
  91.             'currentPhoneNumber' => $currentBillingAddress->getPhoneNumber(),
  92.             'currentBirthDay' => $currentBirthday $currentBirthday->format('d') : null,
  93.             'currentBirthMonth' => $currentBirthday $currentBirthday->format('m') : null,
  94.             'currentBirthYear' => $currentBirthday $currentBirthday->format('Y') : null,
  95.             'invoiceShowPhoneNumber' => $currentBillingAddress->getCountry()->getIso() === static::ISO_NL,
  96.         ]);
  97.         $page->addExtension(CheckoutConfirmData::EXTENSION_NAME$checkoutConfirmData);
  98.     }
  99.     private function getCartHashForCurrentPage(Page $pageSalesChannelContext $salesChannelContext): string
  100.     {
  101.         /** @var AccountEditOrderPage|CheckoutConfirmPage $page */
  102.         $order $this->getOrderForCurrentPage($page);
  103.         $cart $this->cartProvider->getCartForCheckoutConfirmPage($page$order$salesChannelContext);
  104.         return $this->cartHashGenerator->generateCartHash($salesChannelContext$cart);
  105.     }
  106.     private function getOrderForCurrentPage(Page $page): ?OrderEntity
  107.     {
  108.         /** @var AccountEditOrderPage|CheckoutConfirmPage $page */
  109.         if ($page instanceof AccountEditOrderPage) {
  110.             return $page->getOrder();
  111.         }
  112.         return null;
  113.     }
  114.     private function getPrivacyPolicyUrl(SalesChannelContext $salesChannelContext): string
  115.     {
  116.         $merchantId $this->configurationService->getConfiguration(ConfigurationServiceInterface::MERCHANT_ID$salesChannelContext->getSalesChannel()->getId());
  117.         $locale $this->getLocale($salesChannelContext);
  118.         if (empty($merchantId)) {
  119.             return sprintf(str_replace('&mId=%s'''CheckoutConfirmData::PRIVACY_POLICY_URL), $locale);
  120.         }
  121.         return sprintf(CheckoutConfirmData::PRIVACY_POLICY_URL$locale$merchantId);
  122.     }
  123.     private function getLocale(SalesChannelContext $salesChannelContext): string
  124.     {
  125.         $criteria = new Criteria();
  126.         $criteria->addFilter(new EqualsFilter('id'$salesChannelContext->getSalesChannel()->getLanguageId()));
  127.         $criteria->addAssociation('locale');
  128.         $criteria->setLimit(1);
  129.         /** @var LanguageEntity $language */
  130.         $language $this->languageRepository->search($criteria$salesChannelContext->getContext())->first();
  131.         if ($language === null) {
  132.             return static::FALLBACK_LOCALE;
  133.         }
  134.         [$locale] = explode('-'$language->getLocale()->getCode());
  135.         if ($locale === null) {
  136.             return static::FALLBACK_LOCALE;
  137.         }
  138.         return strtolower($locale);
  139.     }
  140. }