<?php
namespace App\Controller\Frontend;
use App\Entity\Commande;
use App\Repository\CommandeRepository;
use App\Repository\ProduitRepository;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use DoctrineExtensions\Query\Mysql\Date;
use DoctrineExtensions\Query\Mysql\Year;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request as Request;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
class MembershipController extends AbstractController
{
/**
* @Route("/membership-index", name="app_memberShip_index")
*/
public function MemberShipBenefits(UserRepository $userRepository, ProduitRepository $produitRepository)
{
// var_dump($this->get('security.token_storage')->getToken()->getUser());
// die();
$produits = $produitRepository->findBy(['IsActif' => '1']);
return $this->render('frontend/memberShip/BenefitsMembership.html.twig', ['produits' => $produits]);
}
/**
* @Route("/admin/membership_index", name="app_membership_index_admin")
*/
public function MemberShipAdminIndex(CommandeRepository $commandeRepository)
{
$commandes = $commandeRepository->findAll();
return $this->render('backend/memberShip/membershipIndexAdmin.html.twig', ['commandes' => $commandes]);
}
/**
* @Route("/membership_new/{user}", name="app_membership_new")
*/
public function membershipNew($user, EntityManagerInterface $em, UserRepository $userRepository, Request $request, ProduitRepository $produitRepository)
{
if ($request->isMethod('post')) {
$getproduit = $request->request->get('produit');
$produit = $produitRepository->findOneBy(['id' => $getproduit]);
$userToBeMember = $userRepository->findOneBy(['id' => $user]);
$lastYear = intval(date('Y'));
//if cotisation is null
if (empty($userToBeMember->getCotisation())) {
$arr = [];
array_push($arr, $lastYear);
$userToBeMember->setCotisation($arr);
// elseif cotisation not null
} elseif (!empty($userToBeMember->getCotisation())) {
$arr = $userToBeMember->getCotisation();
if ($arr[sizeof($arr) - 1] != $lastYear) {
array_push($arr, $arr[sizeof($arr) - 1] + 1);
$userToBeMember->setCotisation($arr);
}
}
$em->persist($userToBeMember);
$commande = new Commande();
$commande->setDate(new \DateTime('now'));
$commande->setUser($userToBeMember);
$commande->setEtatCmd("en attente");
$commande->setProduit($produit);
$commande->setPrix($produit->getPrix());
$em->persist($commande);
$em->flush();
if ($request->getLocale('fr')) {
$this->addFlash('success', 'Votre adhésion est passé avec succès');
} elseif ($request->getLocale('en')) {
$this->addFlash('success', 'Your membership has been successfully completed');
}
return $this->redirectToRoute('app_membershipresult', ['commande' => $commande->getId()]);
}
}
/**
* @Route("/user/membership_result/{commande}", name="app_membershipresult")
*/
public function MemberShipResult($commande, CommandeRepository $commandeRepository, Request $request, EntityManagerInterface $em)
{
$commande = $commandeRepository->findOneBy(['id' => $commande]);
$session = $request->getSession();
if ($session->has('commande') || empty($session->has('commande'))) {
$session->set('commande', []);
}
// var_dump(rand(0, (int) 10000000000));
// die();
$orderRef = rand(0, (int) 10000000000);
$commandeArray = ['orderNumber' => $orderRef, 'montant' => $commande->getPrix()];
$session->set('commande', $commandeArray);
$commande->setOrderRef($orderRef);
$em->persist($commande);
$em->flush();
return $this->render('frontend/memberShip/membershipresult.html.twig', ['commande' => $commande]);
}
/**
* @Route("/smt_notification/{commande}", name="smt_notification")
*/
public function smt_notification(EntityManagerInterface $em, $commande, CommandeRepository $commandeRepository, Request $request)
{
$this->get('session')->getFlashBag()->clear();
$session = $request->getSession();
//if test
if ($this->getParameter('smt_sandbox')) {
$url = $this->getParameter('smt_sandbox_url') . '/payment/rest/register.do';
} else {
//prod
$url = $this->getParameter('smt_prod_url') . '/payment/rest/register.do';
}
$commandeSession = $session->get('commande');
$commandeObj = $commandeRepository->findOneBy(['id' => $commande]);
$postfields = array(
'userName' => $this->getParameter('smt_username'),
'password' => $this->getParameter('smt_password'),
'returnUrl' => $this->getParameter('smt_return_url_result'),
'failUrl' => $this->getParameter('smt_return_url_result'),
'currency' => $this->getParameter('smt_currency'),
'orderNumber' => $commandeSession['orderNumber'],
'amount' => $commandeSession['montant'],
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // On dev server only!
$result = curl_exec($ch);
$preResult = json_decode($result);
$user = $this->container->get('security.token_storage')->getToken()->getUser();
$arr = $user->getRoles();
array_push($arr, "ROLE_MEMBRE_EN_REGLE");
$user->setRoles($arr);
$em->persist($user);
$em->flush();
if (!empty($preResult->errorCode)) {
$this->addFlash("danger", $this->errorInvestigation($preResult->errorCode));
return $this->redirectToRoute('app_membershipresult', array('commande' => $commande));
} else {
$orderId = $preResult->orderId;
$formUrl = $preResult->formUrl;
$commandeObj->setOrderId((string)$orderId);
$em->persist($commandeObj);
$em->flush();
return $this->redirect($formUrl);
}
}
public function errorInvestigation($act)
{
switch ($act) {
case "0":
return "Pas d'erreur système.";
break;
case "1":
return "Numéro de commande dupliqué, commande avec le numéro de commande donner est déjà traitée";
break;
case "3":
return "Monnaie inconnue";
break;
case "4":
return "Paramètre obligatoire n'a pas été spécifié";
break;
case "5":
return "Valeur erronée d'un paramètre de la requête";
break;
case "7":
return "Erreur système.";
break;
}
}
/**
* @Route("/clictopay/smt/result", name="smt_result")
*/
public function ResultSmtAction(Request $request, CommandeRepository $commandeRepository)
{
$this->get('session')->getFlashBag()->clear();
$em = $this->getDoctrine()->getManager();
if ($request->isMethod('GET')) {
$orderId = $request->query->get('orderId');
if ($this->getParameter('smt_sandbox') == 1) {
$url = $this->getParameter('smt_sandbox_url') . '/payment/rest/deposit.do';
} else {
$url = $this->getParameter('smt_prod_url') . '/payment/rest/deposit.do';
}
$session = $request->getSession();
$commandeSession = $session->get('commande');
$postfields = array(
'userName' => $this->getParameter('smt_username'),
'password' => $this->getParameter('smt_password'),
'orderId' => $orderId,
'amount' => $commandeSession['amount'],
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // On dev server only!
$result = curl_exec($ch);
$preResult = json_decode($result);
$commande = $commandeRepository->findOneBy(['orderId' => $orderId]);
// dump($preResult);
// die();
if ($preResult->errorCode == 0) {
$this->addFlash("success", $preResult->errorMessage);
$commande->setEtatCmd('accepté');
} else {
$this->addFlash("danger", $preResult->errorMessage);
$commande->setEtatCmd('refusé');
}
$em->persist($commande);
$em->flush();
}
return $this->render('frontend/memberShip/result.html.twig', array());
}
/**
* @Route("/clicTopay/echec", name="EchecSmt")
*/
public function EchecSmtAction(Request $request, CommandeRepository $commandeRepository, EntityManagerInterface $em)
{
if ($request->isMethod('GET')) {
$orderId = $request->query->get('orderId');
$commande = $commandeRepository->findOneBy(array('orderId' => $orderId));
$commande->setEtatCmd('refusé');
$em->persist($commande);
$em->flush();
}
return $this->render('frontend/memberShip/EchecSmt.html.twig', array());
}
/**
* @Route("/clicTopay/success", name="SuccessSmt")
*/
public function SuccesssSmtAction(Request $request, CommandeRepository $commandeRepository, EntityManagerInterface $em)
{
if ($request->isMethod('GET')) {
$orderId = $request->query->get('orderId');
$commande = $commandeRepository->findOneBy(array('orderId' => $orderId));
$commande->setEtatCmd('accepté');
$em->persist($commande);
$em->flush();
}
return $this->render('frontend/memberShip/SuccesssSmt.html.twig', array());
}
/**
* @Route("/confirmerCommandeMembership", name="confirmerCommandeMembership")
*/
public function ConfirmerCommandeMembership(Request $request, CommandeRepository $commandeRepository, EntityManagerInterface $em)
{
$id = $request->request->get('id');
$commande = $commandeRepository->findOneBy(['id' => $id]);
$commande->setEtatCmd('confirmer');
$em->persist($commande);
$em->flush();
return new JsonResponse('commandemembershipconfirmer');
}
/**
* @Route("/annulerCommandeMembership", name="annulerCommandeMembership")
*/
public function AnnulerCommandeMembership(Request $request, CommandeRepository $commandeRepository, EntityManagerInterface $em)
{
$id = $request->request->get('id');
$commande = $commandeRepository->findOneBy(['id' => $id]);
$commande->setEtatCmd('annuler');
$em->persist($commande);
$em->flush();
return new JsonResponse('commandemembershipannuler');
}
}