Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 完善 Stripe 支付跳转 (#688)
GaoWei Liuebc235ea62d7
diff --git a/apps/desktop/src/main/billing/__tests__/index.test.ts b/apps/desktop/src/main/billing/__tests__/index.test.tsindex aa431f0a34..0b6cde0f8c 100644--- a/apps/desktop/src/main/billing/__tests__/index.test.ts+++ b/apps/desktop/src/main/billing/__tests__/index.test.ts@@ -472,9 +472,11 @@ describe('billing IPC', () => {});});- it('opens only public HTTPS Stripe Checkout URLs from the main top-level frame', async () => {+ it.each([+ 'https://checkout.stripe.com/c/pay/session_fixture#fragment',+ 'https://invoice.stripe.com/i/acct_fixture/test_fixture',+ ])('opens a public HTTPS Stripe payment URL from the main top-level frame: %s', async (url) => {const { call, openExternal } = harness();- const url = 'https://checkout.stripe.com/c/pay/session_fixture#fragment';await expect(call(BILLING_INVOKE.OPEN_PAYMENT_REDIRECT, { url })).resolves.toEqual({success: true,@@ -488,7 +490,9 @@ describe('billing IPC', () => {'javascript:alert(1)','stripe://checkout/session','https://checkout.stripe.com.evil.example/c/pay/test',+ 'https://invoice.stripe.com.evil.example/i/test','https://[email protected]/c/pay/test',+ 'https://[email protected]/i/test','https://user:[email protected]/c/pay/test','https://checkout.stripe.com:444/c/pay/test',`https://checkout.stripe.com/c/pay/${'x'.repeat(2_100)}`,diff --git a/apps/desktop/src/main/billing/__tests__/projection.test.ts b/apps/desktop/src/main/billing/__tests__/projection.test.tsindex dc11321993..644734a614 100644--- a/apps/desktop/src/main/billing/__tests__/projection.test.ts+++ b/apps/desktop/src/main/billing/__tests__/projection.test.ts@@ -772,6 +772,23 @@ describe('plan change projection', () => {});});+ it('accepts a Stripe Hosted Invoice redirect for an awaiting upgrade', () => {+ expect(projectBillingPlanChange(planChange({+ quotedCurrency: 'usd',+ paymentAction: {+ type: 'REDIRECT',+ url: 'https://invoice.stripe.com/i/acct_fixture/test_fixture',+ expiresAt: now,+ },+ }))).toMatchObject({+ status: 'AWAITING_PAYMENT',+ paymentAction: {+ type: 'REDIRECT',+ url: 'https://invoice.stripe.com/i/acct_fixture/test_fixture',+ },+ });+ });+it.each([['unknown status', { status: 'FUTURE_STATUS' }],['unknown change type', { changeType: 'SIDEGRADE' }],diff --git a/apps/desktop/src/main/billing/paymentRedirect.ts b/apps/desktop/src/main/billing/paymentRedirect.tsindex b08f1d247b..72237ce3cf 100644--- a/apps/desktop/src/main/billing/paymentRedirect.ts+++ b/apps/desktop/src/main/billing/paymentRedirect.ts@@ -2,7 +2,10 @@ const MAX_BILLING_REDIRECT_URL_LENGTH = 2_048;// Public provider-owned checkout hosts only. Merchant and Cindy-owned hosts do// not belong here; adding a host changes the desktop trust boundary.-const ALLOWED_BILLING_REDIRECT_HOSTS = new Set(['checkout.stripe.com']);+const ALLOWED_BILLING_REDIRECT_HOSTS = new Set([+ 'checkout.stripe.com',+ 'invoice.stripe.com',+]);export function isAllowedBillingRedirectUrl(value: unknown): value is string {if (diff --git a/apps/desktop/src/renderer/features/billing/BillingCheckoutDialog.tsx b/apps/desktop/src/renderer/features/billing/BillingCheckoutDialog.tsxindex 135ee2ed5d..3f44afb472 100644--- a/apps/desktop/src/renderer/features/billing/BillingCheckoutDialog.tsx+++ b/apps/desktop/src/renderer/features/billing/BillingCheckoutDialog.tsx@@ -30,6 +30,19 @@ export function BillingCheckoutDialog({const action = actionOf(state);const [qrDataUrl, setQrDataUrl] = useState<string | null>(null);const [remainingSeconds, setRemainingSeconds] = useState<number | null>(null);+ const openedRedirectKeyRef = useRef<string | null>(null);+ const redirectUrl = action?.type === 'REDIRECT' ? action.url : null;+ const redirectKey =+ action?.type === 'REDIRECT'+ ? [+ state.kind,+ state.order?.orderId ??+ state.subscription?.purchaseAttemptId ??+ state.subscription?.subscriptionId,+ action.url,+ action.expiresAt,+ ].join(':')+ : null;useEffect(() => {let active = true;@@ -79,6 +92,16 @@ export function BillingCheckoutDialog({if (action?.type === 'REDIRECT') void billingApi.openPaymentRedirect(action.url);};+ useEffect(() => {+ if (!state.open || state.phase !== 'AWAITING_PAYMENT') {+ openedRedirectKeyRef.current = null;+ return;+ }+ if (!redirectKey || !redirectUrl || openedRedirectKeyRef.current === redirectKey) return;+ openedRedirectKeyRef.current = redirectKey;+ void billingApi.openPaymentRedirect(redirectUrl);+ }, [redirectKey, redirectUrl, state.open, state.phase]);+// 过期判定以服务端为权威:服务端只下发仍有效的动作,动作过期后轮询响应里// 会把它置空。因此“曾经有动作、现在没有了”才代表过期,本地时钟偏移不会// 误藏仍可支付的二维码,也不会闪现已过期动作。倒计时仅作展示。diff --git a/apps/desktop/src/renderer/features/billing/PlanChangeDialog.tsx b/apps/desktop/src/renderer/features/billing/PlanChangeDialog.tsxindex b7cf70aa93..37cc8d8067 100644--- a/apps/desktop/src/renderer/features/billing/PlanChangeDialog.tsx+++ b/apps/desktop/src/renderer/features/billing/PlanChangeDialog.tsx@@ -1,4 +1,4 @@-import { useEffect, useMemo, useState } from 'react';+import { useEffect, useMemo, useRef, useState } from 'react';import * as Dialog from '@radix-ui/react-dialog';import * as QRCode from 'qrcode';import {@@ -240,6 +240,12 @@ export function PlanChangeStatusDialog({const change = state.planChange;const action: BillingPaymentAction | null = change?.paymentAction ?? null;const [qrDataUrl, setQrDataUrl] = useState<string | null>(null);+ const openedRedirectKeyRef = useRef<string | null>(null);+ const redirectUrl = action?.type === 'REDIRECT' ? action.url : null;+ const redirectKey =+ action?.type === 'REDIRECT'+ ? [change?.planChangeId, action.url, action.expiresAt].join(':')+ : null;const actionSeconds = useCountdownSeconds(state.phase === 'AWAITING_PAYMENT' ? (action?.expiresAt ?? null) : null,);@@ -264,6 +270,16 @@ export function PlanChangeStatusDialog({};}, [action]);+ useEffect(() => {+ if (!state.open || state.phase !== 'AWAITING_PAYMENT') {+ openedRedirectKeyRef.current = null;+ return;+ }+ if (!redirectKey || !redirectUrl || openedRedirectKeyRef.current === redirectKey) return;+ openedRedirectKeyRef.current = redirectKey;+ void billingApi.openPaymentRedirect(redirectUrl);+ }, [redirectKey, redirectUrl, state.open, state.phase]);+const busy = state.phase === 'QUOTING' || state.phase === 'CONFIRMING';const title = useMemo(() => {switch (state.phase) {diff --git a/apps/desktop/src/renderer/features/billing/__tests__/BillingPage.test.tsx b/apps/desktop/src/renderer/features/billing/__tests__/BillingPage.test.tsxindex c2f7699fdf..987495169f 100644--- a/apps/desktop/src/renderer/features/billing/__tests__/BillingPage.test.tsx+++ b/apps/desktop/src/renderer/features/billing/__tests__/BillingPage.test.tsx@@ -2,7 +2,7 @@import { act, fireEvent, render, screen, waitFor, within } from '@testing-library/react';import { beforeEach, describe, expect, it, vi } from 'vitest';-import type { BillingPaymentOrder, BillingSubscription } from '../../../../shared/billing';+import type { BillingSubscription } from '../../../../shared/billing';const i18n = {language: 'en',@@ -343,6 +343,7 @@ describe('BillingPage remote catalog rendering', () => {],})),getCurrentSubscription: vi.fn(async () => ({ subscription: null })),+ openPaymentRedirect: vi.fn(async () => ({ success: true })),},openExternal: vi.fn(),},@@ -675,6 +676,42 @@ describe('BillingPage remote catalog rendering', () => {expect(screen.queryByAltText('billing.checkout.qrAlt')).toBeNull();});+ it('opens a Stripe Checkout redirect automatically once and keeps the manual fallback', async () => {+ const url = 'https://checkout.stripe.com/c/pay/session_fixture';+ const subscription = {+ subscriptionId: 'subscription_incomplete',+ status: 'INCOMPLETE' as const,+ currentPeriodStartAt: null,+ currentPeriodEndAt: null,+ entitlementValidUntil: null,+ cancelAtPeriodEnd: false,+ effectivePlan: null,+ purchaseAttemptId: 'attempt_redirect',+ paymentAction: {+ type: 'REDIRECT' as const,+ url,+ expiresAt: new Date(Date.now() + 60_000).toISOString(),+ },+ };+ Object.assign(checkout.state, {+ open: true,+ kind: 'SUBSCRIPTION',+ phase: 'AWAITING_PAYMENT',+ subscription,+ });+ const openPaymentRedirect = vi.mocked(window.electronAPI.billing.openPaymentRedirect);++ const view = render(<BillingPage />);+ await waitFor(() => expect(openPaymentRedirect).toHaveBeenCalledWith({ url }));++ Object.assign(checkout.state, { subscription: { ...subscription } });+ view.rerender(<BillingPage />);+ expect(openPaymentRedirect).toHaveBeenCalledTimes(1);++ fireEvent.click(screen.getByText('billing.checkout.openPayment'));+ expect(openPaymentRedirect).toHaveBeenCalledTimes(2);+ });+it('does not show zero or block purchases when balance is not provisioned', async () => {window.electronAPI.billing.getBalance = vi.fn(async () => {throw Object.assign(new Error('[NOT_FOUND] balance account is not provisioned'), {@@ -1270,6 +1307,7 @@ describe('BillingPage plan change', () => {confirmPlanChange: vi.fn(),refreshPlanChange: vi.fn(),cancelPlanChange: vi.fn(),+ openPaymentRedirect: vi.fn(async () => ({ success: true })),});const install = (billing: ReturnType<typeof billingMocks>) => {@@ -1576,6 +1614,46 @@ describe('BillingPage plan change', () => {expect(billing.getCurrentSubscription).toHaveBeenCalledTimes(2);});+ it('opens a Stripe plan-change redirect automatically once and keeps the manual fallback', async () => {+ const billing = install(billingMocks());+ const url = 'https://checkout.stripe.com/c/pay/plan_change_fixture';+ billing.quotePlanChange.mockResolvedValue({+ planChangeId: 'plan_change_redirect',+ changeType: 'UPGRADE',+ status: 'QUOTED',+ quotedAmountMinor: 1100,+ quotedCurrency: 'usd',+ quoteExpiresAt: '2099-01-01T00:00:00.000Z',+ effectiveAt: '2026-07-24T00:00:00.000Z',+ paymentAction: null,+ });+ billing.confirmPlanChange.mockResolvedValue({+ planChangeId: 'plan_change_redirect',+ changeType: 'UPGRADE',+ status: 'AWAITING_PAYMENT',+ quotedAmountMinor: 1100,+ quotedCurrency: 'usd',+ quoteExpiresAt: null,+ effectiveAt: '2026-07-24T00:00:00.000Z',+ paymentAction: {+ type: 'REDIRECT',+ url,+ expiresAt: '2099-01-01T00:00:00.000Z',+ },+ });++ render(<BillingPage />);+ fireEvent.click(await screen.findByText('billing.settings.subscriptionCard.changeAction'));+ fireEvent.click((await screen.findByText('Max plan')).closest('button')!);+ fireEvent.click(await screen.findByText('billing.planChange.confirm'));++ await waitFor(() => expect(billing.openPaymentRedirect).toHaveBeenCalledWith({ url }));+ expect(billing.openPaymentRedirect).toHaveBeenCalledTimes(1);++ fireEvent.click(screen.getByText('billing.checkout.openPayment'));+ expect(billing.openPaymentRedirect).toHaveBeenCalledTimes(2);+ });+it('renders a grandfathered current plan from the captured subscription terms', async () => {const billing = billingMocks();const grandfathered = activeSubscription();diff --git a/apps/desktop/src/renderer/i18n/locales/en/common.json b/apps/desktop/src/renderer/i18n/locales/en/common.jsonindex 95d3a3e2ab..617f1f2dba 100644--- a/apps/desktop/src/renderer/i18n/locales/en/common.json+++ b/apps/desktop/src/renderer/i18n/locales/en/common.json@@ -7388,7 +7388,7 @@},"providers": {"alipay": "Alipay",- "stripe": "Card"+ "stripe": "Stripe"},"currentSubscription": {"title": "Current subscription",diff --git a/apps/desktop/src/renderer/i18n/locales/ja/common.json b/apps/desktop/src/renderer/i18n/locales/ja/common.jsonindex 9da8acabf5..4dc1cd7f20 100644--- a/apps/desktop/src/renderer/i18n/locales/ja/common.json+++ b/apps/desktop/src/renderer/i18n/locales/ja/common.json@@ -7377,7 +7377,7 @@},"providers": {"alipay": "Alipay",- "stripe": "カード"+ "stripe": "Stripe"},"currentSubscription": {"title": "現在のサブスクリプション",diff --git a/apps/desktop/src/renderer/i18n/locales/ko/common.json b/apps/desktop/src/renderer/i18n/locales/ko/common.jsonindex c7b9a7b2a3..c36c7dd03b 100644--- a/apps/desktop/src/renderer/i18n/locales/ko/common.json+++ b/apps/desktop/src/renderer/i18n/locales/ko/common.json@@ -7377,7 +7377,7 @@},"providers": {"alipay": "Alipay",- "stripe": "카드"+ "stripe": "Stripe"},"currentSubscription": {"title": "현재 구독",diff --git a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.jsonindex d4cdbfe89c..1311cccfb8 100644--- a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json+++ b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json@@ -7378,7 +7378,7 @@},"providers": {"alipay": "支付宝",- "stripe": "银行卡"+ "stripe": "Stripe"},"currentSubscription": {"title": "当前订阅",