文档中心

Stripe 多主体混合架构与平迁指南(HK + US)

本文档说明 accounts、portal 与 gitops 如何同时支持: 1. 平迁(Single-Entity Cutover):通过 Vault KV 模板与 ESO 参数化,把默认 Stripe 主体从 HK(acct1SvuMHLIhVa2N0n8)平滑切换到 U...

Stripe 多主体混合架构与平迁指南(HK + US)

本文档说明 accounts、portal 与 gitops 如何同时支持:

  1. 平迁(Single-Entity Cutover):通过 Vault KV 模板与 ESO 参数化,把默认 Stripe 主体从 HK(acct_1SvuMHLIhVa2N0n8)平滑切换到 US。
  2. 多公司业务线并存(Multi-Entity Hybrid):在同一套 accounts + console 中同时挂载 HK 与 US 两个 Stripe 独立商户主体,按套餐(billing_plans.features.stripe_profile)路由 Checkout / 升级 / 续费 / Portal / Webhook / 退款,避免跨公司主体混账。

1. 核心设计原则

  1. 零数据库破坏性迁移:

    • 主体归属直接存放在现有 JSONB 字段:
      • billing_plans.features:stripe_profile、business_line、merchant_entity
      • subscriptions.meta:stripe_profile、stripe_account_id
    • Webhook 幂等去重表 stripe_webhook_events 复用主键 event_id:
      • 默认主体(通常 hk)沿用原始 evt_...(兼容既有已处理记录);
      • 非默认主体(如 us)使用 <profile>:<event_id>(例如 us:evt_...),杜绝跨主体事件 ID 冲突或绕过重放。
    • 不改动 cmd/accountsvc/main.go(保持 native schema source manifest 校验和不变)及 full_business_contract.json(保持 81 张业务表契约不变)。
  2. 安全边界(Strict Isolation):

    • 每个 stripeClient 实例拥有独立的 secretKey、webhookSecret、accountID、merchantEntity、allowedPriceID、portalConfigurationID 与 Quote HMAC 密钥上下文。
    • 不允许将现有 hk 订阅直接通过 PRORATION / Schedule 升级或续费到 us 套餐;跨主体变更会被后端拒绝(需到期/取消后重新在新主体下单)。
    • GET /api/auth/admin/billing/stripe-profiles 仅返回脱敏摘要(profile、accountId、merchantEntity、mode、liveMode、enabled、webhookPath、isDefault),绝不返回 sk_* 或 whsec_*。

2. 环境变量与 Vault KV 模板化

2.1 运行时配置优先级

accounts 保留既有单一 STRIPE_* 环境变量作为默认主体兼容回退,同时支持通过 STRIPE_PROFILES_FILE 或 STRIPE_PROFILES_JSON 声明多个具名主体(默认主体名由 STRIPE_DEFAULT_PROFILE 控制,未设置时默认为 hk):

{
  "defaultProfile": "hk",
  "profiles": {
    "hk": {
      "accountId": "acct_1SvuMHLIhVa2N0n8",
      "merchantEntity": "HK Entity Ltd",
      "mode": "live",
      "secretKey": "sk_live_hk_...",
      "webhookSecret": "whsec_hk_...",
      "frontendUrl": "https://console.svc.plus",
      "portalConfigurationId": "bpc_hk_..."
    },
    "us": {
      "accountId": "acct_us_...",
      "merchantEntity": "US Entity Inc",
      "mode": "live",
      "secretKey": "sk_live_us_...",
      "webhookSecret": "whsec_us_...",
      "frontendUrl": "https://console.svc.plus",
      "portalConfigurationId": "bpc_us_..."
    }
  }
}

若只切换到新主体(例如切换默认收款主体为 US),只需更新 Vault KV 与 STRIPE_DEFAULT_PROFILE=us 或替换对应主体 Key;若仅保留 legacy 单主体 STRIPE_* 环境变量,系统自动将其映射为 defaultProfile(hk)。

2.2 Vault KV 标准路径规划

主体 Vault KV v2 路径(逻辑) Vault API / ESO 策略路径 关键字段
既有单主体(兼容) kv/prod/accounts/runtime kv/data/prod/accounts/runtime STRIPE_SECRET_KEY, STRIPE_WEBHOOK_SECRET, STRIPE_MODE, STRIPE_PORTAL_CONFIGURATION_ID
HK 独立主体 kv/prod/payments/stripe/hk kv/data/prod/payments/stripe/hk account_id, merchant_entity, mode, secret_key, webhook_secret, portal_configuration_id, frontend_url
US 独立主体 kv/prod/payments/stripe/us kv/data/prod/payments/stripe/us account_id, merchant_entity, mode, secret_key, webhook_secret, portal_configuration_id, frontend_url

配套 GitOps 示例模板位于 infra 仓库:gitops/services/prod-k3s-v6/web-saas-prod/vault-secrets.stripe-multi-entity.example.yaml。


3. Webhook 端点与验签路由

