feat(#11): Add the description and fetching the images from Stripe on checkout success page

This commit is contained in:
Josh Creek
2024-10-22 18:40:32 +01:00
parent 7e458b7297
commit 7825d29081
4 changed files with 38 additions and 12 deletions
@@ -0,0 +1,36 @@
import { redirect } from '@sveltejs/kit';
import Stripe from 'stripe';
import { getProductById } from '$lib/utils/supabase/admin';
export const load = async ({ fetch }) => {
const response = await fetch('/api/checkout/status');
if (!response.ok) {
console.error('Failed to fetch checkout status');
return;
}
const responseJson = await response.json();
const checkoutSession: Stripe.Checkout.Session = responseJson.checkoutSession;
const lineItems: Stripe.LineItem[] = responseJson.lineItems.data;
const paymentStatus: Stripe.Checkout.Session.PaymentStatus = responseJson.paymentStatus;
if (paymentStatus == 'unpaid') {
throw redirect(303, '/checkout/cancelled');
}
for (const lineItem of lineItems as LineItemForCheckout[]) {
const product = await getProductById(lineItem.price?.product.toString() ?? '');
lineItem.imgSrc = product.images ? product.images[0] : '';
lineItem.productDescription = product.description ?? '';
}
return {
checkoutSession,
lineItems
};
};
type LineItemForCheckout = Stripe.LineItem & {
imgSrc: string;
productDescription: string;
};