custom/plugins/PayonePayment/src/EventListener/CheckoutConfirmRatepayEventListener.php line 87

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PayonePayment\EventListener;
  4. use PayonePayment\Components\ConfigReader\ConfigReader;
  5. use PayonePayment\Components\Ratepay\DeviceFingerprint\DeviceFingerprintServiceInterface;
  6. use PayonePayment\Components\Ratepay\Installment\InstallmentServiceInterface;
  7. use PayonePayment\Components\Ratepay\Profile\ProfileServiceInterface;
  8. use PayonePayment\PaymentHandler\PaymentHandlerGroups;
  9. use PayonePayment\PaymentHandler\PayoneRatepayInstallmentPaymentHandler;
  10. use PayonePayment\PaymentHandler\PayoneRatepayInvoicingPaymentHandler;
  11. use PayonePayment\PaymentMethod\PayoneRatepayInstallment;
  12. use PayonePayment\Storefront\Struct\RatepayDeviceFingerprintData;
  13. use PayonePayment\Storefront\Struct\RatepayInstallmentCalculatorData;
  14. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  15. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  16. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  17. use Shopware\Core\System\SystemConfig\SystemConfigService;
  18. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  19. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent;
  20. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  21. use Shopware\Storefront\Page\Page;
  22. use Shopware\Storefront\Page\PageLoadedEvent;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. class CheckoutConfirmRatepayEventListener implements EventSubscriberInterface
  25. {
  26.     protected SystemConfigService $systemConfigService;
  27.     protected InstallmentServiceInterface $installmentService;
  28.     protected DeviceFingerprintServiceInterface $deviceFingerprintService;
  29.     protected ProfileServiceInterface $profileService;
  30.     public function __construct(
  31.         SystemConfigService $systemConfigService,
  32.         InstallmentServiceInterface $installmentService,
  33.         DeviceFingerprintServiceInterface $deviceFingerprintService,
  34.         ProfileServiceInterface $profileService
  35.     ) {
  36.         $this->systemConfigService $systemConfigService;
  37.         $this->installmentService $installmentService;
  38.         $this->deviceFingerprintService $deviceFingerprintService;
  39.         $this->profileService $profileService;
  40.     }
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [
  44.             CheckoutConfirmPageLoadedEvent::class => [
  45.                 ['hidePaymentMethodsForCompanies'],
  46.                 ['hidePaymentMethodsByProfiles'],
  47.                 ['addPayonePageData'],
  48.             ],
  49.             AccountEditOrderPageLoadedEvent::class => [
  50.                 ['hidePaymentMethodsForCompanies'],
  51.                 ['hidePaymentMethodsByProfiles'],
  52.                 ['addPayonePageData'],
  53.             ],
  54.             AccountPaymentMethodPageLoadedEvent::class => 'hidePaymentMethodsForCompanies',
  55.         ];
  56.     }
  57.     public function hidePaymentMethodsForCompanies(PageLoadedEvent $event): void
  58.     {
  59.         $page $event->getPage();
  60.         if (
  61.             !method_exists($page'getPaymentMethods')
  62.             || !method_exists($page'setPaymentMethods')
  63.         ) {
  64.             return;
  65.         }
  66.         if (!$this->customerHasCompanyAddress($event->getSalesChannelContext())) {
  67.             return;
  68.         }
  69.         $paymentMethods $this->removePaymentMethods($page->getPaymentMethods(), PaymentHandlerGroups::RATEPAY);
  70.         $page->setPaymentMethods($paymentMethods);
  71.     }
  72.     public function hidePaymentMethodsByProfiles(PageLoadedEvent $event): void
  73.     {
  74.         $page $event->getPage();
  75.         if (
  76.             !method_exists($page'getPaymentMethods')
  77.             || !method_exists($page'setPaymentMethods')
  78.         ) {
  79.             return;
  80.         }
  81.         $paymentMethods $page->getPaymentMethods();
  82.         foreach (PaymentHandlerGroups::RATEPAY as $ratepayPaymentHandler) {
  83.             $profile $this->profileService->getProfileBySalesChannelContext(
  84.                 $event->getSalesChannelContext(),
  85.                 $ratepayPaymentHandler
  86.             );
  87.             if (!$profile) {
  88.                 $paymentMethods $this->removePaymentMethods($paymentMethods, [$ratepayPaymentHandler]);
  89.             }
  90.         }
  91.         $page->setPaymentMethods($paymentMethods);
  92.     }
  93.     public function addPayonePageData(PageLoadedEvent $event): void
  94.     {
  95.         /** @var Page $page */
  96.         $page $event->getPage();
  97.         $context $event->getSalesChannelContext();
  98.         if ($context->getPaymentMethod()->getId() === PayoneRatepayInstallment::UUID) {
  99.             $this->addInstallmentCalculatorData($page$context);
  100.         }
  101.         if (\in_array($context->getPaymentMethod()->getHandlerIdentifier(), PaymentHandlerGroups::RATEPAYtrue)) {
  102.             $this->addDeviceFingerprintData($page$context);
  103.         }
  104.     }
  105.     protected function removePaymentMethods(PaymentMethodCollection $paymentMethods, array $paymentHandler): PaymentMethodCollection
  106.     {
  107.         return $paymentMethods->filter(
  108.             static function (PaymentMethodEntity $paymentMethod) use ($paymentHandler) {
  109.                 return !\in_array($paymentMethod->getHandlerIdentifier(), $paymentHandlertrue);
  110.             }
  111.         );
  112.     }
  113.     protected function customerHasCompanyAddress(SalesChannelContext $context): bool
  114.     {
  115.         $customer $context->getCustomer();
  116.         if ($customer === null) {
  117.             return false;
  118.         }
  119.         $billingAddress $customer->getActiveBillingAddress();
  120.         if ($billingAddress === null) {
  121.             return false;
  122.         }
  123.         return !empty($billingAddress->getCompany());
  124.     }
  125.     protected function addInstallmentCalculatorData(Page $pageSalesChannelContext $context): void
  126.     {
  127.         if (
  128.             !method_exists($page'getPaymentMethods')
  129.             || !method_exists($page'setPaymentMethods')
  130.         ) {
  131.             return;
  132.         }
  133.         $installmentCalculator $this->installmentService->getInstallmentCalculatorData($context);
  134.         if ($installmentCalculator === null) {
  135.             $paymentMethods $this->removePaymentMethods($page->getPaymentMethods(), [
  136.                 PayoneRatepayInstallmentPaymentHandler::class,
  137.             ]);
  138.             $page->setPaymentMethods($paymentMethods);
  139.             return;
  140.         }
  141.         $page->addExtension(RatepayInstallmentCalculatorData::EXTENSION_NAME$installmentCalculator);
  142.     }
  143.     protected function addDeviceFingerprintData(Page $pageSalesChannelContext $context): void
  144.     {
  145.         if ($this->deviceFingerprintService->isDeviceIdentTokenAlreadyGenerated() === false) {
  146.             $snippetId $this->systemConfigService->get(
  147.                 ConfigReader::getConfigKeyByPaymentHandler(
  148.                     PayoneRatepayInvoicingPaymentHandler::class,
  149.                     'DeviceFingerprintSnippetId'
  150.                 ),
  151.                 $context->getSalesChannelId()
  152.             );
  153.             if (!\is_string($snippetId) || $snippetId === '') {
  154.                 $snippetId 'ratepay';
  155.             }
  156.             $deviceIdentToken $this->deviceFingerprintService->getDeviceIdentToken();
  157.             $snippet $this->deviceFingerprintService->getDeviceIdentSnippet($snippetId$deviceIdentToken);
  158.             $extension = new RatepayDeviceFingerprintData();
  159.             $extension->setSnippet($snippet);
  160.             $page->addExtension(RatepayDeviceFingerprintData::EXTENSION_NAME$extension);
  161.         }
  162.     }
  163. }