src/Controller/Front/AbonnementPro/ProcessPaiementController.php line 206

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: Slim Sayari
  5.  * Date: 10/07/2019
  6.  * Time: 13:43
  7.  */
  8. namespace App\Controller\Front\AbonnementPro;
  9. use App\Entity\Abonnement;
  10. use App\Entity\AbonnementPay;
  11. use App\Entity\AbonnementPro;
  12. use App\Entity\AbonnementProCart;
  13. use App\Entity\Cart;
  14. use App\Entity\Customer;
  15. use App\Entity\Product;
  16. use App\Form\AdresseFacturationAbonnementType;
  17. use App\Services\AbonnementService;
  18. use App\Services\CartService;
  19. use App\Services\CheckoutService;
  20. use App\Services\FactureService;
  21. use App\Services\Notifications;
  22. use App\Services\OrderService;
  23. use App\Entity\Adresse;
  24. use PhpParser\Node\Expr\Clone_;
  25. use Symfony\Component\HttpFoundation\RedirectResponse;
  26. use Symfony\Component\Routing\Annotation\Route;
  27. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  28. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  29. use Twig\Environment;
  30. use Symfony\Component\HttpFoundation\Response;
  31. use Symfony\Component\HttpFoundation\Request;
  32. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  33. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  34. use Doctrine\ORM\EntityManagerInterface;
  35. use Symfony\Contracts\Translation\TranslatorInterface;
  36. use App\Services\Liseuse;
  37. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  38. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  39. use App\Services\Compte;
  40. /**
  41.  * @Route("/abonnement-pro")
  42.  */
  43. class ProcessPaiementController extends AbstractController
  44. {
  45.     private $em;
  46.     private $session;
  47.     private $params;
  48.     private $cart_service;
  49.     private $abonnement_service;
  50.     private $facture_service;
  51.     private $notifications;
  52.     private $compte;
  53.     public function __construct(Compte $compte,Notifications $notifications,FactureService $factureService,AbonnementService $abonnement_serviceOrderService $order_service ,CheckoutService $checkout_service,CartService $cart_service,SessionInterface $session,AuthenticationUtils $authenticationUtils  EntityManagerInterface $em,ParameterBagInterface $params)
  54.     {
  55.         $this->em $em;
  56.         $this->session $session;
  57.         $this->params $params;
  58.         $this->cart_service $cart_service;
  59.         $this->checkout_service $checkout_service;
  60.         $this->authenticationUtils $authenticationUtils;
  61.         $this->order_service $order_service;
  62.         $this->abonnement_service $abonnement_service ;
  63.         $this->facture_service $factureService ;
  64.         $this->notifications $notifications ;
  65.         $this->compte $compte ;
  66.     }
  67.     /**
  68.      * @Route("/adresse-facture", name="abonnementPro_adresse_facture")
  69.      */
  70.     public function index(Request $request) : Response
  71.     {
  72.         //check abonnement exist user
  73.         $isPageCheckout $request->get('isPageCheckout');
  74.         if($this->getUser()) {
  75.             $abonnement $this->cart_service->checkIsAbonnementPro($this->getUser());
  76.             if ($abonnement['is_abonnemen_pro']  && $abonnement['is_admin']) {
  77.                 return $this->redirectToRoute('edit_abonnement_page_b2b');
  78.             }else if($abonnement['is_abonnemen_pro']  && !$abonnement['is_admin']){
  79.                 $myAbonnement $abonnement['compte_abonnement_object'];
  80.                 $response $this->render('front/abonnement-pro/deja-abonnee.html.twig',[
  81.                     'is_admin' => false,
  82.                     'myAbonnement' => $myAbonnement
  83.                 ]);
  84.                 return $response;
  85.             }else{
  86.                 $token_cart $this->session->get('token_cart');
  87.                 if ($token_cart) {
  88.                     $cart $this->em->getRepository(Cart::class)->findOneBy(array('token' => $token_cart));
  89.                     if($cart && $cart->getAbonnementProCart()){
  90.                         return $this->redirectToRoute('accueil_page_b2b');
  91.                     }
  92.                 }
  93.                 $typeComptePaiement $request->get('typeComptePaiement',1);
  94.                 $typeAbonnement $request->get('typeAbonnement',1);
  95.                 $abonnementPro $this->em->getRepository(AbonnementPro::class)->findOneBy(array('typeAbonnement'=>$typeAbonnement));
  96.                 $res $this->cart_service->createCompteAboProDirect($request,$this->getUser(),$abonnementPro,$typeComptePaiement,$request->get('isFree',0));
  97.                 if($res){
  98.                     if($isPageCheckout){
  99.                         return $this->redirectToRoute('checkout');
  100.                     }else{
  101.                         return $this->redirectToRoute('accueil_page_b2b');
  102.                     }
  103.                 }
  104.             }
  105.         }
  106.         $typeAbonnement AbonnementPro::TYPE_PRO;
  107.         $typeComptePaiement $request->get('typeComptePaiement',1);
  108.         $isFree $request->get('isFree',0);
  109.         $abonnementPro false;
  110.         if ($typeAbonnement && $typeComptePaiement) {
  111.             $abonnementPro $this->em->getRepository(AbonnementPro::class)->findOneBy(array('typeAbonnement'=>$typeAbonnement));
  112.         }
  113.         if(!$abonnementPro){
  114.              return $this->redirectToRoute('abonnementPro_index');
  115.         }
  116.         //check abonnement pro in cart
  117.         $token_cart $this->session->get('token_cart');
  118.         if ($token_cart) {
  119.             $cart $this->em->getRepository(Cart::class)->findOneBy(array('token' => $token_cart));
  120.             if($cart && $cart->getAbonnementProCart()){
  121.                 return $this->redirectToRoute('accueil_page_b2b');
  122.             }
  123.         }
  124.         $adresseExist null;
  125.         if($this->getUser()){
  126.             $adresseExist $this->em->getRepository(Adresse::class)->getAdresseFacturationByUser($this->getUser());
  127.             if(!$adresseExist){
  128.                 $adresseExist $this->getUser();
  129.             }
  130.         }
  131.         $adresse = new Adresse();
  132.         $form $this->createForm(AdresseFacturationAbonnementType::class, $adresse,array('adresseExist'=>$adresseExist));
  133.         $form->handleRequest($request);
  134.         if ($form->isSubmitted() && $form->isValid()) {
  135.             $newUser false;
  136.             if(!is_object($this->getUser())){
  137.                 $data['firstname'] = $adresse->getFirstname();
  138.                 $data['lastname'] = $adresse->getLastname();
  139.                 $data['email'] = $adresse->getEmail();
  140.                 $data['password'] = $request->get('password');
  141.                 $result $this->compte->addUser($data);
  142.                 if(isset($result['erreur'])){
  143.                     $response $this->render('front/abonnement-pro/checkout/adresse-facture-new.html.twig',[
  144.                         'form'=>$form->createView(),
  145.                         'last_username' => $this->authenticationUtils->getLastUsername(),
  146.                         'error' => $this->authenticationUtils->getLastAuthenticationError(),
  147.                         'adresse' => $adresse,
  148.                         'erreurEmail' => $result['erreur']
  149.                     ]);
  150.                     return $response;
  151.                 }else{
  152.                     $user $result['user'];
  153.                     $user->setPhone($adresse->getPhone());
  154.                     $user->setCodePostal($adresse->getCodePostal());
  155.                     $user->setAddress($adresse->getAdresse());
  156.                     $user->setCity($adresse->getCity());
  157.                     $user->setConpany($adresse->getConpany());
  158.                     $user->setBatiment($adresse->getBatiment());
  159.                     $this->em->persist($user);
  160.                     $this->em->flush();
  161.                     $this->notifications->createUserB2b($user,$request->get('password')); //send mail
  162.                     $this->notifications->sendMailBienVenu($user); // mail bienvenu
  163.                     $this->notifications->sendMailInscrit($user); // mail inscrit is accepte send sms
  164.                     $newUser true;
  165.                 }
  166.             }
  167.             $adresse->setTypeAdresse(Adresse::ADRESSEFACTURATION);
  168.             $this->em->persist($adresse);
  169.             $this->em->flush();
  170.             $adresseLivraison = Clone $adresse;
  171.             $adresseLivraison->setTypeAdresse(Adresse::ADRESSELIVRAISON);
  172.             if($newUser){
  173.                 $adresseLivraison->setCustomer($result['customer']);
  174.             }else{
  175.                 $adresseLivraison->setCustomer$this->em->getRepository(Customer::class)->findOneBy(array('user'=>$this->getUser()->getId())));
  176.             }
  177.             $this->em->persist($adresseLivraison);
  178.             $this->em->flush();
  179.             if ($request->get('typeAbonnementBtb')) {
  180.                 $abonnementPro $this->em->getRepository(AbonnementPro::class)->findOneBy(array('typeAbonnement'=>$request->get('typeAbonnementBtb')));
  181.             }
  182.             $this->cart_service->addAbonnementPro($abonnementPro,$adresse,$typeComptePaiement,$isFree); // add abonnement to cart
  183.             $uniqid md5(uniqid());
  184.             $this->session->set('tk_add'$uniqid);
  185.             if($isPageCheckout){
  186.                 return $this->redirectToRoute('checkout');
  187.             }else{
  188.                 return $this->redirectToRoute('accueil_page_b2b',array('tk_add'=>$uniqid));
  189.             }
  190.         }
  191. //        $response = $this->render('front/abonnement-pro/checkout/adresse-facture.html.twig',[
  192.         $response $this->render('front/abonnement-pro/checkout/adresse-facture-new.html.twig',[
  193.             'form'=>$form->createView(),
  194.             'last_username' => $this->authenticationUtils->getLastUsername(),
  195.             'error' => $this->authenticationUtils->getLastAuthenticationError(),
  196.             'adresse' => $adresse
  197.         ]);
  198.         return $response;
  199.     }
  200.     /**
  201.      * @Route("/adresse-facture-redirect-user", name="abonnementPro_adresse_facture_redirect_user",options={"expose"=true})
  202.      */
  203.     public function indexRedirectUser(Request $request) : Response
  204.     {
  205.         //check abonnement exist user
  206.         if($this->getUser() && $request->get('dataUser')) {
  207.             $abonnement $this->cart_service->checkIsAbonnementPro($this->getUser());
  208.             if ($abonnement['is_abonnemen_pro']  && $abonnement['is_admin']) {
  209.             }else if($abonnement['is_abonnemen_pro']  && !$abonnement['is_admin']){
  210.             }else{
  211.                 $isAbonnee false;
  212.                 $token_cart $this->session->get('token_cart');
  213.                 if ($token_cart) {
  214.                     $cart $this->em->getRepository(Cart::class)->findOneBy(array('token' => $token_cart));
  215.                     if($cart && $cart->getAbonnementProCart()){
  216.                         $isAbonnee =true;
  217.                     }
  218.                 }
  219.                 if($isAbonnee == false) {
  220.                     $typeAbonnement AbonnementPro::TYPE_PRO;
  221.                     $typeComptePaiement $request->get('typeComptePaiement'1);
  222.                     $isFree $request->get('isFree'0);
  223.                     $abonnementPro false;
  224.                     if ($typeAbonnement && $typeComptePaiement) {
  225.                         $abonnementPro $this->em->getRepository(AbonnementPro::class)->findOneBy(array('typeAbonnement' => $typeAbonnement));
  226.                     }
  227.                     if ($abonnementPro) {
  228.                         $user $this->getUser();
  229.                         $customer $this->em->getRepository(Customer::class)->findOneBy(['user' => $user->getId()]);
  230.                         $adresse = new Adresse();
  231.                         $adresse->setCustomer($customer);
  232.                         $adresse->setEmail($user->getEmail());
  233.                         $adresse->setFirstname($user->getFirstname());
  234.                         $adresse->setLastname($user->getlastname());
  235.                         $adresse->setTypeAdresse(Adresse::ADRESSEFACTURATION);
  236.                         $this->em->persist($adresse);
  237.                         $this->em->flush();
  238.                         $adresseLivraison = clone $adresse;
  239.                         $adresseLivraison->setTypeAdresse(Adresse::ADRESSELIVRAISON);
  240.                         $this->em->persist($adresseLivraison);
  241.                         $this->em->flush();
  242.                         $this->cart_service->addAbonnementPro($abonnementPro$adresse$typeComptePaiement$isFree); // add abonnement to cart
  243.                         $uniqid md5(uniqid());
  244.                         $this->session->set('tk_add'$uniqid);
  245.                     }
  246.                 }
  247.             }
  248.         }
  249.         $response = new Response();
  250.         $response->setContent(1);
  251.         return $response;
  252.     }
  253.     /**
  254.      * @Route("/delete/{token}", name="abonnement_pro_deleted",options={"expose"=true})
  255.      */
  256.     public function deleted(Request $request,$token){
  257.         $cart $this->em->getRepository(Cart::class)->findOneBy(array('token'=>$token));
  258.         if($cart){
  259.             $abonnementPro $this->em->getRepository(AbonnementProCart::class)->findOneBy(array('cart'=>$cart->getId()));
  260.             if($abonnementPro){
  261.                 $this->em->remove($abonnementPro);
  262.                 $this->em->flush();
  263.                 $this->cart_service->updatePriceCart($cart);
  264.             }
  265.         }
  266.         return  $this->redirectToRoute('checkout');
  267.     }
  268.     /**
  269.      * @Route("/add-abonnement-cart-request", name="abonnementPro_add_abonnement_cart_with_request",options={"expose"=true})
  270.      */
  271.     public function addAbonnementCartWithRequest(Request $request) : Response
  272.     {
  273.         //check abonnement exist user
  274.         $isPageCheckout 1;
  275.         $rep = array(
  276.             'erreur' =>'',
  277.             'success' =>false,
  278.         );
  279.         $abonnement $this->cart_service->checkIsAbonnementPro($this->getUser());
  280.         if ($abonnement['is_abonnemen_pro']  && $abonnement['is_admin']) {
  281.             $rep['erreur'] = 'deja abonnĂ© !';
  282.         }else if($abonnement['is_abonnemen_pro']  && !$abonnement['is_admin']){
  283.             $rep['erreur'] = 'deja abonnĂ© !';
  284.         }else{
  285.             $token_cart $this->session->get('token_cart');
  286.             if ($token_cart) {
  287.                 $cart $this->em->getRepository(Cart::class)->findOneBy(array('token' => $token_cart));
  288.                 if($cart && $cart->getAbonnementProCart()){
  289.                     $rep['erreur'] = 'deja abonnĂ© !';
  290.                 }
  291.             }
  292.             $data $this->checkout_service->postFormWithBtnAbonnement($request);
  293.             if(isset($data['erreur'])){
  294.                 $rep['erreur'] = $data['erreur'];
  295.             }else{
  296.                 $cart->setCustomer($data['customer']);
  297.                 $cart->setAdresseLivraison($data['adresseLiv']);
  298.                 $cart->setAdresseFacturation($data['adresseFac']);
  299.                 if($cart->getVersionCarrier() == Cart::VERSION_CARRIER_NEW){
  300.                     $cart->setImprimeur(null);
  301.                     $cart->setDateStart(null);
  302.                     $cart->setDateEnd(null);
  303.                     $cart->setCarrier(null);
  304.                     $cart->setTypeLivraisonNational(null);
  305.                     $cart->setIdServicePointLivraison(null);
  306.                 }else {
  307.                     if (count($cart->getCartDetail()) > 0) {
  308.                         $cart->setImprimeur($data['carrierData']['imprimeur']);
  309.                         $cart->setDateStart($data['carrierData']['dateStart']);
  310.                         $cart->setDateEnd($data['carrierData']['dateEnd']);
  311.                         $cart->setTypeLivraisonNational($data['carrierData']['typeLivraionNation']);
  312.                         $cart->setIdServicePointLivraison($data['carrierData']['idServicePointNation']);
  313.                     } else {
  314.                         $cart->setImprimeur(null);
  315.                         $cart->setDateStart(null);
  316.                         $cart->setDateEnd(null);
  317.                         $cart->setCarrier(null);
  318.                     }
  319.                 }
  320.                 $cart->setCommentaire($data['commentaire']);
  321.                 $this->em->persist($cart);
  322.                 $this->em->flush();
  323.                 $typeComptePaiement $request->get('typeComptePaiement',1);
  324.                 $typeAbonnement $request->get('typeAbonnement',1);
  325.                 $abonnementPro $this->em->getRepository(AbonnementPro::class)->findOneBy(array('typeAbonnement'=>$typeAbonnement));
  326.                 $this->cart_service->createCompteAboProDirect($request,$this->getUser(),$abonnementPro,$typeComptePaiement,$request->get('isFree',0));
  327.                 $rep['success'] = true;
  328.             }
  329.         }
  330.         $response = new Response();
  331.         $response->setContent(json_encode($rep));
  332.         $response->setStatusCode(200);
  333.         return $response;
  334.     }
  335. }