src/EventSubscribers/Main/OrderSubscriber.php line 261

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscribers\Main;
  3. use App\Entity\Main\AffiliateNotificationConfig;
  4. use App\Entity\Main\BillingAccount;
  5. use App\Entity\Main\RebillManager;
  6. use App\Entity\Main\Site;
  7. use App\Entity\Main\SiteConfig;
  8. use App\Entity\Main\UserSettings;
  9. use App\Events\Main\Order\OrderAcceptedEvent;
  10. use App\Events\Main\Order\OrderFailedCheckedEvent;
  11. use App\Events\Main\Order\OrderLockedEvent;
  12. use App\Events\Main\Order\OrderPaidByClientEvent;
  13. use App\Services\BillingManager;
  14. use App\Services\CartManager;
  15. use App\Services\EmailManager;
  16. use App\Services\ShippingManager;
  17. use App\Services\AffiliateNotificationManager;
  18. use App\Services\MailingManager;
  19. use App\Services\NudgifyManager;
  20. use App\Services\SiteManager;
  21. use App\Services\TransactionManager;
  22. use App\Services\UserManager;
  23. use App\Tools\ReviewsSystem\AvisVerifies;
  24. use App\Tools\ShortId;
  25. use Doctrine\ORM\EntityManagerInterface;
  26. use Psr\Log\LoggerInterface;
  27. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  28. use Symfony\Component\HttpFoundation\RequestStack;
  29. use Symfony\Component\Routing\RouterInterface;
  30. /**
  31.  * Class OrderSubscriber
  32.  * @package App\EventSubscribers\Main
  33.  */
  34. class OrderSubscriber implements EventSubscriberInterface
  35. {
  36.     const
  37.         ORDER_ACCEPTED 'order.accepted',
  38.         ORDER_LOCKED 'order.locked',
  39.         ORDER_PAID_BY_CLIENT 'order.paid_by_client',
  40.         CHECKED_FOR_FAILED_ORDER 'checked_for_failed_order';
  41.     /**
  42.      * @var EmailManager
  43.      */
  44.     private $emailFactory;
  45.     /**
  46.      * @var ShortId
  47.      */
  48.     private $shortId;
  49.     /**
  50.      * @var EntityManagerInterface
  51.      */
  52.     private $entityManager;
  53.     /**
  54.      * @var CartManager
  55.      */
  56.     private $cartFactory;
  57.     /**
  58.      * @var ShippingManager
  59.      */
  60.     private $shippingFactory;
  61.     /**
  62.      * @var UserManager
  63.      */
  64.     private $userManager;
  65.     /**
  66.      * @var AffiliateNotificationManager
  67.      */
  68.     private $affiliateNotificationManager;
  69.     /**
  70.      * @var TransactionManager
  71.      */
  72.     private $transactionManager;
  73.     /**
  74.      * @var AvisVerifies
  75.      */
  76.     private $avisVerifies;
  77.     /**
  78.      * @var LoggerInterface
  79.      */
  80.     private $logger;
  81.     /**
  82.      * @var SiteManager
  83.      */
  84.     private $siteManager;
  85.     private $avEnabled;
  86.     private $environment;
  87.     /**
  88.      * @var RouterInterface
  89.      */
  90.     private $router;
  91.     /**
  92.      * @var BillingManager
  93.      */
  94.     private $billingFactory;
  95.     private NudgifyManager $nudgifyManager;
  96.     private array $nudgifyConfig;
  97.     private MailingManager $mailingManager;
  98.     /**
  99.      * CartSubscriber constructor.
  100.      * @param EmailManager $emailFactory
  101.      * @param ShortId $shortId
  102.      * @param EntityManagerInterface $entityManager
  103.      * @param CartManager $cartFactory
  104.      * @param ShippingManager $shippingFactory
  105.      * @param UserManager $userManager
  106.      * @param AffiliateNotificationManager $affiliateNotificationManager
  107.      * @param TransactionManager $transactionManager
  108.      * @param AvisVerifies $avisVerifies
  109.      * @param SiteManager $siteManager
  110.      * @param LoggerInterface $logger
  111.      * @param $environment
  112.      * @param RouterInterface $router
  113.      * @throws \Exception
  114.      */
  115.     public function __construct(
  116.         EmailManager $emailFactory,
  117.         ShortId $shortId,
  118.         EntityManagerInterface $entityManager,
  119.         CartManager $cartFactory,
  120.         ShippingManager $shippingFactory,
  121.         UserManager $userManager,
  122.         AffiliateNotificationManager $affiliateNotificationManager,
  123.         TransactionManager $transactionManager,
  124.         AvisVerifies $avisVerifies,
  125.         SiteManager $siteManager,
  126.         LoggerInterface $logger,
  127.         $environment,
  128.         RouterInterface $router,
  129.         BillingManager $billingFactory,
  130.         NudgifyManager $nudgifyManager,
  131.         MailingManager $mailingManager
  132.     ) {
  133.         $this->emailFactory $emailFactory;
  134.         $this->shortId $shortId;
  135.         $this->entityManager $entityManager;
  136.         $this->cartFactory $cartFactory;
  137.         $this->shippingFactory $shippingFactory;
  138.         $this->userManager $userManager;
  139.         $this->affiliateNotificationManager $affiliateNotificationManager;
  140.         $this->transactionManager $transactionManager;
  141.         $this->avisVerifies $avisVerifies;
  142.         $this->logger $logger;
  143.         $this->siteManager $siteManager;
  144.         $this->avEnabled $this->siteManager->getConfigParam(SiteConfig::AVIS_VERIFIES_NAME)->getValue();
  145.         $this->environment $environment;
  146.         $this->router $router;
  147.         $this->billingFactory $billingFactory;
  148.         $this->nudgifyManager $nudgifyManager;
  149.         $this->nudgifyConfig $this->siteManager->getYamlConfigParameter('nudgify');
  150.         $this->mailingManager $mailingManager;
  151.     }
  152.     /**
  153.      * @return array
  154.      */
  155.     public static function getSubscribedEvents()
  156.     {
  157.         return array(
  158.             self::ORDER_ACCEPTED => "orderAccepted",
  159.             self::ORDER_PAID_BY_CLIENT => "orderPaidByClient",
  160.             self::ORDER_LOCKED => "orderLocked",
  161.             self::CHECKED_FOR_FAILED_ORDER => "checkedForFailedOrder",
  162.         );
  163.     }
  164.     /**
  165.      * @param OrderLockedEvent $event
  166.      * @throws \Twig_Error_Loader
  167.      * @throws \Twig_Error_Runtime
  168.      * @throws \Twig_Error_Syntax
  169.      * @throws \Exception
  170.      */
  171.     public function orderLocked(OrderLockedEvent $event)
  172.     {
  173.         // NOTE : This is now where we send the email of the accepted order.
  174.         $cart $event->getCart();
  175.         $user $cart->getUser();
  176.         // Now the coaching is being sent from here with the receipt.
  177.         if ($cart->getSite()->isCoachingSite()) {
  178.             $shippingApi $this->shippingFactory->getShippingApiForElectronicProduct($cart);
  179.             $credentials $shippingApi->getCredentialsForCoaching($cart);
  180.             $this->emailFactory->sendCoachingSitePurchaseEmail($user$credentials);
  181.             // we need to stop the rebill manager as merkav won't do it.
  182.             $this->billingFactory->stopRebillManager($cart->getRebillManagers()[0], RebillManager::CANCEL_ONE_SHOT);
  183.             // we want to mark the order as delivered.
  184.             try {
  185.                 $this->cartFactory->transitState($cart'shippingAwaiting');
  186.                 $this->cartFactory->transitState($cart'shippingDelivery');
  187.                 $this->cartFactory->transitState($cart'shipped');
  188.             } catch (\Exception $e) {
  189.                 $this->logger->critical('Could not change status of a coaching cart. => ' $cart->getCartId());
  190.             }
  191.         } elseif ($this->cartFactory->hasCoaching($cart)) {
  192.             // here we need to put the new email to send :)
  193.             $shippingApi $this->shippingFactory->getShippingApiForElectronicProduct($cart);
  194.             if (null == $shippingApi) {
  195.                 throw new \LogicException("Can't get shipping api for Electronic product to create email");
  196.             }
  197.             $credentials $shippingApi->getCredentialsForCoaching($cart);
  198.             $this->emailFactory->sendPurchasedCombo(
  199.                 $this->shortId->encode($cart->getCartId()),
  200.                 $cart,
  201.                 $this->cartFactory->computeCartTotal($cart),
  202.                 $credentials
  203.             );
  204.         } else {
  205.             $this->emailFactory->sendPurchasedSummary(
  206.                 $this->shortId->encode($cart->getCartId()),
  207.                 $cart,
  208.                 $this->cartFactory->computeCartTotal($cart)
  209.             );
  210.         }
  211.         // We need to create a call to contact this user in 10 days.
  212.         if ($this->userManager->userCanBeCalled($user) && !$cart->getSite()->isCoachingSite()) {
  213.             $this->userManager->createCallForUser($user$cart);
  214.         }
  215.         // queue ordered related scenarios.
  216.         $this->mailingManager->queueOrderRelatedScenarios($cart);
  217.         // if Avis Verifies is enabled we send order info to the system.
  218.         if ($this->avEnabled &&
  219.             !$cart->getSite()->isCoachingSite()
  220.             && 'prod' == $this->environment
  221.             && $this->userManager->userCanGiveFeedback($user)
  222.         ) {
  223.             $orderInfo $this->cartFactory->getOrderInfoFromCartForAvisVerifie($cart$this->router);
  224.             if (!empty($orderInfo)) {
  225.                 try {
  226.                     $this->avisVerifies->sendOrderInfo($orderInfo);
  227.                 } catch (\Exception $e) {
  228.                     $this->logger->error("We could not send Cart info (cart: " $cart->getCartId() . ") to the Avis Verifies sytem. Error: " $e->getMessage());
  229.                 }
  230.             }
  231.         }
  232.     }
  233.     /**
  234.      * @param OrderAcceptedEvent $event
  235.      * @throws \Twig_Error_Loader
  236.      * @throws \Twig_Error_Runtime
  237.      * @throws \Twig_Error_Syntax
  238.      */
  239.     public function orderAccepted(OrderAcceptedEvent $event)
  240.     {
  241.         // NOTE : Here now we send an 'order received email" but it doesn't contain anymore the list of purchases as
  242.         // the customer can now add some upsells :)
  243.         $this->pushToNudgifyApi($event);
  244.         $this->emailFactory->sendOrderReceivedEmail($event->getCart(), $event->getCart()->getEncodedId($this->shortId));
  245.     }
  246.     /**
  247.      * @param OrderPaidByClientEvent $event
  248.      */
  249.     public function orderPaidByClient(OrderPaidByClientEvent $event)
  250.     {
  251.         $cart $event->getCart();
  252.         if (!$cart->getSite()->isCoachingSite()) {
  253.             $this->affiliateNotificationManager->addNotification($cart->getTracking(), $cartAffiliateNotificationConfig::PURCHASE_OVER);
  254.         }
  255.     }
  256.     /**
  257.      * @param OrderFailedCheckedEvent $event
  258.      */
  259.     public function checkedForFailedOrder(OrderFailedCheckedEvent $event)
  260.     {
  261.         $cart $event->getCart();
  262.         $user $cart->getUser();
  263.         $request $event->getRequest();
  264.         //allow the user to use PayPal.
  265.         $userSettings $user->getSettings();
  266.         $userSettings[UserSettings::PAYPAL_ALLOWED] = true;
  267.         $userSettings[UserSettings::PAYPAL_NO_LIMIT] = true;
  268.         $user->setSettings($userSettings);
  269.         $this->entityManager->flush();
  270.         //keep the cart in the session.
  271.         $session $request->getSession();
  272.         $session->set('currentCartId'$cart->getCartId());
  273.         $session->set('usePayPalMessage'true);
  274.         $session->remove(CartManager::CHECK_FAILED_PURCHASE_KEY);
  275.         $session->remove(CartManager::DISCOUNT_CODE_KEY);
  276.     }
  277.     /**
  278.      * @param OrderAcceptedEvent $event
  279.      * @throws \ErrorException
  280.      * @throws \GuzzleHttp\Exception\GuzzleException
  281.      */
  282.     protected function pushToNudgifyApi(OrderAcceptedEvent $event)
  283.     {
  284.         $cart $event->getCart();
  285.         $site $cart->getSite();
  286.         /** @var Site $site */
  287.         if ('it' === $cart->getUser()->getUserLocale()) {
  288.             $locale $cart->getUser()->getUserLocale();
  289.         } elseif(isset($site->getProperties()['translation']) && isset($site->getProperties()['translation']['locale']) && 'it' === $site->getProperties()['translation']['locale']) {
  290.             $locale $site->getProperties()['translation']['locale'];
  291.         } else {
  292.             $locale 'fr';
  293.         }
  294.         if (true === $this->nudgifyConfig[$locale]['enabled'] && $cart->getPaymentTransaction()->getBillingAccount()->getBillingAccountType() !== BillingAccount::BILLING_ACCOUNT_TYPE_DUMMY) {
  295.             $this->nudgifyManager->pushToApiForPurchaseNudges($cart$this->nudgifyConfig$locale);
  296.         }
  297.     }
  298. }