Booking & Payments
The booking lifecycle spans three services: the public frontend, the Express API, and Stripe.
Current flow
There is no standalone POST /bookings endpoint. Creating a booking and starting payment happen together:
POST /api/v1/stripe/create-checkout-session (user auth)The API reserves the booking as PENDING_PAYMENT, then creates the Stripe Checkout session and returns its URL.
Frontend
└─ POST /api/v1/stripe/create-checkout-session
├─ reserveBooking() ── validates activity, holds departure spots
├─ Stripe Checkout session (expires in 30 minutes)
├─ Payment row (PENDING, transactionId = session.id)
└─ { url } ── frontend redirects user to Stripe
User pays on Stripe ──> webhook ──> fulfilCheckout()
└─ conditional updateMany on PENDING_PAYMENT ──> CONFIRMEDReservation and pricing
reserveBooking() validates:
- Activity exists and is
published = trueandstatus = APPROVED. - Activity has a positive
price. - If a
departureIdwas supplied:- Departure belongs to the activity, has not ended, and is not
FULL. - Spots are held in one transaction:
departure.availableSpots -= groupSizewherestatus IN (OPEN, DELAYED)andavailableSpots >= groupSize.
- Departure belongs to the activity, has not ended, and is not
- Otherwise (no departure): guest capacity and activity-level availability (
unavailableDatesvs the chosen date range) are checked.
Computed fields on the booking:
days= departure days (or activity duration in days) →endDate = startDate + days − 1.totalPrice=departure.price × groupSizefor departure-based bookings, elseactivity.price × groupSize.
On any failure after the hold, the pending booking/payment is released and the hold is returned.
Money and revenue
| Value | Source |
|---|---|
| Platform commission | PLATFORM_COMMISSION_RATE (default 0.3) |
| Minimum payout | MIN_PAYOUT_AMOUNT (default 50) |
| Supplier net per booking | gross − commission (rounded per booking via splitRevenue) |
| Supplier balance | confirmed net − PAID − PENDING payouts |
Commission/payout math lives in src/lib/revenue.ts.
Idempotent fulfilment
fulfillCheckout(session) is the single place that confirms a booking. Both the Stripe webhook and the POST /checkout-session/:sessionId status poll can call it.
The confirm step is transactional and conditional:
const confirmed = await tx.booking.updateMany({
where: { id: bookingId, status: "PENDING_PAYMENT" },
data: { status: "CONFIRMED" },
});
if (confirmed.count === 0) return { booking, justConfirmed: false };Only the path that flips the row wins; the other observes count = 0 and skips the follow-up writes and the confirmation email.
After commit:
Payment.status = SUCCESSand the Stripe payment intent id are written.Activity.bookingsCountis incremented once.- If this run flipped the status, confirmation emails go out (traveler + supplier, supplier reply routed via
replyToto the traveller).
If fulfilment throws before commit, nothing is written (transaction); the webhook then 500s and Stripe retries — safe because the conditional makes the retry a no-op.
Stripe state machine
Checkout session (/create-checkout-session):
mode: "payment", card only,customer_emailwhen known.client_reference_id: booking.id,metadata: { bookingId },payment_intent_data.metadata: { bookingId }.expires_at= now +HOLD_MINUTES(30) — the checkout hold window.success_url: {FRONTEND_BASE_URL}/my-bookings?payment=success&session_id={CHECKOUT_SESSION_ID}cancel_url: {FRONTEND_BASE_URL}/cancel?booking_id={booking.id}&return_to={...}
Expiry / failure handling:
checkout.session.expiredorcheckout.session.async_payment_failed→ release the pending booking.- Releasing flips
Booking.status = FAILED,Payment.status = FAILED, and increments the departure'savailableSpotsback (transactional). - A Stripe session that expires without payment can also be expired/cancelled explicitly via
POST /stripe/cancel-pending/:bookingId, which releases the hold for the current user.
Endpoint reference
| Method | Endpoint | Auth | Purpose |
|---|---|---|---|
POST | /api/v1/stripe/create-checkout-session | user | Create booking + checkout session, return { url } |
GET | /api/v1/stripe/checkout-session/:sessionId | user | Retrieve session/payment state; confirm if paid |
POST | /api/v1/stripe/cancel-pending/:bookingId | user | Expire session / release hold if not paid |
POST | /api/v1/stripe/webhook | Stripe signature | Fulfill on paid, release on expired/failed |
Only GET /api/v1/bookings/my-bookings exists for traveller booking reads.
Booking status transitions
| Status | Meaning | Transition |
|---|---|---|
PENDING_PAYMENT | Booking held, awaiting Stripe | → CONFIRMED, → FAILED (expiry/release) |
CONFIRMED | Payment captured | → CANCELLED |
CANCELLED | Cancelled by user/admin | — |
FAILED | Hold released / payment failed / cancelled | → retriable via new booking flow |
Payment.status runs PENDING → SUCCESS | FAILED | REFUNDED.
Refunds
No automated refund path exists. Refunds are processed through the Stripe Dashboard; after the funds are returned, the booking should be moved to CANCELLED (and the platform payout bookkeeping updated accordingly). The platform does not currently issue refunds from the API.
Webhooks processed
From src/lib/stripe.ts webhook handler (signature-verified with STRIPE_WHSEC_KEY):
checkout.session.completed— fulfilcheckout.session.async_payment_succeeded— fulfilcheckout.session.expired— release pending bookingcheckout.session.async_payment_failed— release pending booking
Testing
- Set Stripe to test mode (
sk_test_…). - Forward the webhook:
stripe listen --forward-to localhost:3000/api/v1/stripe/webhook. - Payment method:
4242 4242 4242 4242(any future expiry, any CVC). - Verify booking status becomes
CONFIRMED, paymentSUCCESS, and emails send once even if the status poll triggers the same fulfilment.