Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🐛 Fix race condition by handling customer creation in checkout (#1894)
Luke Vellae5b43c1dec14
diff --git a/apps/web/src/app/api/stripe/checkout/route.ts b/apps/web/src/app/api/stripe/checkout/route.tsindex 72835b0eb..92427c140 100644--- a/apps/web/src/app/api/stripe/checkout/route.ts+++ b/apps/web/src/app/api/stripe/checkout/route.ts@@ -5,6 +5,7 @@ import type { NextRequest } from "next/server";import { NextResponse } from "next/server";import { z } from "zod";import { requireSpace, requireUser } from "@/auth/data";+import type { CustomerMetadata } from "@/features/billing/schema";import type {SubscriptionCheckoutMetadata,SubscriptionMetadata,@@ -48,35 +49,34 @@ export async function POST(request: NextRequest) {},});+ if (space.tier === "pro") {+ // User already has an active subscription. Take them to customer portal+ return NextResponse.redirect(+ new URL("/api/stripe/portal", request.url),+ 303,+ );+ }+if (res.customerId) {customerId = res.customerId;} else {// create a new customer in stripe- const customer = await stripe.customers.create({- email: user.email,- name: user.name,- });-- await prisma.user.update({- where: {- id: user.id,+ const customer = await stripe.customers.create(+ {+ email: user.email,+ name: user.name,+ metadata: {+ userId: user.id,+ } satisfies CustomerMetadata,},- data: {- customerId: customer.id,+ {+ idempotencyKey: `cust_create_${user.id}`,},- });+ );customerId = customer.id;}- if (space.tier === "pro") {- // User already has an active subscription. Take them to customer portal- return NextResponse.redirect(- new URL("/api/stripe/portal", request.url),- 303,- );- }-const proPricingData = await getProPricing();const checkoutSession = await stripe.checkout.sessions.create({diff --git a/apps/web/src/app/api/stripe/webhook/handlers/customer/created.ts b/apps/web/src/app/api/stripe/webhook/handlers/customer/created.tsindex 91f9abd9b..af635c265 100644--- a/apps/web/src/app/api/stripe/webhook/handlers/customer/created.ts+++ b/apps/web/src/app/api/stripe/webhook/handlers/customer/created.ts@@ -1,10 +1,6 @@import type { Stripe } from "@rallly/billing";import { prisma } from "@rallly/database";-import { z } from "zod";--const customerMetadataSchema = z.object({- userId: z.string(),-});+import { customerMetadataSchema } from "@/features/billing/schema";export async function onCustomerCreated(event: Stripe.Event) {const customer = event.data.object as Stripe.Customer;diff --git a/apps/web/src/features/billing/schema.ts b/apps/web/src/features/billing/schema.tsindex cd3e44f4f..22597cf8a 100644--- a/apps/web/src/features/billing/schema.ts+++ b/apps/web/src/features/billing/schema.ts@@ -2,3 +2,8 @@ import z from "zod";export const billingIntervalSchema = z.enum(["month", "year"]);export type BillingInterval = z.infer<typeof billingIntervalSchema>;++export const customerMetadataSchema = z.object({+ userId: z.string(),+});+export type CustomerMetadata = z.infer<typeof customerMetadataSchema>;