custom/plugins/NwbNeo/src/Subscriber/CheckoutSubscriber.php line 22

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Imnxx\Subscriber;
  4. use Imnxx\Services\SepaService;
  5. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class CheckoutSubscriber implements EventSubscriberInterface
  8. {
  9.     public function __construct(private readonly SepaService $sepaService) {}
  10.     public static function getSubscribedEvents(): array
  11.     {
  12.         return [
  13.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmLoaded',
  14.         ];
  15.     }
  16.     public function onCheckoutConfirmLoaded(CheckoutConfirmPageLoadedEvent $event): void
  17.     {
  18.         $customer $event->getSalesChannelContext()->getCustomer();
  19.         if ($customer === null) {
  20.             return;
  21.         }
  22.         $activeBillingAddress $customer->getActiveBillingAddress();
  23.         if ($activeBillingAddress === null) {
  24.             return;
  25.         }
  26.         $addressCustomFields  $activeBillingAddress->getCustomFields() ?? [];
  27.         $customerCustomFields $customer->getCustomFields() ?? [];
  28.         $nwbAddressId $addressCustomFields['custom_address_attributs_nwb_address_id'] ?? null;
  29.         $nwbAccountId $customerCustomFields['custom_customer_attributes_account_id'] ?? null;
  30.         if ($nwbAddressId === null || $nwbAccountId === null) {
  31.             return;
  32.         }
  33.         $activeBisAddress $this->sepaService
  34.             ->getCurrentBillingAddressInformation($nwbAddressId$nwbAccountId);
  35.         if ($activeBisAddress === null) {
  36.             return;
  37.         }
  38.         $iban $activeBisAddress->getIban();
  39.         $accountHolder $activeBisAddress->getAccountHolder();
  40.         if ($iban === null || $accountHolder === null) {
  41.             return;
  42.         }
  43.         $anonymisedIban $this->sepaService->anonymiseIban($iban);
  44.         $event->getPage()->assign([
  45.             'imnxxSepaInfos' => [
  46.                 'iban' => $anonymisedIban,
  47.                 'accountHolder' => $accountHolder
  48.             ]
  49.         ]);
  50.     }
  51. }