Skip to content

Commit 1feb80d

Browse files
Use canonical App domains for Research billing returns
1 parent 7c408ea commit 1feb80d

8 files changed

Lines changed: 153 additions & 18 deletions

File tree

‎apps/maple-research/frontend/src/billing/billingApi.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { isAndroid, isMobile, isTauri } from "@/utils/platform";
2+
import { billingReturnOrigin } from "./returnOrigin";
23
import {
34
isAllowedZapriteCheckoutUrl,
45
parseBillingApiError,
@@ -175,16 +176,8 @@ export async function fetchProducts(version?: string): Promise<BillingProduct[]>
175176

176177
export async function fetchPortalUrl(thirdPartyToken: string): Promise<string> {
177178
assertPurchasingAvailable();
178-
let returnUrl = window.location.origin;
179-
180-
// For all Tauri platforms, use the actual website origin instead of tauri://localhost
181-
if (isTauri()) {
182-
console.log("[Billing] Tauri platform detected, using trymaple.ai as return URL");
183-
returnUrl = "https://trymaple.ai";
184-
}
185-
186179
const requestBody = {
187-
return_url: returnUrl
180+
return_url: billingReturnOrigin()
188181
};
189182

190183
const response = await fetch(
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test";
2+
import * as platform from "@/utils/platform";
3+
import { fetchPortalUrl } from "./billingApi";
4+
import { billingReturnOrigin } from "./returnOrigin";
5+
6+
describe("billing browser returns", () => {
7+
const previousBillingApi = process.env.VITE_MAPLE_BILLING_API_URL;
8+
const previousWindow = Object.getOwnPropertyDescriptor(globalThis, "window");
9+
const cleanup: Array<() => void> = [];
10+
11+
beforeEach(() => {
12+
const native = spyOn(platform, "isTauri").mockReturnValue(true);
13+
const mobile = spyOn(platform, "isMobile").mockReturnValue(false);
14+
const android = spyOn(platform, "isAndroid").mockReturnValue(false);
15+
cleanup.push(
16+
() => native.mockRestore(),
17+
() => mobile.mockRestore(),
18+
() => android.mockRestore()
19+
);
20+
Object.defineProperty(globalThis, "window", {
21+
configurable: true,
22+
writable: true,
23+
value: { location: { origin: "http://tauri.localhost" } }
24+
});
25+
});
26+
27+
afterEach(() => {
28+
cleanup
29+
.splice(0)
30+
.reverse()
31+
.forEach((restore) => restore());
32+
if (previousBillingApi === undefined) delete process.env.VITE_MAPLE_BILLING_API_URL;
33+
else process.env.VITE_MAPLE_BILLING_API_URL = previousBillingApi;
34+
if (previousWindow) Object.defineProperty(globalThis, "window", previousWindow);
35+
else Reflect.deleteProperty(globalThis, "window");
36+
});
37+
38+
for (const [api, origin] of [
39+
["https://billing-dev.opensecret.cloud", "https://app-dev.maple.ai"],
40+
["https://billing-dev.opensecret.cloud/", "https://app-dev.maple.ai"],
41+
["https://billing.opensecret.cloud", "https://app.maple.ai"]
42+
]) {
43+
test(`desktop portal with ${api} returns directly to ${origin}`, async () => {
44+
process.env.VITE_MAPLE_BILLING_API_URL = api;
45+
const request = spyOn(globalThis, "fetch").mockResolvedValue(
46+
Response.json({ portal_url: "https://billing.stripe.com/p/session/test" })
47+
);
48+
cleanup.push(() => request.mockRestore());
49+
50+
expect(billingReturnOrigin()).toBe(origin);
51+
await fetchPortalUrl("fixture-token");
52+
expect(request).toHaveBeenCalledTimes(1);
53+
expect(JSON.parse(String(request.mock.calls[0][1]?.body))).toEqual({
54+
return_url: origin
55+
});
56+
});
57+
}
58+
59+
for (const origin of [
60+
"https://app.maple.ai",
61+
"https://app-dev.maple.ai",
62+
"https://app-dev.trymaple.ai",
63+
"http://127.0.0.1:5173"
64+
]) {
65+
test(`web preserves its actual origin ${origin}`, () => {
66+
spyOn(platform, "isTauri").mockReturnValue(false);
67+
Object.defineProperty(window.location, "origin", { value: origin, configurable: true });
68+
process.env.VITE_MAPLE_BILLING_API_URL = "https://billing-dev.opensecret.cloud";
69+
expect(billingReturnOrigin()).toBe(origin);
70+
});
71+
}
72+
73+
test("mobile keeps the existing associated-link host", () => {
74+
spyOn(platform, "isMobile").mockReturnValue(true);
75+
process.env.VITE_MAPLE_BILLING_API_URL = "https://billing-dev.opensecret.cloud";
76+
expect(billingReturnOrigin()).toBe("https://trymaple.ai");
77+
});
78+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { APP_ORIGIN } from "@/config/domains";
2+
import { isMobile, isTauri } from "@/utils/platform";
3+
4+
/** Browser returns stay with their tab; native returns need an HTTPS website. */
5+
export function billingReturnOrigin(): string {
6+
if (!isTauri()) return window.location.origin;
7+
8+
// Keep the existing Universal/App Link host for mobile clients.
9+
if (isMobile()) return "https://trymaple.ai";
10+
11+
// PR desktop builds use hosted Dev billing without the mobile Dev app variant.
12+
return import.meta.env.VITE_MAPLE_BILLING_API_URL?.replace(/\/+$/, "") ===
13+
"https://billing-dev.opensecret.cloud"
14+
? "https://app-dev.maple.ai"
15+
: APP_ORIGIN;
16+
}

‎apps/maple-research/frontend/src/components/ZapriteUpgradeDialog.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import { Button } from "@/components/ui/button";
1313
import { getBillingService } from "@/billing/billingService";
1414
import { openValidatedCheckoutUrl } from "@/billing/billingApi";
15+
import { billingReturnOrigin } from "@/billing/returnOrigin";
1516
import {
1617
formatUsdFromCents,
1718
isZapriteUpgradeTerminal,
@@ -33,7 +34,7 @@ function zapriteSuccessUrl(): string {
3334
return "https://trymaple.ai/payment-success?source=zaprite";
3435
}
3536
if (isTauri()) {
36-
return "https://trymaple.ai/pricing?success=true";
37+
return `${billingReturnOrigin()}/pricing?success=true`;
3738
}
3839
return `${window.location.origin}/pricing?success=true`;
3940
}

‎apps/maple-research/frontend/src/components/apikeys/ApiCreditsSection.test.tsx‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { act, create, type ReactTestInstance, type ReactTestRenderer } from "rea
33
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
44
import * as sdk from "@mapleai/sdk";
55
import * as platform from "@/utils/platform";
6+
import * as billing from "@/billing/billingService";
67
import { ApiCreditsSection } from "./ApiCreditsSection";
78

89
function textContent(node: ReactTestInstance): string {
@@ -29,6 +30,7 @@ describe("API credits platform policy", () => {
2930
const mobileSpy = spyOn(platform, "isMobile").mockReturnValue(
3031
platformName === "ios" || platformName === "android"
3132
);
33+
const nativeSpy = spyOn(platform, "isTauri").mockReturnValue(platformName !== "web");
3234
const authSpy = spyOn(sdk, "useOpenSecret").mockReturnValue({
3335
auth: {
3436
user: {
@@ -41,6 +43,7 @@ describe("API credits platform policy", () => {
4143
() => iosSpy.mockRestore(),
4244
() => androidSpy.mockRestore(),
4345
() => mobileSpy.mockRestore(),
46+
() => nativeSpy.mockRestore(),
4447
() => authSpy.mockRestore()
4548
);
4649
const client = new QueryClient({
@@ -86,4 +89,45 @@ describe("API credits platform policy", () => {
8689
}
8790
expect(renderer!.root.findAllByType("button")).toHaveLength(7);
8891
});
92+
93+
for (const [api, origin] of [
94+
["https://billing-dev.opensecret.cloud", "https://app-dev.maple.ai"],
95+
["https://billing.opensecret.cloud", "https://app.maple.ai"]
96+
]) {
97+
test(`desktop credit checkout preserves its query and cancel path on ${origin}`, async () => {
98+
const previousApi = process.env.VITE_MAPLE_BILLING_API_URL;
99+
process.env.VITE_MAPLE_BILLING_API_URL = api;
100+
cleanup.push(() => {
101+
if (previousApi === undefined) delete process.env.VITE_MAPLE_BILLING_API_URL;
102+
else process.env.VITE_MAPLE_BILLING_API_URL = previousApi;
103+
});
104+
let payload: Parameters<billing.BillingService["purchaseApiCredits"]>[0] | undefined;
105+
const service = spyOn(billing, "getBillingService").mockReturnValue({
106+
purchaseApiCredits: async (
107+
request: Parameters<billing.BillingService["purchaseApiCredits"]>[0]
108+
) => {
109+
payload = request;
110+
// Stop before the platform opener: no checkout or native process is created.
111+
throw new Error("Fixture stopped after recording checkout request");
112+
}
113+
} as unknown as billing.BillingService);
114+
const errors = spyOn(console, "error").mockImplementation(() => {});
115+
cleanup.push(
116+
() => service.mockRestore(),
117+
() => errors.mockRestore()
118+
);
119+
await mount("desktop");
120+
const button = renderer!.root
121+
.findAllByType("button")
122+
.find((node) => textContent(node).includes("Pay with Card"))!;
123+
await act(async () => {
124+
await button.props.onClick();
125+
});
126+
expect(payload).toMatchObject({
127+
credits: 20000,
128+
success_url: `${origin}/?credits_success=true`,
129+
cancel_url: `${origin}/`
130+
});
131+
});
132+
}
89133
});

‎apps/maple-research/frontend/src/components/apikeys/ApiCreditsSection.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Input } from "@/components/ui/input";
66
import { Loader2, CreditCard, Bitcoin, Coins, CheckCircle, Edit } from "lucide-react";
77
import { useQuery } from "@tanstack/react-query";
88
import { getBillingService } from "@/billing/billingService";
9+
import { billingReturnOrigin } from "@/billing/returnOrigin";
910
import { useOpenSecret } from "@mapleai/sdk";
1011
import { isMobile, isTauri } from "@/utils/platform";
1112
import {
@@ -130,9 +131,10 @@ export function ApiCreditsSection({ showSuccessMessage = false }: ApiCreditsSect
130131
cancelUrl =
131132
method === "stripe" ? `https://trymaple.ai/payment-canceled?source=stripe` : undefined;
132133
} else if (isTauri()) {
133-
// For Tauri desktop, use trymaple.ai since tauri://localhost won't work in external browser
134-
successUrl = `https://trymaple.ai/?credits_success=true`;
135-
cancelUrl = method === "stripe" ? `https://trymaple.ai/` : undefined;
134+
// Desktop returns use the website for the configured billing environment.
135+
const baseUrl = billingReturnOrigin();
136+
successUrl = `${baseUrl}/?credits_success=true`;
137+
cancelUrl = method === "stripe" ? `${baseUrl}/` : undefined;
136138
} else {
137139
// For web, use regular URLs with query params
138140
const baseUrl = window.location.origin;

‎apps/maple-research/frontend/src/routes/pricing.tsx‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { TeamSeatDialog } from "@/components/TeamSeatDialog";
2626
import { ZapriteUpgradeDialog } from "@/components/ZapriteUpgradeDialog";
2727
import { zapritePaidPlanButtonText, zapriteUpgradeTarget } from "@/billing/zapriteUpgrade";
2828
import { isIOS, isAndroid, isMobile, isTauri } from "@/utils/platform";
29+
import { billingReturnOrigin } from "@/billing/returnOrigin";
2930
import { cn } from "@/utils/utils";
3031
import packageJson from "../../package.json";
3132
import { ApplePricingPage } from "@/components/billing/ApplePricingPage";
@@ -518,20 +519,20 @@ function LegacyPricingPage() {
518519
);
519520
}
520521
} else if (isTauri()) {
521-
// For Tauri desktop, use trymaple.ai since tauri://localhost won't work in external browser
522+
const baseUrl = billingReturnOrigin();
522523
if (useBitcoin) {
523524
await billingService.createZapriteCheckoutSession(
524525
email,
525526
productId,
526-
`https://trymaple.ai/pricing?success=true`,
527+
`${baseUrl}/pricing?success=true`,
527528
quantity
528529
);
529530
} else {
530531
await billingService.createCheckoutSession(
531532
email,
532533
productId,
533-
`https://trymaple.ai/pricing?success=true`,
534-
`https://trymaple.ai/pricing?canceled=true`,
534+
`${baseUrl}/pricing?success=true`,
535+
`${baseUrl}/pricing?canceled=true`,
535536
quantity
536537
);
537538
}

‎docs/ios-dev-testflight.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Development and production accounts remain in their respective existing
2424
services; this app does not require another authentication or billing deployment.
2525
All `VITE_*` values are public configuration and must never contain secrets.
2626

27-
The stable Research development website is `https://app-dev.trymaple.ai`.
27+
The stable Research development website is `https://app-dev.maple.ai`.
2828
Hosted native sign-in uses the separate Auth application at
2929
`https://auth-dev.maple.ai`. Both sites are protected by Cloudflare Access;
3030
browser validation requires an authorized Access session.

0 commit comments

Comments
 (0)