|
| 1 | +"use server"; |
| 2 | + |
| 3 | +import { headers } from "next/headers"; |
| 4 | +import { and, eq } from "drizzle-orm"; |
| 5 | +import type Stripe from "stripe"; |
| 6 | + |
| 7 | +import { db } from "@/lib/db"; |
| 8 | +import { coachingSessions, serviceBookings, services } from "@/lib/db/schema"; |
| 9 | +import { getOrCreateStripeCustomer, stripe } from "@/lib/stripe"; |
| 10 | +import { createClient } from "@/utils/supabase/server"; |
| 11 | + |
| 12 | +export type CheckoutResult = { url: string } | { error: string }; |
| 13 | + |
| 14 | +async function getDefaultPriceId(stripeProductId: string): Promise<string> { |
| 15 | + const product = await stripe.products.retrieve(stripeProductId); |
| 16 | + const defaultPriceId = product.default_price; |
| 17 | + if (!defaultPriceId) { |
| 18 | + throw new Error(`Stripe product ${stripeProductId} has no default price`); |
| 19 | + } |
| 20 | + return defaultPriceId as string; |
| 21 | +} |
| 22 | + |
| 23 | +async function getRequestOrigin(): Promise<string> { |
| 24 | + const origin = (await headers()).get("origin"); |
| 25 | + if (!origin) { |
| 26 | + throw new Error("Missing request origin"); |
| 27 | + } |
| 28 | + return origin; |
| 29 | +} |
| 30 | + |
| 31 | +type CreateSessionResult = |
| 32 | + | { session: Stripe.Checkout.Session } |
| 33 | + | { error: string }; |
| 34 | + |
| 35 | +async function createStripeCheckoutSession(params: { |
| 36 | + userId: string; |
| 37 | + email: string; |
| 38 | + stripeProductId: string; |
| 39 | + metadata: Record<string, string>; |
| 40 | +}): Promise<CreateSessionResult> { |
| 41 | + const priceId = await getDefaultPriceId(params.stripeProductId); |
| 42 | + |
| 43 | + const customerId = await getOrCreateStripeCustomer( |
| 44 | + params.userId, |
| 45 | + params.email, |
| 46 | + ); |
| 47 | + const origin = await getRequestOrigin(); |
| 48 | + |
| 49 | + const session = await stripe.checkout.sessions.create({ |
| 50 | + customer: customerId, |
| 51 | + mode: "payment", |
| 52 | + payment_method_types: ["card"], |
| 53 | + line_items: [{ price: priceId, quantity: 1 }], |
| 54 | + success_url: `${origin}/checkout/success`, |
| 55 | + cancel_url: `${origin}/checkout/cancel`, |
| 56 | + metadata: params.metadata, |
| 57 | + }); |
| 58 | + |
| 59 | + if (!session.url) |
| 60 | + return { error: "Stripe did not return a checkout URL" }; |
| 61 | + return { session }; |
| 62 | +} |
| 63 | + |
| 64 | +export async function checkoutServiceBooking({ |
| 65 | + serviceId, |
| 66 | +}: { |
| 67 | + serviceId: string; |
| 68 | +}): Promise<CheckoutResult> { |
| 69 | + const supabase = await createClient(); |
| 70 | + const { |
| 71 | + data: { user }, |
| 72 | + } = await supabase.auth.getUser(); |
| 73 | + if (!user) return { error: "Not authenticated" }; |
| 74 | + |
| 75 | + const service = await db.query.services.findFirst({ |
| 76 | + where: eq(services.id, serviceId), |
| 77 | + }); |
| 78 | + if (!service) return { error: "Service not found" }; |
| 79 | + if (service.status !== "active") |
| 80 | + return { error: "Service is not available" }; |
| 81 | + if (service.type !== "programs") |
| 82 | + return { error: "Service is not a program" }; |
| 83 | + |
| 84 | + const [row] = await db |
| 85 | + .insert(serviceBookings) |
| 86 | + .values({ |
| 87 | + userId: user.id, |
| 88 | + serviceId: service.id, |
| 89 | + status: "awaiting_payment", |
| 90 | + }) |
| 91 | + .returning({ id: serviceBookings.id }); |
| 92 | + |
| 93 | + const result = await createStripeCheckoutSession({ |
| 94 | + userId: user.id, |
| 95 | + email: user.email!, |
| 96 | + stripeProductId: service.stripeProductId, |
| 97 | + metadata: { |
| 98 | + type: "program", |
| 99 | + bookingId: row.id, |
| 100 | + }, |
| 101 | + }); |
| 102 | + if ("error" in result) { |
| 103 | + await db.delete(serviceBookings).where(eq(serviceBookings.id, row.id)); |
| 104 | + return { error: result.error }; |
| 105 | + } |
| 106 | + |
| 107 | + await db |
| 108 | + .update(serviceBookings) |
| 109 | + .set({ stripeOrderId: result.session.id }) |
| 110 | + .where(eq(serviceBookings.id, row.id)); |
| 111 | + |
| 112 | + return { url: result.session.url! }; |
| 113 | +} |
| 114 | + |
| 115 | +export async function checkoutCoachingSession({ |
| 116 | + coachingSessionId, |
| 117 | +}: { |
| 118 | + coachingSessionId: string; |
| 119 | +}): Promise<CheckoutResult> { |
| 120 | + const supabase = await createClient(); |
| 121 | + const { |
| 122 | + data: { user }, |
| 123 | + } = await supabase.auth.getUser(); |
| 124 | + if (!user) return { error: "Not authenticated" }; |
| 125 | + |
| 126 | + const row = await db.query.coachingSessions.findFirst({ |
| 127 | + where: and( |
| 128 | + eq(coachingSessions.id, coachingSessionId), |
| 129 | + eq(coachingSessions.userId, user.id), |
| 130 | + ), |
| 131 | + }); |
| 132 | + if (!row) return { error: "Coaching session not found" }; |
| 133 | + if (row.status !== "awaiting_payment") |
| 134 | + return { error: "Coaching session is not awaiting payment" }; |
| 135 | + |
| 136 | + const service = await db.query.services.findFirst({ |
| 137 | + where: eq(services.id, row.serviceId), |
| 138 | + }); |
| 139 | + if (!service) return { error: "Service not found" }; |
| 140 | + |
| 141 | + const result = await createStripeCheckoutSession({ |
| 142 | + userId: user.id, |
| 143 | + email: user.email!, |
| 144 | + stripeProductId: service.stripeProductId, |
| 145 | + metadata: { |
| 146 | + type: "private_lesson", |
| 147 | + coachingSessionId: row.id, |
| 148 | + }, |
| 149 | + }); |
| 150 | + if ("error" in result) { |
| 151 | + await db |
| 152 | + .delete(coachingSessions) |
| 153 | + .where(eq(coachingSessions.id, row.id)); |
| 154 | + return { error: result.error }; |
| 155 | + } |
| 156 | + |
| 157 | + await db |
| 158 | + .update(coachingSessions) |
| 159 | + .set({ stripeOrderId: result.session.id }) |
| 160 | + .where(eq(coachingSessions.id, row.id)); |
| 161 | + |
| 162 | + return { url: result.session.url! }; |
| 163 | +} |
| 164 | + |
0 commit comments