Skip to content

Commit

Permalink
feat: add user info to stripe checkout session
Browse files Browse the repository at this point in the history
  • Loading branch information
altaywtf committed Dec 16, 2023
1 parent 4487578 commit c9a9172
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
21 changes: 5 additions & 16 deletions lib/services/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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();

Expand Down
16 changes: 14 additions & 2 deletions lib/services/stripe/checkout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use server';

import { getAccountId } from '../account';
import { getUser } from '../supabase/auth';
import { stripe } from './client';

export const createCheckoutSession = async ({
Expand All @@ -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,
};
};
12 changes: 12 additions & 0 deletions lib/services/supabase/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

0 comments on commit c9a9172

Please sign in to comment.