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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cko\Shopware6\Subscriber;
  3. use Cko\Shopware6\Handler\PaymentHandler;
  4. use Cko\Shopware6\Helper\Util;
  5. use Cko\Shopware6\Service\PaymentMethodService;
  6. use Cko\Shopware6\Struct\PaymentMethod\PaymentMethodCustomFieldsStruct;
  7. use Shopware\Core\Checkout\Payment\PaymentEvents;
  8. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class PaymentMethodSubscriber implements EventSubscriberInterface
  13. {
  14.     public const PAYMENT_METHOD_CUSTOM_FIELDS 'checkoutConfig';
  15.     private PaymentMethodService $paymentMethodService;
  16.     public function __construct(PaymentMethodService $paymentMethodService)
  17.     {
  18.         $this->paymentMethodService $paymentMethodService;
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             PaymentEvents::PAYMENT_METHOD_LOADED_EVENT => 'onPaymentMethodLoaded',
  24.             PaymentEvents::PAYMENT_METHOD_SEARCH_RESULT_LOADED_EVENT => 'onPaymentMethodSearchResultLoaded',
  25.         ];
  26.     }
  27.     public function onPaymentMethodLoaded(EntityLoadedEvent $event): void
  28.     {
  29.         /** @var PaymentMethodEntity $paymentMethod */
  30.         foreach ($event->getEntities() as $paymentMethod) {
  31.             $this->setCustomFieldsPaymentMethod($paymentMethod);
  32.         }
  33.     }
  34.     public function onPaymentMethodSearchResultLoaded(EntitySearchResultLoadedEvent $event): void
  35.     {
  36.         /** @var PaymentMethodEntity $paymentMethod */
  37.         foreach ($event->getResult()->getEntities() as $paymentMethod) {
  38.             $this->setCustomFieldsPaymentMethod($paymentMethod);
  39.         }
  40.     }
  41.     private function setCustomFieldsPaymentMethod(PaymentMethodEntity $paymentMethod): void
  42.     {
  43.         $paymentHandler $this->paymentMethodService->getPaymentHandlersByHandlerIdentifier(
  44.             $paymentMethod->getHandlerIdentifier()
  45.         );
  46.         if (!$paymentHandler instanceof PaymentHandler) {
  47.             return;
  48.         }
  49.         $paymentMethodCustomFields = new PaymentMethodCustomFieldsStruct();
  50.         $paymentMethodCustomFields->setMethodType(
  51.             Util::handleCallUserFunc(
  52.                 $paymentMethod->getHandlerIdentifier() . '::getPaymentMethodType',
  53.                 false
  54.             )
  55.         );
  56.         $customFields $paymentMethod->getCustomFields() ?? [];
  57.         $customFields[self::PAYMENT_METHOD_CUSTOM_FIELDS] = $paymentMethodCustomFields->jsonSerialize();
  58.         $paymentMethod->setCustomFields($customFields);
  59.     }
  60. }