> ## 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.

# 快捷 checkout

> 使用 iOS 或 Web SDK，在 Apple Pay 付款单上显示订单明细、提供配送方式，并响应配送地址的变更。

自 Yuno iOS SDK 2.24.0 和 Yuno Web SDK 1.10.8 起可用。

订单明细、配送地址更新和配送方式选择器将 Apple Pay 付款单变为快捷 checkout：

* **订单明细**：显示小计、运费、税费和折扣，而不是单一总额。
* **配送地址更新**：在客户选择或更改配送地址时重新计算订单，无需离开付款单。
* **配送方式选择器**：让客户在付款单上选择 Standard、Express 或 Same-day，价格即时更新。

每项功能均需主动启用。如果您不使用这些功能，您的 Apple Pay 集成将与之前完全相同。

## 要求

| 要求 | 值 |
| - | - |
| Yuno iOS SDK | 2.24.0 或更高版本 |
| Yuno Web SDK | 1.10.8 或更高版本 |
| Apple Pay | 已在您的应用中[完成设置](https://docs.y.uno/zh-hans/docs/wallets/apple-pay/apple-pay-sdk-integration.md)，并已在您的 Yuno 账户中启用 |
| 付款单上的配送地址 | 只要会话包含 `shipping_methods` 或您的应用注册了处理程序，两个平台都会自动请求。您无需在 Checkout Builder 中启用。 |

## 工作原理

1. 您的 backend 在创建结账会话时添加 `summary_items` 和/或 `shipping_methods`。
2. 您的应用注册 `onShippingAddressChanged`：在 iOS 上注册在已传递给支付流程的 delegate 上，在 Web 上在启动 checkout 时注册在 `externalButtons` 中。
3. 在 Apple Pay 付款单上，切换配送方式会即时更新价格，而不会调用您的应用。选择或更改配送地址会调用您的处理程序，因此您可以返回新的总额、新的配送方式或错误。
4. 客户授权后，一次性令牌会包含一个 `shipping` 对象，其中含有所选配送方式和完整地址。

以下两条规则在所有情况下都适用：

* 每个 `summary_items` 列表都必须以总计结尾。其 `amount` 是付款单上显示的总额，其 `label` 作为收款方行显示在总额旁边（我们建议使用您的商店名称）。
* 所有金额都必须使用会话币种。币种不同会导致 SDK 忽略该明细或响应。

## 第 1 步：显示订单明细（仅 backend）

在您[创建结账会话](https://docs.y.uno/zh-hans/reference/checkout-sessions/create-checkout-session.md)时添加 `summary_items`：

```json theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
{
  "amount": { "currency": "USD", "value": 108.00 },
  "customer_id": "customer-uuid",
  "merchant_order_id": "order-123",
  "country": "US",
  "summary_items": [
    { "id": "subtotal", "label": "Subtotal", "amount": { "currency": "USD", "value": 100.00 } },
    { "id": "tax", "label": "Tax", "amount": { "currency": "USD", "value": 8.00 } },
    { "id": "total", "label": "ACME Inc.", "amount": { "currency": "USD", "value": 108.00 } }
  ]
}
```

付款单现在会显示明细，总额的标签为“ACME Inc.”。无需编写应用代码。

* 最后一项显示为总计，其标签显示为收款方行。
* 折扣可以为负数，例如 `-5.00`。
* 如果您同时发送 `shipping_methods`（第 2 步），付款单打开时会显示第一个配送方式的明细，并忽略此列表。
* 如果没有 `summary_items` 也没有 `shipping_methods`，付款单会显示一行会话金额。

## 第 2 步：在付款单上提供配送方式

将 `shipping_methods` 添加到结账会话中。每个配送方式都有自己的 `summary_items`，因此客户切换配送方式时，付款单可以立即更新价格：

```json theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
"shipping_methods": [
  {
    "id": "standard",
    "label": "Standard shipping",
    "detail": "Arrives in 3-5 business days",
    "amount": { "currency": "USD", "value": 5.00 },
    "delivery_estimate": { "from": "2026-10-12", "to": "2026-10-14" },
    "summary_items": [
      { "id": "subtotal", "label": "Subtotal", "amount": { "currency": "USD", "value": 100.00 } },
      { "id": "shipping", "label": "Shipping", "amount": { "currency": "USD", "value": 5.00 } },
      { "id": "total", "label": "ACME Inc.", "amount": { "currency": "USD", "value": 105.00 } }
    ]
  },
  {
    "id": "express",
    "label": "Express shipping",
    "detail": "Arrives in 1-2 business days",
    "amount": { "currency": "USD", "value": 12.00 },
    "summary_items": [
      { "id": "subtotal", "label": "Subtotal", "amount": { "currency": "USD", "value": 100.00 } },
      { "id": "shipping", "label": "Shipping", "amount": { "currency": "USD", "value": 12.00 } },
      { "id": "total", "label": "ACME Inc.", "amount": { "currency": "USD", "value": 112.00 } }
    ]
  }
]
```

* 第一个配送方式为预选项，付款单打开时显示的就是它的明细。
* `label` 是选项名称，`detail` 是其描述。Apple Pay 会在选项旁边显示价格。
* 切换配送方式从不调用您的应用或 backend。总额来自该配送方式的 `summary_items`。没有明细的配送方式会保留付款单上已有的总额。
* `delivery_estimate` 是由 `from` 和 `to` 组成的一对 `YYYY-MM-DD` 日期。在 iOS 15 及更高版本上，以及在 Web 上支持 Apple Pay JS 版本 12 的浏览器（Safari 15 及更高版本）中，它会以日期范围的形式显示在选项旁边。顺序颠倒或格式错误的范围不会显示。
* 付款单最多接受 100 个配送方式、100 个明细项，以及每个配送方式 100 个项目。超出此限制时，iOS SDK 会在没有配送方式的情况下打开付款单，Web SDK 则不会打开付款单并报告错误。

## 第 3 步：响应配送地址的变更

在启动流程时注册 `onShippingAddressChanged`。您的处理程序会收到经过遮盖的地址，并以新的总额、新的配送方式、错误作答，或不返回任何内容以保持付款单不变。

> [tab] iOS
>
> 让您已传递给支付流程的 delegate 遵循 `YunoShippingDelegate`，并实现 `onShippingAddressChanged`。它适用于所有接受 `YunoPaymentDelegate` 的流程：`startCheckout`、`startPaymentLite` 以及 Full 模式的支付方式列表。
>
> ```swift theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> import YunoSDK
>
> final class CheckoutDelegate: YunoShippingDelegate {
>
>     // ... your YunoPaymentDelegate implementation (checkoutSession, countryCode, yunoPaymentResult, ...)
>
>     func onShippingAddressChanged(
>         _ paymentMethodType: String,
>         shippingInfo: YunoShippingInfo
>     ) async -> YunoShippingUpdate? {
>         // paymentMethodType is "APPLE_PAY"
>         await myShippingService.quote(shippingInfo.shippingAddress) // a YunoShippingUpdate, or nil to keep the current values
>     }
> }
> ```
>
> 如果您不使用 `async`，或者您从 Objective-C 进行集成，请改为实现 `YunoPaymentDelegate` 声明为可选的基于 completion 的变体：
>
> ```swift theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> func onShippingAddressChanged(
>     paymentMethodType: String,
>     shippingInfo: YunoShippingInfo,
>     completion: @escaping (YunoShippingUpdate?) -> Void
> ) {
>     myShippingService.quote(shippingInfo.shippingAddress) { quote in
>         completion(quote)
>     }
> }
> ```
>
> 两者实现其一即可。两者都存在时，SDK 会调用 `async` 版本。

> [tab] Web
>
> ```javascript theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> await yuno.startCheckout({
>   checkoutSession: session,
>   countryCode: 'US',
>   yunoCreatePayment: (oneTimeToken) => { /* create the payment on your server */ },
>   externalButtons: {
>     onShippingAddressChanged: async ({ paymentMethodType, shippingInfo }) => {
>       // paymentMethodType is "APPLE_PAY"
>       return await myShippingService.quote(shippingInfo.shippingAddress) // a ShippingUpdate, or null to keep the current values
>     },
>   },
> })
> ```
>
> `startSeamlessCheckout` 中也有相同的 `externalButtons.onShippingAddressChanged` 选项。在 Web 上，该处理程序与 Google Pay 共用：客户使用 Google Pay 支付时，调用参数为 `paymentMethodType: "GOOGLE_PAY"`，因此如果您针对不同钱包给出不同报价，请根据该值进行分支处理。

### 您收到的内容：ShippingInfo

在授权之前，Apple Pay 只共享部分地址，足以用来报价运费和税费：

| 字段 | 类型 | 示例 |
| - | - | - |
| `shippingAddress.city` | `String?` | `"Cupertino"` |
| `shippingAddress.state` | `String?` | `"CA"` |
| `shippingAddress.postalCode` | `String?` | `"95014"`（在某些国家/地区可能会被缩短） |
| `shippingAddress.countryCode` | `String?` | `"US"` |

每个字段都可能缺失：在 iOS 上为 `nil`，在 Web 上则不存在。完整地址会出现在一次性令牌中（第 4 步）。

### 您返回的内容：ShippingUpdate

返回以下内容的任意组合，或返回 `nil`（在 Web 上为 `null`）以保持付款单不变。

> [tab] iOS
>
> ```swift theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> // A) New totals for this address
> YunoShippingUpdate(
>     summaryItems: [
>         YunoSummaryItem(id: "subtotal", label: "Subtotal", amount: YunoShippingAmount(currency: "USD", value: 100.00)),
>         YunoSummaryItem(id: "tax", label: "Tax", amount: YunoShippingAmount(currency: "USD", value: 10.00)),
>         YunoSummaryItem(id: "total", label: "ACME Inc.", amount: YunoShippingAmount(currency: "USD", value: 110.00)) // must be last
>     ]
> )
> ```
>
> ```swift theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> // B) New shipping methods for this address
> YunoShippingUpdate(
>     shippingMethods: [
>         YunoShippingMethod(
>             id: "same-day", label: "Same-day delivery", detail: "Today before 9pm",
>             amount: YunoShippingAmount(currency: "USD", value: 15.00),
>             deliveryEstimate: YunoShippingDeliveryEstimate(from: "2026-10-10", to: "2026-10-10"),
>             summaryItems: [
>                 YunoSummaryItem(id: "subtotal", label: "Subtotal", amount: YunoShippingAmount(currency: "USD", value: 100.00)),
>                 YunoSummaryItem(id: "shipping", label: "Shipping", amount: YunoShippingAmount(currency: "USD", value: 15.00)),
>                 YunoSummaryItem(id: "total", label: "ACME Inc.", amount: YunoShippingAmount(currency: "USD", value: 115.00))
>             ]
>         )
>     ]
> )
> ```
>
> ```swift theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> // C) An address you can't serve
> YunoShippingUpdate(
>     errors: [
>         YunoShippingError(code: "CITY_NOT_SUPPORTED", message: "We don't ship to this city yet") // shown to the customer
>     ]
> )
> ```

> [tab] Web
>
> ```javascript theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> // A) New totals for this address
> return {
>   summaryItems: [
>     { id: 'subtotal', label: 'Subtotal', amount: { currency: 'USD', value: 100.00 } },
>     { id: 'tax', label: 'Tax', amount: { currency: 'USD', value: 10.00 } },
>     { id: 'total', label: 'ACME Inc.', amount: { currency: 'USD', value: 110.00 } }, // must be last
>   ],
> }
> ```
>
> ```javascript theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> // B) New shipping methods for this address
> return {
>   shippingMethods: [
>     {
>       id: 'same-day', label: 'Same-day delivery', detail: 'Today before 9pm',
>       amount: { currency: 'USD', value: 15.00 },
>       deliveryEstimate: { from: '2026-10-10', to: '2026-10-10' },
>       summaryItems: [
>         { id: 'subtotal', label: 'Subtotal', amount: { currency: 'USD', value: 100.00 } },
>         { id: 'shipping', label: 'Shipping', amount: { currency: 'USD', value: 15.00 } },
>         { id: 'total', label: 'ACME Inc.', amount: { currency: 'USD', value: 115.00 } },
>       ],
>     },
>   ],
> }
> ```
>
> ```javascript theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> // C) An address you can't serve
> return {
>   errors: [
>     { code: 'CITY_NOT_SUPPORTED', message: "We don't ship to this city yet" }, // shown to the customer
>   ],
> }
> ```

### 规则

| 情况 | 结果 |
| - | - |
| 您返回 `nil`（在 Web 上不返回任何内容） | 保留当前值。 |
| 您作答较晚 | SDK 本身不设置截止时间：在您作答期间，付款单保持打开。如果 Apple 自身的时间限制先到期，Apple 会关闭付款单，SDK 会报告 `CANCELED_BY_USER`。请尽快作答。 |
| 某个币种与会话不同，或某个金额不是数字 | 整个响应会被忽略。 |
| 您发送 `summaryItems` | 列表必须以总计结尾，该总计将成为付款单总额。它适用于当前所选的配送方式；切换到其他配送方式会恢复该配送方式自己的明细。 |
| 您发送 `shippingMethods` | 它们会替换现有选项，第一个为预选项。请在每个配送方式中包含 `summaryItems`，否则客户切换时总额不会变化。 |
| 您发送 `errors` | Apple Pay 会在付款单上显示该消息，并阻止支付，直到客户选择其他地址。`CITY_NOT_SUPPORTED`、`STATE_NOT_SUPPORTED`、`COUNTRY_NOT_SUPPORTED` 和 `POSTAL_CODE_INVALID` 指向相应的地址字段；在 Web 上，`ADDRESS_NOT_SUPPORTED` 将整个地址标记为无法提供服务。任何其他代码都会显示为通用的“无法配送到此地址”错误。 |
| 超过 100 个条目 | 整个响应会被忽略。 |
| 您的处理程序抛出异常（在 Web 上为 reject） | 保留当前值。 |
| 付款单打开 | Apple Pay 会立即使用客户的默认地址调用您的处理程序。请确保您的处理程序可以安全地被多次调用，并且不要依赖第一次调用。如果您在这次调用中返回配送方式，它们会替换会话的配送方式；返回 `nil` 即可保留。 |
| 在您仍在作答时客户再次更改地址 | 上一次调用会被取消，只应用对最新地址的回答。 |

> [!NOTE]
>
> 与 Google Pay 不同，Apple Pay 付款单上的错误确实会阻止支付：客户必须先选择其他地址才能支付。在创建支付时，您仍需在服务器上验证地址。

## 第 4 步：在一次性令牌中读取结果

客户授权后，您在 `yunoCreatePayment` 中收到的一次性令牌会包含一个 `shipping` 对象，其中含有所选配送方式和完整地址：

```json theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
"shipping": {
  "method": {
    "id": "express",
    "label": "Express shipping",
    "amount": { "currency": "USD", "value": 12.00 }
  },
  "address": {
    "address_line_1": "1 Apple Park Way",
    "address_line_2": "",
    "city": "Cupertino",
    "state": "CA",
    "zip_code": "95014",
    "country": "US"
  }
}
```

* `method` 是在付款单上选择的配送方式，如果客户未更改，则为第一个配送方式。
* `summary_items` 不会被返回。
* 只要付款单收集了配送方式或地址，无论是否有处理程序，都会包含该对象。两者都没有的支付不包含 `shipping`。

在您的服务器上使用此对象来收取最终金额、验证地址并发货。

## 各种情况下的结果

| 已实现处理程序 | 会话中的 `summary_items` | 会话中的 `shipping_methods` | Apple Pay 付款单 |
| - | - | - | - |
| 否 | 否 | 否 | 与之前相同（单一总额） |
| 否 | 是 | 否 | 明细，无地址更新 |
| 否 | 任意 | 是 | 选择器，配送方式即时切换。付款单会请求地址，但不会调用您的应用 |
| 是 | 否 | 否 | 地址更新；总额随您的回答变化。除非您返回配送方式，否则没有选择器 |
| 是 | 是 | 否 | 先显示明细；您的回答可以更新它 |
| 是 | 任意 | 是 | 完整体验：选择器、配送方式即时切换和地址更新 |

## 须知

* 在 iOS 上，iOS Simulator 不支持 Apple Pay。请在实体设备上测试。在 Web 上，请在已设置 Apple Pay 的设备上使用 Safari 测试。
* 在支付获得授权之前，Apple Pay 会遮盖地址，因此您的处理程序永远看不到街道地址行。
* 客户更改地址时，所选配送方式保持选中状态，除非您返回新的配送方式。
* \*\*隐私：\*\*SDK 从不记录地址、电子邮件或支付数据。

## 故障排除

| 症状 | 可能原因 |
| - | - |
| 选择器未出现 | 会话没有有效的 `shipping_methods`，某个币种与会话不同，或者会话超过了 100 个配送方式、明细项或每个配送方式的项目数（在这种情况下，Web 上的付款单根本不会打开）。 |
| 明细未出现 | `summary_items` 无效（没有总计、标签为空、币种混用）。 |
| 付款单不请求配送地址 | 会话没有 `shipping_methods`，且您的应用未实现处理程序。添加其中任意一项，或在 Checkout Builder 中将配送地址设为必填字段。 |
| 我的处理程序从未被调用 | 在 iOS 上，您的 delegate 未遵循 `YunoShippingDelegate`，也未实现基于 completion 的变体。在 Web 上，未将 `externalButtons.onShippingAddressChanged` 传递给 `startCheckout` 或 `startSeamlessCheckout`。 |
| 付款单打开时，我的配送方式替换了会话的配送方式 | 付款单打开时 Apple Pay 会调用您的处理程序，并应用您的回答。在这次调用中返回 `nil` 即可保留会话的配送方式。 |
| 切换配送方式时总额不变 | 配送方式没有 `summaryItems`。请为每个配送方式发送明细。 |
| 我作答后付款单仍保留旧总额 | 某个币种与会话不同，响应超过 100 个条目，或者客户在您作答前再次更改了地址。 |
| 配送日期未显示 | 设备运行 iOS 14 或更早版本，浏览器不支持 Apple Pay JS 版本 12，或者 `from` 晚于 `to`。 |
| 付款单自行关闭，我收到了 `CANCELED_BY_USER` | 您的处理程序耗时超过了 Apple 的时间限制。请更快作答，或者返回 `nil`，并在创建支付时在您的服务器上报价。 |

## 相关文档

* [设置和一次性支付](https://docs.y.uno/zh-hans/docs/wallets/apple-pay/apple-pay-sdk-integration.md)
* [Android 上的 Google Pay 快捷 checkout](https://docs.y.uno/zh-hans/docs/wallets/google-pay/sdk-integration/express-checkout.md)
* [Fast Checkout 配送](https://docs.y.uno/zh-hans/docs/payment-features/fast-checkout-shipping.md)。在 widget 内选择地址的钱包使用相同的会话字段和 `shipping` 节点。
* [iOS SDK 更新日志](https://docs.y.uno/changelog/ios.md)和 [Web SDK 更新日志](https://docs.y.uno/changelog/web.md#v1-10-8)


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