Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix(billing): handle two-outcome checkout response

Tre Dubravaa214eb29f24b
diff --git a/backend/src/ee/routes/v1/license-v2-router.ts b/backend/src/ee/routes/v1/license-v2-router.ts
index 24a694627c..17ac9df237 100644
--- a/backend/src/ee/routes/v1/license-v2-router.ts
+++ b/backend/src/ee/routes/v1/license-v2-router.ts
@@ -187,7 +187,11 @@ export const registerLicenseV2Router = async (server: FastifyZodProvider) => {
returnPath: ReturnPathSchema
}),
response: {
- 200: z.object({ url: z.string() })
+ 200: z.object({
+ outcome: z.enum(["checkout_created", "subscription_updated"]),
+ checkoutUrl: z.string().optional(),
+ subscriptionId: z.string().optional()
+ })
}
},
onRequest: verifyAuth([AuthMode.JWT]),
diff --git a/backend/src/ee/services/license-v2/license-v2-service.ts b/backend/src/ee/services/license-v2/license-v2-service.ts
index ca4fb4c5df..9d2c7502c1 100644
--- a/backend/src/ee/services/license-v2/license-v2-service.ts
+++ b/backend/src/ee/services/license-v2/license-v2-service.ts
@@ -2,7 +2,7 @@ import { ForbiddenError } from "@casl/ability";
import { OrganizationActionScope } from "@app/db/schemas";
import { TEnvConfig } from "@app/lib/config/env";
-import { BadRequestError, NotFoundError } from "@app/lib/errors";
+import { BadRequestError, InternalServerError, NotFoundError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
import { TIdentityOrgDALFactory } from "@app/services/identity/identity-org-dal";
import { TLicenseClientFactory } from "@app/services/license-client";
@@ -425,8 +425,17 @@ export const licenseV2ServiceFactory = ({
throw new BadRequestError({ message: "This product is not available for self-serve checkout" });
}
- const session = await licenseClient.createCheckout(orgId, { items, email, returnPath });
- return { url: session.url };
+ const result = await licenseClient.createCheckout(orgId, { items, email, returnPath });
+ if (result.outcome === "subscription_updated") {
+ return { outcome: "subscription_updated" as const, subscriptionId: result.subscriptionId };
+ }
+
+ // checkout_created (or the legacy { url } shape) means the customer must finish in Stripe Checkout.
+ const checkoutUrl = result.checkoutUrl ?? result.url;
+ if (!checkoutUrl) {
+ throw new InternalServerError({ message: "Checkout session did not return a URL" });
+ }
+ return { outcome: "checkout_created" as const, checkoutUrl };
};
const addPaymentMethod = async ({ orgId, actor, returnPath }: TAddBillingV2PaymentMethodDTO) => {
diff --git a/backend/src/services/license-client/license-client-backends.ts b/backend/src/services/license-client/license-client-backends.ts
index ae72aad618..903532789b 100644
--- a/backend/src/services/license-client/license-client-backends.ts
+++ b/backend/src/services/license-client/license-client-backends.ts
@@ -2,11 +2,13 @@ import jwt from "jsonwebtoken";
import {
catalogResponseSchema,
+ checkoutResultSchema,
cloudPlanResponseSchema,
entitlementsResponseSchema,
sessionResponseSchema,
subscriptionResponseSchema,
TCatalogResponse,
+ TCheckoutResult,
TCloudPlanResponse,
TCreateCheckoutPayload,
TCreatePortalPayload,
@@ -119,7 +121,7 @@ export const licenseServerBackend = (serverUrl: string, signingKey: string): TLi
return cloudPlanResponseSchema.parse(body);
},
- createCheckoutSession: async (orgId: string, payload: TCreateCheckoutPayload): Promise<TSessionResponse> => {
+ createCheckoutSession: async (orgId: string, payload: TCreateCheckoutPayload): Promise<TCheckoutResult> => {
const url = new URL(CHECKOUT_SESSION_PATH, serverUrl);
url.searchParams.set("org_id", orgId);
const res = await fetch(url, {
@@ -132,7 +134,7 @@ export const licenseServerBackend = (serverUrl: string, signingKey: string): TLi
throw new Error(`license server responded with ${res.status}`);
}
const body: unknown = await res.json();
- return sessionResponseSchema.parse(body);
+ return checkoutResultSchema.parse(body);
},
createPortalSession: async (orgId: string, payload: TCreatePortalPayload): Promise<TSessionResponse> => {
diff --git a/backend/src/services/license-client/license-client-types.ts b/backend/src/services/license-client/license-client-types.ts
index b7e1ce5bce..b25490055a 100644
--- a/backend/src/services/license-client/license-client-types.ts
+++ b/backend/src/services/license-client/license-client-types.ts
@@ -109,6 +109,18 @@ export const sessionResponseSchema = z
})
.passthrough();
+// Checkout either needs the customer to complete a Stripe Checkout (checkout_created) or is applied
+// directly to an existing subscription (subscription_updated). `url` is the legacy shape, tolerated so
+// the cloud and license server can deploy in any order.
+export const checkoutResultSchema = z
+ .object({
+ outcome: z.enum(["checkout_created", "subscription_updated"]).optional(),
+ checkoutUrl: z.string().optional(),
+ subscriptionId: z.string().optional(),
+ url: z.string().optional()
+ })
+ .passthrough();
+
// The cloud-plan FeatureSet carries plan caps; the server nulls a limit when it is effectively
// unlimited. Used counts come back zeroed and are overlaid by this app, so we ignore them.
export const cloudPlanResponseSchema = z
@@ -129,6 +141,7 @@ export type TCatalogResponse = z.infer<typeof catalogResponseSchema>;
export type TSubscriptionItem = z.infer<typeof subscriptionItemSchema>;
export type TSubscriptionResponse = z.infer<typeof subscriptionResponseSchema>;
export type TSessionResponse = z.infer<typeof sessionResponseSchema>;
+export type TCheckoutResult = z.infer<typeof checkoutResultSchema>;
export type TCloudPlanResponse = z.infer<typeof cloudPlanResponseSchema>;
export type TCheckoutLineItem = {
@@ -153,6 +166,6 @@ export type TLicenseClientBackend = {
fetchCatalog: () => Promise<TCatalogResponse>;
fetchSubscription: (orgId: string) => Promise<TSubscriptionResponse | null>;
fetchCloudPlan: (orgId: string) => Promise<TCloudPlanResponse | null>;
- createCheckoutSession: (orgId: string, payload: TCreateCheckoutPayload) => Promise<TSessionResponse>;
+ createCheckoutSession: (orgId: string, payload: TCreateCheckoutPayload) => Promise<TCheckoutResult>;
createPortalSession: (orgId: string, payload: TCreatePortalPayload) => Promise<TSessionResponse>;
};
diff --git a/frontend/src/hooks/api/billingV2/mutations.tsx b/frontend/src/hooks/api/billingV2/mutations.tsx
index 4e47389a72..1ae5c8e024 100644
--- a/frontend/src/hooks/api/billingV2/mutations.tsx
+++ b/frontend/src/hooks/api/billingV2/mutations.tsx
@@ -3,6 +3,7 @@ import { useMutation } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request";
import {
+ BillingV2CheckoutResult,
TAddBillingV2PaymentMethodDTO,
TCreateBillingV2CheckoutSessionDTO,
TCreateBillingV2PortalSessionDTO
@@ -32,14 +33,12 @@ export const useCreateBillingV2CheckoutSession = () => {
email,
returnPath
}: TCreateBillingV2CheckoutSessionDTO) => {
- const {
- data: { url }
- } = await apiRequest.post<{ url: string }>(
+ const { data } = await apiRequest.post<BillingV2CheckoutResult>(
`/api/v1/organizations/${orgId}/billing/v2/checkout-session`,
{ productId, cadence, email, returnPath }
);
- return url;
+ return data;
}
});
};
diff --git a/frontend/src/hooks/api/billingV2/types.ts b/frontend/src/hooks/api/billingV2/types.ts
index dd51d1c1fa..7c40376d3e 100644
--- a/frontend/src/hooks/api/billingV2/types.ts
+++ b/frontend/src/hooks/api/billingV2/types.ts
@@ -87,6 +87,12 @@ export type BillingV2Overview = {
entitlements: Record<string, BillingV2Entitlement>;
};
+export type BillingV2CheckoutResult = {
+ outcome: "checkout_created" | "subscription_updated";
+ checkoutUrl?: string;
+ subscriptionId?: string;
+};
+
export type TCreateBillingV2PortalSessionDTO = {
orgId: string;
returnPath?: string;
diff --git a/frontend/src/pages/organization/BillingV2Page/BillingV2Page.tsx b/frontend/src/pages/organization/BillingV2Page/BillingV2Page.tsx
index 415b9846b1..9e067ba958 100644
--- a/frontend/src/pages/organization/BillingV2Page/BillingV2Page.tsx
+++ b/frontend/src/pages/organization/BillingV2Page/BillingV2Page.tsx
@@ -94,14 +94,31 @@ export const BillingV2Page = () => {
const redirectToCheckout = async (productId: string) => {
try {
- const url = await createCheckoutSession.mutateAsync({
+ const result = await createCheckoutSession.mutateAsync({
orgId,
productId,
cadence,
email: billingEmail,
returnPath: window.location.pathname
});
- window.location.href = url;
+
+ // The product was added straight to the existing subscription; no Stripe redirect needed.
+ if (result.outcome === "subscription_updated") {
+ close();
+ createNotification({
+ type: "success",
+ text: "Product added to your subscription. It may take a moment to appear here."
+ });
+ refetch();
+ return;
+ }
+
+ if (result.checkoutUrl) {
+ window.location.href = result.checkoutUrl;
+ return;
+ }
+
+ createNotification({ type: "error", text: "Failed to start checkout." });
} catch {
createNotification({ type: "error", text: "Failed to start checkout." });
}