vendor/store.shopware.com/mltisafemultisafepay/src/Subscriber/SalesChannelContextSwitchEvent.php line 57

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * Copyright © MultiSafepay, Inc. All rights reserved.
  4.  * See DISCLAIMER.md for disclaimer details.
  5.  */
  6. namespace MultiSafepay\Shopware6\Subscriber;
  7. use MultiSafepay\Shopware6\Handlers\AmericanExpressPaymentHandler;
  8. use MultiSafepay\Shopware6\Handlers\MastercardPaymentHandler;
  9. use MultiSafepay\Shopware6\Handlers\VisaPaymentHandler;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\System\SalesChannel\Event\SalesChannelContextSwitchEvent as BaseSalesChannelContextSwitchEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class SalesChannelContextSwitchEvent implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var EntityRepository
  19.      */
  20.     public $customerRepository;
  21.     /**
  22.      * @var EntityRepository
  23.      */
  24.     public $paymentMethodRepository;
  25.     /**
  26.      * SalesChannelContextSwitchEvent constructor.
  27.      *
  28.      * @param EntityRepository $customerRepository
  29.      * @param EntityRepository|\Shopware\Core\Checkout\Payment\DataAbstractionLayer\PaymentMethodRepositoryDecorator $paymentMethodRepository
  30.      */
  31.     public function __construct(
  32.         EntityRepository $customerRepository,
  33.         $paymentMethodRepository
  34.     ) {
  35.         $this->customerRepository $customerRepository;
  36.         $this->paymentMethodRepository $paymentMethodRepository;
  37.     }
  38.     /**
  39.      * @return array|string[]
  40.      */
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [
  44.             BaseSalesChannelContextSwitchEvent::class => 'salesChannelContextSwitchedEvent',
  45.         ];
  46.     }
  47.     /**
  48.      * @param BaseSalesChannelContextSwitchEvent $event
  49.      */
  50.     public function salesChannelContextSwitchedEvent(
  51.         BaseSalesChannelContextSwitchEvent $event
  52.     ): void {
  53.         $databag $event->getRequestDataBag();
  54.         $customer $event->getSalesChannelContext()->getCustomer();
  55.         $issuer $databag->get('issuer');
  56.         if ($issuer) {
  57.             $this->customerRepository->upsert(
  58.                 [
  59.                     [
  60.                         'id' => $customer->getId(),
  61.                         'customFields' => ['last_used_issuer' => $issuer],
  62.                     ],
  63.                 ],
  64.                 $event->getContext()
  65.             );
  66.         }
  67.     }
  68.     /**
  69.      * @param string $paymentMethodId
  70.      * @param Context $context
  71.      * @return mixed
  72.      */
  73.     private function getPaymentMethodHandler(string $paymentMethodIdContext $context)
  74.     {
  75.         return $this->paymentMethodRepository->search(new Criteria([$paymentMethodId]), $context)
  76.             ->get($paymentMethodId)
  77.             ->getHandlerIdentifier();
  78.     }
  79.     /**
  80.      * @param string $paymentMethodId
  81.      * @param Context $context
  82.      * @return string|null
  83.      */
  84.     private function getActiveTokenField(string $paymentMethodIdContext $context): ?string
  85.     {
  86.         switch ($this->getPaymentMethodHandler($paymentMethodId$context)) {
  87.             case AmericanExpressPaymentHandler::class:
  88.                 return 'token_american_express';
  89.             case VisaPaymentHandler::class:
  90.                 return 'token_visa';
  91.             case MastercardPaymentHandler::class:
  92.                 return 'token_mastercard';
  93.         }
  94.         return null;
  95.     }
  96. }