<?php
namespace Boab\EcommerceBundle\Controller;
use Boab\CmsBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Boab\EcommerceBundle\Entity\Order;
use Boab\EcommerceBundle\Entity\OrderInterface;
use Boab\EcommerceBundle\Event\OrderCreatedEvent;
use Boab\EcommerceBundle\Events;
use Boab\EcommerceBundle\Form\OrderType;
use Boab\CmsBundle\Api\Exception\ApiException;
use Boab\CmsBundle\Entity\UserInterface;
use Boab\CmsBundle\Repository\UserRepositoryInterface;
use Boab\EcommerceBundle\Manager\CartManagerInterface;
use Boab\EcommerceBundle\PaymentMethod\PaymentMethodManager;
use Boab\EcommerceBundle\Repository\OrderRepositoryInterface;
use Psr\Log\LoggerInterface;
class CheckoutController extends BaseController
{
private $cartManager;
private $paymentMethodManager;
private $userRepository;
private $orderRepository;
public function __construct(
OrderRepositoryInterface $orderRepository,
CartManagerInterface $cartManager,
PaymentMethodManager $paymentMethodManager,
UserRepositoryInterface $userRepository
)
{
$this->orderRepository = $orderRepository;
$this->cartManager = $cartManager;
$this->paymentMethodManager = $paymentMethodManager;
$this->userRepository = $userRepository;
}
public function indexAction(Request $request)
{
$cart = $this->cartManager->getCart();
if($cart->isEmpty()){
return $this->redirect($this->router->generate("shopping_cart"));
}
$order = $this->createOrder();
$form = $this->createForm(OrderType::class, $order,[
'action' => $this->router->generate('shopping_checkout_process'),
'attr'=>[
'class'=>'form-style'
]
]);
return $this->render('@Ecommerce/Main/checkout.html.twig',[
'cart' => $cart,
'form' => $form->createView(),
'pageTitle' => 'Checkout',
'subTotal' => $this->cartManager->getSubtotal(),
'taxCharges' => $this->cartManager->getCharges(),
'grandTotal' => $this->cartManager->getGrandTotal()
]);
}
public function processAction(Request $request)
{
$cart = $this->cartManager->getCart();
if($cart->isEmpty()){
return $this->redirect($this->router->generate("shopping_cart"));
}
$order = new Order($cart);
$form = $this->createForm(OrderType::class, $order,[
'action' => $this->router->generate('shopping_checkout_process'),
'attr'=>[
'class'=>'form-style'
]
]);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$order = $form->getData();
//$order->setCustomerId($this->getUser()->getId());
try{
$this->save($order);
$response = $this->handleCheckoutPayment($order);
$this->cartManager->destroyCart();
$event = new OrderCreatedEvent($order);
$this->eventDispatcher->dispatch($event, Events::ORDER_CREATED);
return new JsonResponse([
"status"=>"success",
"redirectUrl"=>$response["redirect"]
]);
}catch(\Exception $e){
$this->logger->error($e->getMessage(), ['exception'=>$e]);
throw new ApiException(400, $e->getMessage());
}
}
return $this->render('@Ecommerce/Main/checkout_form.html.twig',[
'cart' => $cart,
'form' => $form->createView(),
'pageTitle' => 'Checkout',
'subTotal' => $this->cartManager->getSubtotal(),
'taxCharges' => $this->cartManager->getCharges(),
'grandTotal' => $this->cartManager->getGrandTotal()
]);
}
private function handleCheckoutPayment(OrderInterface $order)
{
$paymentMthod = $this->paymentMethodManager->getMethod($order->getPaymentMethod());
$redirectUrl = $paymentMthod->execute($order);
return [
"status"=> true,
"redirect"=> $redirectUrl,
];
}
private function createOrder():OrderInterface
{
$order = new Order;
$user = $this->getUser();
if($user instanceof UserInterface){
$order->setFirstName($user->getFirstname())
->setLastName($user->getLastname())
->setAddress($user->getAddress())
->setCountry($user->getCountry())
->setEmail($user->getEmail())
->setContactNumber($user->getContactNumber())
->setPostalCode($user->getPostalCode());
}
return $order;
}
}