import { getPageBlocks } from '$lib/server/content'; import { createDonationCheckout } from '$lib/server/stripe'; import { fail, redirect } from '@sveltejs/kit'; import type { PageServerLoad, Actions } from './$types'; export const load: PageServerLoad = async ({ url, parent }) => { const parentData = await parent(); const content = await getPageBlocks('donate'); const paymentStatus = url.searchParams.get('payment'); const stripeEnabled = Boolean(process.env.STRIPE_SECRET_KEY); return { ...parentData, content, stripeEnabled, paymentStatus }; }; export const actions: Actions = { donate: async ({ request, url }) => { if (!process.env.STRIPE_SECRET_KEY) { return fail(503, { donateError: 'Online payment is not available at this time.' }); } const fd = await request.formData(); const amountStr = fd.get('amount')?.toString().trim(); const email = fd.get('email')?.toString().trim() || undefined; if (!amountStr) { return fail(400, { donateError: 'Please enter a donation amount.' }); } const amount = parseFloat(amountStr); if (isNaN(amount) || amount < 1) { return fail(400, { donateError: 'Please enter a valid amount of at least $1.00.' }); } if (amount > 99999) { return fail(400, { donateError: 'Please enter a smaller amount or contact us directly.' }); } const baseUrl = url.origin; const checkoutUrl = await createDonationCheckout({ amount: Math.round(amount * 100), // convert to cents donorEmail: email, successUrl: `${baseUrl}/donate?payment=success`, cancelUrl: `${baseUrl}/donate?payment=cancelled` }); if (!checkoutUrl) { return fail(503, { donateError: 'Unable to create payment session. Please try again later.' }); } redirect(303, checkoutUrl); } };