local/modules/Paybox/EventListener/SendConfirmationEmail.php line 107

  1. <?php
  2. /*************************************************************************************/
  3. /*                                                                                   */
  4. /*      Thelia 2 Paybox payment module                                               */
  5. /*                                                                                   */
  6. /*      Copyright (c) CQFDev                                                         */
  7. /*      email : thelia@cqfdev.fr                                                     */
  8. /*      web : http://www.cqfdev.fr                                                   */
  9. /*                                                                                   */
  10. /*************************************************************************************/
  11. namespace Paybox\EventListener;
  12. use Paybox\Paybox;
  13. use Psr\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Thelia\Action\BaseAction;
  16. use Thelia\Core\Event\Order\OrderEvent;
  17. use Thelia\Core\Event\TheliaEvents;
  18. use Thelia\Core\Template\ParserInterface;
  19. use Thelia\Log\Tlog;
  20. use Thelia\Mailer\MailerFactory;
  21. use Thelia\Model\ConfigQuery;
  22. /**
  23.  * Paybox payment module
  24.  *
  25.  * @author Franck Allimant <franck@cqfdev.fr>
  26.  */
  27. class SendConfirmationEmail extends BaseAction implements EventSubscriberInterface
  28. {
  29.     /**
  30.      * @var MailerFactory
  31.      */
  32.     protected $mailer;
  33.     /**
  34.      * @var ParserInterface
  35.      */
  36.     protected $parser;
  37.     /**
  38.      * @var EventDispatcherInterface
  39.      */
  40.     protected $eventDispatcher;
  41.     public function __construct(ParserInterface $parserMailerFactory $mailerEventDispatcherInterface $eventDispatcher)
  42.     {
  43.         $this->parser $parser;
  44.         $this->mailer $mailer;
  45.         $this->eventDispatcher $eventDispatcher;
  46.     }
  47.     /**
  48.      * @return \Thelia\Mailer\MailerFactory
  49.      */
  50.     public function getMailer()
  51.     {
  52.         return $this->mailer;
  53.     }
  54.     /**
  55.      * Checks if we are the payment module for the order, and if the order is paid,
  56.      * then send a confirmation email to the customer.
  57.      *
  58.      * @param OrderEvent $event
  59.      * @throws \Exception
  60.      */
  61.     public function updateOrderStatus(OrderEvent $event)
  62.     {
  63.         $paybox = new Paybox();
  64.         if ($event->getOrder()->isPaid() && $paybox->isPaymentModuleFor($event->getOrder())) {
  65.             $contact_email ConfigQuery::read('store_email'false);
  66.             Tlog::getInstance()->debug(
  67.                 "Order " $event->getOrder()->getRef() . ": sending confirmation email from store contact e-mail $contact_email"
  68.             );
  69.             if ($contact_email && Paybox::getConfigValue('send_confirmation_email_on_successful_payment'false)) {
  70.                 $order $event->getOrder();
  71.                 $this->getMailer()->sendEmailToCustomer(
  72.                     Paybox::CONFIRMATION_MESSAGE_NAME,
  73.                     $order->getCustomer(),
  74.                     [
  75.                         'order_id' => $order->getId(),
  76.                         'order_ref' => $order->getRef()
  77.                     ]
  78.                 );
  79.                 Tlog::getInstance()->debug("Order " $order->getRef() . ": confirmation email sent to customer.");
  80.                 $this->eventDispatcher->dispatch($eventTheliaEvents::ORDER_SEND_CONFIRMATION_EMAIL);
  81.             }
  82.         } else {
  83.             Tlog::getInstance()->debug(
  84.                 "Order " $event->getOrder()->getRef() . ": no confirmation email sent (order not paid, or not the proper payment module)."
  85.             );
  86.         }
  87.     }
  88.     /**
  89.      * Send the confirmation message only if the order is paid.
  90.      *
  91.      * @param OrderEvent $event
  92.      */
  93.     public function checkSendOrderConfirmationMessageToCustomer(OrderEvent $event)
  94.     {
  95.         if (Paybox::getConfigValue('send_confirmation_email_on_successful_payment'false)) {
  96.             $paybox = new Paybox();
  97.             if ($paybox->isPaymentModuleFor($event->getOrder())) {
  98.                 if (!$event->getOrder()->isPaid()) {
  99.                     $event->stopPropagation();
  100.                 }
  101.             }
  102.         }
  103.     }
  104.     public static function getSubscribedEvents()
  105.     {
  106.         return array(
  107.             TheliaEvents::ORDER_UPDATE_STATUS => array("updateOrderStatus"128),
  108.             TheliaEvents::ORDER_SEND_CONFIRMATION_EMAIL => array("checkSendOrderConfirmationMessageToCustomer"150)
  109.         );
  110.     }
  111. }