Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

fix stripe rate limits

Bill Yang6b90bd06bb8c
diff --git a/server/src/api/stripe/updateSubscription.ts b/server/src/api/stripe/updateSubscription.ts
index 5fcf694ab..ad7d7b97a 100644
--- a/server/src/api/stripe/updateSubscription.ts
+++ b/server/src/api/stripe/updateSubscription.ts
@@ -4,6 +4,7 @@ import { db } from "../../db/postgres/postgres.js";
import { organization, member } from "../../db/postgres/schema.js";
import { eq, and } from "drizzle-orm";
import Stripe from "stripe";
+import { invalidateStripeSubscriptionCache } from "../../lib/subscriptionUtils.js";
interface UpdateSubscriptionBody {
organizationId: string;
@@ -92,6 +93,9 @@ export async function updateSubscription(
...(isTrialing && subscription.trial_end && { trial_end: subscription.trial_end }),
});
+ // The plan changed, so drop the cached subscription for this customer.
+ invalidateStripeSubscriptionCache(org.stripeCustomerId);
+
// Get the updated subscription details
const updatedSubscriptionDetails = await (stripe as Stripe).subscriptions.retrieve(updatedSubscription.id);
const updatedItem = updatedSubscriptionDetails.items.data[0];
diff --git a/server/src/api/stripe/webhook.ts b/server/src/api/stripe/webhook.ts
index 827d8a7a4..d8e1b204c 100644
--- a/server/src/api/stripe/webhook.ts
+++ b/server/src/api/stripe/webhook.ts
@@ -4,6 +4,7 @@ import { db } from "../../db/postgres/postgres.js";
import { organization } from "../../db/postgres/schema.js";
import { eq } from "drizzle-orm";
import Stripe from "stripe";
+import { invalidateStripeSubscriptionCache } from "../../lib/subscriptionUtils.js";
import dotenv from "dotenv";
dotenv.config();
@@ -43,6 +44,9 @@ export async function handleWebhook(request: FastifyRequest, reply: FastifyReply
const stripeCustomerId = session.customer as string;
const organizationId = session.metadata?.organizationId; // Retrieve organizationId from metadata
+ // A new subscription was created — refresh any cached lookup for this customer.
+ invalidateStripeSubscriptionCache(stripeCustomerId);
+
if (stripeCustomerId && organizationId) {
try {
// Check if organization already has this customer ID
@@ -74,17 +78,14 @@ export async function handleWebhook(request: FastifyRequest, reply: FastifyReply
}
break;
- // case "customer.subscription.updated":
- // const subscriptionUpdated = event.data.object as Stripe.Subscription;
- // console.log("Subscription updated:", subscriptionUpdated.id, subscriptionUpdated.status);
- // // Potential actions: Update user roles/permissions based on status
- // break;
-
- // case "customer.subscription.deleted":
- // const subscriptionDeleted = event.data.object as Stripe.Subscription;
- // console.log("Subscription deleted:", subscriptionDeleted.id);
- // // Potential actions: Update user roles/permissions, mark as unsubscribed
- // break;
+ // Subscription changes made outside updateSubscription (e.g. via the Stripe billing
+ // portal) only reach us through these events — drop the cached lookup so the change
+ // is reflected on the next read instead of waiting out the full TTL.
+ case "customer.subscription.updated":
+ case "customer.subscription.deleted":
+ const changedSubscription = event.data.object as Stripe.Subscription;
+ invalidateStripeSubscriptionCache(changedSubscription.customer as string);
+ break;
// ... handle other event types as needed