This update refactors the handling of checkout and billing portal sessions into a new UserBillingService. The new service centralizes related functions that were previously scattered throughout server-actions and ensures that all related functionality is handled in a single place. This improves maintainability and coherence of the code related to billing sessions.
34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
'use server';
|
|
|
|
import { redirect } from 'next/navigation';
|
|
|
|
import { z } from 'zod';
|
|
|
|
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
|
|
|
|
import { PersonalAccountCheckoutSchema } from './_lib/schema/personal-account-checkout.schema';
|
|
import { UserBillingService } from './_lib/server/user-billing.service';
|
|
|
|
/**
|
|
* @description Creates a checkout session for a personal account.
|
|
*/
|
|
export async function createPersonalAccountCheckoutSession(
|
|
params: z.infer<typeof PersonalAccountCheckoutSchema>,
|
|
) {
|
|
// parse the parameters
|
|
const data = PersonalAccountCheckoutSchema.parse(params);
|
|
const service = new UserBillingService(getSupabaseServerActionClient());
|
|
|
|
return await service.createCheckoutSession(data);
|
|
}
|
|
|
|
/**
|
|
* @description Creates a billing Portal session for a personal account
|
|
*/
|
|
export async function createPersonalAccountBillingPortalSession() {
|
|
const service = new UserBillingService(getSupabaseServerActionClient());
|
|
const url = await service.createBillingPortalSession();
|
|
|
|
return redirect(url);
|
|
}
|