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

# 支付退款

> 介绍如何对已捕获的支付进行全额或部分退款、如何在各个退款 endpoint 之间进行选择，以及如何解读退款结果。

本指南介绍如何在 Yuno 上对之前创建的支付进行退款。

## 要求

要对支付进行退款，您需要：

* 在 Yuno Dashboard 上获取您的 [API 凭证](https://docs.y.uno/zh-hans/docs/using-yuno/settings/developers-credentials.md)，包括：
  * `public-api-key`
  * `private-secret-key`
* 拥有使用 [Create Payment](https://docs.y.uno/zh-hans/reference/payments/create-payment.md) endpoint 创建支付后收到的支付标识数据，以及使用 [Capture Payment](https://docs.y.uno/reference/payments/capture-authorization.md) 执行捕获操作后得到的捕获数据：
  * `id`：支付的唯一标识符，在创建支付后从参数 `id` 中获取。
  * `transaction_id`：交易的唯一标识符。根据您捕获支付的方式，此信息的来源有所不同。
    * 如果您通过一次操作同时创建并捕获了支付，请在使用 [Create Payment](https://docs.y.uno/zh-hans/reference/payments/create-payment.md) endpoint 创建支付后，从参数 `transaction.id` 中获取 `transaction_id`。
    * 如果您先执行了授权，然后再捕获支付，请使用通过 [Capture Payment](https://docs.y.uno/reference/payments/capture-authorization.md) endpoint 捕获支付后收到的参数 `id` 作为 `transaction_id`。
    * 如果您稍后使用 [Retrieve Payment by ID](https://docs.y.uno/reference/payments/retrieve-payment-by-id) endpoint 检索支付，请从 `transactions` 数组中取 `type` 为 `PURCHASE` 或 `CAPTURE` 的交易的 `id`。`REFUND` 交易的 `id` 不能用于退款。

> [!NOTE]
>
> **探索 Yuno Postman Collections**
>
> Yuno 提供 [Postman Collections](https://docs.y.uno/reference/getting-started/postman-collections.md)，您可以使用它们在本地复现各个用例。

## 对支付进行退款

发起退款后，系统会退还之前创建但尚未退款的扣款。该金额将退回到最初被扣款的支付方式。

Yuno 提供 3 个用于支付退款的 endpoint。它们使用相同的请求正文，并且每次调用都需要一个 `X-Idempotency-Key` 标头，每次尝试都使用新值：

* [Refund Payment](https://docs.y.uno/reference/payments/refund-payment.md)：对特定交易进行退款。请提供支付 `id` 和 `transaction_id`。当您需要精确控制退款的交易时使用。
* [Cancel or Refund a Payment](https://docs.y.uno/reference/payments/cancel-or-refund-a-payment.md)：在支付层级运行，因此不需要 `transaction_id`。如果支付尚未捕获，Yuno 会取消该支付；如果已捕获，则会对其退款。`reason` 字段为必填项。
* [Cancel or Refund a Payment with Transaction](https://docs.y.uno/reference/payments/cancel-or-refund-payment-with-transaction.md)：对特定的 `transaction_id` 应用相同的自动判断。

此外，您需要在请求正文中使用 `amount` 对象告知要退款的金额：

* 全额退款：将 `amount` 留空。
* 部分退款：使用参数 `amount` 提供退款金额。

> [!NOTE]
>
> **使用 Yuno 促销的支付**
>
> 退款按客户实际支付的金额计算，即 `amount.value` 减去 `discount_amount`。全额退款会退还该金额，并将支付状态变为 `REFUNDED`；超过剩余可退款金额的 `amount` 会被拒绝，并返回 `400 INVALID_PARAMETERS`。请参阅 [促销](https://docs.y.uno/zh-hans/docs/payment-features/promotions#refunds-captures-and-cancellations)。

> [!WARNING]
>
> **一次只进行一笔退款**
>
> 当同一笔交易的上一笔退款仍在处理中时，请勿发起新的退款。请等待上一笔退款达到最终状态（`SUCCEEDED`、`DECLINED` 或 `ERROR`）后再发送下一笔。

## 退款结果

响应中包含一笔 `transaction.type = REFUND` 的新交易。使用其 `status` 解读退款结果：

* `SUCCEEDED`：服务提供商已接受退款。对于全额退款，支付变为 `status = REFUNDED` 和 `sub_status = REFUNDED`。对于部分退款，支付保持 `status = SUCCEEDED`，其 `sub_status` 变为 `PARTIALLY_REFUNDED`。
* `PENDING`：服务提供商以异步方式确认退款。在确认到达之前，支付显示为 `status = REFUNDED` 和 `sub_status = PENDING_PROVIDER_CONFIRMATION`。请使用 webhooks 或检索 endpoint 获取最终状态，如 [检查支付状态](#check-the-payment-status) 所述。
* `DECLINED` 或 `ERROR`：服务提供商或持卡人的发卡机构拒绝了退款。资金不会发生变动，支付保持其之前的 `status` 和 `sub_status`。请查看交易的 `response_code` 和 `response_message` 了解原因。问题解决后，请使用新的 `X-Idempotency-Key` 再次发送退款，或在请求中将 `simplified_mode` 设置为 `true`，以便 Yuno 自动重试退款，如 [交易重试](https://docs.y.uno/zh-hans/docs/payment-features/transaction-retries.md) 所述。

> [!NOTE]
>
> **行为取决于服务提供商**
>
> 退款是同步确认还是保持 `PENDING`、是否允许部分退款以及对同一笔交易进行多次退款，以及购买后多长时间内接受退款，都取决于处理该支付的服务提供商。许多卡收单机构会将退款作为针对持卡人账户的在线交易进行处理，并立即返回最终状态，这也意味着发卡机构可能会拒绝该退款。如果您的集成依赖于上述任何行为，请向 Yuno 确认您路由中的服务提供商是否支持。

> [!NOTE]
>
> **退款处理时间**
>
> 退款处理时间因支付类型而异。在测试模式下，退款会在 sandbox 环境中立即处理。如果您的 sandbox 连接指向服务提供商自己的测试环境，退款会发送到该环境，并且需要先完成一笔成功的测试购买。

### 检查支付状态

如果出于某种原因，您需要确认支付退款：

* 使用 [Retrieve Payment by ID](https://docs.y.uno/reference/payments/retrieve-payment-by-id) 或 [Retrieve Payment by merchant\_order\_id](https://docs.y.uno/reference/payments/retrieve-payment-by-merchant-order-id.md) 获取支付的详细信息。
* 或者，设置 webhooks 以接收每个事件的通知。请参阅 [Webhooks](https://docs.y.uno/zh-hans/docs/webhooks/configure-webhooks.md) 指南，了解如何在 Yuno 中配置 webhooks。


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