Buchungsprozess: Angebots-Snapshot, atomare Annahme, Auto-Angebot, Kanalfreigabe
Deploy FeWo mit PipelineManager / deploy (push) Failing after 3m38s
Deploy FeWo mit PipelineManager / deploy (push) Failing after 3m38s
- Angebotsbedingungen und Zahlungsdaten werden je Version eingefroren (offer_terms, offer_hash) - Annahme in einer gesperrten Transaktion, idempotent, mit Nachweis; Mail- und Portalfehler machen eine gespeicherte Buchung nicht ungueltig (Postfach, Nachversand) - Je Wohnung: manuelle/automatische Angebote, optionale Vorreservierung - Automatische Freigabe abhaengig von aktuellen Importen und Feed-Abruf bei Sofortbuchungsportalen - Deutsche Fehlerseiten, Buchungsseite zeigt Angebots- und Zustellstatus - Dokumentation: BOOKING-E2E-*.md, BOOKING-COMPLIANCE-HANDOVER.md Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e7cc37a15b
commit
eafab8a2cb
47 files changed
+2099
-104
No files matched your search
+141
-24
@@ -2,53 +2,91 @@
|
||||
|
||||
namespace App\Actions;
|
||||
|
||||
use App\Availability\CalendarClearance;
|
||||
use App\Documents\OfferTerms;
|
||||
use App\Enums\BookingStatus;
|
||||
use App\Enums\InboxMessageKind;
|
||||
use App\Exceptions\ReservationException;
|
||||
use App\Filament\Resources\Bookings\BookingResource;
|
||||
use App\Mail\OfferAnswered;
|
||||
use App\Models\Booking;
|
||||
use App\Support\Content;
|
||||
use App\Support\Inbox;
|
||||
use App\Support\Settings;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* The guest accepts the offer on the signed offer page: the booking is concluded. A record of what was
|
||||
* accepted is kept (offer version, time, fingerprints of price and terms – no IP address), then the booking
|
||||
* is confirmed as usual (contract + confirmation mail as electronic acknowledgement).
|
||||
* The guest accepts the offer on the signed offer page: the booking is concluded. Everything happens in one
|
||||
* locked transaction (property + booking row): status, validity, integrity of the frozen offer and availability
|
||||
* are checked, the acceptance record is written and the booking becomes binding – or nothing changes at all.
|
||||
* Accepting the same version again is idempotent (no second booking, no second mail). Mails follow after the commit
|
||||
* and never undo the acceptance.
|
||||
*/
|
||||
class AcceptOffer
|
||||
{
|
||||
public function __construct(private ConfirmBooking $confirm) {}
|
||||
public function __construct(private ConfirmBooking $confirm, private Inbox $inbox, private CalendarClearance $clearance) {}
|
||||
|
||||
/**
|
||||
* @return bool true when this call concluded the booking, false when the version had already been accepted
|
||||
*
|
||||
* @throws ReservationException
|
||||
*/
|
||||
public function handle(Booking $booking, int $offerVersion): void
|
||||
public function handle(Booking $booking, int $offerVersion): bool
|
||||
{
|
||||
$this->ensureAcceptable($booking, $offerVersion);
|
||||
|
||||
$booking->update([
|
||||
'accepted_at' => now(),
|
||||
'acceptance' => [
|
||||
'offer_version' => $booking->offer_version,
|
||||
'accepted_at' => now()->toIso8601String(),
|
||||
'total' => $booking->total,
|
||||
'quote_sha256' => hash('sha256', json_encode($booking->quote)),
|
||||
'terms_sha256' => hash('sha256', (string) Content::get('contract.terms').'|'.Content::get('stay.cancellation')),
|
||||
'via' => 'offer_link',
|
||||
],
|
||||
]);
|
||||
|
||||
$this->confirm->handle($booking);
|
||||
if ($booking->status === BookingStatus::OfferSent) {
|
||||
$this->ensureCalendarFresh($booking);
|
||||
}
|
||||
|
||||
try {
|
||||
Mail::to(Settings::notificationRecipients())->send(new OfferAnswered($booking, accepted: true));
|
||||
} catch (Throwable $exception) {
|
||||
report($exception);
|
||||
$accepted = DB::transaction(function () use ($booking, $offerVersion): bool {
|
||||
$booking->lockForTransition();
|
||||
|
||||
if ($this->alreadyAccepted($booking, $offerVersion)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->ensureAcceptable($booking, $offerVersion);
|
||||
$this->ensureUnchanged($booking);
|
||||
|
||||
$validUntil = $booking->hold_expires_at?->toIso8601String();
|
||||
$this->confirm->bind($booking);
|
||||
$booking->update([
|
||||
'accepted_at' => now(),
|
||||
'acceptance' => [
|
||||
'offer_version' => $booking->offer_version,
|
||||
'accepted_at' => now()->toIso8601String(),
|
||||
'offer_sent_at' => $booking->offer_sent_at?->toIso8601String(),
|
||||
'valid_until' => $validUntil,
|
||||
'total' => $booking->total,
|
||||
'offer_hash' => $booking->offer_hash,
|
||||
'quote_sha256' => hash('sha256', json_encode($booking->quote)),
|
||||
'terms_sha256' => hash('sha256', json_encode($booking->offer_terms['texts'] ?? [])),
|
||||
'accept_button' => $booking->offerText('offer.accept_button'),
|
||||
'via' => 'offer_link',
|
||||
],
|
||||
]);
|
||||
|
||||
return true;
|
||||
});
|
||||
} catch (ReservationException $exception) {
|
||||
$this->noteRejection($booking, $offerVersion, $exception);
|
||||
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
if ($accepted) {
|
||||
$this->confirm->deliver($booking);
|
||||
$this->notifyLandlord($booking);
|
||||
}
|
||||
|
||||
return $accepted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that make the offer page show the form: version, status and validity (no locks, read only).
|
||||
*
|
||||
* @throws ReservationException
|
||||
*/
|
||||
public function ensureAcceptable(Booking $booking, int $offerVersion): void
|
||||
@@ -65,4 +103,83 @@ public function ensureAcceptable(Booking $booking, int $offerVersion): void
|
||||
throw ReservationException::offerExpired();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The frozen offer must still match its fingerprint (price, dates, terms); offers made before the
|
||||
* fingerprint existed have none and are not checked.
|
||||
*
|
||||
* @throws ReservationException
|
||||
*/
|
||||
public function ensureUnchanged(Booking $booking): void
|
||||
{
|
||||
if ($booking->offer_hash !== null && ! hash_equals($booking->offer_hash, OfferTerms::hash($booking))) {
|
||||
throw ReservationException::offerChanged();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Before the guest becomes bound, the imported portal calendars must be recent (an import is tried first –
|
||||
* network access, so outside the transaction). Otherwise nothing is concluded and the landlord is told.
|
||||
*
|
||||
* @throws ReservationException
|
||||
*/
|
||||
private function ensureCalendarFresh(Booking $booking): void
|
||||
{
|
||||
$property = $booking->property;
|
||||
$this->clearance->refresh($property);
|
||||
|
||||
$stale = $this->clearance->staleImports($property);
|
||||
if ($stale->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->inbox->post(
|
||||
InboxMessageKind::SyncFailed,
|
||||
'accept-'.$booking->id.':'.now()->format('YmdH'),
|
||||
'Annahme von '.$booking->reference.' zurückgestellt: Kalenderstand nicht aktuell',
|
||||
'Der Gast wollte das Angebot annehmen, aber der Import von '.$stale->pluck('name')->implode(', ').' ist nicht aktuell. Bitte Kalenderabgleich prüfen und den Gast kontaktieren.',
|
||||
actionUrl: BookingResource::getUrl('view', ['record' => $booking]),
|
||||
propertyId: $booking->property_id,
|
||||
);
|
||||
|
||||
throw ReservationException::calendarUnverified();
|
||||
}
|
||||
|
||||
private function alreadyAccepted(Booking $booking, int $offerVersion): bool
|
||||
{
|
||||
return in_array($booking->status, BookingStatus::occupying(), true)
|
||||
&& $booking->accepted_at !== null
|
||||
&& ($booking->acceptance['offer_version'] ?? null) === $offerVersion;
|
||||
}
|
||||
|
||||
private function noteRejection(Booking $booking, int $offerVersion, ReservationException $exception): void
|
||||
{
|
||||
if ($offerVersion !== $booking->offer_version || $booking->status !== BookingStatus::OfferSent) {
|
||||
return;
|
||||
}
|
||||
|
||||
$reasonIsUnexpected = $exception->getMessage() === ReservationException::offerChanged()->getMessage()
|
||||
|| $exception->getMessage() === ReservationException::notAvailable()->getMessage();
|
||||
if (! $reasonIsUnexpected) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->inbox->post(
|
||||
InboxMessageKind::OfferRejected,
|
||||
'booking-'.$booking->id.':v'.$offerVersion,
|
||||
'Annahme von Angebot '.$booking->reference.' abgewiesen',
|
||||
'Der Gast wollte das Angebot annehmen, aber: '.$exception->getMessage().' Bitte den Gast kontaktieren oder ein neues Angebot senden.',
|
||||
actionUrl: BookingResource::getUrl('view', ['record' => $booking]),
|
||||
propertyId: $booking->property_id,
|
||||
);
|
||||
}
|
||||
|
||||
private function notifyLandlord(Booking $booking): void
|
||||
{
|
||||
try {
|
||||
Mail::to(Settings::notificationRecipients())->send(new OfferAnswered($booking, accepted: true));
|
||||
} catch (Throwable $exception) {
|
||||
Log::warning('Benachrichtigung über Annahme nicht zugestellt', ['booking' => $booking->id, 'exception' => $exception::class]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user