The public (internet-facing) app logged in as the PocketBase superuser and held the Stripe secret key, so any bug in it could become full control of the database. Baskets, slot holds, Stripe PaymentIntents, the webhook that creates orders, and the payment-result lookup now run in PocketBase JS hooks (pocketbase/pb_hooks/shop, routes under /api/shop/*, plus a cron that releases expired holds every minute). The public app's routes keep their URLs but only proxy to those routes and manage the basket cookie; it has no PocketBase or Stripe secret. Booking emails are still rendered and sent by the public app (same renderer as the admin preview) from order data the webhook hook returns; it records the outcome via a narrow endpoint guarded by INTERNAL_API_SECRET. Also: slot add and order creation are now transactional; shop listings load anonymously (already public via API rules); shop API errors always reach the browser as JSON; the superuser client and stripe package are removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KgF3Kf47LjPGHKQHszpUAv
25 lines
1.2 KiB
TypeScript
25 lines
1.2 KiB
TypeScript
import { json, error } from '@sveltejs/kit';
|
|
import type { RequestHandler } from './$types';
|
|
import { jsonErrors, sendAndRecordOrderEmails, shopFetch } from '$lib/server/shop';
|
|
import type { OrderEmailData } from '$lib/server/email';
|
|
|
|
// Stripe webhook endpoint (keeps the URL configured in the Stripe dashboard).
|
|
// PocketBase verifies the signature and creates the order; this app only
|
|
// sends the booking emails, which it can render (pocketbase/pb_hooks/shop).
|
|
export const POST: RequestHandler = jsonErrors(async ({ request }) => {
|
|
const signature = request.headers.get('stripe-signature');
|
|
if (!signature) throw error(400, 'Missing Stripe-Signature header');
|
|
|
|
// Forward the exact body: the signature covers the raw bytes.
|
|
const result = await shopFetch<{ received: boolean; orderEmail: OrderEmailData | null }>('/stripe-webhook', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': request.headers.get('content-type') || 'application/json', 'Stripe-Signature': signature },
|
|
body: await request.text()
|
|
});
|
|
|
|
// Not awaited: SMTP can be slow and Stripe only needs the 2xx. Failures are
|
|
// recorded on the order and can be resent from the admin's /orders.
|
|
if (result.orderEmail) void sendAndRecordOrderEmails(result.orderEmail);
|
|
|
|
return json({ received: true });
|
|
});
|