# Stripe Checkout webhook (#5736)

Invoices paid in Stripe Checkout are recorded and settled by a signed Stripe webhook, also when the customer never returns to the invoice page. The return page (`invoice.show?success=true`) and the InvoiceUpdater cron still record the same payment; all three lock the invoice row and register a PaymentIntent only once.

- Endpoint: `POST https://post.nuhet.se/stripe/checkout-webhook` (route `stripe.checkout-webhook`, `StripeCheckoutWebhookController@handle`). Stateless: no session, CSRF or login; throttled to 120 requests a minute.
- Events handled: `checkout.session.completed` and `checkout.session.async_payment_succeeded`. Every other type, and every session that is not the Checkout session stored on a Nuhet invoice, is answered 200 and ignored.
- The payload only identifies the session. It is retrieved again from Stripe with the app's Stripe key, recorded through `CardCharger::recordCheckoutSession()` and settled with `InvoiceSettlement::settle()` (expire date, notice, unsuspension). No mail is sent.
- Responses: 200 handled or ignored; 400 missing or invalid signature; 503 (`{"error":"unavailable"}`) no signing secret, or Stripe unreachable; 500 database failure, or a paid session without its PaymentIntent. Stripe retries every non-2xx answer for up to three days.
- On a credited invoice, a credit note or an invoice already paid, a paid session's payment is registered, as on the return page, so the money is visible in Nuhet, but the invoice is not settled again. It is logged as a warning and reported to Sentry (`StripeCheckoutWebhook: paid Checkout session on ...: refund or credit it by hand`); nothing is refunded automatically.
- Nothing is recorded, and a warning is logged and reported to Sentry, when the session's invoice references disagree, the session is not the one stored on the invoice, or the invoice got another session meanwhile. A Checkout payment below the invoice total is registered but not settled, with a warning: the cron may charge the remainder.
- Cashier's own routes (`POST /stripe/webhook`, `GET /stripe/payment/{id}`) are not registered (`Cashier::ignoreRoutes()`): Nuhet uses neither, and Cashier's webhook accepts unsigned events when no secret is configured.

## Signing secret

Production runs with a cached Laravel config. **Do not run `php artisan config:cache`**: it previously broke runtime `env()` consumers. The secret therefore does not come from `.env` or config on production. The app reads it on every request from a private file:

```
/var/www/html/storage/app/stripe-webhook-secret
```

The file holds only the `whsec_...` value and must start with `whsec_`; surrounding whitespace is ignored. Owner `www-data`, mode `600`. `storage/app` is ignored by Git, so **never run `git clean -x` (or `-X`) on production**: it deletes the secret file. In tests and uncached environments the config value `services.stripe.checkout_webhook_secret` overrides the file, and `services.stripe.checkout_webhook_secret_file` overrides its path.

Without a secret every request is refused with 503 and one error per hour is logged: `StripeCheckoutWebhook: no signing secret configured in ...`. Stripe keeps retrying, so events sent before the secret is installed are processed once it is.

## Production setup (Jonathan-approved steps, done by root)

1. In the Stripe Dashboard (live mode), Developers > Webhooks > Add endpoint: URL `https://post.nuhet.se/stripe/checkout-webhook`, events `checkout.session.completed` and `checkout.session.async_payment_succeeded`. Copy its signing secret.
2. On the Nuhet server as root, install the secret without putting it in a command argument, shell history or a log. Paste it after running this, then press Enter and Ctrl-D:

   ```
   install -d -o www-data -g www-data -m 700 /var/www/html/storage/app/private
   install -o www-data -g www-data -m 600 /dev/stdin /var/www/html/storage/app/stripe-webhook-secret
   stat -c '%U %G %a %s' /var/www/html/storage/app/stripe-webhook-secret
   ```

   Expect `www-data www-data 600` and a size of about 40 bytes. Check the prefix without printing the secret: `head -c 6 /var/www/html/storage/app/stripe-webhook-secret; echo` shows `whsec_`. To rotate, roll the secret in the Dashboard and repeat step 2. No config cache rebuild or restart is needed.

## Before and during deploy

- In the Stripe Dashboard, check that no existing webhook endpoint points at `https://post.nuhet.se/stripe/webhook`. Cashier's route is gone after this release, so such an endpoint gets 404 on every event; disable or delete it.
- If `bootstrap/cache/routes-*.php` exists on production, routes are cached: rebuild the route cache with `php artisan route:cache` as part of the deploy (never `config:cache`). Otherwise the new endpoint answers 404 and Cashier's `/stripe/webhook` stays registered.

## Verify after deploy

1. `php artisan route:list --path=stripe` lists only `POST stripe/checkout-webhook` (no `cashier.webhook` or `cashier.payment`).
2. `curl -s -o /dev/null -w '%{http_code}\n' -X POST https://post.nuhet.se/stripe/checkout-webhook` answers `400` (unsigned) once the secret is installed, `503` before.
3. Dashboard > the endpoint > Send test event `checkout.session.completed`: answered 200 and ignored, since the test session names no Nuhet invoice. Its delivery shows `{"received":true,"ignored":"not a Nuhet invoice session"}`.
4. After the next real Checkout payment, `storage/logs/laravel.log` has `StripeCheckoutWebhook: checkout.session.completed evt_... for invoice[ID]: recorded and settled`, the invoice is paid and has one payment with the PaymentIntent as transaction number. The Dashboard shows the delivery as 200.
5. Watch for `StripeCheckoutWebhook` warnings: mismatched invoice references, a session that is not the invoice's, or a paid session on a credited or already paid invoice. Each needs a person to check it in Stripe.
