src/Controller/AffiliationController.php line 52

Open in your IDE?
  1. <?php
  2. // src/Controller/EntrepriseController.php
  3. namespace App\Controller;
  4. use App\Entity\AppUser;
  5. use App\Entity\Boutique;
  6. use App\Entity\Plan;
  7. use App\Form\BoutiqueType;
  8. use App\Entity\CodePromo;
  9. use App\Entity\TVA;
  10. use App\Entity\ValiditeService;
  11. use App\Entity\Service;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  18. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  19. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  20. use Symfony\Component\HttpKernel\KernelInterface;
  21. use Spipu\Html2Pdf\Html2Pdf;
  22. /**
  23.  * @Route("/{_locale}/affiliation")
  24.  */
  25. class AffiliationController extends AbstractController
  26. {    
  27.     
  28.     private $kernel;
  29.     private $session;
  30.     
  31.     public function  __construct(KernelInterface $kernelSessionInterface $session)
  32.     {
  33.         $this->kernel $kernel;
  34.         $this->session $session;
  35.         
  36.     }
  37.     /**
  38.      * @Route("/acceuil", name="affiliation_accueil")
  39.      * @return \Symfony\Component\HttpFoundation\Response
  40.      */
  41.     public function accueilAction(Request $request)
  42.     {
  43.         $em $this->getDoctrine()
  44.                     ->getManager();
  45.         
  46.         $user $this->getUser();
  47.         $codePromo $em->getRepository(CodePromo::class)
  48.                         ->findOneBy(
  49.                             array(
  50.                                 'user' => $user
  51.                             )
  52.                         ); 
  53.         $intervalleDeDate  $this->intervalleSemaine(date('Y-m-d'));
  54.         $dateDebut = new \DateTime($intervalleDeDate['debut']);
  55.         $dateDebut->setTime(00);
  56.         $dateFin = new \DateTime($intervalleDeDate['fin']);
  57.         $dateFin->setTime(2359);
  58.         
  59.         
  60.         $intervalleMois $this->intervalleMois(date('Y-m-d'));
  61.         $startMonth $intervalleMois['debut'];
  62.         $endMonth $intervalleMois['fin'];
  63.         
  64.         $startYear date('Y-01-01');
  65.         $endYear date('Y-12-31');
  66.         return $this->render('affiliation/accueil.html.twig', [
  67.             'codePromo' => $codePromo,
  68.             'dateDebut' => $dateDebut,
  69.             'dateFin' => $dateFin,
  70.             'startMonth' => $startMonth,
  71.             'endMonth' => $endMonth,
  72.             'startYear' => $startYear,
  73.             'endYear' => $endYear,
  74.         ]);
  75.     }
  76.     /**
  77.      * @Route("/clients", name="affiliation_clients")
  78.      * @return \Symfony\Component\HttpFoundation\Response
  79.      */
  80.     public function clientsAction(Request $request)
  81.     {
  82.         $em $this->getDoctrine()
  83.                     ->getManager();
  84.         
  85.         $user $this->getUser();
  86.         $codePromo $em->getRepository(CodePromo::class)
  87.                         ->findOneBy(
  88.                             array(
  89.                                 'user' => $user
  90.                             )
  91.                         ); 
  92.         $intervalleDeDate  $this->intervalleSemaine(date('Y-m-d'));
  93.         $dateDebut = new \DateTime($intervalleDeDate['debut']);
  94.         $dateDebut->setTime(00);
  95.         $dateFin = new \DateTime($intervalleDeDate['fin']);
  96.         $dateFin->setTime(2359);
  97.         
  98.         
  99.         $intervalleMois $this->intervalleMois(date('Y-m-d'));
  100.         $startMonth $intervalleMois['debut'];
  101.         $endMonth $intervalleMois['fin'];
  102.         
  103.         $startYear date('Y-01-01');
  104.         $endYear date('Y-12-31');
  105.         return $this->render('affiliation/clients.html.twig', [
  106.             'codePromo' => $codePromo,
  107.             'dateDebut' => $dateDebut,
  108.             'dateFin' => $dateFin,
  109.             'startMonth' => $startMonth,
  110.             'endMonth' => $endMonth,
  111.             'startYear' => $startYear,
  112.             'endYear' => $endYear,
  113.         ]);
  114.     }
  115.      /**
  116.      * @Route("/new-shop/{plan}", name="affiliation_new_shop")
  117.      
  118.      * @return \Symfony\Component\HttpFoundation\Response
  119.      */
  120.     public function nouvelleBoutiqueAction(Request $request$plan)
  121.     {
  122.    
  123.       if(!$this->isGranted('IS_AUTHENTICATED_FULLY')){
  124.            return $this->redirectToRoute('security_login');
  125.       }
  126.         $em $this->getDoctrine()
  127.                     ->getManager();
  128.         $plan $em->getRepository(Plan::class)
  129.                     ->findOneBySlug($plan);
  130.         $user $this->getUser();
  131.         if(null == $plan){
  132.             return $this->createNotFoundException();
  133.         }
  134.         $codePromo $em->getRepository(CodePromo::class)
  135.                         ->findOneBy(
  136.                             array(
  137.                                 'user' => $user
  138.                             )
  139.                         ); 
  140.         
  141.         $boutique = new Boutique;
  142.         $form $this->createForm(BoutiqueType::class, $boutique);
  143.         
  144.         $ok_soumission '';
  145.         $error '';
  146.         $totalAchat 10;
  147.         $prix $plan->getTarif() * 10;
  148.         $remise 0;
  149.         $coupon '';
  150.         
  151.         $taxes = array();
  152.         $tvaa = array(
  153.         'libelle' =>'TVA normal de 18%',
  154.         'valeur' => 18,
  155.         'libelleInterne' => 'TVA'
  156.         );
  157.         $tvab = array(
  158.         'libelle' =>'TVA réduit de 9%',
  159.         'valeur' => 9,
  160.         'libelleInterne' => 'TVAB'
  161.         );
  162.         $tvac = array(
  163.         'libelle' =>'TVA exec conv de 0%',
  164.         'valeur' => 0,
  165.         'libelleInterne' => 'TVAC'
  166.         );
  167.         $tvad = array(
  168.         'libelle' =>'TVA exec leg de 0%',
  169.         'valeur' => 0,
  170.         'libelleInterne' => 'TVAD'
  171.         );
  172.         array_push($taxes$tvaa);
  173.         array_push($taxes$tvab);
  174.         array_push($taxes$tvac);
  175.         array_push($taxes$tvad);
  176.         
  177.         $totalAchat *= (float)$plan->getTarif();
  178.         $reduction 0;
  179.         if($codePromo != null){
  180.             $remise $codePromo->getRemise() + $codePromo->getRemisePourLePartenaire();
  181.             $reduction $totalAchat $remise/100;
  182.         }
  183.         $totalAchat $totalAchat $reduction;
  184.         if($request->getMethod() == 'POST'){
  185.             
  186.                $form->handleRequest($request);
  187.                if($form->isValid()){
  188.                 $boutique->getSettings()->setEnteDocument($boutique->getNom());
  189.                             $coupon $_POST['coupon'];
  190.                             $remise $_POST['remise'];
  191.                             $mt $_POST['mt'];
  192.                 $taxe $_POST['taxe'];
  193.                     
  194.         if($plan->getNom() == 'Free' /*|| $plan->getNom() == 'Standard'*/){
  195.                         $em->persist($boutique);
  196.                         
  197.                         $service = new Service;
  198.                         $service->setUser($user);
  199.                         $service->setBoutique($boutique);
  200.                         $service->setPlan($plan);
  201.                         $em->persist($service);
  202.                         $em->flush();
  203.             $this->enregistrerDonneesComptabilite($boutique);
  204.             $this->creerRubriquesSalaires($boutique);
  205.                         
  206.                         $em->persist($validiteService);
  207.                         $validiteService->setService($service);
  208.                         $validiteService->setValableJusquAu($today);
  209.                         
  210.                         
  211.                         $userBoutique = new UserBoutique;
  212.                           
  213.                         $userBoutique->setUser($user);
  214.                         $userBoutique->setBoutique($boutique);
  215.                         $userBoutique->setRole(RoleBoutique::ADMIN);
  216.                            $userBoutique->setDroits(DroitUtilisateur::getDroits());
  217.             
  218.                         $em->persist($userBoutique);
  219.                         $em->flush();
  220.                           
  221.                         return $this->redirectToRoute('admin.show_shop', array('slug' => $boutique->getSlug()));
  222.                         $boutique = new Boutique;
  223.                         $user $this->getUser();
  224.                         $form $this->createForm(BoutiqueType::class, $boutique);
  225.                         
  226.                         $ok_soumission =" Votre message a été pris en compte";
  227.                             
  228.                         $boutique = new Boutique;
  229.             
  230.                         
  231.                         $form $this->createForm(BoutiqueType::class, $boutique);
  232.                     } else{
  233.                         
  234.                         //If user need to pay, store data in session and redirect to payment page
  235.             
  236.                         $validiteService = new ValiditeService;
  237.                         $today = new \DateTime();
  238.                         $today->modify('+1 year');
  239.                         $validiteService->setTypeValidite(ValiditeService::TYPE_PAYE);
  240.                         $validiteService->setPayePar(ValiditeService::PAYE_PAR_PARTENAIRE);
  241.                         $validiteService->setValableJusquAu($today);
  242.                         $em->persist($validiteService);
  243.                         $em->flush();
  244.                         $boutique $form->getData();
  245.                         
  246.                         $this->session->set('boutique'$boutique);
  247.                         $this->session->set('taxes'$taxes);
  248.                         $this->session->set('taxe'$taxe);
  249.                         $this->session->set('plan'$plan);        
  250.                         $this->session->set('coupon'$coupon);        
  251.                         $this->session->set('mt'$mt);
  252.                         $this->session->set('montant'$totalAchat);
  253.                                 
  254.                         $this->session->set('remise'$remise);
  255.                            $this->session->set('commande'$validiteService->getId());        
  256.                         
  257.                         return $this->redirectToRoute('affiliation_creer_compte_utilisateur');
  258.                         
  259.                     }
  260.                     
  261.                 } else {
  262.                     $error =" Veillez corrigez vos erreurs";
  263.                 }
  264.         }
  265.     
  266.         return $this->render('affiliation/new_shop.html.twig', [
  267.            'form' => $form->createView(),
  268.            'totalAchat' => $totalAchat,
  269.            'prix' => $prix,
  270.               'taxes' => $taxes,
  271.             'codePromo' => $codePromo,
  272.             'plan' => $plan,
  273.             'remise' => $remise
  274.         ]);
  275.     }
  276.      /**
  277.      * @Route("/creer-compte-utilisateur", name="affiliation_creer_compte_utilisateur")
  278.      * @return \Symfony\Component\HttpFoundation\Response
  279.      */
  280.     public function creerCompteUtilisateurAction(Request $request)
  281.     {
  282.         $em $this->getDoctrine()
  283.                     ->getManager();
  284.         
  285.         $user $this->getUser();
  286.         $nom '';
  287.         $email '';
  288.         $error '';
  289.         $motDePasse1 '';
  290.         $motDePasse2 '';
  291.         $creationCompte 'ancienCompte';
  292.         if($request->getMethod() == 'POST'){
  293.             $creationCompte $request->request->get('creation_compte');
  294.             $nom $request->request->get('nom');
  295.             $email $request->request->get('email');
  296.             $motDePasse1 $request->request->get('motDePasse1');
  297.             $motDePasse2 $request->request->get('motDePasse2');
  298.             $paymentMethod $request->request->get('paymentMethod');
  299.             $this->session->set('creationCompte'$creationCompte);
  300.             $this->session->set('paymentMethod'$paymentMethod);
  301.             if($creationCompte == 'nouveauCompte'){
  302.                 if($nom == ''){
  303.                     $error 'Veuillez indiquer le nom du client';
  304.                 }
  305.                 if($email == ''){
  306.                     $error 'Veuillez indiquer l\'adresse mail';
  307.                 }
  308.                 if($motDePasse1 == '' || $motDePasse1 != $motDePasse2){
  309.                     $error 'Les deux mot de passe ne correspondent pas';
  310.                 }
  311.                 if($error == ''){
  312.                     $this->session->set('nom'$nom);
  313.                     $this->session->set('email'$email);
  314.                     $this->session->set('motDePasse1'$motDePasse1);
  315.                     
  316.                 }
  317.             }
  318.             if($error == ''){
  319.                 return $this->redirectToRoute('affiliation_payment');
  320.             }
  321.             
  322.         
  323.         }
  324.         
  325.         return $this->render('affiliation/creer_compte_utilisateur.html.twig', [
  326.         'nom' => $nom,
  327.         'email' => $email,
  328.         'error' => $error,
  329.         'motDePasse1' => $motDePasse1,
  330.         'motDePasse2' => $motDePasse2,
  331.         'creationCompte' => $creationCompte
  332.         ]);
  333.     }
  334.         
  335.     /**
  336.      * @Route("/payment", name="affiliation_payment")
  337.      * @return \Symfony\Component\HttpFoundation\Response
  338.      */
  339.     public function paymentAction(Request $request)
  340.     {
  341.       if(!$this->isGranted('IS_AUTHENTICATED_FULLY')){
  342.            return $this->redirectToRoute('security_login');
  343.           }
  344.         $em $this->getDoctrine()
  345.                     ->getManager();
  346.         $boutique $this->session->get('boutique');
  347.         $plan $this->session->get('plan');
  348.         $commande $this->session->get('commande');
  349.         //$montant = $this->session->get('montant');
  350.         $montant 100 ;
  351.         $montant ceil($montant $montant 0.015);
  352.         $montant $montant 100;
  353.         
  354.         $coupon =  $this->session->get('coupon');
  355.            $coupon $em->getRepository(CodePromo::class)
  356.                   ->findOneByNom($coupon);
  357.         $remise =  $this->session->get('remise');
  358.         $mt =  $this->session->get('mt');
  359.         $total $plan->getTarif() * 12;
  360.            $laRemise 0;
  361.     //Authentification
  362.     /*
  363.     $ch = curl_init($this->getJekoBaseUrl().'/stores');
  364.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  365.     curl_setopt($ch, CURLOPT_HTTPHEADER, [
  366.         'X-API-KEY:'. $this->getJekoAPI(),
  367.         'X-API-KEY-ID:'.$this->getJekoAPIID(),
  368.         'Content-Type: application/json'
  369.     ]);
  370.     
  371.     $response = curl_exec($ch);
  372.     curl_close($ch);
  373.     $paymentRequest = json_decode($response, true);
  374.     */    
  375.     //Paiement
  376.     $commande =  'reference '.$commande;
  377.     
  378.     $paymentMethod $this->session->get('paymentMethod');
  379.   $successUrl =  $this->generateUrl('affiliation_jeko_success', array('commande' => $commande), UrlGeneratorInterface::ABSOLUTE_URL);
  380.   $errorUrl =  $this->generateUrl('affiliation_jeko_error', array('commande' => $commande), UrlGeneratorInterface::ABSOLUTE_URL);
  381.     $ch curl_init($this->getJekoBaseUrl().'/payment_requests');
  382.     curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  383.     curl_setopt($chCURLOPT_POSTtrue);
  384.     curl_setopt($chCURLOPT_HTTPHEADER, [
  385.         'X-API-KEY:'$this->getJekoAPI(),
  386.         'X-API-KEY-ID:'.$this->getJekoAPIID(),
  387.         'Content-Type: application/json'
  388.     ]);
  389.     curl_setopt($chCURLOPT_POSTFIELDSjson_encode([
  390.         'storeId' => $this->getJekoStoreId(),
  391.         'amountCents' => $montant,
  392.         'currency' => 'XOF',
  393.         'reference' =>$commande,
  394.         'paymentDetails' => [
  395.             'type' => 'redirect',
  396.             'data' => [
  397.                 'paymentMethod' => $paymentMethod,
  398.                 'successUrl' => $successUrl,
  399.                 'errorUrl' => $errorUrl
  400.             ]
  401.         ]
  402.     ]));
  403.     
  404.     $response curl_exec($ch);
  405.     curl_close($ch);
  406.     $paymentRequest json_decode($responsetrue);
  407.     return $this->redirect($paymentRequest['redirectUrl']);
  408.     
  409.         $redirectUrl $this->generateUrl('save_payment');
  410.         return $this->render('affiliation/payment.html.twig', [
  411.            'montant' => $montant
  412.            'redirectUrl' => $redirectUrl
  413.         ]);
  414.     }
  415.      /**
  416.      * @Route("/jeko-success/{commande}", name="affiliation_jeko_success")
  417.      * @return \Symfony\Component\HttpFoundation\Response
  418.      * 
  419.      * Faire très attention à cette fonction, car elle doit influencer aussi tous les achats venant
  420.      * de AppSumo et autre
  421.      */
  422.     public function jekoSuccessAction(Request $request$commande)
  423.     {
  424.         if(!$this->isGranted('IS_AUTHENTICATED_FULLY')){
  425.            return $this->redirectToRoute('security_login');
  426.           }
  427.         $em $this->getDoctrine()
  428.                     ->getManager();
  429.         $user $this->getUser();
  430.         $commande explode(' '$commande);
  431.         $commande $commande[1];
  432.         $boutique $this->session->get('boutique');
  433.         $plan $this->session->get('plan');
  434.         $validiteService $em->getRepository(ValiditeService::class)
  435.                                 ->findOneById($commande);
  436.         $plan $em->getRepository(Plan::class)
  437.                                 ->findOneBySlug($plan);
  438.         
  439.         if(null != $validiteService){
  440.             // Le paiement a bien marché
  441.             $em->persist($boutique);
  442.             $em->flush();
  443.             
  444.             $service = new Service;
  445.             $service->setUser($user);
  446.             $service->setBoutique($boutique);
  447.             //$service->setPlan($plan);
  448.             $em->persist($service);
  449.             $em->flush();
  450.             $validiteService->setEtatTransaction(true);
  451.             $em->flush();
  452.         }
  453.         return $this->render('affiliation/jeko_success.html.twig', [
  454.           
  455.         ]);
  456.     }
  457.     /**
  458.      * @Route("/jeko-error/{commande}", name="affiliation_jeko_error")
  459.      * @return \Symfony\Component\HttpFoundation\Response
  460.      * 
  461.      * Faire très attention à cette fonction, car elle doit influencer aussi tous les achats venant
  462.      * de AppSumo et autre
  463.      */
  464.     public function jekoErrorAction(Request $request$commande)
  465.     {
  466.         return $this->render('affiliation/jeko_error.html.twig', [
  467.           
  468.         ]);
  469.     }
  470.     public function getJekoBaseUrl()
  471.     {
  472.       return 'https://api.jeko.africa/partner_api' ;
  473.     }
  474.     public function getJekoAPI()
  475.     {
  476.         return 'jeko_6e150bc6f5a47803a33191db886520fceb70e14f7223469a12d7122703fbc4b7';
  477.     }
  478.     public function getJekoAPIID()
  479.     {
  480.         return '01e5b9bc-e49b-43d5-a7b5-fa31fbc1a3ce';
  481.     }
  482.     public function getJekoStoreId()
  483.     {
  484.         return '6a0d33da-43bc-4b54-af2c-e68b01c2caa0';
  485.         //return 'a9dd63b6-42ae-4d03-82f1-1e8d2112efd1';
  486.     }
  487.     function intervalleMois($datestr)
  488.     {
  489.        date_default_timezone_set (date_default_timezone_get());
  490.         $dt strtotime ($datestr);
  491.        return array (
  492.          "debut" => date ('Y-m-d'strtotime ('first day of this month'$dt)),
  493.          "fin" => date ('Y-m-d'strtotime ('last day of this month'$dt))
  494.        );
  495.     }
  496.     function intervalleSemaine($datestr) {
  497.        date_default_timezone_set (date_default_timezone_get());
  498.        $dt strtotime ($datestr);
  499.        return array (
  500.          "debut" => date ('N'$dt) == date ('Y-m-d'$dt) : date ('Y-m-d'strtotime ('last monday'$dt)),
  501.          "fin" => date('N'$dt) == date ('Y-m-d'$dt) : date ('Y-m-d'strtotime ('next sunday'$dt))
  502.        );
  503.     }
  504. }