Merge branch 'master' into 11-add-the-description-and-fetching-the-images-from-stripe-on-checkout-success-page

This commit is contained in:
Josh Creek
2024-10-22 19:38:46 +01:00
6 changed files with 117 additions and 31 deletions
+28 -1
View File
@@ -110,6 +110,31 @@ const createCustomerInStripe = async (uuid: string, email: string) => {
return newCustomer.id;
};
const getStripeCustomerId = async (email: string, uuid: string) => {
const { data: existingSupabaseCustomer, error: queryError } = await supabaseAdmin
.from('customers')
.select('*')
.eq('id', uuid)
.maybeSingle();
if (queryError) {
throw new Error(`Supabase customer lookup failed: ${queryError.message}`);
}
let stripeCustomerId: string | undefined;
if (existingSupabaseCustomer?.stripe_customer_id) {
const existingStripeCustomer = await stripeClient.customers.retrieve(
existingSupabaseCustomer.stripe_customer_id
);
stripeCustomerId = existingStripeCustomer.id;
} else {
const stripeCustomers = await stripeClient.customers.list({ email: email });
stripeCustomerId = stripeCustomers.data.length > 0 ? stripeCustomers.data[0].id : undefined;
}
return stripeCustomerId;
};
const createOrRetrieveCustomer = async ({ email, uuid }: { email: string; uuid: string }) => {
// Check if the customer already exists in Supabase
const { data: existingSupabaseCustomer, error: queryError } = await supabaseAdmin
@@ -468,5 +493,7 @@ export {
deductCredits,
getUserCredits,
getActiveProductsWithPrices,
getProductById
getProductById,
upsertCustomerToSupabase,
getStripeCustomerId
};