Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(billing): open the paying workspace on Stripe returns; keep seat conflict explanation (MUL-9925) (#11256)

Multica Eveec0764cb6dce
diff --git a/packages/views/settings/components/billing-tab.test.tsx b/packages/views/settings/components/billing-tab.test.tsx
index 27f3877f54..71879aeb9c 100644
--- a/packages/views/settings/components/billing-tab.test.tsx
+++ b/packages/views/settings/components/billing-tab.test.tsx
@@ -1048,6 +1048,30 @@ describe("BillingTab", () => {
);
});
+ it("hands a return for another workspace to the billing return page", () => {
+ navigationState.search =
+ "tab=billing&workspace_id=22222222-2222-2222-2222-222222222222&result=autopilot_addon&session_id=cs_test_9";
+
+ renderWithI18n(<BillingTab />);
+
+ expect(screen.queryByRole("status", { name: "Add-on return" })).not.toBeInTheDocument();
+ expect(navigationState.replace).toHaveBeenCalledOnce();
+ expect(navigationState.replace).toHaveBeenCalledWith(
+ "/billing/return?workspace_id=22222222-2222-2222-2222-222222222222&result=autopilot_addon&session_id=cs_test_9",
+ );
+ });
+
+ it("consumes a return stamped with this workspace in place", () => {
+ navigationState.search = "tab=billing&workspace_id=workspace-1&result=cancel";
+
+ renderWithI18n(<BillingTab />);
+
+ expect(screen.getByText("Checkout cancelled")).toBeInTheDocument();
+ expect(navigationState.replace).toHaveBeenCalledWith(
+ "/acme/settings?tab=billing&workspace_id=workspace-1",
+ );
+ });
+
it("hands an add-on return's Checkout Session to the return notice after clearing the URL", () => {
navigationState.search = "tab=billing&result=autopilot_addon&session_id=cs_test_7";
@@ -1534,6 +1558,74 @@ describe("BillingTab", () => {
}
});
+ it("keeps the in-progress explanation when a newer quote returns after the refusal", async () => {
+ const user = userEvent.setup();
+ Object.assign(mocks.entitlements, {
+ plan: "pro",
+ status: "active",
+ });
+ mocks.summary.hasStripeCustomer = true;
+ setSeatCapacity();
+ const quote = (version: number, additionalSeats = 1) => ({
+ currentSeats: 5,
+ additionalSeats,
+ resultingSeats: 5 + additionalSeats,
+ purchaseVersion: version,
+ currency: "usd",
+ prorationAmount: 0,
+ nextInvoiceAmount: 6000,
+ quotedAt: "2030-01-01T00:00:00Z",
+ });
+ let resolveLateQuote: (value: ReturnType<typeof quote>) => void = () => {};
+ mocks.previewSeats
+ .mockResolvedValueOnce(quote(9))
+ .mockImplementationOnce(
+ () => new Promise((resolve) => { resolveLateQuote = resolve; }),
+ )
+ .mockResolvedValue(quote(10, 2));
+ let rejectPurchase: (error: unknown) => void = () => {};
+ mocks.purchaseSeats.mockImplementation(
+ () => new Promise((_, reject) => { rejectPurchase = reject; }),
+ );
+
+ const { rerender } = renderWithI18n(<BillingTab />);
+ await user.click(screen.getByRole("button", { name: "Add seats" }));
+ await user.click(await screen.findByRole("button", { name: "Add 1 seat" }));
+
+ // The plan change that wins the race lands while the purchase is pending:
+ // the summary moves to a new version and a new quote goes out.
+ setSeatCapacity({ version: 10 });
+ mocks.summary = { ...mocks.summary };
+ rerender(<BillingTab />);
+ await waitFor(() => expect(mocks.previewSeats).toHaveBeenCalledTimes(2), {
+ timeout: 3_000,
+ });
+
+ rejectPurchase(
+ new ApiError("conflict", 409, "Conflict", {
+ code: "plan_change_in_progress",
+ }),
+ );
+ const copy =
+ "A plan change is still being processed. Try again in a few minutes.";
+ expect(await screen.findByText(copy)).toBeInTheDocument();
+
+ await act(async () => {
+ resolveLateQuote(quote(10));
+ });
+ expect(screen.getByText(copy)).toBeInTheDocument();
+ expect(
+ screen.queryByRole("button", { name: "Add 1 seat" }),
+ ).not.toBeInTheDocument();
+
+ // Editing the seat count asks for a fresh quote.
+ await user.clear(screen.getByLabelText("Additional seats"));
+ await user.type(screen.getByLabelText("Additional seats"), "2");
+ expect(
+ await screen.findByRole("button", { name: "Add 2 seats" }, { timeout: 3_000 }),
+ ).toBeInTheDocument();
+ });
+
it("keeps the seat dialog open while purchase submission is pending", async () => {
const user = userEvent.setup();
Object.assign(mocks.entitlements, {
diff --git a/packages/views/settings/components/billing-tab.tsx b/packages/views/settings/components/billing-tab.tsx
index 46b1fa87d4..55ef8eb400 100644
--- a/packages/views/settings/components/billing-tab.tsx
+++ b/packages/views/settings/components/billing-tab.tsx
@@ -234,13 +234,24 @@ function BillingTabContent() {
const navigation = useNavigation();
const workspace = useCurrentWorkspace();
const wsId = workspace?.id ?? "";
- const returnResultParam = parseReturnResult(
- navigation.searchParams.get("result"),
- );
- const returnSessionId = navigation.searchParams.get("session_id");
- const callbackKey =
+ const hasReturnParams =
navigation.searchParams.has("result") ||
- navigation.searchParams.has("session_id")
+ navigation.searchParams.has("session_id");
+ // Cloud stamps every Stripe return with the workspace it authorized. A
+ // return URL configured with a fixed workspace slug lands here for every
+ // workspace, so a return that belongs to another one is handed to
+ // /billing/return, which opens the workspace that started it.
+ const returnWorkspaceId = navigation.searchParams.get("workspace_id");
+ const returnBelongsElsewhere =
+ hasReturnParams && !!wsId && !!returnWorkspaceId && returnWorkspaceId !== wsId;
+ const returnResultParam = returnBelongsElsewhere
+ ? null
+ : parseReturnResult(navigation.searchParams.get("result"));
+ const returnSessionId = returnBelongsElsewhere
+ ? null
+ : navigation.searchParams.get("session_id");
+ const callbackKey =
+ hasReturnParams && !returnBelongsElsewhere
? `${navigation.searchParams.get("result") ?? ""}:${returnSessionId ?? ""}`
: null;
const [returnState, setReturnState] = useState<{
@@ -321,6 +332,16 @@ function BillingTabContent() {
inputKey: string;
attempts: number;
} | null>(null);
+ // Set when a purchase was refused because another seat purchase or plan
+ // change holds the subscription. The refetched summary would otherwise start
+ // a new quote at once and replace that explanation; the user retries by
+ // editing the seat count or reopening the dialog.
+ const seatPurchaseBlockedRef = useRef(false);
+ const blockSeatPurchase = () => {
+ seatPurchaseBlockedRef.current = true;
+ // A quote already in flight must not clear the explanation either.
+ seatPreviewInputRef.current = "";
+ };
const consumedCallbackKeyRef = useRef<string | null>(null);
const queryClient = useQueryClient();
// The invitation Members held back for the checkout this page is about to
@@ -383,6 +404,19 @@ function BillingTabContent() {
setSeatPurchaseMessage(null);
}, [wsId]);
+ useEffect(() => {
+ if (!returnBelongsElsewhere) return;
+ const forward = new URLSearchParams();
+ for (const key of ["workspace_id", "result", "session_id"]) {
+ const value = navigation.searchParams.get(key);
+ if (value) forward.set(key, value);
+ }
+ const destination = `/billing/return?${forward.toString()}`;
+ if (consumedCallbackKeyRef.current === destination) return;
+ consumedCallbackKeyRef.current = destination;
+ navigation.replace(destination);
+ }, [navigation, returnBelongsElsewhere]);
+
// Consume Stripe callback params once, then remove them with replace so a
// refresh, copied URL, or settings-tab round trip cannot replay a banner or
// restart subscription polling. Keep unrelated params, especially `tab`.
@@ -573,7 +607,7 @@ function BillingTabContent() {
]);
useEffect(() => {
- if (!seatPurchaseOpen) return;
+ if (!seatPurchaseOpen || seatPurchaseBlockedRef.current) return;
const value = additionalSeatsInput.trim();
const additionalSeats = /^\d+$/.test(value) ? Number(value) : 0;
const currentSeats = summaryQuery.data?.seatCapacity?.purchased ?? null;
@@ -1168,6 +1202,7 @@ function BillingTabContent() {
seatPurchaseIntentRef.current = null;
setSeatPreview(null);
if (purchaseErrorCode === "seat_purchase_in_progress") {
+ blockSeatPurchase();
setSeatPurchaseError(
t(($) => $.workspace.seat_purchase.in_progress),
);
@@ -1178,6 +1213,7 @@ function BillingTabContent() {
purchaseErrorCode === "plan_change_in_progress" ||
purchaseErrorCode === "plan_change_unresolved"
) {
+ blockSeatPurchase();
setSeatPurchaseError(
purchaseErrorCode === "plan_change_unresolved"
? t(($) => $.workspace.seat_purchase.plan_change_unresolved)
@@ -1216,6 +1252,7 @@ function BillingTabContent() {
const handleSeatPurchaseOpenChange = (open: boolean) => {
if (!open && purchaseSeatsMutation.isPending) return;
setSeatPurchaseOpen(open);
+ seatPurchaseBlockedRef.current = false;
seatPreviewCapacityRetryRef.current = null;
setSeatPreviewRefreshing(false);
if (!open) {
@@ -2452,6 +2489,7 @@ function BillingTabContent() {
value={additionalSeatsInput}
disabled={purchaseSeatsMutation.isPending}
onChange={(event) => {
+ seatPurchaseBlockedRef.current = false;
seatPreviewCapacityRetryRef.current = null;
setSeatPreviewRefreshing(false);
setAdditionalSeatsInput(event.currentTarget.value);