<?php
namespace App\EventSubscribers\Main;
use App\Repository\Main\TransactionRepository;
use App\Tools\CreditCard\Error\Rule\BaseRule;
use App\Entity\Main\Transaction;
use App\Events\Main\Payment\PaymentErrorEvent;
use App\Events\Main\Payment\PaymentSwap3DSEvent;
use App\Events\Main\Paypal\PaypalCaptureCallbackReceivedEvent;
use App\Services\BillingManager;
use App\Services\CartManager;
use App\Services\EmailManager;
use App\Services\PayPalManager;
use App\Services\SiteManager;
use App\Tools\ShortId;
use App\Twig\GoogleTagManagerExtension;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectRepository;
use phpDocumentor\Reflection\Types\This;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
/**
* Class PaypalSubscriber
* @package App\EventSubscribers\Main
*/
class PaymentSubscriber implements EventSubscriberInterface
{
const
PAYMENT_ERROR = 'payment.error.event',
PAYMENT_3DS_SWAP = 'payment.3ds.swap.event',
PAYMENT_3DS_ERROR = 'payment.3ds.error.event';
private SessionInterface $session;
private ?Request $request;
/**
* CoachingUserSubscriber constructor.
* @param RequestStack $requestStack
* @param EntityManagerInterface $entityManager
* @param EmailManager $emailFactory
*/
public function __construct(
private RequestStack $requestStack,
private EntityManagerInterface $entityManager,
private EmailManager $emailFactory,
) {
$this->request = $this->requestStack->getCurrentRequest();
if ($this->request instanceof Request) {
$this->session = $this->request->getSession();
}
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return array(
self::PAYMENT_ERROR => "paymentError",
self::PAYMENT_3DS_ERROR => "payment3DSError",
self::PAYMENT_3DS_SWAP => "payment3DSSwap"
);
}
/**
* @param PaypalCaptureCallbackReceivedEvent $event
* @throws \Exception
*/
public function paymentError(PaymentErrorEvent $event)
{
// insert the error to be treated by GTM
$this->session->set(
GoogleTagManagerExtension::GTM_SESSION_PAYMENT_ERROR_EVENT_KEY,
$event->getErrorRule() instanceof BaseRule ? $event->getErrorRule()->getErrorMessage() : $event->getOriginalPaymentErrorText()
);
// If there is a rule, check if it require to send a mail. for now we send all the emails in spam mode as per chris request
// if ($event->getErrorRule() instanceof RuleInterface && $event->getErrorRule()->sendMail() && !in_array($this->environment, ['test', 'dev'])) {
$this->emailFactory->sendAlertBillingTransactionResponseError($event->getOriginalPaymentErrorText(), false, $event->getCart(), $event->getErrorRule(), $event->getTransaction());
// }
}
/**
* @param PaypalCaptureCallbackReceivedEvent $event
* @throws \Exception
*/
public function payment3DSError(PaymentErrorEvent $event)
{
// insert the error to be treated by GTM
$this->session->set(
GoogleTagManagerExtension::GTM_SESSION_PAYMENT_3DS_ERROR_EVENT_KEY,
$event->getErrorRule() instanceof Rule ? $event->getErrorRule()->getErrorMessage() : $event->getOriginalPaymentErrorText()
);
// If there is a rule, check if it require to send a mail. for now we send all the emails in spam mode as per chris request
// if ($event->getErrorRule() instanceof RuleInterface && $event->getErrorRule()->sendMail() && !in_array($this->environment, ['test', 'dev'])) {
$this->emailFactory->sendAlertBillingTransactionResponseError($event->getOriginalPaymentErrorText(), true, $event->getCart(), $event->getErrorRule(), $event->getTransaction());
// }
}
/**
* @param PaypalCaptureCallbackReceivedEvent $event
* @throws \Exception
*/
public function payment3DSSwap(PaymentSwap3DSEvent $event)
{
// insert the error to be treated by GTM
$this->session->set(
GoogleTagManagerExtension::GTM_SESSION_PAYMENT_3DS_SWAP_EVENT_KEY,
'Payment Swapped to 3DS'
);
$transaction = $event->getTransaction();
if ($transaction instanceof Transaction) {
$transaction->setForcedSwap(true);
$this->entityManager->flush();
}
$this->emailFactory->sendBilling3DSSwapNotification($event->getSwapReason(), $event->getCart(), $event->getTransaction());
}
}