custom/plugins/UnzerPayment6/src/EventListeners/Checkout/ConfirmPageEventListener.php line 78

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace UnzerPayment6\EventListeners\Checkout;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  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. use UnzerPayment6\Components\ClientFactory\ClientFactoryInterface;
  13. use UnzerPayment6\Components\ConfigReader\ConfigReader;
  14. use UnzerPayment6\Components\ConfigReader\ConfigReaderInterface;
  15. use UnzerPayment6\Components\PaymentFrame\PaymentFrameFactoryInterface;
  16. use UnzerPayment6\Components\Struct\Configuration;
  17. use UnzerPayment6\Components\Struct\PageExtension\Checkout\Confirm\CreditCardPageExtension;
  18. use UnzerPayment6\Components\Struct\PageExtension\Checkout\Confirm\DirectDebitPageExtension;
  19. use UnzerPayment6\Components\Struct\PageExtension\Checkout\Confirm\DirectDebitSecuredPageExtension;
  20. use UnzerPayment6\Components\Struct\PageExtension\Checkout\Confirm\InstallmentSecuredPageExtension;
  21. use UnzerPayment6\Components\Struct\PageExtension\Checkout\Confirm\PaymentFramePageExtension;
  22. use UnzerPayment6\Components\Struct\PageExtension\Checkout\Confirm\PayPalPageExtension;
  23. use UnzerPayment6\Components\Struct\PageExtension\Checkout\Confirm\UnzerDataPageExtension;
  24. use UnzerPayment6\DataAbstractionLayer\Entity\PaymentDevice\UnzerPaymentDeviceEntity;
  25. use UnzerPayment6\DataAbstractionLayer\Repository\PaymentDevice\UnzerPaymentDeviceRepositoryInterface;
  26. use UnzerPayment6\Installer\PaymentInstaller;
  27. class ConfirmPageEventListener implements EventSubscriberInterface
  28. {
  29.     private const INSTALLMENT_SECURED_EFFECTIVE_INTEREST_DEFAULT 4.5;
  30.     /** @var Configuration */
  31.     protected $configData;
  32.     /** @var UnzerPaymentDeviceRepositoryInterface */
  33.     private $deviceRepository;
  34.     /** @var ConfigReaderInterface */
  35.     private $configReader;
  36.     /** @var PaymentFrameFactoryInterface */
  37.     private $paymentFrameFactory;
  38.     /** @var SystemConfigService */
  39.     private $systemConfigReader;
  40.     /** @var EntityRepositoryInterface */
  41.     private $languageRepository;
  42.     public function __construct(
  43.         UnzerPaymentDeviceRepositoryInterface $deviceRepository,
  44.         ConfigReaderInterface $configReader,
  45.         PaymentFrameFactoryInterface $paymentFrameFactory,
  46.         SystemConfigService $systemConfigReader,
  47.         EntityRepositoryInterface $languageRepository
  48.     ) {
  49.         $this->deviceRepository    $deviceRepository;
  50.         $this->configReader        $configReader;
  51.         $this->paymentFrameFactory $paymentFrameFactory;
  52.         $this->systemConfigReader  $systemConfigReader;
  53.         $this->languageRepository  $languageRepository;
  54.     }
  55.     /**
  56.      * {@inheritdoc}
  57.      */
  58.     public static function getSubscribedEvents(): array
  59.     {
  60.         return [
  61.             CheckoutConfirmPageLoadedEvent::class  => 'onCheckoutConfirm',
  62.             AccountEditOrderPageLoadedEvent::class => 'onCheckoutConfirm',
  63.         ];
  64.     }
  65.     public function onCheckoutConfirm(PageLoadedEvent $event): void
  66.     {
  67.         if (!($event instanceof CheckoutConfirmPageLoadedEvent) && !($event instanceof AccountEditOrderPageLoadedEvent)) {
  68.             return;
  69.         }
  70.         $salesChannelContext    $event->getSalesChannelContext();
  71.         $this->configData       $this->configReader->read($salesChannelContext->getSalesChannel()->getId());
  72.         $registerCreditCards    = (bool) $this->configData->get(ConfigReader::CONFIG_KEY_REGISTER_CARDfalse);
  73.         $registerPayPalAccounts = (bool) $this->configData->get(ConfigReader::CONFIG_KEY_REGISTER_PAYPALfalse);
  74.         $registerDirectDebit    = (bool) $this->configData->get(ConfigReader::CONFIG_KEY_REGISTER_DIRECT_DEBITfalse);
  75.         if ($registerCreditCards &&
  76.             $salesChannelContext->getPaymentMethod()->getId() === PaymentInstaller::PAYMENT_ID_CREDIT_CARD
  77.         ) {
  78.             $this->addCreditCardExtension($event);
  79.         }
  80.         if ($registerPayPalAccounts &&
  81.             $salesChannelContext->getPaymentMethod()->getId() === PaymentInstaller::PAYMENT_ID_PAYPAL
  82.         ) {
  83.             $this->addPayPalExtension($event);
  84.         }
  85.         if ($registerDirectDebit &&
  86.             $salesChannelContext->getPaymentMethod()->getId() === PaymentInstaller::PAYMENT_ID_DIRECT_DEBIT
  87.         ) {
  88.             $this->addDirectDebitExtension($event);
  89.         }
  90.         if ($registerDirectDebit &&
  91.             $salesChannelContext->getPaymentMethod()->getId() === PaymentInstaller::PAYMENT_ID_DIRECT_DEBIT_SECURED
  92.         ) {
  93.             $this->addDirectDebitSecuredExtension($event);
  94.         }
  95.         if ($salesChannelContext->getPaymentMethod()->getId() === PaymentInstaller::PAYMENT_ID_INSTALLMENT_SECURED) {
  96.             $this->addInstallmentSecuredExtension($event);
  97.         }
  98.         $this->addPaymentFrameExtension($event);
  99.         $this->addUnzerDataExtension($event);
  100.     }
  101.     private function addUnzerDataExtension(PageLoadedEvent $event): void
  102.     {
  103.         $context $event->getSalesChannelContext()->getContext();
  104.         $extension = new UnzerDataPageExtension();
  105.         $extension->setPublicKey($this->configData->get(ConfigReader::CONFIG_KEY_PUBLIC_KEY));
  106.         $extension->setLocale($this->getLocaleByLanguageId($context->getLanguageId(), $context));
  107.         $extension->setShowTestData((bool) $this->configData->get(ConfigReader::CONFIG_KEY_TEST_DATA));
  108.         $event->getPage()->addExtension(UnzerDataPageExtension::EXTENSION_NAME$extension);
  109.     }
  110.     private function addPaymentFrameExtension(PageLoadedEvent $event): void
  111.     {
  112.         $paymentId           $event->getSalesChannelContext()->getPaymentMethod()->getId();
  113.         $mappedFrameTemplate $this->paymentFrameFactory->getPaymentFrame($paymentId);
  114.         if (!$mappedFrameTemplate) {
  115.             return;
  116.         }
  117.         $shopName $this->systemConfigReader->get(
  118.             'core.basicInformation.shopName',
  119.             $event->getSalesChannelContext()->getSalesChannel()->getId()
  120.         );
  121.         $event->getPage()->addExtension(
  122.             PaymentFramePageExtension::EXTENSION_NAME,
  123.             (new PaymentFramePageExtension())
  124.                 ->setPaymentFrame($mappedFrameTemplate)
  125.                 ->setShopName(is_string($shopName) ? $shopName '')
  126.         );
  127.     }
  128.     private function addCreditCardExtension(PageLoadedEvent $event): void
  129.     {
  130.         $customer $event->getSalesChannelContext()->getCustomer();
  131.         if (!$customer) {
  132.             return;
  133.         }
  134.         $creditCards $this->deviceRepository->getCollectionByCustomer($customer$event->getContext(), UnzerPaymentDeviceEntity::DEVICE_TYPE_CREDIT_CARD);
  135.         $extension   = (new CreditCardPageExtension())->setDisplayCreditCardSelection(true);
  136.         /** @var UnzerPaymentDeviceEntity $creditCard */
  137.         foreach ($creditCards as $creditCard) {
  138.             $extension->addCreditCard($creditCard);
  139.         }
  140.         $event->getPage()->addExtension(CreditCardPageExtension::EXTENSION_NAME$extension);
  141.     }
  142.     private function addPayPalExtension(PageLoadedEvent $event): void
  143.     {
  144.         $customer $event->getSalesChannelContext()->getCustomer();
  145.         if (!$customer) {
  146.             return;
  147.         }
  148.         $payPalAccounts $this->deviceRepository->getCollectionByCustomer($customer$event->getContext(), UnzerPaymentDeviceEntity::DEVICE_TYPE_PAYPAL);
  149.         $extension      = (new PayPalPageExtension())->setDisplaypayPalAccountselection(true);
  150.         /** @var UnzerPaymentDeviceEntity $payPalAccount */
  151.         foreach ($payPalAccounts as $payPalAccount) {
  152.             $extension->addPayPalAccount($payPalAccount);
  153.         }
  154.         $event->getPage()->addExtension(PayPalPageExtension::EXTENSION_NAME$extension);
  155.     }
  156.     private function addDirectDebitExtension(PageLoadedEvent $event): void
  157.     {
  158.         $customer $event->getSalesChannelContext()->getCustomer();
  159.         if (!$customer) {
  160.             return;
  161.         }
  162.         $directDebitDevices $this->deviceRepository->getCollectionByCustomer($customer$event->getContext(), UnzerPaymentDeviceEntity::DEVICE_TYPE_DIRECT_DEBIT);
  163.         $extension          = (new DirectDebitPageExtension())->setDisplayDirectDebitDeviceSelection(true);
  164.         /** @var UnzerPaymentDeviceEntity $directDebitDevice */
  165.         foreach ($directDebitDevices as $directDebitDevice) {
  166.             $extension->addDirectDebitDevice($directDebitDevice);
  167.         }
  168.         $event->getPage()->addExtension(DirectDebitPageExtension::EXTENSION_NAME$extension);
  169.     }
  170.     private function addDirectDebitSecuredExtension(PageLoadedEvent $event): void
  171.     {
  172.         $customer $event->getSalesChannelContext()->getCustomer();
  173.         if (!$customer) {
  174.             return;
  175.         }
  176.         $directDebitDevices $this->deviceRepository->getCollectionByCustomer($customer$event->getContext(), UnzerPaymentDeviceEntity::DEVICE_TYPE_DIRECT_DEBIT_SECURED);
  177.         $extension          = (new DirectDebitSecuredPageExtension())->setDisplayDirectDebitDeviceSelection(true);
  178.         /** @var UnzerPaymentDeviceEntity $directDebitDevice */
  179.         foreach ($directDebitDevices as $directDebitDevice) {
  180.             $extension->addDirectDebitDevice($directDebitDevice);
  181.         }
  182.         $event->getPage()->addExtension(DirectDebitSecuredPageExtension::EXTENSION_NAME$extension);
  183.     }
  184.     private function addInstallmentSecuredExtension(PageLoadedEvent $event): void
  185.     {
  186.         $extension = new InstallmentSecuredPageExtension();
  187.         $extension->setCurrency($event->getSalesChannelContext()->getCurrency()->getIsoCode());
  188.         $extension->setEffectiveInterest((float) $this->configData->get(ConfigReader::CONFIG_KEY_INSTALLMENT_SECURED_INTERESTself::INSTALLMENT_SECURED_EFFECTIVE_INTEREST_DEFAULT));
  189.         if ($event instanceof CheckoutConfirmPageLoadedEvent) {
  190.             $extension->setAmount($event->getPage()->getCart()->getPrice()->getTotalPrice());
  191.         } elseif ($event instanceof AccountEditOrderPageLoadedEvent) {
  192.             $extension->setAmount($event->getPage()->getOrder()->getPrice()->getTotalPrice());
  193.         }
  194.         $extension->setOrderDate(date('Y-m-d'));
  195.         $event->getPage()->addExtension(InstallmentSecuredPageExtension::EXTENSION_NAME$extension);
  196.     }
  197.     private function getLocaleByLanguageId(string $languageIdContext $context): string
  198.     {
  199.         $critera = new Criteria([$languageId]);
  200.         $critera->addAssociation('locale');
  201.         /** @var null|\Shopware\Core\System\Language\LanguageEntity $searchResult */
  202.         $searchResult $this->languageRepository->search($critera$context)->first();
  203.         if ($searchResult === null || $searchResult->getLocale() === null) {
  204.             return ClientFactoryInterface::DEFAULT_LOCALE;
  205.         }
  206.         return $searchResult->getLocale()->getCode();
  207.     }
  208. }