mirror of
https://github.com/jcreek/SvelteKitSaasBoilerplate.git
synced 2026-07-13 19:13:48 +00:00
feat(#11): Add the description and fetching the images from Stripe on checkout success page
This commit is contained in:
@@ -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;
|
||||
};
|
||||
Reference in New Issue
Block a user