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

# onShippingAddressChanged

> 当购物者在钱包支付表单中选择或更改收货地址时调用。

每当购物者在钱包的支付表单中选择或更改收货地址时，都会调用 `onShippingAddressChanged`，包括支付表单打开时钱包的默认地址。SDK 会传递经过遮盖的地址，并将您返回的内容实时应用到支付表单上：新的总额、配送选项，或在购物者选择其他地址之前阻止支付的错误。覆盖它是可选的。如果不覆盖，钱包流程的行为与没有 callback 时完全相同。

## 支持的钱包

| 平台 | 钱包 | `paymentMethodType` |
| - | - | - |
| Web | Apple Pay、Google Pay | `APPLE_PAY`、`GOOGLE_PAY` |
| iOS | Apple Pay | `APPLE_PAY` |
| Android | Google Pay | `GOOGLE_PAY` |
| React Native | iOS 上的 Apple Pay，Android 上的 Google Pay | `APPLE_PAY`、`GOOGLE_PAY` |

仅适用于支付流程。对于卡或任何其他支付方式，不会触发此 callback。

## 您会收到的内容

| 字段 | 类型 | 描述 |
| - | - | - |
| `paymentMethodType` | String | 触发 callback 的钱包，如 [支持的钱包](#supported-wallets) 中所列。 |
| `shippingInfo.shippingAddress.city` | String? | 所选地址的城市。 |
| `shippingInfo.shippingAddress.state` | String? | 州或地区。 |
| `shippingInfo.shippingAddress.postalCode` | String? | 邮政编码。 |
| `shippingInfo.shippingAddress.countryCode` | String? | 国家代码。 |

在支付获得授权之前，钱包会遮盖地址，因此任何地址字段都可能缺失。

## 您需要返回的内容

一个 `ShippingUpdate`，或返回 `null`（iOS 上为 `nil`）以保留支付表单当前显示的内容。更新中的每个字段都是可选的：省略某个字段即可保留其当前值。

| 字段 | 类型 | 描述 |
| - | - | - |
| `errors` | \[ShippingError]? | 无法满足所选地址的原因，每个原因都包含一个 `code` 和一个 `message`。返回其中任何一个都会使支付表单保持打开，并在购物者选择其他地址之前阻止支付。 |
| `summaryItems` | \[SummaryItem]? | 支付表单的明细：每一行包含 `id`、`label` 和 `amount`。最后一行显示为总计，其标签显示为收款方行。标签会原样显示在支付表单上。 |
| `shippingMethods` | \[ShippingMethod]? | 配送选项：`id`、`label`、可选的 `detail`、`amount`、可选的 `deliveryEstimate`，以及选中该选项时适用的 `summaryItems`。发送这些选项会预先选中第一个。当购物者切换选项时，SDK 会根据每个选项自身的明细作出响应，而不会再次调用您，因此没有明细的选项无法重新计算支付表单上的价格。 |

金额为 `ShippingAmount` 值（在 Android 和 React Native 上为 `SummaryAmount`）：`value` 以 `currency` 的主单位表示（`12.34` 表示 USD 12.34）。`currency` 必须与结账会话的币种一致；不一致会使整个更新无效。`deliveryEstimate` 是由 `from` 和 `to` 组成的一对 `YYYY-MM-DD` 日期，在 iOS 15 及更高版本中显示在选项旁边。

## 时间限制

在 iOS 上，SDK 本身不设置截止时间，因此唯一的时间限制来自钱包：在您作出响应期间，支付表单会保持打开。如果您始终未作出响应，则适用钱包自身的截止时间，支付表单会被关闭，交易以已取消状态结束。

## 签名

> [tab] Web
>
> 在 Web 上，它在交易配置的 `externalButtons` 中接入。SDK 会从其渲染的每个 Apple Pay 和 Google Pay 按钮调用它，包括您通过 [`getPaymentMethodsViews()`](https://docs.y.uno/zh-hans/docs/yuno-sdk/features/payment-methods-list) 挂载的按钮。返回更新，或返回 `null` 以保留支付表单当前显示的内容。抛出异常的处理程序同样会保留当前值。
>
> ```typescript theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> const transaction = sdkPayments.transaction({
>   checkoutSession,
>   countryCode: 'CO',
>   onStatus: (result) => console.log(result.status),
>   externalButtons: {
>     onShippingAddressChanged: async ({ paymentMethodType, shippingInfo }) => {
>       const quote = await backend.quoteShipping(shippingInfo.shippingAddress)
>       return { summaryItems: quote.summaryItems, shippingMethods: quote.shippingMethods }
>     },
>   },
> })
> ```

> [tab] iOS
>
> 让您作为 `controller` 传入的对象遵循 `ShippingAddressHandling`，这是一个继承自 `TransactionController` 的协议。SDK 会检查控制器是否采用了该协议；未采用该协议的控制器不会收到任何配送 callback。
>
> ```swift theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> final class Checkout: ShippingAddressHandling {
>
>     func onStatus(status: TransactionStatus) { /* ... */ }
>
>     func onShippingAddressChanged(
>         _ paymentMethodType: String,
>         shippingInfo: ShippingInfo
>     ) async -> ShippingUpdate? {
>         let quote = await backend.quoteShipping(to: shippingInfo.shippingAddress)
>         return ShippingUpdate(
>             summaryItems: quote.summaryItems,
>             shippingMethods: quote.shippingMethods
>         )
>     }
> }
> ```

> [tab] Android
>
> 在 Android 上，它作为 [`TransactionConfig`](https://docs.y.uno/zh-hans/docs/yuno-sdk/reference/objects-you-pass#transaction-config) 支付构造函数中的一个字段接入，类型为 `(suspend (String, ShippingInfo) -> ShippingUpdate?)?`，默认值为 `null`。注册构造函数不接受该字段。
>
> ```kotlin theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> onShippingAddressChanged = { paymentMethodType, shippingInfo ->
>     backend.quoteShipping(shippingInfo.shippingAddress)
> }
> ```

> [tab] React Native
>
> 在控制器中实现它。返回更新，或返回 `null` 以保留支付表单当前显示的内容。被拒绝的 Promise 同样会保留当前值。
>
> ```tsx theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> onShippingAddressChanged: async (paymentMethodType, shippingInfo) => {
>   const quote = await backend.quoteShipping(shippingInfo.shippingAddress);
>   return { summaryItems: quote.summaryItems, shippingMethods: quote.shippingMethods };
> },
> ```

## 相关内容

* [监听事件](https://docs.y.uno/docs/yuno-sdk/controllers/listen-to-events)。其他信息类控制器。
* [`onStatus`](https://docs.y.uno/zh-hans/docs/yuno-sdk/controllers/on-status)。交易的最终结果。
* [控制器概览](https://docs.y.uno/zh-hans/docs/yuno-sdk/controllers/overview.md)。各平台如何接入控制器。


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