Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(billing): handle null values in cloudConfig stripe fields (#10680)

Michael Fröhlich452a0b59f55f
diff --git a/packages/shared/src/interfaces/cloudConfigSchema.ts b/packages/shared/src/interfaces/cloudConfigSchema.ts
index 211de70694..05b84cc6ef 100644
--- a/packages/shared/src/interfaces/cloudConfigSchema.ts
+++ b/packages/shared/src/interfaces/cloudConfigSchema.ts
@@ -10,19 +10,18 @@ export const CloudConfigSchema = z.object({
// need to update stripe webhook if you change this, it fetches from db via these fields
stripe: z
.object({
- customerId: z.string().optional(),
- activeSubscriptionId: z.string().optional(),
- activeProductId: z.string().optional(),
- activeUsageProductId: z.string().optional(),
- subscriptionStatus: z.string().optional(), // should be one of ["active","past_due", "unpaid", "canceled", "incomplete", "incomplete_expired", "paused"]; we don't enforce to have a backwards compatibility for this field
+ customerId: z.string().nullish(),
+ activeSubscriptionId: z.string().nullish(),
+ activeProductId: z.string().nullish(),
+ activeUsageProductId: z.string().nullish(),
+ subscriptionStatus: z.string().nullish(), // should be one of ["active","past_due", "unpaid", "canceled", "incomplete", "incomplete_expired", "paused"]; we don't enforce to have a backwards compatibility for this field
})
.transform((data) => ({
...data,
isLegacySubscription:
- data?.activeProductId !== undefined &&
- data?.activeUsageProductId === undefined,
+ data?.activeProductId != null && data?.activeUsageProductId == null,
}))
- .optional(),
+ .nullish(),
// custom rate limits for an organization
rateLimitOverrides: CloudConfigRateLimit.optional(),
diff --git a/web/src/ee/features/billing/server/stripeBillingService.ts b/web/src/ee/features/billing/server/stripeBillingService.ts
index c15eb960a8..7cb8081add 100644
--- a/web/src/ee/features/billing/server/stripeBillingService.ts
+++ b/web/src/ee/features/billing/server/stripeBillingService.ts
@@ -671,7 +671,8 @@ class BillingService {
})();
const returnUrl = `${env.NEXTAUTH_URL}/organization/${orgId}/settings/billing`;
- const stripeCustomerId = parsedOrg.cloudConfig?.stripe?.customerId;
+ const stripeCustomerId =
+ parsedOrg.cloudConfig?.stripe?.customerId ?? undefined;
const clientReferenceId = createStripeClientReference(orgId);
const subscriptionMetadata: StripeSubscriptionMetadata = {
orgId: orgId,
diff --git a/web/src/ee/features/billing/server/stripeWebhookHandler.ts b/web/src/ee/features/billing/server/stripeWebhookHandler.ts
index 15990815af..6524cc8b71 100644
--- a/web/src/ee/features/billing/server/stripeWebhookHandler.ts
+++ b/web/src/ee/features/billing/server/stripeWebhookHandler.ts
@@ -573,17 +573,15 @@ async function handleSubscriptionChanged(
after: updatedCloudConfig,
});
} else if (action === "deleted") {
+ // When subscription is deleted, only keep customerId and remove all other subscription fields
+ // Note: We omit fields entirely rather than setting to undefined, as undefined gets converted
+ // to null in PostgreSQL JSONB, which can cause validation issues
const updatedCloudConfig = {
...parsedOrg.cloudConfig,
stripe: {
- ...parsedOrg.cloudConfig?.stripe,
- ...CloudConfigSchema.shape.stripe.parse({
- customerId: stripeCustomerId,
- activeProductId: undefined,
- activeSubscriptionId: undefined,
- activeUsageProductId: undefined,
- subscriptionStatus: undefined,
- }),
+ customerId: stripeCustomerId,
+ // Explicitly omit activeProductId, activeSubscriptionId, activeUsageProductId, subscriptionStatus
+ // They will not be present in the saved JSON rather than being null
},
};