custom/plugins/VRPaymentVRPay/src/Subscriber/CheckoutSubscriber.php line 54

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace VRPayment\VRPay\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Cart;
  4. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use VRPayment\VRPay\Core\Checkout\Order\TransactionDefinition;
  12. /**
  13.  * Class CheckoutSubscriber
  14.  * Filters available payment methods
  15.  *
  16.  * @package VRPayment\VRPay\Subscriber
  17.  */
  18. class CheckoutSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var SystemConfigService $systemConfigService
  22.      */
  23.     private $systemConfigService;
  24.     public static function getSubscribedEvents()
  25.     {
  26.         return [
  27.             CheckoutConfirmPageLoadedEvent::class => 'onConfirmPageLoaded',
  28.         ];
  29.     }
  30.     /**
  31.      * CheckoutSubscriber constructor.
  32.      *
  33.      * @param SystemConfigService $systemConfigService
  34.      */
  35.     public function __construct(SystemConfigService $systemConfigService)
  36.     {
  37.         $this->systemConfigService $systemConfigService;
  38.     }
  39.     /**
  40.      * called when the confirm page was loaded. Filters out any of our payment methods that are currently not
  41.      * available (because they're inactive, not properly configured or any other restrictions apply)
  42.      *
  43.      * @param CheckoutConfirmPageLoadedEvent $event
  44.      */
  45.     public function onConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event)
  46.     {
  47.         $page $event->getPage();
  48.         $cart $page->getCart();
  49.         $salesChannelContext $event->getSalesChannelContext();
  50.         $page->setPaymentMethods(
  51.             $page->getPaymentMethods()->filter(function(PaymentMethodEntity $item) use ($cart$salesChannelContext) {
  52.                 if(!== strpos($item->getHandlerIdentifier(), 'VRPayment\VRPay\Service\Payment'))
  53.                 {
  54.                     return true;
  55.                 }
  56.                 return $this->isPaymentMethodAvailable($item$cart$salesChannelContext);
  57.             })
  58.         );
  59.     }
  60.     /*
  61.      * internals
  62.      */
  63.     /**
  64.      * checks if the given payment method is currently available
  65.      *
  66.      * @param PaymentMethodEntity $method
  67.      * @param Cart $cart
  68.      * @param SalesChannelContext $salesChannelContext
  69.      *
  70.      * @return bool
  71.      */
  72.     private function isPaymentMethodAvailable(PaymentMethodEntity $methodCart $cartSalesChannelContext $salesChannelContext)
  73.     {
  74.         $handler $method->getHandlerIdentifier();
  75.         if(is_callable([$handler'isActive']))
  76.         {
  77.             return $handler::isActive($cart$this->systemConfigService$salesChannelContext);
  78.         }
  79.         return true;
  80.     }
  81. }