local/modules/PayPal/EventListeners/Form/TheliaOrderPaymentForm.php line 68

  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\Form;
  24. use PayPal\Form\PayPalFormFields;
  25. use PayPal\Form\Type\PayPalCreditCardType;
  26. use PayPal\Model\PaypalPlanifiedPayment;
  27. use PayPal\Model\PaypalPlanifiedPaymentQuery;
  28. use PayPal\PayPal;
  29. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  30. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  31. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  32. use Symfony\Component\HttpFoundation\RequestStack;
  33. use Thelia\Core\Event\TheliaEvents;
  34. use Thelia\Core\Event\TheliaFormEvent;
  35. use Thelia\Core\HttpFoundation\Session\Session;
  36. use Thelia\Core\Translation\Translator;
  37. use Thelia\Model\Cart;
  38. use Thelia\Model\Country;
  39. use Thelia\Model\Order;
  40. /**
  41.  * Class TheliaOrderPaymentForm
  42.  * @package PayPal\EventListeners\Form
  43.  */
  44. class TheliaOrderPaymentForm implements EventSubscriberInterface
  45. {
  46.     /** @var RequestStack */
  47.     protected $requestStack;
  48.     /** @var EventDispatcherInterface */
  49.     protected $dispatcher;
  50.     /**
  51.      * TheliaOrderPaymentForm constructor.
  52.      * @param RequestStack $requestStack
  53.      */
  54.     public function __construct(RequestStack $requestStackEventDispatcherInterface $dispatcher)
  55.     {
  56.         $this->requestStack $requestStack;
  57.         $this->dispatcher $dispatcher;
  58.     }
  59.     /**
  60.      * @param TheliaFormEvent $event
  61.      */
  62.     public function afterBuildTheliaOrderPayment(TheliaFormEvent $event)
  63.     {
  64.         $event->getForm()->getFormBuilder()
  65.             ->add(
  66.                 PayPalFormFields::FIELD_PAYPAL_METHOD,
  67.                 ChoiceType::class,
  68.                 [
  69.                     'choices' => [
  70.                         PayPal::PAYPAL_METHOD_PAYPAL => PayPal::PAYPAL_METHOD_PAYPAL,
  71.                         PayPal::PAYPAL_METHOD_EXPRESS_CHECKOUT => PayPal::PAYPAL_METHOD_EXPRESS_CHECKOUT,
  72.                         PayPal::PAYPAL_METHOD_CREDIT_CARD => PayPal::PAYPAL_METHOD_CREDIT_CARD,
  73.                         PayPal::PAYPAL_METHOD_PLANIFIED_PAYMENT => PayPal::PAYPAL_METHOD_PLANIFIED_PAYMENT
  74.                     ],
  75.                     'label' => Translator::getInstance()->trans('PayPal method', [], PayPal::DOMAIN_NAME),
  76.                     'label_attr' => ['for' => PayPalFormFields::FIELD_PAYPAL_METHOD],
  77.                     'required' => false,
  78.                 ]
  79.             )
  80.             ->add(
  81.                 PayPalCreditCardType::TYPE_NAME,
  82.                 PayPalCreditCardType::class,
  83.                 [
  84.                     'label_attr' => [
  85.                         'for' => PayPalCreditCardType::TYPE_NAME
  86.                     ]
  87.                 ]
  88.             )
  89.             ->add(
  90.                 PayPalFormFields::FIELD_PAYPAL_PLANIFIED_PAYMENT,
  91.                 ChoiceType::class,
  92.                 [
  93.                     'choices' => $this->getAllowedPlanifiedPayments(),
  94.                     'choice_label' => function ($value$key$index) {
  95.                         return $value->getTitle();
  96.                     },
  97.                     'choice_value' => function ($value) {
  98.                         if ($value !== null) {
  99.                             return $value->getId();
  100.                         }
  101.                         return null;
  102.                     },
  103.                     "required" => false,
  104.                     'empty_data'  => null,
  105.                     'label' => Translator::getInstance()->trans('Frequency', [], PayPal::DOMAIN_NAME),
  106.                     'label_attr' => ['for' => PayPalFormFields::FIELD_PAYPAL_PLANIFIED_PAYMENT],
  107.                 ]
  108.             )
  109.         ;
  110.     }
  111.     /**
  112.      * @return array|mixed|\Propel\Runtime\Collection\ObjectCollection
  113.      */
  114.     protected function getAllowedPlanifiedPayments()
  115.     {
  116.         /** @var Session $session */
  117.         $session $this->requestStack->getCurrentRequest()->getSession();
  118.         /** @var \Thelia\Model\Lang $lang */
  119.         $lang $session->getLang();
  120.         /** @var Cart $cart */
  121.         $cart $session->getSessionCart($this->dispatcher);
  122.         /** @var Order $order */
  123.         $order $session->get('thelia.order');
  124.         $country Country::getDefaultCountry();
  125.         $planifiedPayments = (new PaypalPlanifiedPaymentQuery())->joinWithI18n($lang->getLocale())->find();
  126.         if (null !== $cart && null !== $order && null !== $country) {
  127.             $totalAmount $cart->getTaxedAmount($country) + (float)$order->getPostage();
  128.             $restrictedPlanifiedAmounts = [];
  129.             /** @var PaypalPlanifiedPayment $planifiedPayment */
  130.             foreach ($planifiedPayments as $planifiedPayment) {
  131.                 if ($planifiedPayment->getMinAmount() > && $planifiedPayment->getMinAmount() > $totalAmount) {
  132.                     continue;
  133.                 }
  134.                 if ($planifiedPayment->getMaxAmount() > && $planifiedPayment->getMaxAmount() < $totalAmount) {
  135.                     continue;
  136.                 }
  137.                 $restrictedPlanifiedAmounts[] = $planifiedPayment;
  138.             }
  139.             $planifiedPayments $restrictedPlanifiedAmounts;
  140.         }
  141.         return $planifiedPayments;
  142.     }
  143.     /**
  144.      * @return array The event names to listen to
  145.      */
  146.     public static function getSubscribedEvents()
  147.     {
  148.         return [
  149.             TheliaEvents::FORM_AFTER_BUILD '.thelia_order_payment' => ['afterBuildTheliaOrderPayment'128]
  150.         ];
  151.     }
  152. }