a tool for shared writing and social publishing
at feature/analytics 32 lines 873 B view raw
1"use server"; 2 3import { getIdentityData } from "./getIdentityData"; 4import { getStripe } from "stripe/client"; 5import { supabaseServerClient } from "supabase/serverClient"; 6import { Ok, Err, type Result } from "src/result"; 7 8export async function createBillingPortalSession( 9 returnUrl: string, 10): Promise<Result<{ url: string }, string>> { 11 const identity = await getIdentityData(); 12 if (!identity) { 13 return Err("Not authenticated"); 14 } 15 16 const { data: sub } = await supabaseServerClient 17 .from("user_subscriptions") 18 .select("stripe_customer_id") 19 .eq("identity_id", identity.id) 20 .single(); 21 22 if (!sub?.stripe_customer_id) { 23 return Err("No subscription found"); 24 } 25 26 const session = await getStripe().billingPortal.sessions.create({ 27 customer: sub.stripe_customer_id, 28 return_url: returnUrl, 29 }); 30 31 return Ok({ url: session.url }); 32}