<?php
declare(strict_types=1);
namespace Imnxx\Subscriber;
use Imnxx\Services\SepaService;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CheckoutSubscriber implements EventSubscriberInterface
{
public function __construct(private readonly SepaService $sepaService) {}
public static function getSubscribedEvents(): array
{
return [
CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmLoaded',
];
}
public function onCheckoutConfirmLoaded(CheckoutConfirmPageLoadedEvent $event): void
{
$customer = $event->getSalesChannelContext()->getCustomer();
if ($customer === null) {
return;
}
$activeBillingAddress = $customer->getActiveBillingAddress();
if ($activeBillingAddress === null) {
return;
}
$addressCustomFields = $activeBillingAddress->getCustomFields() ?? [];
$customerCustomFields = $customer->getCustomFields() ?? [];
$nwbAddressId = $addressCustomFields['custom_address_attributs_nwb_address_id'] ?? null;
$nwbAccountId = $customerCustomFields['custom_customer_attributes_account_id'] ?? null;
if ($nwbAddressId === null || $nwbAccountId === null) {
return;
}
$activeBisAddress = $this->sepaService
->getCurrentBillingAddressInformation($nwbAddressId, $nwbAccountId);
if ($activeBisAddress === null) {
return;
}
$iban = $activeBisAddress->getIban();
$accountHolder = $activeBisAddress->getAccountHolder();
if ($iban === null || $accountHolder === null) {
return;
}
$anonymisedIban = $this->sepaService->anonymiseIban($iban);
$event->getPage()->assign([
'imnxxSepaInfos' => [
'iban' => $anonymisedIban,
'accountHolder' => $accountHolder
]
]);
}
}