src/Controller/TunnelController.php line 188

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Page;
  4. use App\Entity\User;
  5. use App\Entity\Country;
  6. use App\Entity\PromoCode;
  7. use App\Helper\GeoPlugin;
  8. use App\Entity\UserPayment;
  9. use App\Helper\IpToCountry;
  10. use App\Entity\ReminderUser;
  11. use App\Entity\UserTempData;
  12. use App\Helper\FacebookSAPI;
  13. use App\Entity\PaymentMethod;
  14. use App\Service\MailchimpAPI;
  15. use App\Service\MailingTransac;
  16. use Doctrine\Persistence\ManagerRegistry;
  17. use Symfony\Bundle\SecurityBundle\Security;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Karser\Recaptcha3Bundle\Form\Recaptcha3Type;
  22. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  23. use Symfony\Component\Routing\Generator\UrlGenerator;
  24. use Symfony\Component\Form\Extension\Core\Type\TextType;
  25. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  26. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  27. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  28. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  29. use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3;
  30. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  31. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  32. #[Route('/{_locale}')]
  33. class TunnelController extends AbstractController
  34. {
  35.     public $doctrine;
  36.     private $entityManager;
  37.     public function __construct(ManagerRegistry $doctrine)
  38.     {
  39.         $this->doctrine $doctrine;
  40.         $this->entityManager $this->doctrine->getManager();
  41.     }
  42.     
  43.     #[Route('/default'name'tunnel_default')]
  44.     public function index(): Response
  45.     {
  46.         return $this->render('tunn/index.html.twig', [
  47.             'controller_name' => 'DefaultController',
  48.         ]);
  49.     }
  50.     
  51.     public static function detectCountry(Request $request)
  52.     {
  53.         $session $request->getSession();
  54.         $country = new IpToCountry();
  55.         $country->setUserCountry($request$session);
  56.     }
  57.     
  58.     public static function setPromoCode(Request $request$session$doctrine){
  59.         if($request->getMethod() == 'POST'){
  60.             $promoCode $request->request->get('cp',false);
  61.             if($promoCode !== false){
  62.                 $promoCode addslashes(str_replace(' '''trim(mb_strtolower($promoCode'UTF-8'))));
  63.                 
  64.                 $code $doctrine->getRepository(PromoCode::class)->getPromoCode($promoCode);
  65.                 if($code != null){
  66.                     $session->set('promo_code',$promoCode);
  67.                     $session->set('url_promo_code',true);
  68.                 }else{
  69.                     $session->set('promo_code','');
  70.                     $session->set('url_promo_code',false);
  71.                 }
  72.             }
  73.         }
  74.     }
  75.     protected function getUserIsoCode($request$session)
  76.     {
  77.         $ipAddress $request->getClientIp();
  78.         if ($session->get('country_iso',null) == null) {
  79.             try {
  80.                 $geo = new GeoPlugin();
  81.                 $geo->locate($ipAddress);
  82.                 $countryCode $geo->countryCode;
  83.                 if($countryCode == null)
  84.                     $countryCode 'FR';
  85.             } catch (\Exception $e) {
  86.                 $countryCode 'FR';
  87.             }
  88.             $session->set('country_iso'$countryCode);
  89.         } else {
  90.             $countryCode $session->get('country_iso',null);
  91.         }
  92.         return $countryCode;
  93.     }
  94.     static function findUser($controller$session)
  95.     {
  96.         $user $controller->getUser();
  97.         if($user != null
  98.         {
  99.             return $user;
  100.         } else {
  101.             $tempId $session->get('user_data_id',null);
  102.             if($tempId != null
  103.             {
  104.                 $user $controller->doctrine->getRepository(UserTempData::class)->findOneById($tempId);
  105.                 if($user != null)
  106.                 {
  107.                     return $user;
  108.                 }
  109.             }
  110.         }
  111.         return null;
  112.         //retourne un user ou un tempUserData
  113.     }
  114.     
  115.     #[Route('/inscription/gratuit.html'name'tunnel_simple_register')]
  116.     public function registerSimple(Request $requestUserPasswordHasherInterface $passwordHasherMailchimpAPI $mailchimpSecurity $security): Response
  117.     {
  118.         $session $request->getSession();
  119.         $user = new User(); //on crée un user vide pour récupérer les informations
  120.         $form $this->createFormBuilder($user, ['validation_groups' => ['Default''registration'], 'translation_domain' => 'tunnel'])
  121.             ->add('username'TextType::class, ['label' => 'Choisissez un pseudo'])
  122.             ->add('email'RepeatedType::class, [
  123.                 'type' => EmailType::class,
  124.                 'required' => true,
  125.                 'first_options'  => ['label' => 'Entrez votre e-mail'],
  126.                 'second_options' => ['label' => 'Répétez votre e-mail'],
  127.             ])
  128.             ->add('plainPassword'PasswordType::class, ['attr' => ['class' => 'w-75'], 'label' => 'Choisissez un mot de passe'])
  129.             ->add('captcha'Recaptcha3Type::class, [
  130.                 'constraints' => new Recaptcha3(),
  131.                 'action_name' => 'subscriptionFree',
  132.                 'locale' => $request->getLocale(),
  133.             ])
  134.             ->add('submit'SubmitType::class, ['label' => 'Continuer'])
  135.             ->getForm();
  136.         $form->handleRequest($request);
  137.         if ($form->isSubmitted() && $form->isValid())
  138.         {
  139.             $user $form->getData();
  140.             $password $user->getPlainPassword();
  141.             //hasher le password
  142.             $hashedPassword $passwordHasher->hashPassword(
  143.                 $user,
  144.                 $password
  145.             );
  146.             $user->setPassword($hashedPassword);
  147.             $user->setLocale($request->getLocale());
  148.             
  149.             $session $request->getSession();
  150.             $countryCode $this->getUserIsoCode($request$session);
  151.             $defaultCountry $this->doctrine->getRepository(Country::class)->findOneByIsoCode($countryCode);
  152.             if(!$defaultCountry || $countryCode == "FR")
  153.                 $defaultCountry $this->doctrine->getRepository(Country::class)->findOneByCanonicalName('france-metropolitaine');
  154.             
  155.             $user->setEcountry($defaultCountry);
  156.             $this->entityManager->persist($user);
  157.             $this->entityManager->flush();
  158.     
  159.             $session $request->getSession();
  160.             $session->set('user_data_id',$user->getId());
  161.     
  162.             //FB API !
  163.             $event_id $this->sendFBMsgRegister($user);
  164.             $session->set('reg_event_id',$event_id);
  165.     
  166.             $this->doctrine->getRepository(ReminderUser::class)->addEmail($user->getEmail(),$user->getUsername(), $request->getLocale());
  167.     
  168.             $mailchimp->addUser($user);
  169.             $security->login($user'security.authenticator.form_login.main');
  170.             return $this->redirectToRoute('tunnel_simple_confirm');
  171.         }
  172.         return $this->renderForm('tunnel/simple/register.html.twig', [
  173.             'form' => $form
  174.         ]);
  175.     }
  176.     
  177.     #[Route('/inscription/essai.html'name'tunnel_simple_confirm')]
  178.     public function registerSimpleConfirm(Request $request): Response
  179.     {
  180.         //confirmer l'inscription 
  181.         //proposer abonnement ou tester gratuitement
  182.         $user $this->getUser();
  183.         return $this->render('tunnel/simple/confirm.html.twig', [
  184.             'user' => $user,
  185.         ]);
  186.     }
  187.     
  188.     #[Route('/inscription/inscription.html'name'tunnel_register')]
  189.     public function register(Request $requestUserPasswordHasherInterface $passwordHasherMailchimpAPI $mailchimp): Response
  190.     {
  191.         $session $request->getSession();
  192.         $user self::findUser($this$session);
  193.         //$user = $this->getUser();
  194.         if($user instanceof User
  195.         {
  196.             //on passe la phase registration
  197.             return $this->redirectToRoute('tunnel_subscription_choice');
  198.             //return $this->redirectToRoute('tunnel_billing');
  199.         } else {
  200.             //ON SIMPLIFIE L'INSCRIPTION !! 
  201.             return $this->redirectToRoute('tunnel_simple_register');
  202.         }
  203.         $user = new User(); //on crée un user vide pour récupérer les informations
  204.         $form $this->createFormBuilder($user, ['validation_groups' => ['Default''registration'], 'translation_domain' => 'tunnel'])
  205.             ->add('username'TextType::class, ['label' => 'Choisissez un pseudo'])
  206.             ->add('email'RepeatedType::class, [
  207.                 'type' => EmailType::class,
  208.                 'required' => true,
  209.                 'first_options'  => ['label' => 'Entrez votre e-mail'],
  210.                 'second_options' => ['label' => 'Répétez votre e-mail'],
  211.             ])
  212.             //->add('emailConfirm', EmailType::class, ['label' => 'Répétez votre e-mail'])
  213.             ->add('plainPassword'PasswordType::class, ['attr' => ['class' => 'w-75'], 'label' => 'Choisissez un mot de passe'])
  214.             ->add('submit'SubmitType::class, ['label' => 'Continuer'])
  215.             ->getForm();
  216.         $form->handleRequest($request);
  217.         if ($form->isSubmitted() && $form->isValid())
  218.         {
  219.             $user $form->getData();
  220.             
  221.             $email $user->getEmail();
  222.             $password $user->getPlainPassword();
  223.             //hasher le password
  224.             $hashedPassword $passwordHasher->hashPassword(
  225.                 $user,
  226.                 $password
  227.             );
  228.             $user->setPassword($hashedPassword);
  229.             $currentUser $this->doctrine->getRepository(UserTempData::class)->findOneByEmail($email);
  230.             if($currentUser == null)
  231.                 $currentUser = new UserTempData();
  232.             
  233.             $currentUser->setUsername($user->getUsername());
  234.             $currentUser->setEmail($user->getEmail());
  235.             $currentUser->setPassword($hashedPassword);
  236.             $currentUser->setLocale($request->getLocale());
  237.             $session $request->getSession();
  238.             $countryCode $this->getUserIsoCode($request$session);
  239.             $defaultCountry $this->doctrine->getRepository(Country::class)->findOneByIsoCode($countryCode);
  240.             if(!$defaultCountry || $countryCode == "FR")
  241.                 $defaultCountry $this->doctrine->getRepository(Country::class)->findOneByCanonicalName('france-metropolitaine');
  242.             //$currentUser->setSiteUrl(IpToCountry::getExtension($session));
  243.             $currentUser->setEcountry($defaultCountry);
  244.             $this->entityManager->persist($currentUser);
  245.             $this->entityManager->flush();
  246.             $session $request->getSession();
  247.             $session->set('user_data_id',$currentUser->getId());
  248.             //FB API !
  249.             $event_id $this->sendFBMsgRegister($user);
  250.             $session->set('reg_event_id',$event_id);
  251.             $this->doctrine->getRepository(ReminderUser::class)->addEmail($user->getEmail(),$user->getUsername(), $request->getLocale());
  252.             $mailchimp->addTempUser($currentUser);
  253.             
  254.             //si on est passé par la page prix.html
  255.             $forceChoice $session->get('forceChoice'false);
  256.             if($forceChoice) {
  257.                 //cela ne fonctionne qu'une fois
  258.                 $session->set('forceChoice'false);
  259.                 return $this->redirectToRoute('tunnel_billing');
  260.             } else {
  261.                 return $this->redirectToRoute('tunnel_subscription_choice');
  262.             }
  263.             
  264.         }
  265.         return $this->renderForm('tunnel/register.html.twig', [
  266.             'form' => $form
  267.         ]);
  268.     }
  269.     
  270.     #[Route('/inscription/facturation.html'name'tunnel_billing')]
  271.     public function billing(Request $request): Response
  272.     {
  273.         $session $request->getSession();
  274.         $currentUser self::findUser($this$session);
  275.         if($currentUser == null)
  276.         {
  277.             //aucun user connecté ou tempUser
  278.             return $this->redirectToRoute('tunnel_register');
  279.         }
  280.         
  281.         $user = new User(); //on crée un user vide pour récupérer les informations
  282.         $countryCode $this->getUserIsoCode($request$session);
  283.         $defaultCountry $this->doctrine->getRepository(Country::class)->findOneByIsoCode($countryCode);
  284.         if(!$defaultCountry || $countryCode == "FR")
  285.             $defaultCountry $this->doctrine->getRepository(Country::class)->findOneByCanonicalName('france-metropolitaine');
  286.         
  287.         $user->setFirstName($currentUser->getFirstName());
  288.         $user->setLastName($currentUser->getLastName());
  289.         $user->setAddress($currentUser->getAddress());
  290.         $user->setZipCode($currentUser->getZipCode());
  291.         $user->setCity($currentUser->getCity());
  292.         $user->setEcountry($currentUser->getEcountry());
  293.         $user->setECountry($defaultCountry);
  294.         $form $this->createFormBuilder($user, ['validation_groups' => ['facturation'], 'translation_domain' => 'tunnel'])
  295.             ->add('firstName'TextType::class, ['label' => 'Nom'])
  296.             ->add('lastName'TextType::class, ['label' => 'Prénom'])
  297.             ->add('address'TextType::class, ['label' => 'Adresse'])
  298.             ->add('zipCode'TextType::class, ['label' => 'Code postal'])
  299.             ->add('city'TextType::class, ['label' => 'Ville'])
  300.             ->add('ecountry'EntityType::class, ['class' => Country::class, 'label' => 'Pays''choice_label' => 'canonicalName''choice_translation_domain' => 'countries'])
  301.             ->add('submit'SubmitType::class, ['label' => 'Continuer'])
  302.         ->getForm();
  303.         
  304.         $form->handleRequest($request);
  305.         if ($form->isSubmitted() && $form->isValid())
  306.         {
  307.             $user $form->getData();
  308.             $currentUser->setFirstName($user->getFirstName());
  309.             $currentUser->setLastName($user->getLastName());
  310.             $currentUser->setAddress($user->getAddress());
  311.             $currentUser->setZipCode($user->getZipCode());
  312.             $currentUser->setCity($user->getCity());
  313.             $currentUser->setEcountry($user->getEcountry());
  314.             
  315.             $this->entityManager->flush();
  316.             return $this->redirectToRoute('tunnel_payment');
  317.         }
  318.         return $this->renderForm('tunnel/billing.html.twig', [
  319.             'form' => $form
  320.         ]);
  321.     }
  322.     
  323.     #[Route('/inscription/reglement.html'name'tunnel_payment')]
  324.     public function payment(Request $request): Response
  325.     {
  326.         $session $request->getSession();
  327.         $currentUser self::findUser($this$session);
  328.         if($currentUser == null)
  329.         {
  330.             //aucun user connecté ou tempUser
  331.             return $this->redirectToRoute('tunnel_register');
  332.         }
  333.         $type 'cb';
  334.         if($request->getMethod() == 'POST')
  335.         {
  336.             $type $request->get('type','cb');
  337.         }
  338.         $currentChoice $session->get('formule'null);
  339.         if($currentChoice == null)
  340.         {
  341.             return $this->redirectToRoute('default_price');
  342.         }
  343.         $payment_id $this->doctrine->getRepository(PaymentMethod::class)->getChoiceSubscription($currentChoice,$session);
  344.         $paymentMethod $this->doctrine->getRepository(PaymentMethod::class)->findOneById($payment_id);
  345.         if($paymentMethod != null && $paymentMethod->getType() != PaymentMethod::TYPE_CODECADEAU)
  346.         {
  347.             $prixBase $prix $paymentMethod->getValeur();
  348.             
  349.             $reduction self::getReduction($session$this->doctrine);
  350.             $url_promo_code $session->get('url_promo_code',false);
  351.             $prix self::getPriceReduction($paymentMethod,$prix,$reduction,$url_promo_code);
  352.             
  353.             if(!$url_promo_code || ($url_promo_code && $paymentMethod->getMontant() != 1)){
  354.                 $displayReduction $reduction !== false;
  355.             }else{
  356.                 $displayReduction false;
  357.             }
  358.             $montant $paymentMethod->getMontant();
  359.             $prixMois round($prix $montant2PHP_ROUND_HALF_DOWN);
  360.             $prix number_format($prix2','' ');
  361.             $prixBase number_format($prixBase2','' ');
  362.             $prixMois number_format($prixMois2','' ');
  363.             
  364.             $currency IpToCountry::getCurrency($session);
  365.             
  366.             $time $paymentMethod->getMontant();
  367.             return $this->render('tunnel/payment.html.twig', [
  368.                 'user' => $currentUser,
  369.                 'paymentMethod' => $session->get('formule'),
  370.                 'paymentType' => $paymentMethod->getType(),
  371.                 'paymentDuration' => $montant,
  372.                 'currency' => $currency,
  373.                 'nom' => $currentUser->getLastName(),
  374.                 'prenom' => $currentUser->getFirstName(),
  375.                 'adresse' => $currentUser->getAddress(),
  376.                 'ville' => $currentUser->getCity(),
  377.                 'zipCode' => $currentUser->getZipCode(),
  378.                 'pays' => $currentUser->getCountry(),
  379.                 'schoolName' => $currentUser->getSchoolName(),
  380.                 'prix' => $prix,
  381.                 'prixBase' => $prixBase,
  382.                 'prixMois' => $prixMois,
  383.                 'time' => $time,
  384.                 'libelle' => $paymentMethod->getName(),
  385.                 'displayReduction' => $displayReduction,
  386.                 'type' => $type,
  387.             ]);
  388.         }
  389.     }
  390.     
  391.     #[Route('/delayed_payment.html'name'delayed_payment')]
  392.     public function bankTransferSubscription(Request $request): Response
  393.     {
  394.         $session $request->getSession();
  395.         $currentUser self::findUser($this$session);
  396.         if($currentUser == null)
  397.         {
  398.             //aucun user connecté ou tempUser
  399.             return $this->redirectToRoute('tunnel_register');
  400.         }
  401.         $formule $request->get('formule',0);
  402.         $type $request->get('paymentMethod',0);
  403.         $isTransfer $request->get('transfer',null) != null;
  404.         $payment_id 0;
  405.         $payment_id $this->doctrine->getRepository(PaymentMethod::class)->getChoiceSubscription($formule,$session);
  406.         $paymentMethod $this->doctrine->getRepository(PaymentMethod::class)->findOneById($payment_id);
  407.         if($paymentMethod != null)
  408.         {
  409.             switch ($type){
  410.                 case "cheque":
  411.                     $route 'tunnel_subscription_check';
  412.                 break;
  413.                 case "virement":
  414.                     $route 'tunnel_subscription_transfer';
  415.                 break;
  416.                 /*case "paiement-cadhoc":
  417.                     $route = 'envoi-cadhoc';
  418.                     $type = 'cadhoc';
  419.                 break;*/
  420.             }
  421.             $time '' time() . '';
  422.             $sReference base_convert($currentUser->getId(), 1036) . '-' base_convert($time1036);
  423.         
  424.             $sMontant $paymentMethod->getValeur();
  425.             $reduction self::getReduction($session$this->doctrine);
  426.             $displayReduction $reduction !== false;
  427.             
  428.             $url_promo_code $session->get('url_promo_code',false);
  429.             $sMontant $this->getPriceReduction($paymentMethod,$sMontant,$reduction,$url_promo_code);
  430.             $isTempUser = !($currentUser instanceof User);
  431.             $searchParam $isTempUser 'userData' 'user';
  432.             //pour éviter de créer des payment en masse, on cherche un paiement précédent non validé fait par la personne
  433.             $payment $this->doctrine->getRepository(UserPayment::class)->findOneBy([
  434.                 $searchParam => $currentUser->getId(),
  435.                 'type' => $type,
  436.                 'waitingPayment' => true
  437.             ]);
  438.             if($payment == null)
  439.                 $payment = new UserPayment();
  440.             if($isTempUser)
  441.                 $payment->setUserData($currentUser);
  442.             else 
  443.                 $payment->setUser($currentUser);
  444.             $payment->setLocale($currentUser->getLocale());
  445.             $payment->setECountry($currentUser->getECountry());
  446.             $payment->setFirstName($currentUser->getFirstName());
  447.             $payment->setLastName($currentUser->getLastName());
  448.             $payment->setMontant($sMontant);
  449.             $payment->setMontantReel($sMontant/$paymentMethod->getBaseReel());
  450.             $payment->setPayment($paymentMethod);
  451.             $payment->setReference($sReference);
  452.             $payment->setWaitingPayment(true);
  453.             $payment->setIsCredit(false);
  454.             $payment->setCreatedAt(new \DateTimeImmutable());
  455.             $payment->setType($type);
  456.             $payment->setOccurence(1);
  457.             $payment->setStatut("waiting");
  458.             
  459.             $code $this->doctrine->getRepository(PromoCode::class)->getPromoCode($session->get('promo_code',null));
  460.             $payment->setPromoCode($code);
  461.             
  462.             $this->entityManager->persist($payment);
  463.             $this->entityManager->flush();
  464.             
  465.             $country_code $session->get('payment_code');
  466.             $currency $this->getCurrency($country_code);
  467.             return $this->render('tunnel/form_cheque.html.twig', [
  468.                 'montant' => $sMontant,
  469.                 'time' => $paymentMethod->getMontant(), 
  470.                 'reference' => $sReference,
  471.                 'currency' => $currency,
  472.                 'route' => $route,
  473.             ]);
  474.         }
  475.     }
  476.     
  477.     #[Route('/virement-bancaire.html'name'tunnel_subscription_transfer')]
  478.     public function sendBankTransfer(Request $requestMailingTransac $mailer): Response
  479.     {
  480.         $session $request->getSession();
  481.         $currentUser self::findUser($this$session);
  482.         if($currentUser == null)
  483.         {
  484.             //aucun user connecté ou tempUser
  485.             return $this->redirectToRoute('tunnel_register');
  486.         }
  487.         
  488.         $montant $request->get('montant',0);
  489.         $currency $request->get('currency',"");
  490.         $reference $request->get('reference',"");
  491.         $time intval($request->get('time',0));
  492.         $montant number_format($montant,2);
  493.         $montant str_replace("."","$montant);
  494.         if($reference != '' && $session->get('ref_mail',null) != $reference)
  495.         {
  496.             $session->set('ref_mail',$reference);
  497.             $mailer->sendBankTransferMail($currentUser->getEmail(),$montant,$currency,$reference,$time,"",$currentUser);
  498.         }
  499.         return $this->render('tunnel/sendBankTransfer.html.twig', [
  500.             'montant' => $montant
  501.             'reference' => $reference,
  502.             'time' => $time,
  503.             'currency' => $currency,
  504.         ]);
  505.     }
  506.     
  507.     #[Route('/envoi-cheque.html'name'tunnel_subscription_check')]
  508.     public function sendCheck(Request $requestMailingTransac $mailer): Response
  509.     {
  510.         $session $request->getSession();
  511.         $currentUser self::findUser($this$session);
  512.         if($currentUser == null)
  513.         {
  514.             //aucun user connecté ou tempUser
  515.             return $this->redirectToRoute('tunnel_register');
  516.         }
  517.         $montant $request->get('montant',0);
  518.         $currency $request->get('currency',0);
  519.         $reference $request->get('reference',"");
  520.         $time intval($request->get('time',0));
  521.         //$time = $this->transformTime($time);
  522.         $montant number_format($montant,2);
  523.         $montant str_replace("."","$montant);
  524.         if($reference != '' && $session->get('ref_mail',null) != $reference){
  525.             $session->set('ref_mail',$reference);
  526.             $mailer->sendChequeMail($currentUser->getEmail(),$montant,$currency,$reference,$time,"",$currentUser);
  527.         }
  528.         return $this->render('tunnel/sendCheque.html.twig', [
  529.             'montant' => $montant
  530.             'reference' => $reference,
  531.             'time' => $time,
  532.             'currency' => $currency,
  533.         ]);
  534.     }
  535.     
  536.     #[Route('/abonnement-cb-reussi.html'name'tunnel_subscription_cb')]
  537.     #[Route('/abonnement-reussi.html'name'tunnel_subscription_confirmed')]
  538.     #[Route('/abonnement-paypal-reussi.html'name'tunnel_subscription_paypal')]
  539.     public function subscriptionConfirmed(Request $request): Response
  540.     {
  541.         $session $request->getSession();
  542.         $currentUser $this->getUser();
  543.         $value 0;
  544.         $currency 'EUR';
  545.         if($currentUser instanceof User && $currentUser->getSubscriptionConfirmed() != null)
  546.         {
  547.             $payment $this->doctrine->getRepository(UserPayment::class)->getLastConfirmedByUser($currentUser->getId(),false);
  548.             if($payment != null)
  549.             {
  550.                 $value intval(round($payment->getMontantReel()));
  551.                 $method $payment->getPayment();
  552.                 $currency $method->getCurrency();
  553.             }
  554.         }
  555.         return $this->render('tunnel/subscriptionConfirmed.html.twig', [
  556.             'currency' => $currency
  557.             'value' => $value,
  558.             'user' => $currentUser,
  559.         ]);
  560.     }
  561.     #[Route('/inscription/abonnement.html'name'tunnel_subscription_choice')]
  562.     public function chooseSubscription(Request $request): Response
  563.     {
  564.         $session $request->getSession();
  565.         if ($request->getMethod() == 'POST')
  566.         {
  567.             if($request->get('subscription',null) != null)
  568.             {
  569.                 $session->set('forceChoice'true);
  570.                 $session->set('formule'$request->get('subscription',null));
  571.                 $currentUser self::findUser($this$session);
  572.                 $this->sendFBMsgInitCheckout($currentUser);
  573.                 return $this->redirectToRoute('tunnel_billing');
  574.             }
  575.         }
  576.         self::detectCountry($request);
  577.         self::setPromoCode($request$session$this->doctrine);
  578.         $default 'choice3';
  579.         $choice $session->get('formule'$default);
  580.         $choice = ($choice) ? $choice $default;
  581.         $country_code $session->get('payment_code');
  582.     
  583.         $url_promo_code $session->get('url_promo_code',false);
  584.         $promoCode $session->get('promo_code',false);
  585.         $reduction self::getReduction($session$this->doctrine);
  586.         $displayReduction $reduction !== false;
  587.         $prices $this->doctrine->getRepository(PaymentMethod::class)->getPrices($country_code,$reduction,$url_promo_code);
  588.         $currency $this->getCurrency($country_code);
  589.         return $this->render('tunnel/price.html.twig', [
  590.             'prices' => $prices
  591.             'currency' => $currency
  592.             'choice' => $choice,
  593.             'displayReduction' => $displayReduction
  594.             'codeReduction' => $promoCode
  595.             'montantReduction' => floor($reduction),
  596.             'route' => 'tunnel_subscription_choice'
  597.             //'isPromoUrl' => $url_promo_code
  598.         ]);
  599.     }
  600.     
  601.     #[Route('/prix.html'name'default_price')]
  602.     public function prices(Request $request): Response
  603.     {
  604.         $session $request->getSession();
  605.         if ($request->getMethod() == 'POST')
  606.         {
  607.             if($request->get('subscription',null) != null)
  608.             {
  609.                 $session->set('forceChoice'true);
  610.                 $session->set('formule'$request->get('subscription',null));
  611.                 $currentUser self::findUser($this$session);
  612.                 if($currentUser instanceof User//tester si on est déjà enregistré ou pas
  613.                 {
  614.                     $this->sendFBMsgInitCheckout($currentUser);
  615.                     return $this->redirectToRoute('tunnel_billing');
  616.                 } else {
  617.                     $this->sendFBMsgInitCheckout($currentUser);
  618.                     return $this->redirectToRoute('tunnel_register');
  619.                 }
  620.             }
  621.         }
  622.         self::detectCountry($request);
  623.         self::setPromoCode($request$session$this->doctrine);
  624.         $default 'choice3';
  625.         $choice $session->get('formule'$default);
  626.         $choice = ($choice) ? $choice $default;
  627.         $country_code $session->get('payment_code');
  628.     
  629.         $url_promo_code $session->get('url_promo_code',false);
  630.         $promoCode $session->get('promo_code',false);
  631.         $reduction self::getReduction($session$this->doctrine);
  632.         $displayReduction $reduction !== false;
  633.         $prices $this->doctrine->getRepository(PaymentMethod::class)->getPrices($country_code,$reduction,$url_promo_code);
  634.         $currency $this->getCurrency($country_code);
  635.         return $this->render('tunnel/price.html.twig', [
  636.             'prices' => $prices
  637.             'currency' => $currency
  638.             'choice' => $choice,
  639.             'displayReduction' => $displayReduction
  640.             'codeReduction' => $promoCode
  641.             'montantReduction' => floor($reduction),
  642.             'route' => 'default_price'
  643.             //'isPromoUrl' => $url_promo_code
  644.         ]);
  645.     }
  646.     #[Route('/pop-cg.html'name'tunnel_cg')]
  647.     public function getContentCG(Request $request)
  648.     {
  649.         $locale $request->query->get('locale''fr');
  650.         $pageRepository $this->doctrine->getRepository(Page::class);
  651.         if( $content $pageRepository->getI18nPageByRoute('cgv'$locale))
  652.         {
  653.             return new Response($content->getContent());
  654.         }else{
  655.              throw $this->createNotFoundException();
  656.         }
  657.     }
  658.     
  659.     static function getPriceReduction($paymentMethod,$price,$reduction$url_promo_code){
  660.         if($reduction !== false)
  661.         {
  662.             //si on est pas arrivé via une url promo ou que l'on a choisit le paiement a 6 mois
  663.             if(!$url_promo_code || ($url_promo_code && $paymentMethod->getMontant() != 1)){
  664.                 if(is_float($reduction)){
  665.                     $price round($price * ($reduction/100), 2PHP_ROUND_HALF_DOWN);
  666.                 }else{
  667.                     $price $paymentMethod->getValeurPromo();
  668.                 }
  669.             }
  670.         }
  671.         return $price;
  672.     }
  673.     
  674.     static function getReduction($session$doctrine)
  675.     {
  676.         if($session != null){
  677.             $promoCode $session->get('promo_code',false);
  678.             if($promoCode != false){
  679.                 $code $doctrine->getRepository(PromoCode::class)->getPromoCode($promoCode);
  680.                 if($code != null) {
  681.                     return $code->getReduction();
  682.                 }
  683.             }
  684.         }
  685.         return false;
  686.     }
  687.     
  688.     protected function getCurrency($country_code){
  689.         switch($country_code){
  690.             case 'CA':
  691.                 $currency '$CAN';
  692.             break;
  693.             case 'CH':
  694.                 $currency "CHF";
  695.             break;
  696.             case 'MA':
  697.                 $currency "MAD";
  698.             break;
  699.             default:
  700.                 $currency "&euro;";
  701.             break;
  702.         }
  703.         return $currency;
  704.     }
  705.     
  706.     protected function sendFBMsgInitCheckout($user) {
  707.         try {
  708.             
  709.             $url $this->generateUrl('routeName', [], UrlGenerator::ABSOLUTE_URL);
  710.             
  711.             $fb = new FacebookSAPI($this->getParameter('fb.business_api'), $this->getParameter('fb.pixel_id'));
  712.             $data $fb->createCheckoutData($user$url);
  713.             $fb->sendRequest($data);
  714.         } catch(\Exception $e) {
  715.             
  716.         }
  717.     }
  718.     
  719.     protected function sendFBMsgRegister($user) {
  720.         try {
  721.             $url $this->generateUrl('tunnel_register', [], UrlGenerator::ABSOLUTE_URL);
  722.             
  723.             $fb = new FacebookSAPI($this->getParameter('fb.business_api'), $this->getParameter('fb.pixel_id'));
  724.             $data $fb->createRegisterData($user$url);
  725.             $fb->sendRequest($data);
  726.             
  727.             return $data["event_id"];
  728.         } catch(\Exception $e) {
  729.             
  730.         }
  731.     }
  732. }