feat(#31): Add ability to record the stripe customer id in the customers table when a purchase is made

This commit is contained in:
Josh Creek
2024-10-20 18:37:40 +01:00
parent 03157a6d2f
commit 7d994d1c09
4 changed files with 80 additions and 8 deletions
+24 -4
View File
@@ -1,5 +1,7 @@
import { type RequestHandler, redirect } from '@sveltejs/kit';
import { stripe as stripeClient } from '$lib/utils/stripe';
import { getStripeCustomerId } from '$lib/utils/supabase/admin';
import type Stripe from 'stripe';
// Create a checkout session
export const POST: RequestHandler = async ({ request, cookies, locals: { safeGetSession } }) => {
@@ -26,9 +28,16 @@ export const POST: RequestHandler = async ({ request, cookies, locals: { safeGet
quantity: item.quantity
}));
const checkoutSession = await stripeClient.checkout.sessions.create({
customer_creation: 'always',
customer_email: userEmail,
let stripeCustomerId = null;
// Get the existing Stripe customer ID from Supabase, if it exists
try {
stripeCustomerId = await getStripeCustomerId(userEmail!, userId);
} catch (error) {
console.error('Error fetching Stripe customer ID:', error);
}
const stripeCheckoutSessionObject = {
line_items: lineItems,
mode: 'payment',
success_url: `${request.headers.get('origin')}/checkout/success`,
@@ -36,7 +45,18 @@ export const POST: RequestHandler = async ({ request, cookies, locals: { safeGet
metadata: {
userId: userId
}
});
} as Stripe.Checkout.SessionCreateParams;
if (stripeCustomerId) {
// If the user has a Stripe customer ID, attach it to the checkout session
stripeCheckoutSessionObject.customer = stripeCustomerId;
} else {
// If the user doesn't have a Stripe customer ID, create a new customer
stripeCheckoutSessionObject.customer_creation = 'always';
stripeCheckoutSessionObject.customer_email = userEmail;
}
const checkoutSession = await stripeClient.checkout.sessions.create(stripeCheckoutSessionObject);
// Store the checkout session.id for access from the frontend
cookies.set('checkout_session_id', checkoutSession.id, { path: '/' });
@@ -0,0 +1,16 @@
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { upsertCustomerToSupabase } from '$lib/utils/supabase/admin';
export const GET: RequestHandler = async ({ url, locals: { safeGetSession } }) => {
const stripeCustomerId = url.searchParams.get('stripeCustomerId');
const { session } = await safeGetSession();
if (!session || !stripeCustomerId) {
return json({ error: 'Unauthorized' }, { status: 401 });
}
await upsertCustomerToSupabase(session?.user.id, stripeCustomerId);
return json({ message: 'Customer created' });
};
+12 -3
View File
@@ -2,8 +2,8 @@ import type { PageLoad } from './$types';
import { redirect } from '@sveltejs/kit';
import Stripe from 'stripe';
export const load: PageLoad = async ({fetch}) => {
const response = await fetch('/api/checkout/status');
export const load: PageLoad = async ({ fetch }) => {
const response = await fetch('/api/checkout/status');
if (!response.ok) {
console.error('Failed to fetch checkout status');
return;
@@ -18,10 +18,19 @@ export const load: PageLoad = async ({fetch}) => {
throw redirect(303, '/checkout/cancelled');
}
// Save the stripe customer id in the customers table
const stripeCustomerId = checkoutSession.customer as string;
console.log('stripeCustomerId', stripeCustomerId);
const createCustomerResponse = await fetch(
'/api/checkout/create-customer?stripeCustomerId=' + stripeCustomerId
);
if (!createCustomerResponse.ok) {
console.error('Failed to create customer');
}
return {
checkoutSession,
lineItems,
paymentStatus
};
};