Skip to content

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

Enhance Stripe subscription handling with error management options

Bill Yang2e312551eb65
diff --git a/server/src/lib/subscriptionUtils.ts b/server/src/lib/subscriptionUtils.ts
index d94bd881d..8a52d10fe 100644
--- a/server/src/lib/subscriptionUtils.ts
+++ b/server/src/lib/subscriptionUtils.ts
@@ -197,7 +197,10 @@ export function invalidateStripeSubscriptionCache(stripeCustomerId: string | nul
* Gets Stripe subscription info for an organization
* @returns Stripe subscription info or null if no active subscription found
*/
-export async function getStripeSubscription(stripeCustomerId: string | null): Promise<StripeSubscriptionInfo | null> {
+export async function getStripeSubscription(
+ stripeCustomerId: string | null,
+ { throwOnError = false }: { throwOnError?: boolean } = {}
+): Promise<StripeSubscriptionInfo | null> {
if (!stripeCustomerId) {
return null;
}
@@ -216,9 +219,20 @@ export async function getStripeSubscription(stripeCustomerId: string | null): Pr
return value;
} catch (error) {
console.error("Error fetching Stripe subscription:", error);
- // On error, fall back to the last cached value (even if expired) rather than
- // treating the org as having no subscription.
- return cached?.value ?? null;
+ // Prefer the last known value (even if expired) over treating the org as having
+ // no subscription — that would silently downgrade a paying customer. A cached
+ // `null` is also trustworthy: it means we previously confirmed there was no sub.
+ if (cached) {
+ return cached.value;
+ }
+ // No cached value at all, so we genuinely don't know this customer's status.
+ // Callers that must not mistake a transient Stripe failure for "no subscription"
+ // (e.g. the usage cron) opt into throwing so they can preserve the org's existing
+ // state instead of downgrading it to free.
+ if (throwOnError) {
+ throw error;
+ }
+ return null;
}
}
@@ -343,7 +357,8 @@ export async function getCustomPlanSubscription(organizationId: string): Promise
*/
export async function getBestSubscription(
organizationId: string,
- stripeCustomerId: string | null
+ stripeCustomerId: string | null,
+ { throwOnStripeError = false }: { throwOnStripeError?: boolean } = {}
): Promise<SubscriptionInfo> {
// Check custom plan first - highest priority
const customSub = await getCustomPlanSubscription(organizationId);
@@ -360,7 +375,7 @@ export async function getBestSubscription(
// Get both subscription types
const [appsumoSub, stripeSub] = await Promise.all([
getAppSumoSubscription(organizationId),
- getStripeSubscription(stripeCustomerId),
+ getStripeSubscription(stripeCustomerId, { throwOnError: throwOnStripeError }),
]);
diff --git a/server/src/services/usageService.ts b/server/src/services/usageService.ts
index 6556f91ba..2f49aa605 100644
--- a/server/src/services/usageService.ts
+++ b/server/src/services/usageService.ts
@@ -134,8 +134,13 @@ class UsageService {
return [Infinity, this.getStartOfMonth()];
}
- // Get the best subscription (highest event limit from AppSumo or Stripe)
- const subscription = await getBestSubscription(orgData.id, orgData.stripeCustomerId);
+ // Get the best subscription (highest event limit from AppSumo or Stripe).
+ // throwOnStripeError ensures a transient Stripe failure surfaces as an error here
+ // instead of silently resolving to the free tier (which would wrongly flag a paying
+ // org as over-limit). The caller skips the org when this throws.
+ const subscription = await getBestSubscription(orgData.id, orgData.stripeCustomerId, {
+ throwOnStripeError: true,
+ });
// Log subscription details
if (subscription.source === "appsumo") {
@@ -241,7 +246,21 @@ class UsageService {
const wasOverLimit = orgData.overMonthlyLimit ?? false;
const alreadyNotifiedApproaching = orgData.approachingLimitNotifiedPeriodStart === monthStart;
- const [eventLimit] = await this.getOrganizationSubscriptionInfo(orgData);
+ let eventLimit: number;
+ try {
+ [eventLimit] = await this.getOrganizationSubscriptionInfo(orgData);
+ } catch (error) {
+ // We couldn't determine this org's subscription (e.g. a Stripe rate limit).
+ // Treating that as the free tier would wrongly mark a paying org as over-limit,
+ // blocking its event ingestion and emailing its owner. Skip the org and leave
+ // its existing limit state untouched until a later run can fetch the real plan.
+ this.logger.warn(
+ `Skipping organization ${orgData.name}: could not determine subscription (${
+ (error as Error).message
+ }). Preserving previous limit state.`
+ );
+ continue;
+ }
const isOverLimit = eventCount > eventLimit;
let sendApproaching = false;