Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/maple-research/frontend/src/billing/billingApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isAndroid, isMobile, isTauri } from "@/utils/platform";
import { billingReturnOrigin } from "./returnOrigin";
import { billingPortalReturnOrigin } from "./returnOrigin";
import {
isAllowedZapriteCheckoutUrl,
parseBillingApiError,
Expand Down Expand Up @@ -177,7 +177,7 @@ export async function fetchProducts(version?: string): Promise<BillingProduct[]>
export async function fetchPortalUrl(thirdPartyToken: string): Promise<string> {
assertPurchasingAvailable();
const requestBody = {
return_url: billingReturnOrigin()
return_url: billingPortalReturnOrigin()
};

const response = await fetch(
Expand Down
25 changes: 24 additions & 1 deletion apps/maple-research/frontend/src/billing/returnOrigin.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test";
import * as platform from "@/utils/platform";
import { fetchPortalUrl } from "./billingApi";
import { billingReturnOrigin } from "./returnOrigin";
import { billingPortalReturnOrigin, billingReturnOrigin } from "./returnOrigin";

describe("billing browser returns", () => {
const previousBillingApi = process.env.VITE_MAPLE_BILLING_API_URL;
Expand Down Expand Up @@ -67,6 +67,7 @@ describe("billing browser returns", () => {
Object.defineProperty(window.location, "origin", { value: origin, configurable: true });
process.env.VITE_MAPLE_BILLING_API_URL = "https://billing-dev.opensecret.cloud";
expect(billingReturnOrigin()).toBe(origin);
expect(billingPortalReturnOrigin()).toBe(origin);
});
}

Expand All @@ -75,4 +76,26 @@ describe("billing browser returns", () => {
process.env.VITE_MAPLE_BILLING_API_URL = "https://billing-dev.opensecret.cloud";
expect(billingReturnOrigin()).toBe("https://trymaple.ai");
});

for (const [api, origin] of [
["https://billing-dev.opensecret.cloud", "https://app-dev.maple.ai"],
["https://billing-dev.opensecret.cloud/", "https://app-dev.maple.ai"],
["https://billing.opensecret.cloud", "https://app.maple.ai"]
]) {
test(`mobile portal returns directly to ${origin} without changing payment links`, async () => {
spyOn(platform, "isMobile").mockReturnValue(true);
process.env.VITE_MAPLE_BILLING_API_URL = api;
const request = spyOn(globalThis, "fetch").mockResolvedValue(
Response.json({ portal_url: "https://billing.stripe.com/p/session/test" })
);
cleanup.push(() => request.mockRestore());

await fetchPortalUrl("fixture-token");
expect(request).toHaveBeenCalledTimes(1);
expect(JSON.parse(String(request.mock.calls[0][1]?.body))).toEqual({
return_url: origin
});
expect(billingReturnOrigin()).toBe("https://trymaple.ai");
});
}
});
11 changes: 10 additions & 1 deletion apps/maple-research/frontend/src/billing/returnOrigin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ export function billingReturnOrigin(): string {
// Keep the existing Universal/App Link host for mobile clients.
if (isMobile()) return "https://trymaple.ai";

// PR desktop builds use hosted Dev billing without the mobile Dev app variant.
return nativeAppOrigin();
}

/** Portal exits stay on the website and do not need a mobile payment deep link. */
export function billingPortalReturnOrigin(): string {
return isTauri() ? nativeAppOrigin() : window.location.origin;
}

function nativeAppOrigin(): string {
// PR builds can use hosted Dev billing without the mobile Dev app variant.
return import.meta.env.VITE_MAPLE_BILLING_API_URL?.replace(/\/+$/, "") ===
"https://billing-dev.opensecret.cloud"
? "https://app-dev.maple.ai"
Expand Down
Loading