> ## Documentation Index
> Fetch the complete documentation index at: https://docs.y.uno/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Test in the sandbox first: base URL https://api-sandbox.y.uno (Test Mode in the Dashboard). Production is https://api.y.uno (Live Mode).
> Every API request needs the public-api-key and private-secret-key headers. Get them in the Dashboard at https://dashboard.y.uno/developers (Developers > Credentials); sandbox and production keys are different.
> Send an X-Idempotency-Key (a UUID) when creating payments, refunds and cancels, so retries do not create duplicates.
> Start with the Quickstart (https://docs.y.uno/docs/quickstart.md) and the agent guide (https://docs.y.uno/agents.md). To search these docs from a coding agent, connect the docs MCP server at https://docs.y.uno/mcp.

# 设置外部订阅支付方式

> 将 Stripe Billing 订阅关联到客户已在 Yuno 中验证的卡，或将已关联的订阅转移到另一种已注册的支付方式，不发起支付。

在以下两种情况下，将此 endpoint 用于 [Stripe Billing 订阅](https://docs.y.uno/zh-hans/docs/payment-features/subscriptions/stripe-billing.md)：

* **订阅尚未关联到 Yuno。** 例如，在您集成 Yuno 之前于 Stripe 中创建的订阅，其客户已在 Yuno 中注册并验证了一张卡。Yuno 会将该订阅关联到这张卡，并由 Yuno 对后续续订收费。
* **订阅已经关联。** Yuno 会将其转移到同一客户的另一种已存储的支付方式。

> [!NOTE]
>
> **不会发起支付**
>
> Yuno 不会向客户收费，也不会执行 \$0 验证或 3DS。Yuno 会在 Stripe 中将该支付方式设为订阅的默认支付方式，后续续订将从该支付方式扣款。

## 请求

| 字段 | 描述 |
| - | - |
| `external_subscription_id`（path） | Stripe 订阅 ID（`sub_...`），长度为 3 到 255 个字符。 |
| `account_id` | 已接入计费桥接的 Yuno 账户（UUID）。 |
| `customer_payer.id` | 拥有该订阅的 **Yuno 客户 ID**（UUID）。它不是 Stripe 客户 ID（`cus_...`）。 |
| `vaulted_token` | 该客户某个已注册支付方式的存储令牌（UUID）。 |
| `external_customer_id` | 该订阅的 **Stripe 客户 ID**（`cus_...`），长度为 3 到 255 个字符。关联尚未关联的订阅时必填。转移已关联的订阅时可选：发送后，Yuno 会检查它是否为该订阅的 Stripe 客户。空值视为未发送。 |

`X-Idempotency-Key` 标头为必填项。使用相同的幂等键重放相同的请求会返回第一次的结果。

响应会返回请求中的字段。当请求发送了 `external_customer_id` 时，响应还会返回 `external_customer_id` 和 `result`：

| `result` | 含义 |
| - | - |
| `LINKED` | 该订阅此前未关联到 Yuno。Yuno 已将其关联到该支付方式。 |
| `PAYMENT_METHOD_CHANGED` | 该订阅此前已关联。Yuno 已将其转移到该支付方式。 |
| `NO_CHANGE` | 该订阅已在使用该支付方式扣款。未做任何更改。 |

关联尚未关联的订阅时：

* 请求必须发送 `external_customer_id`。如果不发送，未关联的订阅会返回 `EXTERNAL_SUBSCRIPTION_NOT_FOUND`。
* Stripe 订阅必须处于 `active` 或 `trialing` 状态，并且自动收费（`collection_method: charge_automatically`），其客户必须是 `external_customer_id`。
* 该卡必须此前已在 Yuno 中验证，例如在注册时进行过 \$0 验证。Yuno 会使用该验证，以商户发起的方式对续订收费。

此 API 按账户启用。请联系您的大客户经理申请访问权限。

## 错误

| 状态 | 代码 | 含义 | 处理方式 |
| - | - | - | - |
| `400` | `INVALID_PARAMETERS` | 某个字段或 `X-Idempotency-Key` 标头缺失或格式错误，或者该幂等键已用于另一个订阅或支付方式。 | 修正请求。如需进行其他更改，请使用新的幂等键。 |
| `400` | `CUSTOMER_NOT_FOUND` | 该客户在此账户中不存在。 | 检查 `customer_payer.id` 是否为 Yuno 客户 ID，而不是 Stripe 的 `cus_` ID。 |
| `400` | `VAULTED_TOKEN_NOT_FOUND` | `vaulted_token` 不属于该客户的支付方式。 | 使用该客户[已注册的支付方式](https://docs.y.uno/zh-hans/reference/payment-methods-direct-workflow/retrieve-enrolled-payment-methods-api.md)中的令牌。 |
| `400` | `VAULTED_TOKEN_NOT_ENROLLED` | 该支付方式未注册。仅当请求发送了 `external_customer_id` 时适用。 | 使用状态为 `ENROLLED` 的支付方式。 |
| `400` | `VAULTED_TOKEN_WITHOUT_COUNTRY` | 该支付方式没有国家信息，而关联订阅需要该信息。 | 使用该客户的另一种支付方式，或重新注册该卡。 |
| `400` | `VAULTED_TOKEN_WITHOUT_NETWORK_TRANSACTION_ID` | 该卡从未验证，因此 Yuno 无法以商户发起的方式对续订收费。 | 先验证该卡：使用 `external_subscription` 对象执行 \$0 验证，与[免费试用](https://docs.y.uno/zh-hans/docs/payment-features/subscriptions/stripe-billing.md#free-trials)相同。 |
| `400` | `EXTERNAL_SUBSCRIPTION_NOT_FOUND` | Stripe 中该账户下没有具有此 ID 的订阅，或者请求未发送 `external_customer_id` 且该订阅未关联到 Yuno。 | 检查 Stripe 订阅 ID 和账户。如需关联订阅，请发送 `external_customer_id`。 |
| `400` | `EXTERNAL_SUBSCRIPTION_NOT_ACTIVE` | Stripe 订阅既不是 `active` 也不是 `trialing` 状态。 | 只有处于 active 或 trialing 状态的订阅才能被关联。 |
| `400` | `EXTERNAL_SUBSCRIPTION_NOT_CHARGED_AUTOMATICALLY` | 该 Stripe 订阅通过发送发票收费，而不是自动扣款。 | 在 Stripe 中将 `collection_method` 设为 `charge_automatically`。 |
| `400` | `CUSTOMER_MISMATCH` | 该订阅属于其他客户，或者 `external_customer_id` 不是该订阅的 Stripe 客户。 | 发送拥有该订阅的客户。 |
| `403` | `PRODUCT_NOT_ENABLED` | 该账户未启用计费桥接。 | 联系您的大客户经理。 |
| `409` | `CONCURRENT_CHANGE` | 该订阅的另一个支付方式更改已先完成。 | 在发送新的更改之前，先在 Stripe 中检查该订阅。 |
| `409` | `EXTERNAL_SUBSCRIPTION_PENDING_ACTIVATION` | 该订阅正在迁移到 Yuno。仅当请求发送了 `external_customer_id` 时适用；不发送时，返回 `EXTERNAL_SUBSCRIPTION_NOT_FOUND`。 | 迁移完成后重试。 |
| `502` | `BILLING_PLATFORM_ERROR` | 计费平台无法确认此更改。原支付方式保持不变，或者订阅保持未关联状态。 | 使用新的 `X-Idempotency-Key` 重试。 |


## OpenAPI

````yaml openapi/zh-hans/subscriptions/change-external-subscription-payment-method.json POST /external-subscriptions/{external_subscription_id}/payment-method
openapi: 3.1.0
info:
  title: subscription
  version: 1.0.2
servers:
  - url: https://api-sandbox.y.uno/v1
    description: Sandbox
  - url: https://api.y.uno/v1
    description: Production (US)
security:
  - sec0: []
    sec1: []
paths:
  /external-subscriptions/{external_subscription_id}/payment-method:
    post:
      summary: 设置外部订阅支付方式
      description: >-
        设置 Stripe Billing 订阅所扣款的支付方式，无需发起支付。适用于两种情况：


        - **订阅尚未关联到 Yuno**：发送 `external_customer_id`，Yuno 会将其关联到客户已在 Yuno
        中验证过的卡。Stripe 订阅必须处于 `active` 或 `trialing` 状态，自动扣款（`collection_method:
        charge_automatically`），并且属于 `external_customer_id`。该卡必须已经过验证，这样 Yuno
        才能以商户发起的方式扣取续订款项。

        - **订阅已经关联**：Yuno 将其转移到客户已在 Yuno
        中注册的另一种支付方式。在这种情况下，`external_customer_id` 是可选的。


        不会产生支付：Yuno 不会向客户扣款，也不会执行 $0 验证或 3DS。之后的续订会向 `vaulted_token` 中的支付方式扣款。


        `customer_payer.id` 是 Yuno 客户 ID；`external_customer_id` 是 Stripe 客户
        ID（`cus_...`）。发送订阅已在使用的支付方式会返回 `200`，并且不做任何更改（`result` 为
        `NO_CHANGE`）。当请求发送了 `external_customer_id` 时，响应会包含
        `external_customer_id` 和 `result`。


        该 API 按账户启用：请联系您的大客户经理申请访问权限。
      operationId: change-external-subscription-payment-method
      parameters:
        - name: external_subscription_id
          in: path
          description: 账单平台中订阅的 ID，例如 Stripe 订阅 ID（`sub_...`）。长度为 3 到 255 个字符。
          schema:
            type: string
            minLength: 3
            maxLength: 255
          required: true
          example: sub_1QF2abc
        - name: X-Idempotency-Key
          in: header
          description: >-
            此次变更的唯一键，例如 UUID。必填：没有该键的请求会返回 `400`
            `INVALID_PARAMETERS`。使用相同的键重放相同的请求，会返回第一次请求的结果。将一个键用于另一个订阅或支付方式，会返回
            `400` `INVALID_PARAMETERS`。在出现 `502` 后重试时，请发送新的键。
          schema:
            type: string
          required: true
          example: 7c2e0a4b-3f5d-4c1e-9b8a-6d2f1e0c9a77
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - account_id
                - customer_payer
                - vaulted_token
              properties:
                account_id:
                  type: string
                  format: uuid
                  description: 已接入账单桥接的 Yuno 账户的唯一标识符（UUID）。
                  examples:
                    - 9104911d-5df9-429e-8488-ad41abea1a4b
                customer_payer:
                  type: object
                  required:
                    - id
                  description: 拥有该订阅和支付方式的客户。
                  properties:
                    id:
                      type: string
                      format: uuid
                      description: 拥有该订阅的 Yuno 客户 ID（UUID）。这不是 Stripe 客户 ID（`cus_...`）。
                      examples:
                        - cfae0941-7234-427a-a739-ef4fce966c79
                vaulted_token:
                  type: string
                  format: uuid
                  description: 客户已注册的某个支付方式的存储令牌（UUID）。订阅此后会向它扣款。
                  examples:
                    - 2deff5c9-da62-4888-8f05-df76ab2967a5
                external_customer_id:
                  type: string
                  minLength: 3
                  maxLength: 255
                  description: >-
                    订阅的 Stripe 客户 ID（`cus_...`）。长度为 3 到 255
                    个字符。关联尚未关联的订阅时必填；转移已关联的订阅时可选。发送后，Yuno 会检查该 Stripe
                    订阅是否属于此客户。空值视为未发送。
                  examples:
                    - cus_RplXYZ
            examples:
              Change the payment method:
                value:
                  account_id: 9104911d-5df9-429e-8488-ad41abea1a4b
                  customer_payer:
                    id: cfae0941-7234-427a-a739-ef4fce966c79
                  vaulted_token: 2deff5c9-da62-4888-8f05-df76ab2967a5
                  external_customer_id: cus_RplXYZ
      responses:
        '200':
          description: 订阅现在会向 `vaulted_token` 中的支付方式扣款。`result` 说明它是新关联、已变更，还是本来就在使用该支付方式。
          content:
            application/json:
              schema:
                type: object
                properties:
                  external_subscription_id:
                    type: string
                    description: 外部账单平台中订阅的 ID。
                  account_id:
                    type: string
                    format: uuid
                    description: 请求中的 Yuno 账户 ID。
                  customer_payer:
                    type: object
                    properties:
                      id:
                        type: string
                        format: uuid
                        description: 请求中的 Yuno 客户 ID。
                  vaulted_token:
                    type: string
                    format: uuid
                    description: 订阅此后扣款所使用的存储令牌。
                  external_customer_id:
                    type: string
                    description: 请求中的 Stripe 客户 ID。仅当请求发送了 `external_customer_id` 时返回。
                  result:
                    type: string
                    enum:
                      - LINKED
                      - PAYMENT_METHOD_CHANGED
                      - NO_CHANGE
                    description: >-
                      仅当请求发送了 `external_customer_id` 时返回。订阅尚未关联到 Yuno 时为
                      `LINKED`，转移到该支付方式时为 `PAYMENT_METHOD_CHANGED`，本来就向它扣款时为
                      `NO_CHANGE`。
              examples:
                Subscription linked:
                  value:
                    external_subscription_id: sub_1QF2abc
                    account_id: 9104911d-5df9-429e-8488-ad41abea1a4b
                    customer_payer:
                      id: cfae0941-7234-427a-a739-ef4fce966c79
                    vaulted_token: 2deff5c9-da62-4888-8f05-df76ab2967a5
                    external_customer_id: cus_RplXYZ
                    result: LINKED
                Payment method changed:
                  value:
                    external_subscription_id: sub_1QF2abc
                    account_id: 9104911d-5df9-429e-8488-ad41abea1a4b
                    customer_payer:
                      id: cfae0941-7234-427a-a739-ef4fce966c79
                    vaulted_token: 2deff5c9-da62-4888-8f05-df76ab2967a5
                    external_customer_id: cus_RplXYZ
                    result: PAYMENT_METHOD_CHANGED
        '400':
          description: 请求无效，未找到客户、支付方式或订阅，或者它们不匹配，或者订阅或卡无法关联。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              examples:
                INVALID_PARAMETERS:
                  value:
                    code: INVALID_PARAMETERS
                    messages:
                      - >-
                        The field 'customer_payer.id' is required and must be a
                        UUID.
                CUSTOMER_NOT_FOUND:
                  value:
                    code: CUSTOMER_NOT_FOUND
                    messages:
                      - The customer does not exist in the account.
                VAULTED_TOKEN_NOT_FOUND:
                  value:
                    code: VAULTED_TOKEN_NOT_FOUND
                    messages:
                      - >-
                        The vaulted_token is not one of the customer's payment
                        methods.
                EXTERNAL_SUBSCRIPTION_NOT_FOUND:
                  value:
                    code: EXTERNAL_SUBSCRIPTION_NOT_FOUND
                    messages:
                      - >-
                        The billing platform has no subscription with this id
                        for the account.
                EXTERNAL_SUBSCRIPTION_NOT_FOUND_WITHOUT_EXTERNAL_CUSTOMER_ID:
                  summary: Without external_customer_id
                  value:
                    code: EXTERNAL_SUBSCRIPTION_NOT_FOUND
                    messages:
                      - >-
                        No active external subscription with this id for the
                        account.
                CUSTOMER_MISMATCH:
                  value:
                    code: CUSTOMER_MISMATCH
                    messages:
                      - >-
                        The external subscription belongs to another customer or
                        to another external_customer_id.
                CUSTOMER_MISMATCH_WITHOUT_EXTERNAL_CUSTOMER_ID:
                  summary: Without external_customer_id
                  value:
                    code: CUSTOMER_MISMATCH
                    messages:
                      - The external subscription belongs to another customer.
                VAULTED_TOKEN_NOT_ENROLLED:
                  summary: Only when the request sends external_customer_id
                  value:
                    code: VAULTED_TOKEN_NOT_ENROLLED
                    messages:
                      - The vaulted_token is not enrolled.
                VAULTED_TOKEN_WITHOUT_COUNTRY:
                  value:
                    code: VAULTED_TOKEN_WITHOUT_COUNTRY
                    messages:
                      - The vaulted_token has no country.
                VAULTED_TOKEN_WITHOUT_NETWORK_TRANSACTION_ID:
                  value:
                    code: VAULTED_TOKEN_WITHOUT_NETWORK_TRANSACTION_ID
                    messages:
                      - >-
                        The vaulted_token has no network transaction id to
                        charge the renewals merchant-initiated.
                EXTERNAL_SUBSCRIPTION_NOT_ACTIVE:
                  value:
                    code: EXTERNAL_SUBSCRIPTION_NOT_ACTIVE
                    messages:
                      - >-
                        The external subscription is neither active nor
                        trialing.
                EXTERNAL_SUBSCRIPTION_NOT_CHARGED_AUTOMATICALLY:
                  value:
                    code: EXTERNAL_SUBSCRIPTION_NOT_CHARGED_AUTOMATICALLY
                    messages:
                      - The external subscription is not charged automatically.
        '403':
          description: PRODUCT_NOT_ENABLED：该账户未启用账单桥接。请联系您的大客户经理。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              examples:
                PRODUCT_NOT_ENABLED:
                  value:
                    code: PRODUCT_NOT_ENABLED
                    messages:
                      - >-
                        External subscriptions are not enabled for this account.
                        Please contact your Key Account Manager.
        '409':
          description: >-
            CONCURRENT_CHANGE：该订阅的另一次支付方式变更已先完成。EXTERNAL_SUBSCRIPTION_PENDING_ACTIVATION：订阅正在迁移到
            Yuno；请在迁移完成后重试。仅当请求发送了 external_customer_id 时才会出现；未发送时，订阅会返回
            EXTERNAL_SUBSCRIPTION_NOT_FOUND。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              examples:
                CONCURRENT_CHANGE:
                  value:
                    code: CONCURRENT_CHANGE
                    messages:
                      - >-
                        Another payment method change for this subscription
                        completed first.
                EXTERNAL_SUBSCRIPTION_PENDING_ACTIVATION:
                  value:
                    code: EXTERNAL_SUBSCRIPTION_PENDING_ACTIVATION
                    messages:
                      - >-
                        The external subscription is being migrated to Yuno.
                        Retry once the migration finishes.
        '502':
          description: BILLING_PLATFORM_ERROR：账单平台无法确认该变更。订阅仍按之前的方式扣款。请使用新的幂等键重试。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
              examples:
                BILLING_PLATFORM_ERROR:
                  value:
                    code: BILLING_PLATFORM_ERROR
                    messages:
                      - >-
                        The billing platform could not confirm the change. The
                        subscription keeps charging what it charged before.
                        Retry with a new idempotency key.
                BILLING_PLATFORM_ERROR_WITHOUT_EXTERNAL_CUSTOMER_ID:
                  summary: Without external_customer_id
                  value:
                    code: BILLING_PLATFORM_ERROR
                    messages:
                      - >-
                        The billing platform could not confirm the change. The
                        previous payment method stays. Retry with a new
                        idempotency key.
      deprecated: false
components:
  schemas:
    Error:
      type: object
      properties:
        code:
          type: string
          description: 错误代码。
        messages:
          type: array
          items:
            type: string
          description: 错误的详细说明，便于阅读。
  securitySchemes:
    sec0:
      type: apiKey
      in: header
      name: public-api-key
    sec1:
      type: apiKey
      in: header
      name: private-secret-key

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.