diff --git a/src/main/ipc/profiles/orca-cloud-console-handlers.ts b/src/main/ipc/profiles/orca-cloud-console-handlers.ts index 583a029f878..421ebf90ad4 100644 --- a/src/main/ipc/profiles/orca-cloud-console-handlers.ts +++ b/src/main/ipc/profiles/orca-cloud-console-handlers.ts @@ -2,6 +2,7 @@ import { ipcMain } from 'electron' import { isOrcaCloudConsoleSection, + parseOrcaCloudConsoleBillingHint, type OrcaCloudConsoleOpenResult } from '../../../shared/orca-cloud-console' import { openOrcaCloudConsole } from '../../orca-profiles/cloud-console/cloud-console-link' @@ -18,7 +19,14 @@ export function registerOrcaCloudConsoleHandlers(): void { if (!isOrcaCloudConsoleSection(section)) { return { status: 'failed', error: 'invalid_orca_cloud_console_section' } } - return openOrcaCloudConsole(getProfileUserDataPath(), section) + const billingHint = + section === 'billing' && + typeof rawArgs === 'object' && + rawArgs !== null && + 'billingHint' in rawArgs + ? parseOrcaCloudConsoleBillingHint(rawArgs.billingHint) + : null + return openOrcaCloudConsole(getProfileUserDataPath(), section, { billingHint }) } ) } diff --git a/src/main/orca-profiles/cloud-console/cloud-console-link.test.ts b/src/main/orca-profiles/cloud-console/cloud-console-link.test.ts index c8b66be71f5..9608086e02d 100644 --- a/src/main/orca-profiles/cloud-console/cloud-console-link.test.ts +++ b/src/main/orca-profiles/cloud-console/cloud-console-link.test.ts @@ -3,7 +3,11 @@ import { describe, expect, it, vi } from 'vitest' vi.mock('electron', () => ({ app: { isPackaged: false }, shell: { openExternal: vi.fn() } })) import { CLOUD_CONSOLE_TICKET_PATH, cloudConsoleOrigin } from './cloud-console-config' -import { acceptedTicketUrl, requestCloudConsoleTicket } from './cloud-console-link' +import { + acceptedTicketUrl, + cloudConsoleTicketBody, + requestCloudConsoleTicket +} from './cloud-console-link' const config = { apiBaseUrl: 'https://login.onorca.dev' } @@ -59,6 +63,23 @@ describe('console ticket', () => { expect(JSON.parse(String(seen[0]?.init?.body))).toEqual({ page: 'billing' }) }) + it('sends the billing hint only to an auth that advertises it, and only for billing', () => { + const hint = { plan: 'team', interval: 'year' as const } + const advertised = { capabilities: { flags: { 'console.billing_hint': true } } } + expect( + cloudConsoleTicketBody(advertised, { section: 'billing', orgId: 'org_1', billingHint: hint }) + ).toEqual({ page: 'billing', orgId: 'org_1', plan: 'team', interval: 'year' }) + expect( + cloudConsoleTicketBody( + { capabilities: { flags: {} } }, + { section: 'billing', orgId: 'org_1', billingHint: hint } + ) + ).toEqual({ page: 'billing', orgId: 'org_1' }) + expect( + cloudConsoleTicketBody(advertised, { section: 'team', orgId: 'org_1', billingHint: hint }) + ).toEqual({ page: 'team', orgId: 'org_1' }) + }) + it('throws on a refusal so the console opens without a ticket', async () => { await expect( requestCloudConsoleTicket( diff --git a/src/main/orca-profiles/cloud-console/cloud-console-link.ts b/src/main/orca-profiles/cloud-console/cloud-console-link.ts index 6c2a22f8743..5b694f11e31 100644 --- a/src/main/orca-profiles/cloud-console/cloud-console-link.ts +++ b/src/main/orca-profiles/cloud-console/cloud-console-link.ts @@ -2,9 +2,11 @@ // URL from auth, so the browser lands signed in; when that fails for any reason the console opens // without one and the browser asks the person to sign in. The ticket is never logged or kept. import { app, shell } from 'electron' -import type { - OrcaCloudConsoleOpenResult, - OrcaCloudConsoleSection +import { + ORCA_CLOUD_CONSOLE_BILLING_HINT_CAPABILITY, + type OrcaCloudConsoleBillingHint, + type OrcaCloudConsoleOpenResult, + type OrcaCloudConsoleSection } from '../../../shared/orca-cloud-console' import { getOrcaCloudAuthConfig, @@ -45,11 +47,40 @@ export function acceptedTicketUrl( } } +/** + * The ticket request body. The billing hint goes only to an auth that advertises it: an older + * ticket route takes a strict body and would refuse the extra fields, losing the signed-in landing. + */ +export function cloudConsoleTicketBody( + session: { capabilities?: { flags: Record } }, + args: { + section: OrcaCloudConsoleSection + orgId: string | null + billingHint?: OrcaCloudConsoleBillingHint | null + } +): Record { + const hint = + args.section === 'billing' && + args.billingHint && + session.capabilities?.flags[ORCA_CLOUD_CONSOLE_BILLING_HINT_CAPABILITY] === true + ? args.billingHint + : null + return { + page: args.section, + ...(args.orgId ? { orgId: args.orgId } : {}), + ...(hint ? { plan: hint.plan, interval: hint.interval } : {}) + } +} + /** Asks auth for a single-use console ticket; answers its `url` unchecked (see acceptedTicketUrl). */ export async function requestCloudConsoleTicket( config: Pick, - session: Pick, - args: { section: OrcaCloudConsoleSection; orgId: string | null }, + session: Pick & Partial>, + args: { + section: OrcaCloudConsoleSection + orgId: string | null + billingHint?: OrcaCloudConsoleBillingHint | null + }, fetchImpl: typeof fetch = fetch ): Promise { const response = await fetchImpl(cloudConsoleTicketUrl(config), { @@ -58,7 +89,7 @@ export async function requestCloudConsoleTicket( 'content-type': 'application/json', authorization: `Bearer ${session.accessToken}` }, - body: JSON.stringify({ page: args.section, ...(args.orgId ? { orgId: args.orgId } : {}) }), + body: JSON.stringify(cloudConsoleTicketBody(session, args)), // Never follow a redirect with the access token attached. redirect: 'error', signal: AbortSignal.timeout(CLOUD_CONSOLE_TICKET_TIMEOUT_MS) @@ -73,7 +104,8 @@ export async function requestCloudConsoleTicket( async function mintTicketUrl( config: OrcaCloudAuthConfig, userDataPath: string, - section: OrcaCloudConsoleSection + section: OrcaCloudConsoleSection, + billingHint: OrcaCloudConsoleBillingHint | null ): Promise { if (isOrcaCloudDevAuthEnabled()) { return null @@ -84,7 +116,7 @@ async function mintTicketUrl( } const orgId = active.profile.cloud.activeOrgId ?? null const result = await runOrgMemberCall(config, active, userDataPath, (session) => - requestCloudConsoleTicket(config, session, { section, orgId }) + requestCloudConsoleTicket(config, session, { section, orgId, billingHint }) ) return result.status === 'ok' ? result.value : null } @@ -92,8 +124,12 @@ async function mintTicketUrl( export async function openOrcaCloudConsole( userDataPath: string, section: OrcaCloudConsoleSection, - openExternal: (url: string) => Promise = (url) => shell.openExternal(url) + options: { + billingHint?: OrcaCloudConsoleBillingHint | null + openExternal?: (url: string) => Promise + } = {} ): Promise { + const openExternal = options.openExternal ?? ((url: string) => shell.openExternal(url)) const configState = getOrcaCloudAuthConfig() if (!configState.configured) { return { status: 'unconfigured' } @@ -101,7 +137,9 @@ export async function openOrcaCloudConsole( const { config } = configState const origin = cloudConsoleOrigin(config, process.env, !app.isPackaged) const ticketUrl = acceptedTicketUrl( - await mintTicketUrl(config, userDataPath, section).catch(() => null), + await mintTicketUrl(config, userDataPath, section, options.billingHint ?? null).catch( + () => null + ), [origin, new URL(config.apiBaseUrl).origin] ) try { diff --git a/src/renderer/src/components/billing/plans/PlansDialogHost.tsx b/src/renderer/src/components/billing/plans/PlansDialogHost.tsx index 2e0a2cbe59d..2d150d58c2f 100644 --- a/src/renderer/src/components/billing/plans/PlansDialogHost.tsx +++ b/src/renderer/src/components/billing/plans/PlansDialogHost.tsx @@ -29,9 +29,9 @@ export function PlansDialogHost(): React.JSX.Element { catalog={catalog.result} canManage={canManage} opening={opening} - onContinue={() => { + onContinue={(hint) => { setOpening(true) - void openCloudConsole('billing').finally(() => setOpening(false)) + void openCloudConsole('billing', hint).finally(() => setOpening(false)) }} onContactSales={(url) => void window.api.shell.openUrl(url)} onRetry={() => { diff --git a/src/renderer/src/components/billing/plans/PlansDialogView.tsx b/src/renderer/src/components/billing/plans/PlansDialogView.tsx index 0b24c76963b..282af69f3c0 100644 --- a/src/renderer/src/components/billing/plans/PlansDialogView.tsx +++ b/src/renderer/src/components/billing/plans/PlansDialogView.tsx @@ -14,6 +14,7 @@ import { type OrcaBillingStatusResult } from '../../../../../shared/orca-billing' import type { OrcaPlanCatalogResult } from '../../../../../shared/orca-billing-plans' +import type { OrcaCloudConsoleBillingHint } from '../../../../../shared/orca-cloud-console' import { PaymentIssueNotice } from '../PaymentIssueNotice' import { isActivePlan } from '../plan-billing-format' import { @@ -35,7 +36,8 @@ export type PlansDialogViewProps = { canManage: boolean /** The console is being opened (a ticket is minted first). */ opening: boolean - onContinue: () => void + /** `hint`: the plan and period picked here, for the console to preselect (never to buy). */ + onContinue: (hint?: OrcaCloudConsoleBillingHint) => void onContactSales: (url: string) => void onRetry: () => void } @@ -129,7 +131,7 @@ function PlansBody(props: PlansDialogViewProps): React.JSX.Element { props.onContinue()} /> @@ -161,7 +163,7 @@ function PlansBody(props: PlansDialogViewProps): React.JSX.Element { +
+ {phase.via === 'plans' ? ( + + ) : null} + +
) : kind === 'spendCap' ? (
=> { - setPhase({ kind: 'browser', checking: true, stillBlocked: false }) + const via = (current: BillingBlockerPhase): 'plans' | 'portal' => + current.kind === 'browser' ? current.via : 'portal' + setPhase((current) => ({ + kind: 'browser', + checking: true, + stillBlocked: false, + via: via(current) + })) const status = await window.api.orcaProfiles.billingStatus().catch(() => null) // The service's own verdict: a fixed payment makes new sandboxes startable again. if (status?.status === 'ok' && status.entitlement.canStartSandbox) { onContinue() return } - setPhase({ kind: 'browser', checking: false, stillBlocked: true }) + setPhase((current) => ({ + kind: 'browser', + checking: false, + stillBlocked: true, + via: via(current) + })) }, [onContinue]) // Back from the browser: check once; if billing is fine the create continues on its own. @@ -385,11 +415,11 @@ export function VmBuildBillingBlocker({ onTurnOnOverage={() => void saveOverage(defaultCapCents)} onRaiseCap={(dollars) => void saveOverage(dollars * 100)} onComparePlans={() => { - setPhase({ kind: 'browser', checking: false, stillBlocked: false }) + setPhase({ kind: 'browser', checking: false, stillBlocked: false, via: 'plans' }) openPlansDialog() }} onUpdatePayment={() => { - setPhase({ kind: 'browser', checking: false, stillBlocked: false }) + setPhase({ kind: 'browser', checking: false, stillBlocked: false, via: 'portal' }) void window.api.orcaProfiles.billingOpenPortal().catch(() => {}) }} onCheckAgain={() => void checkBilling()} diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index a656776dce8..487ae1ad727 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -29171,7 +29171,10 @@ "checkAgain": "Check again", "raiseCapAndContinue": "Raise spend cap and continue", "backToDraft": "Back to draft", - "saveFailed": "Couldn’t save that. Try again, or change it in Plan & billing." + "saveFailed": "Couldn’t save that. Try again, or change it in Plan & billing.", + "plansStill": "No plan yet. Choose one, then check again.", + "plans": "Choose a plan, then check again. Orca checks again when you come back.", + "seePlans": "See plans" }, "cloneAccess": { "reachableTitle": "Couldn’t clone {{repo}}", diff --git a/src/renderer/src/lib/orca-cloud/open-cloud-console.ts b/src/renderer/src/lib/orca-cloud/open-cloud-console.ts index 93bf39ec804..2d83f418cef 100644 --- a/src/renderer/src/lib/orca-cloud/open-cloud-console.ts +++ b/src/renderer/src/lib/orca-cloud/open-cloud-console.ts @@ -3,18 +3,27 @@ // code the browser asks the person to sign in, so a failed request still opens the page. import { toast } from 'sonner' import { translate } from '@/i18n/i18n' -import type { OrcaCloudConsoleSection } from '../../../../shared/orca-cloud-console' +import type { + OrcaCloudConsoleBillingHint, + OrcaCloudConsoleSection +} from '../../../../shared/orca-cloud-console' let opening: OrcaCloudConsoleSection | null = null -export async function openCloudConsole(section: OrcaCloudConsoleSection): Promise { +/** `billingHint`: the plan and period picked in the plans dialog, for the billing page to preselect. */ +export async function openCloudConsole( + section: OrcaCloudConsoleSection, + billingHint?: OrcaCloudConsoleBillingHint +): Promise { // A double click mints one code, not two. if (opening === section) { return } opening = section try { - const result = await window.api.orcaProfiles.openCloudConsole({ section }) + const result = await window.api.orcaProfiles.openCloudConsole( + billingHint ? { section, billingHint } : { section } + ) if (result.status !== 'opened') { toast.error( translate('settings.cloudConsole.openFailed', 'Couldn’t open the browser. Try again.') diff --git a/src/shared/orca-cloud-console.ts b/src/shared/orca-cloud-console.ts index a63c06c474e..8f16fd271d7 100644 --- a/src/shared/orca-cloud-console.ts +++ b/src/shared/orca-cloud-console.ts @@ -2,6 +2,8 @@ // only links to it (Settings rows, account menu, "Fix in browser" buttons); main opens the page in // the system browser through a single-use ticket URL from auth. +import { ORCA_BILLING_INTERVALS, type OrcaBillingInterval } from './orca-billing' + /** Console pages the desktop can ask auth for (the ticket's `page`). */ export const ORCA_CLOUD_CONSOLE_SECTIONS = [ 'home', @@ -12,7 +14,39 @@ export const ORCA_CLOUD_CONSOLE_SECTIONS = [ ] as const export type OrcaCloudConsoleSection = (typeof ORCA_CLOUD_CONSOLE_SECTIONS)[number] -export type OrcaCloudConsoleOpenArgs = { section: OrcaCloudConsoleSection } +/** + * The plan and billing period picked in the desktop's plans dialog, for the console's billing page + * to preselect (a hint: the page never buys on its own). Sent only to an auth that advertises + * ORCA_CLOUD_CONSOLE_BILLING_HINT_CAPABILITY, so an older one never sees fields it would refuse. + */ +export type OrcaCloudConsoleBillingHint = { plan: string; interval: OrcaBillingInterval } + +/** Desktop capability flag auth sets once its console ticket accepts `plan` and `interval`. */ +export const ORCA_CLOUD_CONSOLE_BILLING_HINT_CAPABILITY = 'console.billing_hint' + +export type OrcaCloudConsoleOpenArgs = { + section: OrcaCloudConsoleSection + /** Billing page only; ignored for every other section. */ + billingHint?: OrcaCloudConsoleBillingHint +} + +const PLAN_ID = /^[a-z][a-z0-9_]{0,39}$/ + +/** The hint as main accepts it from the renderer, or null for anything else. */ +export function parseOrcaCloudConsoleBillingHint( + value: unknown +): OrcaCloudConsoleBillingHint | null { + if (typeof value !== 'object' || value === null) { + return null + } + const plan = 'plan' in value ? value.plan : undefined + const interval = 'interval' in value ? value.interval : undefined + if (typeof plan !== 'string' || !PLAN_ID.test(plan)) { + return null + } + const known = ORCA_BILLING_INTERVALS.find((entry) => entry === interval) + return known ? { plan, interval: known } : null +} export type OrcaCloudConsoleOpenResult = /** `signedIn: false`: no ticket, so the browser asks the person to sign in. */