<?php
namespace App\Services\Transaction\Workflow\EventSubscriber\Sepa\Outbound;
use App\Services\Transaction\StatusManagement\ToSignTransactionComponent;
use App\Services\Transaction\Fee\Builder\FeeBuilder;
use App\Services\Transaction\Fee\PaymentFeeService;
use App\Services\Transaction\FeeTransactionService;
use App\Services\Transaction\TransactionCancelService;
use App\Services\Transaction\TransactionReturnService;
use App\Entity\Transaction;
use BrokerAction\DTO\TransactionMessage\MessageComponent\Payload;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\Event;
class OutboundTrxSubscriber implements EventSubscriberInterface
{
/**
* @var TransactionCancelService
*/
protected $trxCancelService;
/**
* @var FeeTransactionService
*/
protected $feeTransactionService;
/**
* @var TransactionReturnService
*/
private $trxReturnService;
/**
* @var PaymentFeeService
*/
private $paymentFeeService;
public function __construct(
TransactionCancelService $trxReadyToClearComponent,
FeeTransactionService $feeTransactionService,
TransactionReturnService $trxReturnService,
ToSignTransactionComponent $toSignTransactionComponent,
FeeBuilder $feeBuilder,
PaymentFeeService $paymentFeeService
) {
$this->trxCancelService = $trxReadyToClearComponent;
$this->feeTransactionService = $feeTransactionService;
$this->trxReturnService = $trxReturnService;
$this->toSignTransactionComponent = $toSignTransactionComponent;
$this->feeBuilder = $feeBuilder;
$this->paymentFeeService = $paymentFeeService;
}
public static function getSubscribedEvents()
{
return [
'workflow.sepa_transaction_creating.completed.cancel' => 'updateCancellationEntity',
'workflow.sepa_transaction_creating.enter.rejected' => 'cancelOutboundMainTrx',
'workflow.sepa_transaction_creating.completed.set_reserved' => 'makeOutboundTrx',
'workflow.swift_outbound.enter.rejected' => 'cancelOutboundMainTrx',
];
}
public function updateCancellationEntity(Event $event)
{
/** @var Transaction $trxEntity */
$trxEntity = $event->getSubject()->getEntity();
if ($trxEntity->hasTypePayment()) {
$trxEntity->getCancellation()
&& $this->trxCancelService->completeCancellation($trxEntity->getCancellation());
$this->feeTransactionService->cancelFeeByMainTrx($trxEntity);
}
}
public function cancelOutboundMainTrx(Event $event)
{
/** @var Transaction $pmtTrx */
$pmtTrx = $event->getSubject()->getEntity();
$previousStatus = $event->getTransition()->getFroms()[0];
if (
$previousStatus != 'review'
&& ($pmtTrx->hasTypePayment() || $pmtTrx->hasTypePaymentReturn())
&& $pmtTrx->getOutbound() && $pmtTrx->isMoneyReleased()
) {
$this->trxReturnService->processSepaSentButRejectedTrx($pmtTrx);
}
$this->feeTransactionService->cancelFeeByMainTrx($pmtTrx);
}
public function makeOutboundTrx(Event $event)
{
/** @var Transaction $mainTransaction */
$mainTransaction = $event->getSubject()->getEntity();
$product = $mainTransaction->getProduct();
if ($product->debtorPaysFee() && !$mainTransaction->isDropFees()) {
$this->paymentFeeService->createOutbound(new Payload(['transaction_id' => $mainTransaction->getId()]));
}
}
}