167 lines
6.4 KiB
PHP
167 lines
6.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller\Booking;
|
|
|
|
use App\BusProNet\ApiClient;
|
|
use App\BusProNet\Model\Notification;
|
|
use App\Form\BookingCreateStep3Type;
|
|
use App\Htmx\HxTrait;
|
|
use App\Service\BookingPriceCalculatorService;
|
|
use App\Service\BookingService;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
|
|
/**
|
|
* Handles the third step of the booking creation process (payment method selection).
|
|
*/
|
|
class CreateStep3Controller extends AbstractController
|
|
{
|
|
use BookingCreateTrait;
|
|
use BookingExceptionHandlerTrait;
|
|
use HxTrait;
|
|
|
|
public function __construct(
|
|
private readonly BookingService $bookingService,
|
|
private readonly ApiClient $apiClient,
|
|
private readonly BookingPriceCalculatorService $priceCalculator,
|
|
private readonly LoggerInterface $logger,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Displays and processes the payment method form.
|
|
*/
|
|
#[Route('/bookings/create/payment', name: 'app_booking_create_step_3')]
|
|
public function step3(Request $request): Response
|
|
{
|
|
$result = $this->getOrCreateBookingCreateDto($this->bookingService, $request);
|
|
if ($result instanceof Response) {
|
|
return $result;
|
|
}
|
|
$bookingCreateDto = $result;
|
|
|
|
// Validate step access
|
|
if ($redirect = $this->validateStepAccess($bookingCreateDto, 3)) {
|
|
return $redirect;
|
|
}
|
|
|
|
$form = $this->createForm(BookingCreateStep3Type::class, $bookingCreateDto, [
|
|
'attr' => [
|
|
'hx-post' => $this->generateUrl('app_booking_create_step_3'),
|
|
'hx-target' => '#form-wrapper',
|
|
'hx-select' => '#form-wrapper',
|
|
'hx-swap' => 'outerHTML',
|
|
],
|
|
]);
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
try {
|
|
// Validate booking data with API (inquiry)
|
|
$inquiryResponse = $this->apiClient->createBookingInquiry($bookingCreateDto);
|
|
|
|
if ($inquiryResponse instanceof Notification) {
|
|
$this->logger->error('Booking inquiry failed', [
|
|
'message' => $inquiryResponse->message,
|
|
]);
|
|
$this->addFlash('error', 'Ein Fehler ist aufgetreten. Bitte versuchen Sie es erneut.');
|
|
|
|
return $this->render('booking/create_step_3.html.twig', [
|
|
'bookingCreateDto' => $bookingCreateDto,
|
|
'form' => $form->createView(),
|
|
...$this->getSummaryVariables($bookingCreateDto),
|
|
]);
|
|
}
|
|
|
|
if (false === $inquiryResponse->isInquiryValid()) {
|
|
$this->logger->error('Booking inquiry validation failed', [
|
|
'status' => $inquiryResponse->status,
|
|
]);
|
|
$this->addFlash('error', 'Buchung konnte nicht validiert werden.');
|
|
|
|
return $this->render('booking/create_step_3.html.twig', [
|
|
'bookingCreateDto' => $bookingCreateDto,
|
|
'form' => $form->createView(),
|
|
...$this->getSummaryVariables($bookingCreateDto),
|
|
]);
|
|
}
|
|
|
|
// Validate price match (exact comparison)
|
|
$apiTotal = $inquiryResponse->totalPrice ?? 0.0;
|
|
$calculatedTotal = $this->priceCalculator->calculateGrandTotal($bookingCreateDto);
|
|
|
|
if ($apiTotal !== $calculatedTotal) {
|
|
$this->logger->error('Price mismatch detected - payload incomplete', [
|
|
'apiTotal' => $apiTotal,
|
|
'calculatedTotal' => $calculatedTotal,
|
|
'difference' => abs($apiTotal - $calculatedTotal),
|
|
]);
|
|
$this->addFlash('error', 'Ein technischer Fehler ist aufgetreten.');
|
|
|
|
return $this->render('booking/create_step_3.html.twig', [
|
|
'bookingCreateDto' => $bookingCreateDto,
|
|
'form' => $form->createView(),
|
|
...$this->getSummaryVariables($bookingCreateDto),
|
|
]);
|
|
}
|
|
|
|
// Validation successful - proceed to confirmation step
|
|
$bookingCreateDto->currentStep = 4;
|
|
$this->bookingService->saveBookingCreateDto($request, $bookingCreateDto);
|
|
|
|
return $this->hxRedirect($request, $this->generateUrl('app_booking_create_step_4'));
|
|
} catch (\Exception $e) {
|
|
$this->logger->error('Booking inquiry exception', [
|
|
'exception' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
|
|
$this->addFlash('error', 'Ein technischer Fehler ist aufgetreten.');
|
|
|
|
return $this->render('booking/create_step_3.html.twig', [
|
|
'bookingCreateDto' => $bookingCreateDto,
|
|
'form' => $form->createView(),
|
|
...$this->getSummaryVariables($bookingCreateDto),
|
|
]);
|
|
}
|
|
}
|
|
|
|
return $this->render('booking/create_step_3.html.twig', [
|
|
'bookingCreateDto' => $bookingCreateDto,
|
|
'form' => $form->createView(),
|
|
...$this->getSummaryVariables($bookingCreateDto),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Handles HTMX refresh when payment method changes.
|
|
*/
|
|
#[Route('/bookings/create/payment/refresh', name: 'app_booking_create_step_3_refresh')]
|
|
public function refresh(Request $request): Response
|
|
{
|
|
$result = $this->getOrCreateBookingCreateDto($this->bookingService, $request);
|
|
if ($result instanceof Response) {
|
|
return $result;
|
|
}
|
|
$bookingCreateDto = $result;
|
|
|
|
$form = $this->createForm(BookingCreateStep3Type::class, $bookingCreateDto, [
|
|
'validation_groups' => false,
|
|
]);
|
|
$form->handleRequest($request);
|
|
|
|
$this->bookingService->saveBookingCreateDto($request, $bookingCreateDto);
|
|
|
|
return $this->render('booking/create_step_3.html.twig', [
|
|
'bookingCreateDto' => $bookingCreateDto,
|
|
'form' => $form->createView(),
|
|
...$this->getSummaryVariables($bookingCreateDto),
|
|
]);
|
|
}
|
|
}
|