<?php
declare(strict_types=1);
namespace App\Controller\Front\Order;
use App\Application\Service\Helper\ToolsService;
use App\Application\Service\Order\OrderService;
use App\Application\Service\Session\SessionService;
use App\Entity\System\Cart;
use App\Entity\System\PaymentMethod;
use App\Exception\PaymentException;
use App\Factory\Order\OrderPaymentViewFactory;
use App\Manager\System\CartManager;
use App\Manager\System\OrderManager;
use App\Service\Order\OrderPaymentService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class OrderPaymentController extends AbstractController
{
public function __construct(
private readonly OrderManager $orderManager,
private readonly CartManager $cartManager,
private readonly SessionService $sessionService,
private readonly OrderPaymentService $orderPaymentService,
private readonly OrderService $orderService,
private readonly OrderPaymentViewFactory $orderPaymentViewFactory,
private readonly ToolsService $toolsService,
) {
}
/**
* @IsGranted("IS_AUTHENTICATED_FULLY")
*
* @Route("/order/{cartParentId}/payment", name="order_payment", methods={"GET", "POST"})
*/
public function payOrder(int $cartParentId): Response
{
$requestedCartParent = $this->cartManager->findOneById($cartParentId);
if (!$requestedCartParent) {
return $this->redirectToRoute('homepage');
}
$orderPendingToPay = $this->orderManager->findNextNotVisibleOrderByParentCart($cartParentId);
if (!$orderPendingToPay) {
$cartParent = $this->cartManager->findOneById($cartParentId);
$totalPaidOrders = $this->orderManager->findAllByParentCartId($cartParentId);
if (empty($totalPaidOrders)) {
$totalPaidOrders = [$cartParent->getOrder()];
}
$this->sessionService->delete('id_cart');
$this->sessionService->delete('id_cart_services');
return $this->renderTemplate($cartParent, count($totalPaidOrders), $totalPaidOrders);
}
$parentCart = $orderPendingToPay->getCart()->getCartParent();
try {
$paymentUrl = $this->orderPaymentService->payOrder($orderPendingToPay, $parentCart);
return $this->redirect($paymentUrl);
} catch (PaymentException $exception) {
[
$twigName,
$parentCart,
$ordersCount,
$paidOrders,
$exceptionMessages,
] = $this->orderPaymentService->managePaymentException($parentCart, [$orderPendingToPay], $exception->getMessage(), []);
$this->orderService->sendOrderToPubSub($orderPendingToPay);
return $this->renderTemplate($parentCart, $ordersCount, $paidOrders, $exceptionMessages);
}
}
private function renderTemplate(
Cart $cart,
?int $totalCreatedOrders = 0,
?array $successOrders = null,
?array $errors = null,
): Response {
$twigName = $cart->getPaymentMethod()->getId() === PaymentMethod::TRANSFER_PAYMENT_METHOD_ID ? 'success_bankwire.html.twig' : 'success.html.twig';
if ($cart->isVirtual()) {
$cartType = 'services';
$orderPaymentView = $this->orderPaymentViewFactory->buildForServices(
$cart->getCustomer(),
$cart->getId(),
$totalCreatedOrders,
$successOrders,
$errors
);
} else {
$cartType = 'products';
$orderPaymentView = $this->orderPaymentViewFactory->buildForProducts(
$cart->getCustomer()->getId(),
$cart->getId(),
$totalCreatedOrders,
$successOrders,
$errors
);
}
if ($this->toolsService::isMobile()) {
return $this->render(
'front/order/'.$cartType.'/mobile/'.$twigName,
$this->orderPaymentViewFactory->createViewParameters($orderPaymentView)
);
}
return $this->render(
'front/order/'.$cartType.'/'.$twigName,
$this->orderPaymentViewFactory->createViewParameters($orderPaymentView)
);
}
}