feat(#1): use stripe session to populate checkout success

This commit is contained in:
OllyNicholass
2024-05-15 01:26:57 +01:00
parent c4bd0d78bc
commit f5a6733698
6 changed files with 83 additions and 20 deletions
+3 -4
View File
@@ -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);
};
+21
View File
@@ -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);
};