custom/plugins/RpayPayments/src/Components/StateMachine/Subscriber/PaymentSuccessfulSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * Copyright (c) Ratepay GmbH
  5.  *
  6.  * For the full copyright and license information, please view the LICENSE
  7.  * file that was distributed with this source code.
  8.  */
  9. namespace Ratepay\RpayPayments\Components\StateMachine\Subscriber;
  10. use Ratepay\RpayPayments\Components\PaymentHandler\Event\PaymentSuccessfulEvent;
  11. use Ratepay\RpayPayments\Components\PluginConfig\Service\ConfigService;
  12. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionDefinition;
  13. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates;
  14. use Shopware\Core\System\StateMachine\StateMachineRegistry;
  15. use Shopware\Core\System\StateMachine\Transition;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class PaymentSuccessfulSubscriber implements EventSubscriberInterface
  18. {
  19.     private StateMachineRegistry $stateMachineRegistry;
  20.     private ConfigService $configService;
  21.     public function __construct(StateMachineRegistry $stateMachineRegistryConfigService $configService)
  22.     {
  23.         $this->stateMachineRegistry $stateMachineRegistry;
  24.         $this->configService $configService;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             PaymentSuccessfulEvent::class => 'changeTransactionState',
  30.         ];
  31.     }
  32.     public function changeTransactionState(PaymentSuccessfulEvent $event): void
  33.     {
  34.         $paymentMethod $event->getTransaction()->getOrderTransaction()->getPaymentMethod();
  35.         $newState $this->configService->getPaymentStatusForMethod($paymentMethod);
  36.         if ($newState && $newState !== OrderTransactionStates::STATE_OPEN) {
  37.             $this->stateMachineRegistry->transition(
  38.                 new Transition(
  39.                     OrderTransactionDefinition::ENTITY_NAME,
  40.                     $event->getTransaction()->getOrderTransaction()->getId(),
  41.                     $newState,
  42.                     'stateId'
  43.                 ),
  44.                 $event->getSalesChannelContext()->getContext()
  45.             );
  46.         }
  47.     }
  48. }