# Deposits

Collect a deposit with the Stripe Payment Element on your own site, and complete the booking after a redirect (iDEAL, Bancontact).

Deposits only work if the venue has activated online payments (Stripe connected at `/integrations`). `GET /venue` shows this with `venue.booking.deposits_enabled`. The money goes straight to the venue (a direct charge on its Stripe account); you never touch card details.

## Does anything have to be paid?

- Up front (to show the price): `GET /deposit-policy?party_size=2&service_ids=…&variant_ids=…` → `{ required, amount_cents, currency, refund_window_hours }`.
- At the hold: `POST /holds` returns `deposit: { required, amount_cents, currency }`. That is the amount that counts.

A booking that requires a deposit never succeeds without one: `POST /bookings` without a valid payment returns `409 deposit_required`.

## The flow

1. **Hold** as always (`POST /holds`).
2. **Collect the guest's details** + Turnstile token.
3. **Start the payment**:

```js
const pay = await api(`/holds/${hold.hold_id}/deposit`, { method: 'POST', body: {
  guest: { first_name, last_name, email, phone },
  notes, locale: 'nl',
  turnstile_token: turnstileToken,
}});
// → { payment_intent_id, client_secret, amount_cents, currency, stripe_publishable_key, stripe_account }
```

The server checks the time again before a payment is created. The guest's details are stored with the payment, so the booking can be completed after a redirect.

4. **Show the Payment Element** with Stripe.js, on the venue's account:

```html
<script src="https://js.stripe.com/v3/"></script>
<div id="payment-element"></div>
```

```js
const stripe = Stripe(pay.stripe_publishable_key, { stripeAccount: pay.stripe_account });
const elements = stripe.elements({ clientSecret: pay.client_secret });
elements.create('payment').mount('#payment-element');

const { error, paymentIntent } = await stripe.confirmPayment({
  elements,
  redirect: 'if_required',
  confirmParams: { return_url: window.location.href.split('?')[0] },
});
if (error) { /* show error.message */ }
else if (paymentIntent.status === 'succeeded') { /* step 5 */ }
```

5. **Book** with the payment:

```js
await book({ hold_id: hold.hold_id, guest, notes, locale: 'nl',
             payment_intent_id: pay.payment_intent_id, turnstile_token: nieuwToken });
```

Turnstile tokens are single-use: render the widget again (or `turnstile.reset()`) before the booking call.

**Paid, but the hold has expired in the meantime?** (e.g. 3D Secure took a long time) `POST /bookings` then returns `409 hold_expired`. **Never** send a guest who has already paid back to the time selection: complete the booking with `POST /bookings/recover` and the `payment_intent_id` (see below). It books the time again if it is still free.

## Recovery after a redirect (iDEAL, Bancontact)

With some payment methods the guest leaves your site and comes back to your `return_url` with `?payment_intent=pi_…&redirect_status=succeeded` in the URL. Your JavaScript variables are then gone. Complete the booking with:

```js
const params = new URLSearchParams(location.search);
const pi = params.get('payment_intent');
if (pi) {
  const { booking } = await api('/bookings/recover', { method: 'POST', body: { payment_intent_id: pi } });
  // show the confirmation
}
```

- Only works for payments started with the **same key**; otherwise `404 not_found`.
- Already booked? Then you get the same booking back (`200`). A new booking returns `201`.
- Not paid yet (e.g. `processing`): `409 deposit_required` with `details.reason = "not_paid"`. Try again after a few seconds.
- If the time is gone in the meantime (the hold expired during a long payment), the payment is **not** used: `409 slot_unavailable`. Ask the guest to contact the venue; the venue can refund the deposit.

## Testing

Use a venue with a Stripe account in **test mode** and Stripe's test cards:

| Card | Result |
|---|---|
| `4242 4242 4242 4242` | succeeds |
| `4000 0025 0000 3155` | requires 3D Secure |
| `4000 0000 0000 9995` | declined (insufficient funds) |

Any future expiry date and any CVC work. For iDEAL and Bancontact, choose "Authorize" on Stripe's test page.

Apple Pay and Google Pay require your site's domain to be registered with the venue's Stripe account; check with Callyndar before you offer them.