端点 用途 行为
POST /api/billing/stripe/webhook 兼容旧入口 使用 defaultProfile(默认 hk)对应的 whsec_* 验签,并与默认主体共享未加前缀的幂等 key
POST /api/billing/stripe/webhook/hk HK 主体专用入口 严格使用 hk 主体凭据验签与回查 Stripe API
POST /api/billing/stripe/webhook/us US 主体专用入口 严格使用 us 主体凭据验签与回查 Stripe API;若 us 未配置则返回 503 stripe_not_configured

在 Stripe Live 控制台为每个商户主体配置各自的 Webhook Endpoint URL,并把对应的 Signing secret 写入各自 Vault KV 路径即可,无需互相覆盖。


4. 目录同步与 Ops 后台操作

4.1 同步指定主体的 Stripe Catalog

使用 scripts/stripe-sync-catalog.sh --stripe-profile <hk|us> 过滤并同步对应主体的商品/价格,同时校验对应主体的 /api/billing/stripe/webhook/<profile>:

STRIPE_SECRET_KEY=sk_live_us_... \
STRIPE_WEBHOOK_URL=https://accounts.svc.plus/api/billing/stripe/webhook/us \
STRIPE_WEBHOOK_SECRET=whsec_us_... \
ACCOUNTS_ADMIN_TOKEN=... \
ACCOUNTS_BASE_URL=https://accounts.svc.plus \
  scripts/stripe-sync-catalog.sh --env prod --stripe-profile us --domain-base svc.plus --write-catalog

4.2 Ops 后台配置(Portal)与财务对账

  1. 套餐与主体路由矩阵(用户无感知):

    • 公开接口 GET /api/billing/plans 自动剥离 stripeProfile、businessLine、merchantEntity、reconciliationTag、regionScope,终端用户在前台完全无感知。
    • Plus(JP / PH / APAC 订阅) -> 路由到 hk(businessLine=hk_apac_plus、reconciliationTag=FIN-HK-PLUS-APAC、regionScope=JP,PH,APAC)。
    • Pro / Custom(美国边缘节点以上) -> 路由到 us(businessLine=us_edge_pro、reconciliationTag=FIN-US-PRO-EDGE、regionScope=US)。
    • 当用户从 HK 的 Plus 升级到 US 的 Pro+ 时,后端无缝在新主体创建订阅,并在新主体首期付款回调激活后自动取消 HK 原订阅(避免双重扣费),用户全程无报错或中断感知。
  2. Stripe Metadata 与财务对账导出:

    • 每次 Checkout / 订阅 / PaymentIntent / Stripe Price 均自动写入 stripe_profile、business_line、reconciliation_tag、region_scope、merchant_entity。
    • Ops 后台 /panel/ops/billing/ledger 调用 GET /api/auth/admin/billing/reconciliation,支持按 stripeProfile(hk / us)、reconciliationTag、businessLine 聚合净额并一键导出带 BOM 的 CSV(?format=csv)。

5. 上线与平迁 Runbook

