Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add user info to stripe checkout session #75

Merged
merged 1 commit into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
altaywtf marked this conversation as resolved.
Show resolved Hide resolved
},
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;
};
Loading