<?php
namespace App\Services\Transaction\Workflow\EventSubscriber\Internal;
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\Services\Transaction\TransactionService;
use App\Entity\Transaction;
use App\Repository\TransactionRepository;
use BrokerAction\DTO\TransactionMessage\MessageComponent\Payload;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\Event;
/**
* Class OutboundTrxSubscriber.
*/
class OutboundTrxSubscriber implements EventSubscriberInterface
{
/**
* @var FeeTransactionService
*/
private $feeTransactionService;
/**
* @var TransactionReturnService
*/
private $trxReturnService;
/**
* @var PaymentFeeService
*/
private $paymentFeeService;
public function __construct(
FeeTransactionService $feeTransactionService,
TransactionRepository $transactionRepository,
TransactionService $transactionService,
FeeBuilder $feeBuilder,
TransactionReturnService $trxReturnService,
ToSignTransactionComponent $toSignTransactionComponent,
TransactionCancelService $trxCancelService,
PaymentFeeService $paymentFeeService
) {
$this->feeTransactionService = $feeTransactionService;
$this->transactionRepository = $transactionRepository;
$this->transactionService = $transactionService;
$this->feeBuilder = $feeBuilder;
$this->trxReturnService = $trxReturnService;
$this->toSignTransactionComponent = $toSignTransactionComponent;
$this->trxCancelService = $trxCancelService;
$this->paymentFeeService = $paymentFeeService;
}
public static function getSubscribedEvents()
{
return [
'workflow.internal_transaction_creating.completed.reject' => 'cancelOutboundMainTrx',
'workflow.internal_transaction_creating.completed.set_reserved' => 'makeOutboundTrx',
];
}
public function cancelOutboundMainTrx(Event $event)
{
/** @var Transaction $pmtTrx */
$pmtTrx = $event->getSubject()->getEntity();
$isRejectedByBank = $event->getTransition()->getFroms()[0] == 'review';
if (
!$isRejectedByBank
&& ($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()]));
}
}
}