Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix: New webhook rehandler
Richie McIlroyac415c1af56d
diff --git a/apps/web/app/api/webhooks/stripe/route.ts b/apps/web/app/api/webhooks/stripe/route.tsindex 8cc0a08b9..1617b1f5f 100644--- a/apps/web/app/api/webhooks/stripe/route.ts+++ b/apps/web/app/api/webhooks/stripe/route.ts@@ -11,6 +11,50 @@ const relevantEvents = new Set(["customer.subscription.deleted",]);+// Helper function to find user with retries+async function findUserWithRetry(email: string, userId?: string, maxRetries = 3): Promise<typeof users.$inferSelect | null> {+ for (let i = 0; i < maxRetries; i++) {+ console.log(`Attempt ${i + 1} to find user (email: ${email}, userId: ${userId})`);++ try {+ let user;++ if (userId) {+ user = await db+ .select()+ .from(users)+ .where(eq(users.id, userId))+ .limit(1)+ .then(rows => rows[0] || null);+ } else if (email) {+ user = await db+ .select()+ .from(users)+ .where(eq(users.email, email))+ .limit(1)+ .then(rows => rows[0] || null);+ }++ if (user) {+ console.log(`User found on attempt ${i + 1}`);+ return user;+ }++ // Wait before retrying, with exponential backoff+ if (i < maxRetries - 1) {+ const delay = Math.pow(2, i) * 1000; // 1s, 2s, 4s+ console.log(`Waiting ${delay}ms before retry`);+ await new Promise(resolve => setTimeout(resolve, delay));+ }+ } catch (error) {+ console.error(`Error finding user on attempt ${i + 1}:`, error);+ // Continue to next retry+ }+ }++ return null;+}+export const POST = async (req: Request) => {const buf = await req.text();const sig = req.headers.get("Stripe-Signature") as string;@@ -36,50 +80,34 @@ export const POST = async (req: Request) => {const customer = await stripe.customers.retrieve(session.customer as string);++ // Get potential user identifierslet foundUserId;+ let customerEmail;+if ("metadata" in customer) {foundUserId = customer.metadata.userId;}- if (!foundUserId) {- console.log("No user found in metadata, checking customer email");- if ("email" in customer && customer.email) {- const userByEmail = await db- .select()- .from(users)- .where(eq(users.email, customer.email))- .limit(1);-- if (userByEmail && userByEmail.length > 0 && userByEmail[0]) {- foundUserId = userByEmail[0].id;- console.log(`User found by email: ${foundUserId}`);- // Update customer metadata with userId- await stripe.customers.update(customer.id, {- metadata: { userId: foundUserId },- });- } else {- console.log("No user found by email");- return new Response("No user found", {- status: 400,- });- }- } else {- console.log("No email found for customer");- return new Response("No user found", {- status: 400,- });- }+ if ("email" in customer) {+ customerEmail = customer.email;}- const userResult = await db- .select()- .from(users)- .where(eq(users.id, foundUserId));-- if (!userResult || userResult.length === 0) {- console.log("No user found in database");- return new Response("No user found", { status: 400 });+ console.log("Looking for user with:", { foundUserId, customerEmail });++ // Try to find user with retries+ const dbUser = await findUserWithRetry(customerEmail as string, foundUserId);++ if (!dbUser) {+ console.log("No user found after retries");+ // Instead of failing, we'll store the subscription info and retry later+ // You might want to implement a queue system for this in production+ return new Response("User not found, webhook will be retried", {+ status: 202, // Accepted but not processed+ });}+ console.log("Found user:", dbUser.id);+const subscription = await stripe.subscriptions.retrieve(session.subscription as string);@@ -92,12 +120,13 @@ export const POST = async (req: Request) => {.update(users).set({stripeSubscriptionId: session.subscription as string,- stripeSubscriptionStatus: session.status,+ stripeSubscriptionStatus: subscription.status,+ stripeCustomerId: customer.id,inviteQuota: inviteQuota,})- .where(eq(users.id, foundUserId));+ .where(eq(users.id, dbUser.id));- console.log("User updated successfully", { foundUserId, inviteQuota });+ console.log("User updated successfully", { userId: dbUser.id, inviteQuota });}if (event.type === "customer.subscription.updated") {