<?php
declare(strict_types=1);
/*
* Copyright (c) Ratepay GmbH
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Ratepay\RpayPayments\Components\StateMachine\Subscriber;
use Ratepay\RpayPayments\Components\PaymentHandler\Event\PaymentSuccessfulEvent;
use Ratepay\RpayPayments\Components\PluginConfig\Service\ConfigService;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionDefinition;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates;
use Shopware\Core\System\StateMachine\StateMachineRegistry;
use Shopware\Core\System\StateMachine\Transition;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PaymentSuccessfulSubscriber implements EventSubscriberInterface
{
private StateMachineRegistry $stateMachineRegistry;
private ConfigService $configService;
public function __construct(StateMachineRegistry $stateMachineRegistry, ConfigService $configService)
{
$this->stateMachineRegistry = $stateMachineRegistry;
$this->configService = $configService;
}
public static function getSubscribedEvents(): array
{
return [
PaymentSuccessfulEvent::class => 'changeTransactionState',
];
}
public function changeTransactionState(PaymentSuccessfulEvent $event): void
{
$paymentMethod = $event->getTransaction()->getOrderTransaction()->getPaymentMethod();
$newState = $this->configService->getPaymentStatusForMethod($paymentMethod);
if ($newState && $newState !== OrderTransactionStates::STATE_OPEN) {
$this->stateMachineRegistry->transition(
new Transition(
OrderTransactionDefinition::ENTITY_NAME,
$event->getTransaction()->getOrderTransaction()->getId(),
$newState,
'stateId'
),
$event->getSalesChannelContext()->getContext()
);
}
}
}