mirror of
https://github.com/jcreek/SvelteKitSaasBoilerplate.git
synced 2026-07-13 11:03:49 +00:00
feat(#1): use stripe session to populate checkout success
This commit is contained in:
@@ -5,7 +5,7 @@ import stripe from 'stripe';
|
||||
const stripeClient = new stripe(PUBLIC_STRIPE_SECRET_KEY);
|
||||
|
||||
// Create a checkout session
|
||||
export const POST: RequestHandler = async ({ request }) => {
|
||||
export const POST: RequestHandler = async ({ request, cookies }) => {
|
||||
const formData = new URLSearchParams(await request.text());
|
||||
const items: { priceId: string; quantity: number }[] = [];
|
||||
|
||||
@@ -26,8 +26,7 @@ export const POST: RequestHandler = async ({ request }) => {
|
||||
cancel_url: `${request.headers.get('origin')}/checkout/cancelled`
|
||||
});
|
||||
|
||||
// TODO - Store the checkout session.id for access from the frontend
|
||||
// https://docs.stripe.com/api/checkout/sessions/retrieve
|
||||
|
||||
// Store the checkout session.id for access from the frontend
|
||||
cookies.set('checkout_session_id', session.id, { path: '/' });
|
||||
return redirect(303, session.url as string);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { PUBLIC_STRIPE_SECRET_KEY } from '$env/static/public';
|
||||
import stripe from 'stripe';
|
||||
import type { RequestHandler } from './$types';
|
||||
const stripeClient = new stripe(PUBLIC_STRIPE_SECRET_KEY);
|
||||
|
||||
export const GET: RequestHandler = async ({ cookies }) => {
|
||||
const sessionId = cookies.get('checkout_session_id');
|
||||
if (!sessionId) {
|
||||
return json({ error: 'No checkout session found' }, { status: 400 });
|
||||
}
|
||||
const checkoutSession = await stripeClient.checkout.sessions.retrieve(sessionId, { expand: ['line_items'] });
|
||||
|
||||
const statusResponse = {
|
||||
checkoutSession: checkoutSession,
|
||||
paymentStatus: checkoutSession.payment_status,
|
||||
lineItems: checkoutSession.line_items
|
||||
};
|
||||
|
||||
return json(statusResponse);
|
||||
};
|
||||
Reference in New Issue
Block a user