local/modules/PayPal/EventListeners/OrderListener.php line 80

  1. <?php
  2. /*************************************************************************************/
  3. /*                                                                                   */
  4. /*      Thelia                                                                         */
  5. /*                                                                                   */
  6. /*      Copyright (c) OpenStudio                                                     */
  7. /*      email : info@thelia.net                                                      */
  8. /*      web : http://www.thelia.net                                                  */
  9. /*                                                                                   */
  10. /*      This program is free software; you can redistribute it and/or modify         */
  11. /*      it under the terms of the GNU General Public License as published by         */
  12. /*      the Free Software Foundation; either version 3 of the License                */
  13. /*                                                                                   */
  14. /*      This program is distributed in the hope that it will be useful,              */
  15. /*      but WITHOUT ANY WARRANTY; without even the implied warranty of               */
  16. /*      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                */
  17. /*      GNU General Public License for more details.                                 */
  18. /*                                                                                   */
  19. /*      You should have received a copy of the GNU General Public License            */
  20. /*        along with this program. If not, see <http://www.gnu.org/licenses/>.         */
  21. /*                                                                                   */
  22. /*************************************************************************************/
  23. namespace PayPal\EventListeners;
  24. use PayPal\Event\PayPalCartEvent;
  25. use PayPal\Event\PayPalEvents;
  26. use PayPal\Form\PayPalFormFields;
  27. use PayPal\Form\Type\PayPalCreditCardType;
  28. use PayPal\PayPal;
  29. use PayPal\Service\PayPalAgreementService;
  30. use PayPal\Service\PayPalPaymentService;
  31. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  32. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  33. use Symfony\Component\HttpFoundation\RequestStack;
  34. use Thelia\Core\Event\Order\OrderEvent;
  35. use Thelia\Core\Event\TheliaEvents;
  36. use Thelia\Mailer\MailerFactory;
  37. /**
  38.  * Class OrderListener
  39.  * @package PayPal\EventListeners
  40.  */
  41. class OrderListener implements EventSubscriberInterface
  42. {
  43.     /** @var MailerFactory */
  44.     protected $mailer;
  45.     /** @var EventDispatcherInterface */
  46.     protected $dispatcher;
  47.     /** @var RequestStack */
  48.     protected $requestStack;
  49.     /** @var PayPalPaymentService */
  50.     protected $payPalPaymentService;
  51.     /** @var PayPalAgreementService */
  52.     protected $payPalAgreementService;
  53.     /**
  54.      * @param MailerFactory $mailer
  55.      * @param EventDispatcherInterface $dispatcher
  56.      * @param RequestStack $requestStack
  57.      * @param PayPalPaymentService $payPalPaymentService
  58.      * @param PayPalAgreementService $payPalAgreementService
  59.      */
  60.     public function __construct(MailerFactory $mailerEventDispatcherInterface $dispatcherRequestStack $requestStackPayPalPaymentService $payPalPaymentServicePayPalAgreementService $payPalAgreementService)
  61.     {
  62.         $this->dispatcher $dispatcher;
  63.         $this->mailer $mailer;
  64.         $this->requestStack $requestStack;
  65.         $this->payPalPaymentService $payPalPaymentService;
  66.         $this->payPalAgreementService $payPalAgreementService;
  67.     }
  68.     /**
  69.      * @param OrderEvent $event
  70.      */
  71.     public function CancelPayPalTransaction(OrderEvent $event)
  72.     {
  73.         // @TODO : Inform PayPal that this payment is canceled ?
  74.     }
  75.     /**
  76.      * @param OrderEvent $event
  77.      *
  78.      * @throws \Exception if the message cannot be loaded.
  79.      */
  80.     public function sendConfirmationEmail(OrderEvent $event)
  81.     {
  82.         if (PayPal::getConfigValue('send_confirmation_message_only_if_paid')) {
  83.             // We send the order confirmation email only if the order is paid
  84.             $order $event->getOrder();
  85.             if (! $order->isPaid() && $order->getPaymentModuleId() == Paypal::getModuleId()) {
  86.                 $event->stopPropagation();
  87.             }
  88.         }
  89.     }
  90.     /**
  91.      * Checks if order payment module is paypal and if order new status is paid, send an email to the customer.
  92.      *
  93.      * @param OrderEvent $event
  94.      */
  95.     public function updateStatus(OrderEvent $event)
  96.     {
  97.         $order $event->getOrder();
  98.         if ($order->isPaid() && $order->getPaymentModuleId() === Paypal::getModuleId()) {
  99.             if (Paypal::getConfigValue('send_payment_confirmation_message')) {
  100.                 $this->mailer->sendEmailToCustomer(
  101.                     PayPal::CONFIRMATION_MESSAGE_NAME,
  102.                     $order->getCustomer(),
  103.                     [
  104.                         'order_id'  => $order->getId(),
  105.                         'order_ref' => $order->getRef()
  106.                     ]
  107.                 );
  108.             }
  109.             // Send confirmation email if required.
  110.             if (Paypal::getConfigValue('send_confirmation_message_only_if_paid')) {
  111.                 $this->dispatcher->dispatch($eventTheliaEvents::ORDER_SEND_CONFIRMATION_EMAIL);
  112.             }
  113.                 $this->dispatcher->dispatch($eventTheliaEvents::ORDER_SEND_NOTIFICATION_EMAIL);
  114.         }
  115.     }
  116.     /**
  117.      * @param OrderEvent $event
  118.      * @throws \Exception
  119.      */
  120.     public function checkPayPalMethod(OrderEvent $event)
  121.     {
  122.         //First be sure that there is no OLD CREDIT card saved in paypal_cart because of fatal error
  123.         $payPalCartEvent = new PayPalCartEvent($this->payPalPaymentService->getCurrentPayPalCart());
  124.         $this->dispatcher->dispatch($payPalCartEventPayPalEvents::PAYPAL_CART_DELETE);
  125.         $postedData $this->requestStack->getCurrentRequest()->get('thelia_order_payment');
  126.         if (isset($postedData[PayPalFormFields::FIELD_PAYMENT_MODULE]) && PayPal::getModuleId() === $event->getOrder()->getPaymentModuleId()) {
  127.             $this->usePayPalMethod($postedData);
  128.         }
  129.     }
  130.     /**
  131.      * @param OrderEvent $event
  132.      */
  133.     public function recursivePayment(OrderEvent $event)
  134.     {
  135.         $this->payPalAgreementService->duplicateOrder($event->getOrder());
  136.         if (PayPal::getConfigValue('send_recursive_message')) {
  137.             $this->mailer->sendEmailToCustomer(
  138.                 PayPal::RECURSIVE_MESSAGE_NAME,
  139.                 $event->getOrder()->getCustomer(),
  140.                 [
  141.                     'order_id'  => $event->getOrder()->getId(),
  142.                     'order_ref' => $event->getOrder()->getRef()
  143.                 ]
  144.             );
  145.         }
  146.     }
  147.     /**
  148.      * @param array $postedData
  149.      */
  150.     protected function usePayPalMethod($postedData = [])
  151.     {
  152.         if (isset($postedData[PayPalFormFields::FIELD_PAYPAL_METHOD])) {
  153.             $payPalMethod $postedData[PayPalFormFields::FIELD_PAYPAL_METHOD];
  154.             switch ($payPalMethod) {
  155.                 case PayPal::PAYPAL_METHOD_CREDIT_CARD:
  156.                     $this->usePayPalCreditCardMethod($postedData);
  157.                     break;
  158.                 case PayPal::PAYPAL_METHOD_PLANIFIED_PAYMENT:
  159.                     $this->usePayPalPlanifiedPaymentMethod($postedData);
  160.                     break;
  161.             }
  162.         }
  163.     }
  164.     /**
  165.      * @param array $postedData
  166.      * @throws \Exception
  167.      */
  168.     protected function usePayPalCreditCardMethod($postedData = [])
  169.     {
  170.         if ($this->isValidPaidByPayPalCreditCard($postedData)) {
  171.             //save credit card in cart because we will need it in pay() method for payment module
  172.             $creditCardId $this->payPalPaymentService->getPayPalCreditCardId(
  173.                 $postedData[PayPalCreditCardType::TYPE_NAME][PayPalFormFields::FIELD_CARD_TYPE],
  174.                 $postedData[PayPalCreditCardType::TYPE_NAME][PayPalFormFields::FIELD_CARD_NUMBER],
  175.                 $postedData[PayPalCreditCardType::TYPE_NAME][PayPalFormFields::FIELD_CARD_EXPIRE_MONTH],
  176.                 $postedData[PayPalCreditCardType::TYPE_NAME][PayPalFormFields::FIELD_CARD_EXPIRE_YEAR],
  177.                 $postedData[PayPalCreditCardType::TYPE_NAME][PayPalFormFields::FIELD_CARD_CVV]
  178.             );
  179.             $payPalCart $this->payPalPaymentService->getCurrentPayPalCart();
  180.             $payPalCart->setCreditCardId($creditCardId);
  181.             $payPalCartEvent = new PayPalCartEvent($payPalCart);
  182.             $this->dispatcher->dispatch($payPalCartEventPayPalEvents::PAYPAL_CART_UPDATE);
  183.         }
  184.     }
  185.     /**
  186.      * @param array $postedData
  187.      * @return bool
  188.      */
  189.     protected function isValidPaidByPayPalCreditCard($postedData = [])
  190.     {
  191.         $isValid false;
  192.         if (isset($postedData[PayPalCreditCardType::TYPE_NAME][PayPalFormFields::FIELD_CARD_TYPE]) && $this->isNotBlank($postedData[PayPalCreditCardType::TYPE_NAME][PayPalFormFields::FIELD_CARD_TYPE]) &&
  193.             isset($postedData[PayPalCreditCardType::TYPE_NAME][PayPalFormFields::FIELD_CARD_NUMBER]) && $this->isNotBlank($postedData[PayPalCreditCardType::TYPE_NAME][PayPalFormFields::FIELD_CARD_NUMBER]) &&
  194.             isset($postedData[PayPalCreditCardType::TYPE_NAME][PayPalFormFields::FIELD_CARD_EXPIRE_MONTH]) && $this->isNotBlank($postedData[PayPalCreditCardType::TYPE_NAME][PayPalFormFields::FIELD_CARD_EXPIRE_MONTH]) &&
  195.             isset($postedData[PayPalCreditCardType::TYPE_NAME][PayPalFormFields::FIELD_CARD_EXPIRE_YEAR]) && $this->isNotBlank($postedData[PayPalCreditCardType::TYPE_NAME][PayPalFormFields::FIELD_CARD_EXPIRE_YEAR]) &&
  196.             isset($postedData[PayPalCreditCardType::TYPE_NAME][PayPalFormFields::FIELD_CARD_CVV]) && $this->isNotBlank($postedData[PayPalCreditCardType::TYPE_NAME][PayPalFormFields::FIELD_CARD_CVV])) {
  197.             $isValid true;
  198.         }
  199.         return $isValid;
  200.     }
  201.     /**
  202.      * @param array $postedData
  203.      */
  204.     protected function usePayPalPlanifiedPaymentMethod($postedData = [])
  205.     {
  206.         if (isset($postedData[PayPalFormFields::FIELD_PAYPAL_PLANIFIED_PAYMENT]) &&
  207.             $this->isNotBlank($postedData[PayPalFormFields::FIELD_PAYPAL_PLANIFIED_PAYMENT])) {
  208.             $payPalCart $this->payPalPaymentService->getCurrentPayPalCart();
  209.             $payPalCart->setPlanifiedPaymentId($postedData[PayPalFormFields::FIELD_PAYPAL_PLANIFIED_PAYMENT]);
  210.             $payPalCartEvent = new PayPalCartEvent($payPalCart);
  211.             $this->dispatcher->dispatch($payPalCartEventPayPalEvents::PAYPAL_CART_UPDATE);
  212.         }
  213.     }
  214.     /**
  215.      * @param $value
  216.      * @return bool
  217.      */
  218.     protected function isNotBlank($value)
  219.     {
  220.         if (false === $value || (empty($value) && '0' != $value)) {
  221.             return false;
  222.         }
  223.         return true;
  224.     }
  225.     /**
  226.      * @return array The event names to listen to
  227.      */
  228.     public static function getSubscribedEvents()
  229.     {
  230.         return [
  231.             TheliaEvents::ORDER_UPDATE_STATUS => [
  232.                 ['CancelPayPalTransaction'128],
  233.                 ['updateStatus'128],
  234.             ],
  235.             TheliaEvents::ORDER_SEND_CONFIRMATION_EMAIL => ['sendConfirmationEmail'129],
  236.             TheliaEvents::ORDER_SEND_NOTIFICATION_EMAIL => ['sendConfirmationEmail'129],
  237.             TheliaEvents::ORDER_SET_PAYMENT_MODULE => ['checkPayPalMethod'120],
  238.             PayPalEvents::PAYPAL_RECURSIVE_PAYMENT_CREATE => ['recursivePayment'128]
  239.         ];
  240.     }
  241. }