Skip to content

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 ──> CONFIRMED

Reservation and pricing ​

reserveBooking() validates:

  • Activity exists and is published = true and status = APPROVED.
  • Activity has a positive price.
  • If a departureId was supplied:
    • Departure belongs to the activity, has not ended, and is not FULL.
    • Spots are held in one transaction: departure.availableSpots -= groupSize where status IN (OPEN, DELAYED) and availableSpots >= groupSize.
  • Otherwise (no departure): guest capacity and activity-level availability (unavailableDates vs 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 × groupSize for departure-based bookings, else activity.price × groupSize.

On any failure after the hold, the pending booking/payment is released and the hold is returned.

Money and revenue ​

ValueSource
Platform commissionPLATFORM_COMMISSION_RATE (default 0.3)
Minimum payoutMIN_PAYOUT_AMOUNT (default 50)
Supplier net per bookinggross − commission (rounded per booking via splitRevenue)
Supplier balanceconfirmed 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:

ts
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 = SUCCESS and the Stripe payment intent id are written.
  • Activity.bookingsCount is incremented once.
  • If this run flipped the status, confirmation emails go out (traveler + supplier, supplier reply routed via replyTo to 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_email when 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.expired or checkout.session.async_payment_failed → release the pending booking.
  • Releasing flips Booking.status = FAILED, Payment.status = FAILED, and increments the departure's availableSpots back (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 ​

MethodEndpointAuthPurpose
POST/api/v1/stripe/create-checkout-sessionuserCreate booking + checkout session, return { url }
GET/api/v1/stripe/checkout-session/:sessionIduserRetrieve session/payment state; confirm if paid
POST/api/v1/stripe/cancel-pending/:bookingIduserExpire session / release hold if not paid
POST/api/v1/stripe/webhookStripe signatureFulfill on paid, release on expired/failed

Only GET /api/v1/bookings/my-bookings exists for traveller booking reads.

Booking status transitions ​

StatusMeaningTransition
PENDING_PAYMENTBooking held, awaiting Stripe→ CONFIRMED, → FAILED (expiry/release)
CONFIRMEDPayment captured→ CANCELLED
CANCELLEDCancelled by user/admin—
FAILEDHold 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 — fulfil
  • checkout.session.async_payment_succeeded — fulfil
  • checkout.session.expired — release pending booking
  • checkout.session.async_payment_failed — release pending booking

Testing ​

  1. Set Stripe to test mode (sk_test_…).
  2. Forward the webhook: stripe listen --forward-to localhost:3000/api/v1/stripe/webhook.
  3. Payment method: 4242 4242 4242 4242 (any future expiry, any CVC).
  4. Verify booking status becomes CONFIRMED, payment SUCCESS, and emails send once even if the status poll triggers the same fulfilment.

Built with VitePress