src/Controller/Front/CheckoutController.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Session\Session;
  7. use App\Manager\CartManager;
  8. use App\Manager\CustomerManager;
  9. use App\Manager\OrderManager;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. class CheckoutController extends FrontController
  12. {
  13. /**
  14. * @Route("/{_locale}/checkout/cart-validation", name="checkout_cart_validation")
  15. */
  16. public function cartValidation(Request $request, CartManager $cartMgr, CustomerManager $customerMgr, Session $session)
  17. {
  18. $cart = $cartMgr->getCart();
  19. $noGift = $request->get('noGift',false);
  20. if($cart->canAddGift() && !$cart->hasGift() && ($noGift===false) && !$customerMgr->isExpert()){
  21. $gifts = $cartMgr->getGifts();
  22. if(count($gifts)){
  23. $popup = $this->renderView('front/cart/gift-choice.html.twig', [
  24. 'popup'=>true,
  25. 'gifts'=>$gifts
  26. ]);
  27. $session->getFlashBag()->add('popup', $popup);
  28. return $this->redirectToRoute('cart_details');
  29. }
  30. }
  31. $customer = $customerMgr->getCustomer();
  32. if(empty($customer))
  33. return $this->redirectToRoute('customer_login',[
  34. 'from' => 'checkout_cart_validation'
  35. ]);
  36. if(!$cartMgr->checkCartItems($cart)){
  37. return $this->redirectToRoute('cart_details');
  38. }
  39. return $this->redirectToRoute('checkout_shipping');
  40. }
  41. /**
  42. * @Route("/{_locale}/aj/checkout/cart", name="checkout_cart")
  43. */
  44. public function cartBlock(CartManager $cartMgr, CustomerManager $customerMgr)
  45. {
  46. $em = $this->getDoctrine()->getManager();
  47. $customer = $customerMgr->getCustomer();
  48. if(empty($customer))
  49. return $this->redirectToRoute('cart_details');
  50. $output = [
  51. 'success' => true,
  52. 'content' => $this->renderView('front/checkout/cart.html.twig',[
  53. 'cart'=>$cartMgr->getCart()
  54. ])
  55. ];
  56. return $this->renderJsonResponse($output);
  57. }
  58. /**
  59. * @Route("/{_locale}/checkout/shipping", name="checkout_shipping")
  60. */
  61. public function shipping(CartManager $cartMgr, CustomerManager $customerMgr, Session $session, \App\Manager\CarrierManager $carrierMgr, \Doctrine\ORM\EntityManagerInterface $em, \App\Manager\PriceManager $priceMgr)
  62. {
  63. $em = $this->getDoctrine()->getManager();
  64. $customer = $customerMgr->getCustomer();
  65. if(empty($customer))
  66. return $this->redirectToRoute('cart_details');
  67. $cart = $cartMgr->getCart();
  68. $priceMgr->updateCartPrice($cart);
  69. $shippingAddress = $cart->getShippingAddress();
  70. $billingAddress = $cart->getBillingAddress();
  71. if(empty($shippingAddress)){
  72. $shippingAddress = $customer->getDefaultAddress();
  73. }
  74. if(empty($billingAddress)){
  75. $billingAddress = $customer->getDefaultAddress();
  76. }
  77. $form = $this->createForm(\App\Form\Checkout\CartCommentType::class, $cart, [
  78. 'action' => $this->generateUrl('checkout_shipping_validation'),
  79. 'method' => 'post'
  80. ]);
  81. $carriers = $carrierMgr->getCarriersByCountry($shippingAddress->getCountry(), $customerMgr->getPriceContext());
  82. return $this->render('front/checkout/shipping.html.twig',[
  83. 'carriers'=>[],
  84. 'cart'=>$cart,
  85. 'shippingAddress'=>$shippingAddress,
  86. 'billingAddress'=>$billingAddress,
  87. 'sameAs'=>$shippingAddress==$billingAddress,
  88. 'form'=>$form->createView(),
  89. 'carriers'=>$carriers
  90. ]);
  91. }
  92. /**
  93. * @Route("/{_locale}/checkout/carrier/select", name="checkout_carrier_select")
  94. */
  95. public function carrierSelect(Request $request, CartManager $cartMgr, CustomerManager $customerMgr, Session $session, \Doctrine\ORM\EntityManagerInterface $em)
  96. {
  97. $em = $this->getDoctrine()->getManager();
  98. $customer = $customerMgr->getCustomer();
  99. if(empty($customer))
  100. return $this->redirectToRoute('cart_details');
  101. $output = [
  102. 'success' => false,
  103. 'message' => ''
  104. ];
  105. if($request->isMethod('POST')){
  106. $carrierId = $request->get('carrier',false);
  107. if($carrierId){
  108. $id = $request->get('id',false);
  109. $code = $request->get('code',false);
  110. $carrier = $em->getRepository('App:Carrier')->find($carrierId);
  111. if($id && $code && $carrier){
  112. $store = $em->getRepository('App:Store')->findOneBy([
  113. 'id' => $id,
  114. 'code' => $code
  115. ]);
  116. if($store){
  117. try{
  118. $cart = $cartMgr->getCart();
  119. $cart->setCarrier($carrier);
  120. $cart->setShippingInfos($store->getCode());
  121. $cart->save();
  122. $output['success'] = true;
  123. } catch (\Exception $ex) {
  124. $output['message'] = $ex->getMessage();
  125. }
  126. }else{
  127. $output['message'] = "Point de retrait non valide.";
  128. }
  129. }else{
  130. $output['message'] = "Transporteur non valide.";
  131. }
  132. }else{
  133. $output['message'] = "Données incorrectes...";
  134. }
  135. }
  136. return $this->renderJsonResponse($output);
  137. }
  138. /**
  139. * @Route("/{_locale}/checkout/shipping/collect/{id}", name="checkout_pickup_select", requirements={"id"="\d+"})
  140. */
  141. public function pickupSelect(CartManager $cartMgr, CustomerManager $customerMgr, \Doctrine\ORM\EntityManagerInterface $em, $id, \Symfony\Component\HttpFoundation\Session\SessionInterface $session)
  142. {
  143. $customer = $customerMgr->getCustomer();
  144. if(empty($customer))
  145. return $this->redirectToRoute('checkout_identification');
  146. $cart = $cartMgr->getCart();
  147. $store = $em->getRepository('App:Store')->find($id);
  148. if(empty($store))
  149. throw new NotFoundHttpException ();
  150. $cart->setPickupStore($store);
  151. $cartMgr->save($cart);
  152. return $this->redirectToRoute('checkout_shipping');
  153. }
  154. /**
  155. * @Route("/{_locale}/checkout/shipping-validation", name="checkout_shipping_validation")
  156. */
  157. public function shippingValidation(Request $request, CartManager $cartMgr, CustomerManager $customerMgr, \Doctrine\ORM\EntityManagerInterface $em, \Symfony\Component\HttpFoundation\Session\SessionInterface $session, \Symfony\Contracts\Translation\TranslatorInterface $translator)
  158. {
  159. $customer = $customerMgr->getCustomer();
  160. if(empty($customer))
  161. return $this->redirectToRoute('checkout_identification');
  162. $cart = $cartMgr->getCart();
  163. $carrier = $cart->getCarrier();
  164. if(empty($carrier)){
  165. $session->getFlashBag()->add('error',$translator->trans('Veuillez sélectionner un mode de livraison.'));
  166. return $this->redirectToRoute('checkout_shipping');
  167. }
  168. $form = $this->createForm(\App\Form\Checkout\CartCommentType::class, $cart, [
  169. 'action' => $this->generateUrl('checkout_shipping_validation'),
  170. 'method' => 'post'
  171. ]);
  172. if($request->isMethod('POST')){
  173. $form->handleRequest($request);
  174. if($form->isValid()){
  175. $cart = $form->getData();
  176. $em->persist($cart);
  177. $em->flush();
  178. }
  179. }
  180. return $this->redirectToRoute('checkout_payment');
  181. }
  182. /**
  183. * @Route("/{_locale}/checkout/payment", name="checkout_payment")
  184. */
  185. public function payment(CartManager $cartMgr, CustomerManager $customerMgr)
  186. {
  187. $em = $this->getDoctrine()->getManager();
  188. $customer = $customerMgr->getCustomer();
  189. if(empty($customer))
  190. return $this->redirectToRoute('cart_details');
  191. $cart = $cartMgr->getCart();
  192. $shippingAddress = $cart->getShippingAddress();
  193. $billingAddress = $cart->getBillingAddress();
  194. if(empty($billingAddress)){
  195. $billingAddress = $customer->getDefaultAddress();
  196. }
  197. $form = $this->createForm(\App\Form\Checkout\CartPaymentType::class, $cart, [
  198. 'action' => $this->generateUrl('checkout_payment_validation'),
  199. 'method' => 'post'
  200. ]);
  201. return $this->render('front/checkout/payment.html.twig',[
  202. 'customer' => $customer,
  203. 'cart' => $cart,
  204. 'billingAddress' => $billingAddress,
  205. 'form' => $form->createView()
  206. ]);
  207. }
  208. /**
  209. * @Route("/{_locale}/checkout/payment-validation", name="checkout_payment_validation")
  210. */
  211. public function paymentValidation(Request $request, CartManager $cartMgr, CustomerManager $customerMgr, \Doctrine\ORM\EntityManagerInterface $em, Session $session, \Symfony\Contracts\Translation\TranslatorInterface $translator)
  212. {
  213. $customer = $customerMgr->getCustomer();
  214. if(empty($customer))
  215. return $this->redirectToRoute('checkout_identification');
  216. $cart = $cartMgr->getCart();
  217. $payment = $request->get('payment');
  218. $form = $this->createForm(\App\Form\Checkout\CartPaymentType::class, $cart, [
  219. 'action' => $this->generateUrl('checkout_payment_validation'),
  220. 'method' => 'post'
  221. ]);
  222. if($request->isMethod('POST') && ctype_alpha($payment)){
  223. $form->handleRequest($request);
  224. if($form->isValid()){
  225. $cart = $form->getData();
  226. $paymentMean = $em->getRepository(\App\Entity\PaymentMean::class)->findOneByCode($payment);
  227. if($paymentMean){
  228. $output['success'] = true;
  229. $cart->setPaymentMean($paymentMean);
  230. $em->persist($cart);
  231. $em->flush();
  232. return $this->redirectToRoute('checkout_review');
  233. }else{
  234. $session->getFlashBag()->add('error',$translator->trans('Mode de payment inconnu... ('.$payment.')'));
  235. }
  236. }
  237. }
  238. return $this->redirectToRoute('checkout_payment');
  239. if($request->isMethod('POST')){
  240. $form->handleRequest($request);
  241. if($form->isValid()){
  242. $cart = $form->getData();
  243. }
  244. }
  245. // if($payment == 'TEST'){
  246. // try{
  247. // $order = $orderMgr->createFromCart($cart,$payment);
  248. // if($order){
  249. // $session->set('order_id',$order->getId());
  250. // //die('id : '.$order->getId().' '.$session->get('order_id'));
  251. // return $this->redirectToRoute('checkout_review');
  252. // }
  253. // }
  254. // catch (\Exception $ex) {
  255. // $session->getFlashBag()->add('error', $ex->getMessage());
  256. // }
  257. // }else{
  258. // $session->getFlashBag()->add('error', 'Moyen de paiement non valide.');
  259. // }
  260. return $this->redirectToRoute('checkout_payment');
  261. }
  262. /**
  263. * @Route("/{_locale}/checkout/review", name="checkout_review")
  264. */
  265. public function review(CartManager $cartMgr, CustomerManager $customerMgr, \Doctrine\ORM\EntityManagerInterface $em, Session $session, \App\Manager\PriceManager $priceMgr, \Symfony\Contracts\Translation\TranslatorInterface $translator)
  266. {
  267. $em = $this->getDoctrine()->getManager();
  268. $customer = $customerMgr->getCustomer();
  269. if(empty($customer))
  270. return $this->redirectToRoute('cart_details');
  271. $cart = $cartMgr->getCart();
  272. if(!$cartMgr->checkShippingRate($cart)){
  273. $session->getFlashBag()->add('error',$translator->trans('Veuillez selectionner un mode de livraison disponible pour votre adresse de livraison...'."(".$cart->getCarrier()->getId().")"));
  274. return $this->redirectToRoute('checkout_shipping');
  275. }
  276. $priceMgr->updateCartPrice($cart);
  277. $paymentMean = $cart->getPaymentMean();
  278. return $this->render('front/checkout/review.html.twig',[
  279. 'customer' => $customer,
  280. 'cart' => $cart,
  281. 'buttonLabel' => ($paymentMean && $paymentMean->getCode() == 'cb') ? $translator->trans('Procéder au paiement') : $translator->trans('Confirmer votre commande')
  282. ]);
  283. }
  284. /**
  285. * @Route("/{_locale}/checkout/review-validation", name="checkout_review_validation")
  286. */
  287. public function reviewValidation(Request $request, CartManager $cartMgr, \App\Manager\OrderManager $orderMgr, CustomerManager $customerMgr, Session $session, \App\Service\Mailer $mailer, \App\Manager\PriceManager $priceMgr, \Symfony\Contracts\Translation\TranslatorInterface $translator, \App\Manager\DiscountManager $discountMgr)
  288. {
  289. $em = $this->getDoctrine()->getManager();
  290. $customer = $customerMgr->getCustomer();
  291. if(empty($customer))
  292. return $this->redirectToRoute('cart_details');
  293. $cart = $cartMgr->getCart();
  294. $priceMgr->updateCartPrice($cart);
  295. if(!$cartMgr->checkDropShipping($cart)){
  296. $session->getFlashBag()->add('error',$translator->trans('Veuillez utiliser des adresses de livraison et de facturation différentes pour utiliser la livraison chez votre client...'));
  297. return $this->redirectToRoute('checkout_review');
  298. }
  299. $couponCheck = $discountMgr->checkCoupons($cart);
  300. if($couponCheck!==true){
  301. $session->getFlashBag()->add('error',$translator->trans($couponCheck));
  302. return $this->redirectToRoute('checkout_review');
  303. }
  304. $paymentMean = $cart->getPaymentMean();
  305. $order = $orderMgr->createFromCart($cart, $request->getLocale());
  306. $session->set('orderId',$order->getId());
  307. if($paymentMean->getCode() == 'cb'){
  308. return $this->redirectToRoute('cnp_redirect');
  309. }elseif($paymentMean->getCode() == 'paypal'){
  310. return $this->redirectToRoute('paypal_create_payment');
  311. }else{
  312. $em->refresh($order);
  313. $mailer->sendOrderConfirmation($order);
  314. $cartMgr->empty();
  315. }
  316. return $this->redirectToRoute('checkout_confirmation');
  317. }
  318. /**
  319. * @Route("/{_locale}/checkout/confirmation", name="checkout_confirmation")
  320. */
  321. public function confirmation(CartManager $cartMgr, CustomerManager $customerMgr, \Doctrine\ORM\EntityManagerInterface $em, Session $session)
  322. {
  323. $em = $this->getDoctrine()->getManager();
  324. $customer = $customerMgr->getCustomer();
  325. if(empty($customer))
  326. return $this->redirectToRoute('cart_details');
  327. $cartMgr->empty();
  328. $orderId = $session->get('orderId');
  329. $session->remove('orderId');
  330. return $this->render('front/checkout/confirmation.html.twig');
  331. }
  332. }