wip: submit booking to api

This commit is contained in:
Björn Fromme
2025-10-06 11:22:02 +02:00
parent 139ec3c1db
commit 7f1ef4059d
34 changed files with 3316 additions and 31 deletions
@@ -33,18 +33,23 @@ trait BookingExceptionHandlerTrait
return $bookingService->getOrCreateBookingCreateDto($request);
} catch (BookingSessionNotFoundException $e) {
$this->addFlash('error', 'Ihre Buchungssitzung ist abgelaufen. Bitte starten Sie eine neue Buchung.');
return $this->redirectToRoute('app_booking_create_error');
} catch (TravelNotFoundException $e) {
$this->addFlash('error', 'Die angeforderte Reise wurde nicht gefunden.');
return $this->redirectToRoute('app_booking_create_error');
} catch (HotelNotFoundException $e) {
$this->addFlash('error', 'Das angeforderte Hotel wurde nicht gefunden.');
return $this->redirectToRoute('app_booking_create_error');
} catch (HotelNotInTravelException $e) {
$this->addFlash('error', 'Das Hotel ist für diese Reise nicht verfügbar.');
return $this->redirectToRoute('app_booking_create_error');
} catch (NoRoomsAvailableException $e) {
$this->addFlash('error', 'Für diese Reise sind aktuell keine Zimmer verfügbar.');
return $this->redirectToRoute('app_booking_create_error');
}
}
@@ -59,8 +64,8 @@ trait BookingExceptionHandlerTrait
{
try {
return $bookingService->getOrCreateBookingCreateDto($request);
} catch (BookingSessionNotFoundException | TravelNotFoundException | HotelNotFoundException | HotelNotInTravelException | NoRoomsAvailableException $e) {
} catch (BookingSessionNotFoundException|TravelNotFoundException|HotelNotFoundException|HotelNotInTravelException|NoRoomsAvailableException $e) {
return new Response('', 400);
}
}
}
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace App\Controller\Booking;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class BookingSuccessController extends AbstractController
{
#[Route('/bookings/success', name: 'app_booking_success')]
public function success(Request $request): Response
{
$bookingNumber = $request->getSession()->getFlashBag()->get('booking_number')[0] ?? null;
// Redirect to homepage if no booking number (direct access or refresh)
if (null === $bookingNumber) {
return $this->redirect('https://www.ep-reisen.de');
}
return $this->render('booking/success.html.twig', [
'bookingNumber' => $bookingNumber,
]);
}
}
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Controller\Booking;
use App\BusProNet\XmlLoader\AgencyLoader;
use App\Exception\HotelNotFoundException;
use App\Exception\HotelNotInTravelException;
use App\Exception\NoRoomsAvailableException;
@@ -25,6 +26,7 @@ class CreateInitController extends AbstractController
{
public function __construct(
private readonly BookingService $bookingService,
private readonly AgencyLoader $agencyLoader,
) {
}
@@ -34,6 +36,10 @@ class CreateInitController extends AbstractController
* This endpoint provides a clean way to start the booking flow with just
* dateId and hotelId parameters. It clears any existing booking session
* and creates a fresh BookingCreateDto before redirecting to step 1.
*
* Optionally accepts an agency code parameter. If provided and valid, the
* corresponding agency ID is stored in the booking. If not provided or invalid,
* defaults to agency code '0001'.
*/
#[Route('/bookings/create/{dateId}/{hotelId}', name: 'app_booking_create_init', requirements: ['dateId' => '\d+', 'hotelId' => '\d+'])]
public function init(Request $request, int $dateId, int $hotelId): Response
@@ -42,8 +48,11 @@ class CreateInitController extends AbstractController
// Clear any existing booking session to ensure fresh start
$this->bookingService->clearBookingSession($request);
// Determine agency ID from optional query parameter
$agencyId = $this->resolveAgencyId($request->query->get('agency'));
// Create fresh booking session with the provided parameters
$this->bookingService->startFreshBooking($request, $dateId, $hotelId);
$this->bookingService->startFreshBooking($request, $dateId, $hotelId, $agencyId);
// Redirect to step 1 of the booking flow
return $this->redirectToRoute('app_booking_create_step_1');
@@ -58,6 +67,37 @@ class CreateInitController extends AbstractController
}
}
/**
* Resolves the agency ID from the provided agency code.
*
* If the code is null or the agency is not found, returns the default agency ID.
*
* @param string|null $agencyCode The agency code from the request parameter
*
* @return int|null The agency ID, or null if default agency not found
*/
private function resolveAgencyId(?string $agencyCode): ?int
{
// Use default agency if no code provided
if (null === $agencyCode || '' === trim($agencyCode)) {
$defaultAgency = $this->agencyLoader->loadDefault();
return $defaultAgency?->id;
}
// Try to find agency by provided code
$agency = $this->agencyLoader->loadByCode($agencyCode);
// Fall back to default agency if code not found
if (null === $agency) {
$defaultAgency = $this->agencyLoader->loadDefault();
return $defaultAgency?->id;
}
return $agency->id;
}
/**
* Displays user-friendly error messages for booking initialization failures.
*
@@ -4,15 +4,17 @@ declare(strict_types=1);
namespace App\Controller\Booking;
use App\BusProNet\Constants;
use App\BusProNet\ApiClient;
use App\BusProNet\Model\Notification;
use App\Controller\Traits\HtmxControllerTrait;
use App\Form\BookingCreateStep3Type;
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;
use Symfony\Component\Security\Http\Attribute\IsGranted;
/**
* Handles the third step of the booking creation process (payment method selection).
@@ -25,6 +27,9 @@ class CreateStep3Controller extends AbstractController
public function __construct(
private readonly BookingService $bookingService,
private readonly ApiClient $apiClient,
private readonly BookingPriceCalculatorService $priceCalculator,
private readonly LoggerInterface $logger,
) {
}
@@ -49,10 +54,74 @@ class CreateStep3Controller extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$bookingCreateDto->currentStep = 4;
$this->bookingService->saveBookingCreateDto($request, $bookingCreateDto);
try {
// Validate booking data with API (inquiry)
$inquiryResponse = $this->apiClient->createBookingInquiry($bookingCreateDto);
return $this->redirectToRoute('app_booking_create_step_4');
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->redirectToRoute('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', [
@@ -4,13 +4,17 @@ declare(strict_types=1);
namespace App\Controller\Booking;
use App\BusProNet\ApiClient;
use App\BusProNet\Model\Notification;
use App\Controller\Traits\HtmxControllerTrait;
use App\Form\BookingCreateStep4Type;
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 fourth step of the booking creation process (confirmation).
*/
@@ -22,6 +26,8 @@ class CreateStep4Controller extends AbstractController
public function __construct(
private readonly BookingService $bookingService,
private readonly ApiClient $apiClient,
private readonly LoggerInterface $logger,
) {
}
@@ -46,13 +52,49 @@ class CreateStep4Controller extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// TODO: Perform inquiry API call
// TODO: If inquiry successful, perform booking API call
// TODO: Clear session and redirect to success page
try {
// Submit final booking (already validated in Step 3)
$bookingResponse = $this->apiClient->createBooking($bookingCreateDto);
$this->addFlash('success', 'Buchung erfolgreich abgeschlossen.');
if ($bookingResponse instanceof Notification) {
$this->addFlash('error', $bookingResponse->message);
return $this->redirectToRoute('app_booking_create_step_4');
return $this->render('booking/create_step_4.html.twig', [
'bookingCreateDto' => $bookingCreateDto,
'form' => $form->createView(),
...$this->getSummaryVariables($bookingCreateDto),
]);
}
if (false === $bookingResponse->isBookingSuccessful()) {
$this->addFlash('error', 'Buchung konnte nicht erstellt werden.');
return $this->render('booking/create_step_4.html.twig', [
'bookingCreateDto' => $bookingCreateDto,
'form' => $form->createView(),
...$this->getSummaryVariables($bookingCreateDto),
]);
}
// Success: Store booking number in flash and clear session
$this->addFlash('booking_number', $bookingResponse->transactionNumber);
$this->bookingService->clearBookingCreateDto($request);
return $this->redirectToRoute('app_booking_success');
} catch (\Exception $e) {
$this->logger->error('Booking creation failed', [
'exception' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
$this->addFlash('error', 'Ein technischer Fehler ist aufgetreten.');
return $this->render('booking/create_step_4.html.twig', [
'bookingCreateDto' => $bookingCreateDto,
'form' => $form->createView(),
...$this->getSummaryVariables($bookingCreateDto),
]);
}
}
return $this->render('booking/create_step_4.html.twig', [
@@ -61,4 +103,4 @@ class CreateStep4Controller extends AbstractController
...$this->getSummaryVariables($bookingCreateDto),
]);
}
}
}