src/EventSubscribers/Main/PaymentSubscriber.php line 75

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscribers\Main;
  3. use App\Repository\Main\TransactionRepository;
  4. use App\Tools\CreditCard\Error\Rule\BaseRule;
  5. use App\Entity\Main\Transaction;
  6. use App\Events\Main\Payment\PaymentErrorEvent;
  7. use App\Events\Main\Payment\PaymentSwap3DSEvent;
  8. use App\Events\Main\Paypal\PaypalCaptureCallbackReceivedEvent;
  9. use App\Services\BillingManager;
  10. use App\Services\CartManager;
  11. use App\Services\EmailManager;
  12. use App\Services\PayPalManager;
  13. use App\Services\SiteManager;
  14. use App\Tools\ShortId;
  15. use App\Twig\GoogleTagManagerExtension;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Doctrine\Persistence\ObjectRepository;
  18. use phpDocumentor\Reflection\Types\This;
  19. use Psr\Log\LoggerInterface;
  20. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\RequestStack;
  24. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  25. /**
  26.  * Class PaypalSubscriber
  27.  * @package App\EventSubscribers\Main
  28.  */
  29. class PaymentSubscriber implements EventSubscriberInterface
  30. {
  31.     const
  32.         PAYMENT_ERROR 'payment.error.event',
  33.         PAYMENT_3DS_SWAP 'payment.3ds.swap.event',
  34.         PAYMENT_3DS_ERROR 'payment.3ds.error.event';
  35.     private SessionInterface $session;
  36.     private ?Request $request;
  37.     /**
  38.      * CoachingUserSubscriber constructor.
  39.      * @param RequestStack $requestStack
  40.      * @param EntityManagerInterface $entityManager
  41.      * @param EmailManager $emailFactory
  42.      */
  43.     public function __construct(
  44.         private RequestStack $requestStack,
  45.         private EntityManagerInterface $entityManager,
  46.         private EmailManager $emailFactory,
  47.     ) {
  48.         $this->request $this->requestStack->getCurrentRequest();
  49.         if ($this->request instanceof Request) {
  50.             $this->session $this->request->getSession();
  51.         }
  52.     }
  53.     /**
  54.      * @return array
  55.      */
  56.     public static function getSubscribedEvents()
  57.     {
  58.         return array(
  59.             self::PAYMENT_ERROR => "paymentError",
  60.             self::PAYMENT_3DS_ERROR => "payment3DSError",
  61.             self::PAYMENT_3DS_SWAP => "payment3DSSwap"
  62.         );
  63.     }
  64.     /**
  65.      * @param PaypalCaptureCallbackReceivedEvent $event
  66.      * @throws \Exception
  67.      */
  68.     public function paymentError(PaymentErrorEvent $event)
  69.     {
  70.         // insert the error to be treated by GTM
  71.         $this->session->set(
  72.             GoogleTagManagerExtension::GTM_SESSION_PAYMENT_ERROR_EVENT_KEY,
  73.             $event->getErrorRule() instanceof BaseRule $event->getErrorRule()->getErrorMessage() : $event->getOriginalPaymentErrorText()
  74.         );
  75.         // 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
  76. //        if ($event->getErrorRule() instanceof RuleInterface && $event->getErrorRule()->sendMail() && !in_array($this->environment, ['test', 'dev'])) {
  77.             $this->emailFactory->sendAlertBillingTransactionResponseError($event->getOriginalPaymentErrorText(), false$event->getCart(), $event->getErrorRule(), $event->getTransaction());
  78. //        }
  79.     }
  80.     /**
  81.      * @param PaypalCaptureCallbackReceivedEvent $event
  82.      * @throws \Exception
  83.      */
  84.     public function payment3DSError(PaymentErrorEvent $event)
  85.     {
  86.         // insert the error to be treated by GTM
  87.         $this->session->set(
  88.             GoogleTagManagerExtension::GTM_SESSION_PAYMENT_3DS_ERROR_EVENT_KEY,
  89.             $event->getErrorRule() instanceof Rule $event->getErrorRule()->getErrorMessage() : $event->getOriginalPaymentErrorText()
  90.         );
  91.         // 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
  92. //        if ($event->getErrorRule() instanceof RuleInterface && $event->getErrorRule()->sendMail() && !in_array($this->environment, ['test', 'dev'])) {
  93.             $this->emailFactory->sendAlertBillingTransactionResponseError($event->getOriginalPaymentErrorText(), true$event->getCart(), $event->getErrorRule(), $event->getTransaction());
  94. //        }
  95.     }
  96.     /**
  97.      * @param PaypalCaptureCallbackReceivedEvent $event
  98.      * @throws \Exception
  99.      */
  100.     public function payment3DSSwap(PaymentSwap3DSEvent $event)
  101.     {
  102.         // insert the error to be treated by GTM
  103.         $this->session->set(
  104.             GoogleTagManagerExtension::GTM_SESSION_PAYMENT_3DS_SWAP_EVENT_KEY,
  105.             'Payment Swapped to 3DS'
  106.         );
  107.         $transaction $event->getTransaction();
  108.         if ($transaction instanceof Transaction) {
  109.             $transaction->setForcedSwap(true);
  110.             $this->entityManager->flush();
  111.         }
  112.         $this->emailFactory->sendBilling3DSSwapNotification($event->getSwapReason(), $event->getCart(), $event->getTransaction());
  113.     }
  114. }