custom/plugins/CkoShopware6/src/Subscriber/CheckoutPaymentMethodPageSubscriber.php line 42

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cko\Shopware6\Subscriber;
  3. use Cko\Shopware6\CkoShopware6;
  4. use Cko\Shopware6\Helper\Util;
  5. use Cko\Shopware6\Service\CustomerService;
  6. use Cko\Shopware6\Struct\Customer\CustomerSourceCollection;
  7. use ReflectionClass;
  8. use Shopware\Core\Checkout\Customer\CustomerEntity;
  9. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  10. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  12. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  13. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent;
  14. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class CheckoutPaymentMethodPageSubscriber implements EventSubscriberInterface
  17. {
  18.     public const PAYMENT_METHOD_CUSTOM_FIELDS 'checkoutSource';
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoaded',
  23.             AccountEditOrderPageLoadedEvent::class => 'onAccountEditOrderPageLoaded',
  24.             AccountPaymentMethodPageLoadedEvent::class => 'onAccountPaymentMethodPageLoaded',
  25.         ];
  26.     }
  27.     public function onCheckoutConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event): void
  28.     {
  29.         $this->addCheckoutSource(false$event->getPage()->getPaymentMethods(), $event->getSalesChannelContext());
  30.     }
  31.     public function onAccountEditOrderPageLoaded(AccountEditOrderPageLoadedEvent $event): void
  32.     {
  33.         $this->addCheckoutSource(false$event->getPage()->getPaymentMethods(), $event->getSalesChannelContext());
  34.     }
  35.     public function onAccountPaymentMethodPageLoaded(AccountPaymentMethodPageLoadedEvent $event): void
  36.     {
  37.         $this->addCheckoutSource(true$event->getPage()->getPaymentMethods(), $event->getSalesChannelContext());
  38.     }
  39.     private function addCheckoutSource(bool $setToAllPaymentMethodCollection $paymentMethodsSalesChannelContext $context): void
  40.     {
  41.         $checkoutComNamespace = (new ReflectionClass(CkoShopware6::class))->getNamespaceName();
  42.         $customer $context->getCustomer();
  43.         if (!$customer instanceof CustomerEntity) {
  44.             return;
  45.         }
  46.         foreach ($paymentMethods as $paymentMethod) {
  47.             // We check is the payment method is a Checkout.com payment method
  48.             $isCheckoutComPaymentMethod strpos($paymentMethod->getHandlerIdentifier(), $checkoutComNamespace) !== false;
  49.             if (!$isCheckoutComPaymentMethod) {
  50.                 continue;
  51.             }
  52.             // If the flag is not set to all payment methods, skip if the current payment method is selected
  53.             if (!$setToAll && $paymentMethod->getId() !== $context->getPaymentMethod()->getId()) {
  54.                 continue;
  55.             }
  56.             $this->setPaymentMethodSource($paymentMethod$customer);
  57.         }
  58.     }
  59.     private function setPaymentMethodSource(PaymentMethodEntity $paymentMethodCustomerEntity $customer): void
  60.     {
  61.         $customerCustomFields CustomerService::getCheckoutSourceCustomFields($customer);
  62.         $paymentMethodType Util::handleCallUserFunc(
  63.             $paymentMethod->getHandlerIdentifier() . '::getPaymentMethodType',
  64.             false
  65.         );
  66.         $source $customerCustomFields->getSourceByType($paymentMethodType);
  67.         if (!$source instanceof CustomerSourceCollection) {
  68.             return;
  69.         }
  70.         $customFields $paymentMethod->getCustomFields() ?? [];
  71.         $customFields[self::PAYMENT_METHOD_CUSTOM_FIELDS] = Util::serializeStruct($source);
  72.         $paymentMethod->setCustomFields($customFields);
  73.     }
  74. }