diff --git a/cloud/apps/billing/src/index.ts b/cloud/apps/billing/src/index.ts index c2fe3ab9d34..23d596c93d2 100644 --- a/cloud/apps/billing/src/index.ts +++ b/cloud/apps/billing/src/index.ts @@ -31,6 +31,7 @@ import { OverageRequestSchema, updateOverage } from './overage' import { LiveModeRefusedError, StripeError, createStripeClient, type StripeClient } from './stripe' import { UsageReportSchema, flushMeterReports, recordUsage, validateSegment } from './usage' import { handleStripeWebhook } from './webhook' +import { renderReturnPage, returnOutcome } from './return-page' const MAX_JSON_BYTES = 16 * 1024 @@ -68,13 +69,8 @@ export async function handleRequest(request: Request, env: Env, ctx: ExecutionCo try { switch (route) { case 'GET /return': - // Where Checkout and the Portal send the browser back. Static; reveals nothing. - return new Response( - 'Orca' + - '' + - '

You can close this tab and return to Orca.

', - { headers: { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store' } } - ) + // Where Checkout and the Portal send the browser back; hands off to the desktop app. + return renderReturnPage(returnOutcome(url)) case 'POST /v1/stripe/webhook': return await handleStripeWebhook(env, stripe, request, now) diff --git a/cloud/apps/billing/src/return-page.ts b/cloud/apps/billing/src/return-page.ts new file mode 100644 index 00000000000..b3c9c63822e --- /dev/null +++ b/cloud/apps/billing/src/return-page.ts @@ -0,0 +1,87 @@ +// Where Stripe Checkout and the Customer Portal send the browser back. It hands the user back to +// the desktop app through an orca:// link (opened at once, plus a button if the browser asks first). +// Static per outcome: query values only pick one of three fixed pages, nothing is reflected. + +type Outcome = 'checkout-success' | 'checkout-cancel' | 'portal' + +const COPY: Record = { + 'checkout-success': { + title: 'Payment complete', + body: 'Your team is on Orca Cloud. Head back to Orca to invite people and start a cloud workspace.', + link: 'orca://billing/return?checkout=success' + }, + 'checkout-cancel': { + title: 'Checkout canceled', + body: 'Nothing was charged. You can pick a plan again from Orca any time.', + link: 'orca://billing/return?checkout=cancel' + }, + portal: { + title: 'Billing updated', + body: 'Your changes are saved. Orca picks them up when you switch back.', + link: 'orca://billing/return?portal=return' + } +} + +export function returnOutcome(url: URL): Outcome { + if (url.searchParams.get('checkout') === 'success') { + return 'checkout-success' + } + if (url.searchParams.get('checkout') === 'cancel') { + return 'checkout-cancel' + } + return 'portal' +} + +const CHECK = + '' + +export function renderReturnPage(outcome: Outcome): Response { + const copy = COPY[outcome] + const success = outcome === 'checkout-success' + const html = ` + + + + + + +${copy.title} · Orca + + + +
+
${success ? CHECK : ''}
+

${copy.title}

+

${copy.body}

+ Open Orca + You can close this tab. +
+ +` + return new Response(html, { + headers: { + 'content-type': 'text/html; charset=utf-8', + 'cache-control': 'no-store', + // No scripts, no external loads; only inline styles. + 'content-security-policy': "default-src 'none'; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'", + 'referrer-policy': 'no-referrer', + 'x-content-type-options': 'nosniff' + } + }) +} diff --git a/cloud/apps/billing/test/auth-dev-keys.test.ts b/cloud/apps/billing/test/auth-dev-keys.test.ts index 6bdbe1d3187..dc198d6dafa 100644 --- a/cloud/apps/billing/test/auth-dev-keys.test.ts +++ b/cloud/apps/billing/test/auth-dev-keys.test.ts @@ -41,6 +41,16 @@ describe('dev operator keys', () => { passThroughOnException() {} } as unknown as ExecutionContext) expect(response.status).toBe(200) - expect(await response.text()).toContain('return to Orca') + const html = await response.text() + expect(html).toContain('Payment complete') + expect(html).toContain('orca://billing/return?checkout=success') + expect(response.headers.get('content-security-policy')).toContain("default-src 'none'") + const cancel = await handleRequest(new Request('https://billing.test/return?checkout=cancel&x=