vendor/store.shopware.com/wexoquickpay/src/Subscriber/OrderDetailSubscriber.php line 109

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Wexo\Quickpay\Subscriber;
  3. use GuzzleHttp\Exception\GuzzleException;
  4. use Shopware\Core\Checkout\Cart\Order\OrderConvertedEvent;
  5. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates;
  6. use Shopware\Core\Checkout\Order\OrderEntity;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\System\StateMachine\Event\StateMachineTransitionEvent;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Wexo\Quickpay\Service\SubscriptionQuickpayService;
  15. use Wexo\Quickpay\ServiceInterface\QuickpayInterface;
  16. /**
  17.  * Class OrderDetailSubscriber
  18.  * @package Wexo\Quickpay\Subscriber
  19.  */
  20. class OrderDetailSubscriber implements EventSubscriberInterface
  21. {
  22.     protected EntityRepositoryInterface $orderRepository;
  23.     protected SystemConfigService $systemConfigService;
  24.     protected QuickpayInterface $paymentService;
  25.     protected SubscriptionQuickpayService $subscriptionQuickpayService;
  26.     /**
  27.      * @param EntityRepositoryInterface $orderRepository
  28.      * @param SystemConfigService $systemConfigService
  29.      * @param QuickpayInterface $paymentService
  30.      */
  31.     public function __construct(
  32.         EntityRepositoryInterface $orderRepository,
  33.         SystemConfigService $systemConfigService,
  34.         QuickpayInterface $paymentService,
  35.         SubscriptionQuickpayService $subscriptionQuickpayService
  36.     ) {
  37.         $this->orderRepository $orderRepository;
  38.         $this->systemConfigService $systemConfigService;
  39.         $this->paymentService $paymentService;
  40.         $this->subscriptionQuickpayService $subscriptionQuickpayService;
  41.     }
  42.     /**
  43.      * @return array|string[]
  44.      */
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             StateMachineTransitionEvent::class => 'onStateMachineTransitionEvent',
  49.             OrderConvertedEvent::class => 'orderConvertedEvent'
  50.         ];
  51.     }
  52.     /**
  53.      * @param StateMachineTransitionEvent $event
  54.      * @throws GuzzleException
  55.      */
  56.     public function onStateMachineTransitionEvent(StateMachineTransitionEvent $event)
  57.     {
  58.         $eventName $event->getToPlace()->getTechnicalName();
  59.         $transactionId $event->getEntityId();
  60.         $criteria = new Criteria();
  61.         $criteria->addAssociation('transactions.paymentMethod');
  62.         $criteria->addFilter(new EqualsFilter('transactions.id'$transactionId));
  63.         $criteria->addFilter(new ContainsFilter('transactions.paymentMethod.handlerIdentifier''Quickpay'));
  64.         /** @var OrderEntity $order */
  65.         $order $this->orderRepository->search(
  66.             $criteria,
  67.             $event->getContext()
  68.         )->first();
  69.         $capture $event->getContext()->getExtension('capture');
  70.         if ($order) {
  71.             if ($eventName === OrderTransactionStates::STATE_CANCELLED) {
  72.                 $this->paymentService->cancel($order);
  73.             }
  74.             if ($capture && ! $capture->get('amount')) {
  75.                 return;
  76.             }
  77.             if ($eventName === OrderTransactionStates::STATE_PAID) {
  78.                 $this->paymentService->capture($order->getId());
  79.             }
  80.             if ($eventName === OrderTransactionStates::STATE_PARTIALLY_PAID &&
  81.                 $capture && $capture->get('amount')
  82.             ) {
  83.                 $this->paymentService->capture(
  84.                     $order->getId(),
  85.                     (float) $capture->get('amount')
  86.                 );
  87.             }
  88.         }
  89.     }
  90.     /**
  91.      * @param OrderConvertedEvent $event
  92.      * @return void
  93.      * @throws GuzzleException
  94.      */
  95.     public function orderConvertedEvent(OrderConvertedEvent $event)
  96.     {
  97.         $subscription $event->getContext()->getExtension('subscriptionOrder');
  98.         if ($subscription) {
  99.             $this->subscriptionQuickpayService->recurring($event->getOrder()->getId());
  100.         }
  101.     }
  102. }