lib/boab/ecommerce-bundle/src/Controller/CheckoutController.php line 41

Open in your IDE?
  1. <?php
  2. namespace Boab\EcommerceBundle\Controller;
  3. use Boab\CmsBundle\Controller\BaseController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Boab\EcommerceBundle\Entity\Order;
  7. use Boab\EcommerceBundle\Entity\OrderInterface;
  8. use Boab\EcommerceBundle\Event\OrderCreatedEvent;
  9. use Boab\EcommerceBundle\Events;
  10. use Boab\EcommerceBundle\Form\OrderType;
  11. use Boab\CmsBundle\Api\Exception\ApiException;
  12. use Boab\CmsBundle\Entity\UserInterface;
  13. use Boab\CmsBundle\Repository\UserRepositoryInterface;
  14. use Boab\EcommerceBundle\Manager\CartManagerInterface;
  15. use Boab\EcommerceBundle\PaymentMethod\PaymentMethodManager;
  16. use Boab\EcommerceBundle\Repository\OrderRepositoryInterface;
  17. use Psr\Log\LoggerInterface;
  18. class CheckoutController extends BaseController
  19. {
  20.     private $cartManager;
  21.     private $paymentMethodManager;
  22.     private $userRepository;
  23.     private $orderRepository;
  24.     public function __construct(
  25.         OrderRepositoryInterface $orderRepository
  26.         CartManagerInterface $cartManager,
  27.         PaymentMethodManager $paymentMethodManager,
  28.         UserRepositoryInterface $userRepository
  29.     )
  30.     {
  31.         $this->orderRepository $orderRepository;
  32.         $this->cartManager $cartManager;
  33.         $this->paymentMethodManager $paymentMethodManager;
  34.         $this->userRepository $userRepository;
  35.     }
  36.     public function indexAction(Request $request)       
  37.     {
  38.         $cart $this->cartManager->getCart();
  39.         if($cart->isEmpty()){
  40.             return $this->redirect($this->router->generate("shopping_cart"));
  41.         }    
  42.         $order $this->createOrder();
  43.         $form $this->createForm(OrderType::class, $order,[
  44.             'action' => $this->router->generate('shopping_checkout_process'),
  45.             'attr'=>[
  46.                 'class'=>'form-style'
  47.             ]
  48.         ]);
  49.         return $this->render('@Ecommerce/Main/checkout.html.twig',[
  50.             'cart' => $cart,
  51.             'form' => $form->createView(),
  52.             'pageTitle' => 'Checkout',
  53.             'subTotal' => $this->cartManager->getSubtotal(),
  54.             'taxCharges' => $this->cartManager->getCharges(),
  55.             'grandTotal' => $this->cartManager->getGrandTotal()
  56.         ]);        
  57.     }
  58.     public function processAction(Request $request)
  59.     { 
  60.         $cart $this->cartManager->getCart();
  61.         if($cart->isEmpty()){
  62.             return $this->redirect($this->router->generate("shopping_cart"));
  63.         }
  64.         $order = new Order($cart);
  65.         $form $this->createForm(OrderType::class, $order,[
  66.             'action' => $this->router->generate('shopping_checkout_process'),
  67.             'attr'=>[
  68.                 'class'=>'form-style'
  69.             ]
  70.         ]);              
  71.         $form->handleRequest($request);
  72.         if($form->isSubmitted() && $form->isValid()){
  73.             $order $form->getData();
  74.             //$order->setCustomerId($this->getUser()->getId());
  75.             try{
  76.                 $this->save($order);
  77.                 $response $this->handleCheckoutPayment($order);
  78.                 $this->cartManager->destroyCart();
  79.                 $event =  new OrderCreatedEvent($order);
  80.                 $this->eventDispatcher->dispatch($eventEvents::ORDER_CREATED);                
  81.                 
  82.                 return new JsonResponse([
  83.                     "status"=>"success",
  84.                     "redirectUrl"=>$response["redirect"]
  85.                 ]); 
  86.             }catch(\Exception $e){
  87.                 $this->logger->error($e->getMessage(), ['exception'=>$e]);
  88.                 throw new ApiException(400$e->getMessage());
  89.             }
  90.         } 
  91.         return $this->render('@Ecommerce/Main/checkout_form.html.twig',[
  92.             'cart' => $cart,
  93.             'form' => $form->createView(),
  94.             'pageTitle' => 'Checkout',
  95.             'subTotal' => $this->cartManager->getSubtotal(),
  96.             'taxCharges' => $this->cartManager->getCharges(),
  97.             'grandTotal' => $this->cartManager->getGrandTotal()
  98.         ]); 
  99.     }
  100.     private function handleCheckoutPayment(OrderInterface $order)
  101.     {
  102.         $paymentMthod $this->paymentMethodManager->getMethod($order->getPaymentMethod());
  103.         $redirectUrl $paymentMthod->execute($order);
  104.         return [
  105.             "status"=> true,
  106.             "redirect"=> $redirectUrl,
  107.         ]; 
  108.     }
  109.     private function createOrder():OrderInterface
  110.     {
  111.         $order =  new Order;
  112.         $user $this->getUser();
  113.         if($user instanceof UserInterface){
  114.             $order->setFirstName($user->getFirstname())
  115.             ->setLastName($user->getLastname())
  116.             ->setAddress($user->getAddress())
  117.             ->setCountry($user->getCountry())
  118.             ->setEmail($user->getEmail())
  119.             ->setContactNumber($user->getContactNumber())
  120.             ->setPostalCode($user->getPostalCode());
  121.         }
  122.         return $order;
  123.     }
  124. }