src/Controller/Frontend/MembershipController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Entity\Commande;
  4. use App\Repository\CommandeRepository;
  5. use App\Repository\ProduitRepository;
  6. use App\Repository\UserRepository;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use DoctrineExtensions\Query\Mysql\Date;
  9. use DoctrineExtensions\Query\Mysql\Year;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request as Request;
  13. use Symfony\Component\PropertyAccess\PropertyAccess;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Security\Core\Security;
  16. class MembershipController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("/membership-index", name="app_memberShip_index")
  20.      */
  21.     public function MemberShipBenefits(UserRepository $userRepository, ProduitRepository $produitRepository)
  22.     {
  23.         // var_dump($this->get('security.token_storage')->getToken()->getUser());
  24.         // die();
  25.         $produits = $produitRepository->findBy(['IsActif' => '1']);
  26.         return $this->render('frontend/memberShip/BenefitsMembership.html.twig', ['produits' => $produits]);
  27.     }
  28.     /**
  29.      * @Route("/admin/membership_index", name="app_membership_index_admin")
  30.      */
  31.     public function MemberShipAdminIndex(CommandeRepository $commandeRepository)
  32.     {
  33.         $commandes = $commandeRepository->findAll();
  34.         return $this->render('backend/memberShip/membershipIndexAdmin.html.twig', ['commandes' => $commandes]);
  35.     }
  36.     /**
  37.      * @Route("/membership_new/{user}", name="app_membership_new")
  38.      */
  39.     public function membershipNew($user, EntityManagerInterface $em, UserRepository $userRepository, Request $request, ProduitRepository $produitRepository)
  40.     {
  41.         if ($request->isMethod('post')) {
  42.             $getproduit = $request->request->get('produit');
  43.             $produit = $produitRepository->findOneBy(['id' => $getproduit]);
  44.             $userToBeMember = $userRepository->findOneBy(['id' => $user]);
  45.             $lastYear = intval(date('Y'));
  46.             //if cotisation is null
  47.             if (empty($userToBeMember->getCotisation())) {
  48.                 $arr = [];
  49.                 array_push($arr, $lastYear);
  50.                 $userToBeMember->setCotisation($arr);
  51.                 // elseif cotisation not null 
  52.             } elseif (!empty($userToBeMember->getCotisation())) {
  53.                 $arr = $userToBeMember->getCotisation();
  54.                 if ($arr[sizeof($arr) - 1] != $lastYear) {
  55.                     array_push($arr, $arr[sizeof($arr) - 1] + 1);
  56.                     $userToBeMember->setCotisation($arr);
  57.                 }
  58.             }
  59.             $em->persist($userToBeMember);
  60.             $commande = new Commande();
  61.             $commande->setDate(new \DateTime('now'));
  62.             $commande->setUser($userToBeMember);
  63.             $commande->setEtatCmd("en attente");
  64.             $commande->setProduit($produit);
  65.             $commande->setPrix($produit->getPrix());
  66.             $em->persist($commande);
  67.             $em->flush();
  68.             if ($request->getLocale('fr')) {
  69.                 $this->addFlash('success', 'Votre adhésion est passé avec succès');
  70.             } elseif ($request->getLocale('en')) {
  71.                 $this->addFlash('success', 'Your membership has been successfully completed');
  72.             }
  73.             return $this->redirectToRoute('app_membershipresult', ['commande' => $commande->getId()]);
  74.         }
  75.     }
  76.     /**
  77.      * @Route("/user/membership_result/{commande}", name="app_membershipresult")
  78.      */
  79.     public function MemberShipResult($commande, CommandeRepository $commandeRepository, Request $request, EntityManagerInterface $em)
  80.     {
  81.         $commande = $commandeRepository->findOneBy(['id' => $commande]);
  82.         $session = $request->getSession();
  83.         if ($session->has('commande') || empty($session->has('commande'))) {
  84.             $session->set('commande', []);
  85.         }
  86.         // var_dump(rand(0, (int) 10000000000));
  87.         // die();
  88.         $orderRef = rand(0, (int) 10000000000);
  89.         $commandeArray = ['orderNumber' => $orderRef, 'montant' => $commande->getPrix()];
  90.         $session->set('commande', $commandeArray);
  91.         $commande->setOrderRef($orderRef);
  92.         $em->persist($commande);
  93.         $em->flush();
  94.         return $this->render('frontend/memberShip/membershipresult.html.twig', ['commande' => $commande]);
  95.     }
  96.     /**
  97.      * @Route("/smt_notification/{commande}", name="smt_notification")
  98.      */
  99.     public function smt_notification(EntityManagerInterface $em, $commande, CommandeRepository $commandeRepository, Request $request)
  100.     {
  101.         $this->get('session')->getFlashBag()->clear();
  102.         $session = $request->getSession();
  103.         //if test
  104.         if ($this->getParameter('smt_sandbox')) {
  105.             $url = $this->getParameter('smt_sandbox_url') . '/payment/rest/register.do';
  106.         } else {
  107.             //prod
  108.             $url = $this->getParameter('smt_prod_url') . '/payment/rest/register.do';
  109.         }
  110.         $commandeSession = $session->get('commande');
  111.         $commandeObj = $commandeRepository->findOneBy(['id' => $commande]);
  112.         $postfields = array(
  113.             'userName' => $this->getParameter('smt_username'),
  114.             'password' => $this->getParameter('smt_password'),
  115.             'returnUrl' => $this->getParameter('smt_return_url_result'),
  116.             'failUrl' => $this->getParameter('smt_return_url_result'),
  117.             'currency' => $this->getParameter('smt_currency'),
  118.             'orderNumber' => $commandeSession['orderNumber'],
  119.             'amount' => $commandeSession['montant'],
  120.         );
  121.         $ch = curl_init();
  122.         curl_setopt($ch, CURLOPT_URL, $url);
  123.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  124.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  125.         curl_setopt($ch, CURLOPT_POST, 1);
  126.         curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
  127.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // On dev server only!
  128.         $result = curl_exec($ch);
  129.         $preResult = json_decode($result);
  130.         $user = $this->container->get('security.token_storage')->getToken()->getUser();
  131.         $arr = $user->getRoles();
  132.         array_push($arr, "ROLE_MEMBRE_EN_REGLE");
  133.         $user->setRoles($arr);
  134.         $em->persist($user);
  135.         $em->flush();
  136.         if (!empty($preResult->errorCode)) {
  137.             $this->addFlash("danger", $this->errorInvestigation($preResult->errorCode));
  138.             return $this->redirectToRoute('app_membershipresult', array('commande' => $commande));
  139.         } else {
  140.             $orderId = $preResult->orderId;
  141.             $formUrl = $preResult->formUrl;
  142.             $commandeObj->setOrderId((string)$orderId);
  143.             $em->persist($commandeObj);
  144.             $em->flush();
  145.             return $this->redirect($formUrl);
  146.         }
  147.     }
  148.     public function errorInvestigation($act)
  149.     {
  150.         switch ($act) {
  151.             case "0":
  152.                 return "Pas d'erreur système.";
  153.                 break;
  154.             case "1":
  155.                 return "Numéro de commande dupliqué, commande avec le numéro de commande donner est déjà traitée";
  156.                 break;
  157.             case "3":
  158.                 return "Monnaie inconnue";
  159.                 break;
  160.             case "4":
  161.                 return "Paramètre obligatoire n'a pas été spécifié";
  162.                 break;
  163.             case "5":
  164.                 return "Valeur erronée d'un paramètre de la requête";
  165.                 break;
  166.             case "7":
  167.                 return "Erreur système.";
  168.                 break;
  169.         }
  170.     }
  171.     /**
  172.      * @Route("/clictopay/smt/result", name="smt_result")
  173.      */
  174.     public function ResultSmtAction(Request $request, CommandeRepository $commandeRepository)
  175.     {
  176.         $this->get('session')->getFlashBag()->clear();
  177.         $em = $this->getDoctrine()->getManager();
  178.         if ($request->isMethod('GET')) {
  179.             $orderId = $request->query->get('orderId');
  180.             if ($this->getParameter('smt_sandbox') == 1) {
  181.                 $url = $this->getParameter('smt_sandbox_url') . '/payment/rest/deposit.do';
  182.             } else {
  183.                 $url = $this->getParameter('smt_prod_url') . '/payment/rest/deposit.do';
  184.             }
  185.             $session = $request->getSession();
  186.             $commandeSession = $session->get('commande');
  187.             $postfields = array(
  188.                 'userName' => $this->getParameter('smt_username'),
  189.                 'password' => $this->getParameter('smt_password'),
  190.                 'orderId' => $orderId,
  191.                 'amount' => $commandeSession['amount'],
  192.             );
  193.             $ch = curl_init();
  194.             curl_setopt($ch, CURLOPT_URL, $url);
  195.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  196.             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  197.             curl_setopt($ch, CURLOPT_POST, 1);
  198.             curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
  199.             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // On dev server only!
  200.             $result = curl_exec($ch);
  201.             $preResult = json_decode($result);
  202.             $commande = $commandeRepository->findOneBy(['orderId' => $orderId]);
  203.             //            dump($preResult);
  204.             //            die();
  205.             if ($preResult->errorCode == 0) {
  206.                 $this->addFlash("success", $preResult->errorMessage);
  207.                 $commande->setEtatCmd('accepté');
  208.             } else {
  209.                 $this->addFlash("danger", $preResult->errorMessage);
  210.                 $commande->setEtatCmd('refusé');
  211.             }
  212.             $em->persist($commande);
  213.             $em->flush();
  214.         }
  215.         return $this->render('frontend/memberShip/result.html.twig', array());
  216.     }
  217.     /**
  218.      * @Route("/clicTopay/echec", name="EchecSmt")
  219.      */
  220.     public function EchecSmtAction(Request $request, CommandeRepository $commandeRepository, EntityManagerInterface $em)
  221.     {
  222.         if ($request->isMethod('GET')) {
  223.             $orderId = $request->query->get('orderId');
  224.             $commande = $commandeRepository->findOneBy(array('orderId' => $orderId));
  225.             $commande->setEtatCmd('refusé');
  226.             $em->persist($commande);
  227.             $em->flush();
  228.         }
  229.         return $this->render('frontend/memberShip/EchecSmt.html.twig', array());
  230.     }
  231.     /**
  232.      * @Route("/clicTopay/success", name="SuccessSmt")
  233.      */
  234.     public function SuccesssSmtAction(Request $request, CommandeRepository $commandeRepository, EntityManagerInterface $em)
  235.     {
  236.         if ($request->isMethod('GET')) {
  237.             $orderId = $request->query->get('orderId');
  238.             $commande = $commandeRepository->findOneBy(array('orderId' => $orderId));
  239.             $commande->setEtatCmd('accepté');
  240.             $em->persist($commande);
  241.             $em->flush();
  242.         }
  243.         return $this->render('frontend/memberShip/SuccesssSmt.html.twig', array());
  244.     }
  245.     /**
  246.      * @Route("/confirmerCommandeMembership", name="confirmerCommandeMembership")
  247.      */
  248.     public function ConfirmerCommandeMembership(Request $request, CommandeRepository $commandeRepository, EntityManagerInterface $em)
  249.     {
  250.         $id = $request->request->get('id');
  251.         $commande = $commandeRepository->findOneBy(['id' => $id]);
  252.         $commande->setEtatCmd('confirmer');
  253.         $em->persist($commande);
  254.         $em->flush();
  255.         return new JsonResponse('commandemembershipconfirmer');
  256.     }
  257.     /**
  258.      * @Route("/annulerCommandeMembership", name="annulerCommandeMembership")
  259.      */
  260.     public function AnnulerCommandeMembership(Request $request, CommandeRepository $commandeRepository, EntityManagerInterface $em)
  261.     {
  262.         $id = $request->request->get('id');
  263.         $commande = $commandeRepository->findOneBy(['id' => $id]);
  264.         $commande->setEtatCmd('annuler');
  265.         $em->persist($commande);
  266.         $em->flush();
  267.         return new JsonResponse('commandemembershipannuler');
  268.     }
  269. }