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

# 快速入门

> 在 sandbox 中接受您的第一笔支付，不涉及真实资金流动。

只需四步：安装 SDK，向您的购物者展示其支付方式，完成
支付，并确认支付已成功。Yuno 负责处理其中的表单、路由
和 3DS。

**1. 安装**

将 SDK 添加到您的平台，并使用您的公共 API 密钥对其进行初始化。

**2. 展示支付方式**

您的 backend 创建结账会话；SDK 展示支付方式。

**3. 完成支付**

启动交易。Yuno 渲染表单并执行 3DS。

**4. 确认支付已成功**

SDK 告知您结果；您的服务器对其进行确认。

## 开始之前

您需要：

* 一个 Yuno 账户和您的**公共 API 密钥**，可在 [Yuno Dashboard](https://dashboard.y.uno/) 的 **Developers** > **Credentials** 下获取。
* 一个能够调用 Yuno API 来创建结账会话以及（可选）创建支付的 backend。

## 安装

为您的平台添加 SDK，然后在应用或页面启动时
对其进行一次初始化。构造函数只接受您的公共 API 密钥。所有
与支付相关的配置都在之后进行。

> [tab] Web
>
> 从 CDN 加载 SDK。它会暴露一个全局 `SdkPayments`，并在准备就绪后于 `window` 上派发 `sdk-payments-ready` 事件。
>
> ```html theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> <script src="https://sdk-web.y.uno/v2/main.js"></script>
> ```
>
> `/v2/main.js` 始终提供最新的 2.x 版本，因此您的页面无需修改代码即可获得修复和新功能。如需固定使用某一个版本，请使用其完整版本路径，例如 `https://sdk-web.y.uno/v2.7.2/main.js`。子资源完整性（SRI）不适用于 `sdk-web.y.uno` 路径：如需使用 SRI，请加载 [versions-sri.json](https://sdk-web.y.uno/versions-sri.json) 中与 `integrity` 哈希配对的 `url`，并设置 `crossorigin="anonymous"`。请参阅[子资源完整性（SRI）](https://docs.y.uno/docs/sdks/resources/references/web.md#subresource-integrity-sri)。
>
> 然后初始化 SDK。该 bundle 会同步设置 `window.SdkPayments`，并在加载时立即于 `window` 上触发 `sdk-payments-ready`，且**仅触发一次**。如果您的代码在脚本加载完成后才运行，该事件已经过去。请先检查全局对象，仅在脚本仍在加载时才回退到该事件：
>
> ```javascript theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> async function startYuno() {
>     const sdkPayments = await SdkPayments.initialize('your-public-api-key');
>     // Build a transaction from here (see "Take the payment").
> }
>
> if (window.SdkPayments) {
>     startYuno();
> } else {
>     window.addEventListener('sdk-payments-ready', startYuno);
> }
> ```
>
> 无论您以何种方式加载脚本（同步、`async` 或 `defer`），此方法均适用。`SdkPayments.initialize` 返回一个 Promise；请在使用 SDK 之前对其执行 `await`。

> [tab] iOS
>
> SdkPayments 已不再发布到 CocoaPods；最后一个 CocoaPods 版本是 3.1.0。请使用 Swift Package Manager 进行安装：
>
> ```swift theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> dependencies: [
>     .package(url: "https://github.com/yuno-payments/yuno-sdk-ios", from: "3.2.0")
> ]
> ```
>
> 将 `SdkPayments`（动态）或 `SdkPayments-Static`（静态，即 CocoaPods 所提供的版本）添加到您的应用 target，切勿同时添加两者。
>
> 然后根据您应用的入口点，在 `AppDelegate` 或 `App` 结构体中进行初始化：
>
> ```swift theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> import SdkPayments
>
> SdkPayments.initialize(apiKey: "your-public-api-key")
> ```
>
> > [!NOTE]
> >
> > `initialize(apiKey:)` 还提供 `async` 版本，供异步上下文使用。

> [tab] Android
>
> **build.gradle**
>
> ```groovy theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> repositories {
>     maven { url "https://yunopayments.jfrog.io/artifactory/snapshots-libs-release" }
> }
>
> dependencies {
>     implementation 'com.yuno.payments:android-sdk:3.0.0'
>     // The SDK ships androidx.activity at runtime scope only, so add it to compile ComponentActivity
>     implementation 'androidx.activity:activity-ktx:1.9.3'
> }
> ```
>
> 然后在您的 `Application` 类中进行初始化，确保 SDK 在任何 activity 使用它之前已准备就绪：
>
> ```kotlin theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> import com.yuno.payments.sdk.api.SdkPayments
>
> class MyApplication : Application() {
>     override fun onCreate() {
>         super.onCreate()
>         SdkPayments.initialize(this, apiKey = "your-public-api-key")
>     }
> }
> ```

> [tab] React Native
>
> 需要 React Native 0.75 或更高版本、Android `minSdkVersion` 23 和 iOS 15.1。版本 2.2.0 在 iOS 上基于原生 Yuno SDK 3.2.0 运行，在 Android 上基于 3.1.0 运行。
>
> ```bash theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> npm install @yuno-payments/yuno-sdk-react-native
> cd ios && pod install
> ```
>
> 在 iOS 上，请在 `ios/Podfile` 中设置 `platform :ios, '15.1'` 或更高版本。`pod install` 会通过 Swift Package Manager 将 `SdkPayments-Static` 添加到该软件包的 pod 中，因此应用本身无需添加 Yuno iOS SDK。
>
> 在 Android 上，请将 Yuno Maven 仓库添加到 `android/build.gradle`。您的 `MainActivity` 必须是 `ReactActivity`（默认即是），它就是 SDK 用于展示其界面的 `ComponentActivity`：
>
> ```groovy theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> allprojects {
>     repositories {
>         maven { url "https://yunopayments.jfrog.io/artifactory/snapshots-libs-release" }
>     }
> }
> ```
>
> 然后在创建交易之前进行一次初始化：
>
> ```typescript theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> import { SdkPayments } from '@yuno-payments/yuno-sdk-react-native';
>
> await SdkPayments.initialize('your-public-api-key');
> ```

> [!NOTE]
>
> SDK 根据您 API 密钥的前缀确定目标环境：`sandbox_` 密钥使用 sandbox，`prod_` 密钥使用生产环境。

## 展示支付方式

询问 Yuno 该购物者可以使用哪些支付方式，并将其展示在屏幕上。您的
backend 首先创建一个**结账会话**（即该支付的身份标识），并
将其交给您的应用。

### 创建结账会话（backend）

结账会话是 Yuno 对这一笔支付的记录：您的服务器向
Yuno 请求创建它，并将其 ID 交给您的应用。

* **Endpoint**：`POST /v1/checkout/sessions`
* **认证**：`public-api-key` 和 `private-secret-key` 标头（可在 [Dashboard](https://dashboard.y.uno/) 的 **Developers** > **Credentials** 下获取）
* **必填**：`account_id`、`merchant_order_id`、`payment_description`、`country`
* **可选**：`customer_id`（让该会话可以访问该客户已注册的支付方式）、`amount`、`workflow`
* **返回**：`checkout_session`（即您作为 `checkoutSession` 传入的值）
* **参考**：[`POST /v1/checkout/sessions`](https://docs.y.uno/zh-hans/reference/checkout-sessions/create-checkout-session)，包含完整的请求/响应和认证详情

```http theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
POST https://api-sandbox.y.uno/v1/checkout/sessions
public-api-key: <your-public-api-key>
private-secret-key: <your-private-secret-key>

{
  "account_id": "your-account-id",
  "merchant_order_id": "order-123",
  "payment_description": "Order 123",
  "country": "US",
  "customer_id": "customer-123",
  "amount": { "currency": "USD", "value": 25 },
  "workflow": "SDK_SEAMLESS"
}
```

`amount.value` 以货币的主单位表示：`25` 表示 USD 25.00，而不是 25 美分。

> [!WARNING]
>
> 请使用 `workflow: "SDK_SEAMLESS"` 创建会话，该字段位于上方的会话创建请求体中，**而不是** `TransactionConfig` 中。正是它让 SDK 能够为您创建支付。如果没有它，交易将以 `INTERNAL_ERROR` 结束。请参阅 [`workflow` 字段](https://docs.y.uno/zh-hans/reference/checkout-sessions/create-checkout-session#body-workflow)。

### 将支付方式展示在屏幕上

购物者的选择以 [`PaymentMethodSelected`](https://docs.y.uno/zh-hans/docs/yuno-sdk/reference/objects-you-pass#payment-method-selected) 的形式传给 SDK：只有 `paymentMethodType`（在 Web、Android 和 React Native 上为 `type`）是必填项；`vaultedToken` 和 `transactionData`（在 Web 上为 `data`）是可选项。获取它的两种方式：

* **使用 SDK 预构建的支付方式列表。** 调用 `getPaymentMethodsViews` 渲染支付方式列表。当用户选择其中一个时，您的 `onPaymentSelected` 控制器会收到一个 `PaymentMethodSelected`，您可以将其传给 `start()`。请参阅[支付方式列表](https://docs.y.uno/zh-hans/docs/yuno-sdk/features/payment-methods-list.md)。
* **构建您自己的列表。** 从 Yuno API 获取可用的支付方式，并根据用户的选择自行构造 `PaymentMethodSelected`。请参阅 [`GET /v1/checkout/sessions/{checkout_session}/payment-methods`](https://docs.y.uno/reference/checkout-sessions/retrieve-payment-methods-for-checkout)。

## 完成支付

当购物者确认后，启动支付。Yuno 渲染该支付方式的
表单，在银行要求时执行 3DS，并创建支付。一个完整的
交易包含三个部分：

1. 由您的 backend 创建的**结账会话**。
2. 包含会话、国家、语言和控制器的 `TransactionConfig`。
3. 使用 `PaymentMethodSelected` 启动的 `Transaction`。

控制器是您的代码接收 SDK 事件的方式。至少需要 `onStatus`，它会传递交易的最终结果。请通过在配置中传入的 `TransactionController` 对象提供控制器。

> [tab] Web
>
> 在 Web 上，`TransactionConfig` 是一个普通对象。请将其直接传给 `sdkPayments.transaction(...)`。
>
> ```typescript theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> // Create the checkout session on your backend, then pass it here.
> const transaction = sdkPayments.transaction({
>     checkoutSession: 'session-from-backend',
>     countryCode: 'US',
>     language: 'en', // optional, falls back to browser language
>     // elementSelector: the DOM element the form renders into (form payments only).
>     elementSelector: '#payment-form',
>     onStatus: (result) => {
>         console.log('Final status:', result.status)
>     },
> })
>
> // Tell the SDK which method to use ("CARD", "PIX", ...).
> // See "Put the methods on screen" above.
> const paymentMethod = { type: 'CARD' }
>
> await transaction.start({ paymentMethod })
> ```
>
> 在 Web 上，不带 `data` 的 `start()` 会将该支付方式的表单挂载到 `elementSelector` 中，并在表单出现在页面上后完成解析。该表单有自己的 Pay 按钮。购物者点击该按钮后，结果随后会在 `onStatus` 中返回。如需使用您自己的按钮，请在配置中传入 `showPayButton: false`，并从该按钮调用 `transaction.submit()`。如需使用您自行收集的信息进行支付，请在 `paymentMethod` 上传入 `data`，SDK 将在不显示表单的情况下执行支付。

> [tab] iOS
>
> 在 iOS 上，控制器通过在配置中传入的 `TransactionController` 对象提供。下面的示例使用一个独立的类，因此在 SwiftUI 和 UIKit 中的用法相同。请在适合您应用的位置持有一个 `Checkout` 实例（`View` 中的 `@State`、`UIViewController` 中的属性或任何其他持有者），并从您的支付按钮调用 `start(...)`。
>
> ```swift theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> import SdkPayments
>
> final class Checkout: SdkPayments.TransactionController {
>
>     func start() {
>         // Tell the SDK which method to use ("CARD", "PIX", ...).
>         // See "Put the methods on screen" above.
>         let paymentSelected = SdkPayments.PaymentMethodSelected(paymentMethodType: "CARD")
>
>         // Create the checkout session on your backend, then pass it here.
>         let config = SdkPayments.TransactionConfig(
>             checkoutSession: "session-from-backend",
>             countryCode: "US",
>             language: "en",          // optional, falls back to device language
>             controller: self
>         )
>
>         SdkPayments.Transaction(config: config)
>             .start(paymentSelected: paymentSelected)
>     }
>
>     func onStatus(status: SdkPayments.TransactionStatus) {
>         print("Final status: \(status.status)")
>     }
> }
> ```

> [tab] Android
>
> 在 Android 上，控制器通过在配置中传入的 `TransactionController` 对象提供。
>
> ```kotlin theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> import com.yuno.payments.sdk.api.Transaction
> import com.yuno.payments.sdk.api.TransactionConfig
> import com.yuno.payments.sdk.api.TransactionController
> import com.yuno.payments.sdk.api.models.PaymentMethodSelected
> import com.yuno.payments.sdk.api.models.TransactionStatus
>
> class CheckoutActivity : ComponentActivity(), TransactionController {
>
>     override fun onStatus(status: TransactionStatus) {
>         println("Final status: ${status.status}")
>     }
>
>     private fun startCheckout() {
>         // Tell the SDK which method to use ("CARD", "PIX", ...).
>         // See "Put the methods on screen" above.
>         val paymentSelected = PaymentMethodSelected(type = "CARD")
>
>         // Create the checkout session on your backend, then pass it here.
>         val config = TransactionConfig(
>             checkoutSession = "session-from-backend",
>             countryCode = "US",
>             language = "en",          // optional, falls back to device language
>
>             controller = this
>         )
>
>         Transaction(config)
>             .start(activity = this, paymentSelected = paymentSelected)
>     }
> }
> ```

> [tab] React Native
>
> 在 React Native 上，控制器是在配置中传入的普通对象。请从您的支付按钮调用 `startCheckout`。保留它返回的交易（例如保存在 `useRef` 中），并在持有该交易的界面关闭时调用 `transaction.dispose()`。
>
> ```typescript theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
> import { Transaction } from '@yuno-payments/yuno-sdk-react-native';
>
> async function startCheckout(): Promise<Transaction> {
>     // Create the checkout session on your backend, then pass it here.
>     const transaction = new Transaction({
>         checkoutSession: 'session-from-backend',
>         countryCode: 'US',
>         language: 'en', // optional, falls back to device language
>         controller: {
>             onStatus: (status) => {
>                 console.log('Final status:', status.status);
>             },
>         },
>     });
>
>     // Tell the SDK which method to use ("CARD", "PIX", ...).
>     // See "Put the methods on screen" above.
>     await transaction.start({ type: 'CARD' });
>     return transaction;
> }
> ```

### 此处使用的 `TransactionConfig` 字段

| 字段 | 必填 | 用途 |
| - | - | - |
| `checkoutSession` | 是 | 由您的 backend 返回的会话 ID。 |
| `countryCode` | 是 | ISO-3166 alpha-2 国家代码（例如 `US`、`CO`）。 |
| `language` | 否 | ISO-639 代码（例如 `en`、`es`）。未提供时回退到设备或浏览器语言。 |
| `controller` | 是（仅限移动端） | 实现 `TransactionController` 的对象。Web 上不存在该字段，Web 的事件直接在配置中内联绑定。 |
| `elementSelector` | 是（Web，表单支付） | 表单渲染所在的 CSS 选择器或 `HTMLElement`。对于注册为可选项，此时 SDK 会追加自己的容器。传入 `data` 时会被忽略。 |

如需查看完整的字段列表，请参阅 [`TransactionConfig`](https://docs.y.uno/zh-hans/docs/yuno-sdk/reference/objects-you-pass#transaction-config)。

## 确认支付已成功

SDK 展示表单、对输入进行令牌化、创建支付、在需要时处理 3DS，并将最终状态发送到 `onStatus`。请用它向购物者展示收据。在 Web 上，SDK 默认显示自己的结果界面。如需自行绘制收据，请在交易配置中传入 `showStatusScreen: false`。

> [!NOTE]
>
> `onStatus` 驱动您的 UI。对于履约（发货、授予访问权限），请通过 webhooks 或支付 API 在服务器端确认结果；请参阅[在您的服务器上确认](https://docs.y.uno/zh-hans/docs/yuno-sdk/controllers/on-status.md#confirm-on-your-server)。

### 自行创建支付（可选）

默认情况下，SDK 会为您创建支付。您不需要此 endpoint。

如果您覆盖 `createPayment` 以自行控制支付的创建，您的 backend 将使用 SDK 提供的一次性令牌调用此 endpoint。

* **Endpoint**：`POST /v1/payments`
* **必填**：一次性令牌、结账会话、`account_id`、`merchant_order_id`、`description`、`country`、`amount`（`value` + `currency`）、`customer_payer.id`（创建该会话时所针对的客户）以及 `X-Idempotency-Key` 标头
* **参考**：[`POST /v1/payments`](https://docs.y.uno/zh-hans/reference/payments/create-payment)

```http theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
POST https://api-sandbox.y.uno/v1/payments
public-api-key: <your-public-api-key>
private-secret-key: <your-private-secret-key>
X-Idempotency-Key: <unique-key-per-payment>

{
  "account_id": "your-account-id",
  "merchant_order_id": "order-123",
  "description": "Order 123",
  "country": "US",
  "amount": { "currency": "USD", "value": 25 },
  "customer_payer": { "id": "customer-123" },
  "payment_method": { "token": "one-time-token-from-sdk" },
  "checkout": { "session": "checkout-session-id" }
}
```

有关覆盖流程，请参阅 [`createPayment`](https://docs.y.uno/zh-hans/docs/yuno-sdk/controllers/create-payment)。

## 测试卡

> [!WARNING]
>
> 测试卡**因服务提供商而异**。下列卡片仅在您的 sandbox 将卡支付路由到 [Yuno Test Payment Gateway](https://docs.y.uno/zh-hans/docs/direct-integration-use-cases/yuno-testing-gateway.md) 时有效。如果您的路由指向其他服务提供商（Adyen、Stripe 等），这些卡将被拒绝，且通常没有错误消息。此时，请改用该服务提供商自己的测试卡。

请在 sandbox 中使用这些卡来测试不同场景。请严格按照所列内容输入完整的信息组合：

| 卡号 | 有效期 | CVV | 持卡人 | 结果 |
| - | - | - | - | - |
| 4507 9900 0000 0002 | 11/28 | 123 | John Doe | SUCCEEDED |
| 4507 9900 0000 0028 | 11/28 | 123 | John Doe | DECLINED\_BY\_BANK |
| 4507 9900 0000 0010 | 11/28 | 123 | John Doe | INSUFFICIENT\_FUNDS |

如需查看完整的卡片和场景列表（Mastercard、Amex、更多拒绝代码），请参阅[卡片详情](https://docs.y.uno/zh-hans/docs/direct-integration-use-cases/yuno-testing-gateway.md#test-card-payments-with-yuno-testing-gateway)。对于 3DS 流程，请使用 [3DS 测试卡](https://docs.y.uno/zh-hans/docs/direct-integration-use-cases/yuno-testing-gateway.md#test-card-payments-with-yuno-testing-gateway)。

## 后续步骤

* [配置](https://docs.y.uno/zh-hans/docs/yuno-sdk/reference/objects-you-pass.md#transaction-config)。`TransactionConfig` 的完整参考。另请参阅 [`PaymentMethodSelected`](https://docs.y.uno/zh-hans/docs/yuno-sdk/reference/objects-you-pass#payment-method-selected) 和 [`TransactionData`](https://docs.y.uno/zh-hans/docs/yuno-sdk/reference/objects-you-pass#transaction-data)。
* [控制器](https://docs.y.uno/zh-hans/docs/yuno-sdk/controllers/overview.md)。覆盖 `showLoading` 或 `createPayment`，以自定义渲染、加载或支付创建。
* [指南](https://docs.y.uno/zh-hans/docs/yuno-sdk/features/payment-methods-list.md)。支付方式列表、注册、样式等。


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