场景 A:保持 HK 在线并灰度启用 US 业务线(零中断)

  1. 在 Vault 创建 kv/prod/payments/stripe/hk(同步现有 HK 凭据)和 kv/prod/payments/stripe/us(写入 US 凭据)。
  2. 为 web-saas-secrets-reader VaultWorkloadIdentity / Vault Policy 追加 kv/data/prod/payments/stripe/* 只读权限。
  3. 在 gitops/services/prod-k3s-v6/web-saas-prod/ 应用参考 vault-secrets.stripe-multi-entity.example.yaml 生成 stripe-profiles.json 并挂载到 accounts。
  4. 在 US Stripe Dashboard 创建 Endpoint:https://accounts.svc.plus/api/billing/stripe/webhook/us。
  5. 执行 scripts/stripe-sync-catalog.sh --stripe-profile us --write-catalog 或在 Ops 控制台新建 stripeProfile=us 的专属套餐并上线。

场景 B:将新增默认收款从 HK 平迁到 US

  1. 确保 us 主体已按场景 A 启用且已有老客户留在 hk(老客户续费/取消/Portal/退款会自动按 subscriptions.meta.stripe_profile == "hk" 继续路由到 HK)。
  2. 将新增套餐的 stripeProfile(或 STRIPE_DEFAULT_PROFILE)切换为 us 即可,已有在网 HK 订阅不受任何影响。

6. US Stripe 接入配置详细向导(HK 收款 Plus + US 收款 Pro/Custom)

启用 US Stripe(同时保持 HK 在线收款 Plus、US 收款 Pro/Custom)需要依次完成以下 4 部分配置,并严格遵守第 5 点的切流顺序:

6.1 US Stripe Dashboard(Stripe 后台)

在 US 主体 Stripe Live 账户中准备并记录以下信息:

  1. API Secret Key:sk_live_...(或受限 Key rk_live_...,需具备 Checkout / Subscriptions / Invoices / Customers / Billing Portal / Prices 权限)。
  2. Account ID:acct_...(US 主体的 Stripe Account ID)。
  3. Webhook Endpoint:
    • URL:https://accounts.svc.plus/api/billing/stripe/webhook/us
    • 监听事件:至少包含 checkout.session.completed、customer.subscription.created、customer.subscription.updated、customer.subscription.deleted、invoice.paid、invoice.payment_failed、charge.refunded、charge.dispute.created。
    • 记录生成的 Signing secret:whsec_...。
  4. Customer Portal Configuration:
    • 在 Billing → Customer Portal 配置好自助账单 / 支付方式管理后,记录 Configuration ID:bpc_...。
  5. Product & Prices(商品与价格):
    • 在 US Stripe 中创建或同步 PRO-* / CUSTOM-* 对应的 Price(获得 price_...),也可直接通过第 6.4 节中的 scripts/stripe-sync-catalog.sh 自动创建并打上 FIN-US-PRO-EDGE 标签。

6.2 Vault 凭据与权限(https://vault.svc.plus)

  1. 写入 HK 与 US 凭据:
    • 写入 HK 路径 kv/prod/payments/stripe/hk(同步现有 kv/prod/accounts/runtime 的 HK Live 凭据)与 US 路径 kv/prod/payments/stripe/us,两条路径均需包含以下 7 个字段:
      • account_id(如 acct_...)
      • merchant_entity(如 US Entity Inc / HK Entity Ltd)
      • mode(live)
      • secret_key(sk_live_...)
      • webhook_secret(whsec_...)
      • portal_configuration_id(bpc_...)
      • frontend_url(https://console.svc.plus)
  2. Vault 读取策略放行:
    • 为 prod-k3s-v6-web-saas-secrets-reader(对应 gitops/operations/workload-identities/prod-k3s-v6/web-saas-secrets-reader.yaml)的 Vault Policy 追加 kv/data/prod/payments/stripe/* 只读权限。

6.3 GitOps / K3s(ExternalSecret 与 accounts 挂载)

  1. 参考模板 gitops/services/prod-k3s-v6/web-saas-prod/vault-secrets.stripe-multi-entity.example.yaml,在 gitops/services/prod-k3s-v6/web-saas-prod/vault-secrets.yaml 中新增 ExternalSecret/accounts-stripe-profiles,将 hk 与 us 凭据渲染为 stripe-profiles.json。
  2. 在 gitops/services/prod-k3s-v6/web-saas-prod/helmrelease.yaml 中将 Secret accounts-stripe-profiles 挂载到 web-saas-accounts 容器(如 /var/run/accounts-stripe-profiles),并设置环境变量:
    • STRIPE_PROFILES_FILE=/var/run/accounts-stripe-profiles/stripe-profiles.json
    • STRIPE_DEFAULT_PROFILE=hk(未指定主体或兜底路由继续走 HK)

以上均就绪后,POST https://accounts.svc.plus/api/billing/stripe/webhook/us 会从 503 变为 401(等待 Stripe 签名校验),GET /api/auth/admin/billing/stripe-profiles 也会同时返回 hk 和 us 两个 enabled: true 主体。

6.4 同步 US Price 并切换套餐路由(最后一跳)

在 accounts 容器已加载 us 主体后,执行以下任一方式将 PRO-* / CUSTOM-* 指向 US:

  • 方式 A(脚本自动同步 + 写入目录):
    STRIPE_SECRET_KEY=sk_live_us_... \
    STRIPE_WEBHOOK_URL=https://accounts.svc.plus/api/billing/stripe/webhook/us \
    STRIPE_WEBHOOK_SECRET=whsec_us_... \
    ACCOUNTS_ADMIN_TOKEN=... \
    ACCOUNTS_BASE_URL=https://accounts.svc.plus \
      scripts/stripe-sync-catalog.sh --env prod --stripe-profile us --domain-base svc.plus --write-catalog
    
  • 方式 B(Ops 后台可视化切换):进入 https://console.svc.plus → 运营台 → 套餐目录:
    1. 对 Plus 套餐点击 「套用 HK · Plus(JP/PH)」(确保标签 FIN-HK-PLUS-APAC)。
    2. 对 Pro / Custom 套餐填入 US Stripe 的 Stripe Price ID(price_...),点击 「套用 US · Pro+(美国边缘节点)」(自动填入 us、us_edge_pro、FIN-US-PRO-EDGE、US)并保存。

6.5 切流前安全红线

[!WARNING] 在第 6.2 与 6.3 步(Vault + ESO)生效前,不要提前把线上数据库中的 PRO-* / CUSTOM-* 套餐的 stripeProfile 改为 us,否则用户购买 Pro 时会因为 us 客户端未初始化而无法创建 Checkout。

反馈

这篇文档有帮助吗?

XWorkTech

云中立 · AI 工作区产品与文档

产品文档、解决方案与电子书,源自 knowledge 知识库持续维护。