From c9a91725f22bf5ceb3099f0fabf04cfee73d2eae Mon Sep 17 00:00:00 2001 From: Altay Date: Sat, 16 Dec 2023 10:24:22 +0300 Subject: [PATCH] feat: add user info to stripe checkout session --- lib/services/account.ts | 21 +++++---------------- lib/services/stripe/checkout.ts | 16 ++++++++++++++-- lib/services/supabase/auth.ts | 12 ++++++++++++ 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/lib/services/account.ts b/lib/services/account.ts index 805f7e0..a30d1a3 100644 --- a/lib/services/account.ts +++ b/lib/services/account.ts @@ -4,27 +4,16 @@ import { cookies } from 'next/headers'; import { z } from 'zod'; import { DatabaseError } from '../errors'; +import { getUser } from './supabase/auth'; import { createSupabaseServerClient } from './supabase/server'; -const getUserId = async () => { - const supabase = createSupabaseServerClient(cookies()); - - const userQuery = await supabase.auth.getUser(); - - if (userQuery.error) { - throw userQuery.error; - } - - return userQuery.data.user.id; -}; - export const getAccountId = async () => { const supabase = createSupabaseServerClient(cookies()); const accountQuery = await supabase .from('account') .select('id') - .eq('user_id', await getUserId()) + .eq('user_id', (await getUser()).id) .single(); if (accountQuery.error) { @@ -40,7 +29,7 @@ export const fetchAccount = async () => { const accountQuery = await supabase .from('account') .select('*') - .eq('user_id', await getUserId()) + .eq('user_id', (await getUser()).id) .single(); if (accountQuery.error) { @@ -56,7 +45,7 @@ export const fetchAccountAICredits = async () => { const accountQuery = await supabase .from('account') .select('ai_credit') - .eq('user_id', await getUserId()) + .eq('user_id', (await getUser()).id) .single(); if (accountQuery.error) { @@ -85,7 +74,7 @@ export const updateAccountAICredits = async (amount: number) => { const accountQuery = await supabase .from('account') .update({ ai_credit: amount }) - .eq('user_id', await getUserId()) + .eq('user_id', (await getUser()).id) .select('ai_credit') .single(); diff --git a/lib/services/stripe/checkout.ts b/lib/services/stripe/checkout.ts index 59baf78..ebd9b8b 100644 --- a/lib/services/stripe/checkout.ts +++ b/lib/services/stripe/checkout.ts @@ -1,5 +1,7 @@ 'use server'; +import { getAccountId } from '../account'; +import { getUser } from '../supabase/auth'; import { stripe } from './client'; export const createCheckoutSession = async ({ @@ -9,17 +11,27 @@ export const createCheckoutSession = async ({ baseUrl: string; priceId: string; }) => { - const session = await stripe.checkout.sessions.create({ + const user = await getUser(); + const accountId = await getAccountId(); + + const checkoutSession = await stripe.checkout.sessions.create({ cancel_url: `${baseUrl}?status=cancel`, + customer_email: user.email, line_items: [ { price: priceId, quantity: 1, }, ], + metadata: { + accountId, + userId: user.id, + }, mode: 'payment', success_url: `${baseUrl}?status=success`, }); - return session; + return { + url: checkoutSession.url, + }; }; diff --git a/lib/services/supabase/auth.ts b/lib/services/supabase/auth.ts index a3470b0..00e1f4d 100644 --- a/lib/services/supabase/auth.ts +++ b/lib/services/supabase/auth.ts @@ -74,3 +74,15 @@ export const getIsAuthenticated = async () => { return session !== null; }; + +export const getUser = async () => { + const supabase = createSupabaseServerClient(cookies()); + + const userQuery = await supabase.auth.getUser(); + + if (userQuery.error) { + throw userQuery.error; + } + + return userQuery.data.user; +};