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

# Headless SDK（Web 注册）

> 在 Web 上使用 Headless SDK 注册支付方式，为卡片数据生成 存储令牌

> [!NOTE]
>
> **This page documents a legacy SDK.** Starting a new integration? Use the [Yuno SDK](https://docs.y.uno/docs/yuno-sdk/get-started.md). Already on a legacy SDK? Follow the [migration guide](https://docs.y.uno/docs/yuno-sdk/migration/overview.md).
>
> The new **Yuno SDK** (Web v2.x, iOS v3.x, Android v3.x, React Native v2.x) replaces the Full, Lite, Seamless, Render, Headless, and Secure Fields SDKs with one API across every platform. Legacy SDKs remain supported side by side during the transition and are scheduled for deprecation in **2027**. Contact your Account Manager for migration planning.

> [!WARNING]
>
> **Orientation: Choosing Your Integration Flow**, before you begin, please review the [Official Integration Flow](https://docs.y.uno/docs/sdks/overview/understanding-flows.md).
>
> * **Standard Flow ([Full Checkout](https://docs.y.uno/docs/sdks/full-checkout/web-payments))**: Recommended for most merchants. Yuno handles the UI, security, and automatic updates for payment methods.
> * **Custom Flow (This SDK)**: Use this only if you require full control over the UX. **Note**: You will be responsible for manually handling payment statuses, 3DS transitions, and fraud routing data collection.

Yuno 的 Headless SDK 让您可以注册支付方式并对卡片进行令牌化，将其保存以供将来使用。

以下步骤介绍如何使用 Yuno 的 Headless SDK 注册支付方式。

## 要求

要执行注册流程，您需要在[第 3 步](#step-3-create-a-customer-session)中提供 `customer_session` 来启动注册流程。要获取 `customer_session`，您需要：

1. **创建客户**：注册支付需要一个客户。使用 [Create Customer](https://docs.y.uno/reference/customers/create-customer.md) endpoint 创建新客户。在响应中，您将收到客户 `id`，用于创建客户会话。
2. **创建客户会话**：使用客户 `id` 和 [Create Customer Session](https://docs.y.uno/reference/customer-sessions-enrollment/create-customer-session.md) endpoint 获取 `customer_session`。

## 第 1 步：在项目中引入库

在开始实现 Headless SDK 之前，请参阅 [SDK 集成概述](https://docs.y.uno/zh-hans/docs/sdks/overview/quickstart.md)，了解如何将 SDK 正确集成到您的项目中的详细说明。

集成指南提供了三种灵活的方法：

1. 直接通过 HTML 脚本引入
2. 通过 JavaScript 动态注入
3. 安装 NPM 模块

选择最适合您的开发工作流程和技术要求的集成方法。完成 SDK 集成后，您可以继续执行以下步骤来实现 headless 注册功能。

> [!NOTE]
>
> **TypeScript 库**
>
> 如果您使用 TypeScript，Yuno 提供了一个[库](https://www.npmjs.com/package/@yuno-payments/sdk-web-types)，您可以用它查看 Yuno Web SDK 中所有可用的方法。

## 第 2 步：使用公钥初始化 Headless SDK

在您的 JavaScript 应用中，提供有效的 `PUBLIC_API_KEY`，创建 `Yuno` 类的实例。

```javascript theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
const yuno = await Yuno.initialize(PUBLIC_API_KEY);
```

> [!NOTE]
>
> **凭证**
>
> 如需了解更多信息，请参阅凭证页面：[/reference/getting-started/authentication](https://docs.y.uno/reference/getting-started/authentication.md)

## 第 3 步：创建客户会话

要开始注册流程，您需要提供 `customer_session`。首先，您需要创建一个客户。您需要一个客户来注册支付。使用 [Create Customer](https://docs.y.uno/reference/customers/create-customer.md) endpoint 创建新客户。在响应中，您将收到客户 `id`，用于创建客户会话。

创建客户后，您就可以创建客户会话。使用客户 `id` 和 [Create Customer Session](https://docs.y.uno/reference/customer-sessions-enrollment/create-customer-session.md) endpoint。`customer_session` 将在响应中提供。每次注册新的支付方式时，您都需要一个新的 `customer_session`。

## 第 4 步：创建注册支付方式对象

您需要一个注册支付方式对象，以设置用于注册的 Headless SDK 集成。您可以使用 [Enroll Payment Method](https://docs.y.uno/reference/payment-methods-checkout/enroll-payment-method-checkout.md) endpoint 创建该对象。创建支付方式对象时，您需要定义客户可以注册哪种支付方式。目前，Headless SDK 仅支持 CARD。

> [!WARNING]
>
> **验证卡片**
>
> 如果您想在注册之前验证卡片（零金额授权），您需要提供 `verify` 对象。请在为客户会话创建支付方式对象时包含该对象。

## 第 5 步：启动注册流程

接下来，您将使用 `apiClientEnroll` 函数并提供必要的配置参数来启动注册流程。

### 参数

使用以下选项配置注册：

| 参数 | 说明 |
| - | - |
| `country_code` | 此参数指定为其设置支付流程的国家。使用表示所需国家代码的 `ENUM` 值。您可以在[国家覆盖范围](https://docs.y.uno/zh-hans/docs/sdks/resources/country-coverage.md)页面上找到支持的国家及其对应代码的完整列表。 |
| `customer_session` | 指当前注册的[客户会话](https://docs.y.uno/zh-hans/docs/basic-concepts/sessions.md)，作为 [Create Customer Session](https://docs.y.uno/reference/customer-sessions-enrollment/create-customer-session.md) endpoint 的响应接收。示例：`'438413b7-4921-41e4-b8f3-28a5a0141638'` |

```javascript theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
const apiClientEnroll = await yuno.apiClientEnroll({
  country_code: "CO",
  customer_session: "eec6578e-ac2f-40a0-8065-25b5957f6dd3"
});
```

## 第 6 步：生成 存储令牌

收集完所有用户信息后，您就可以开始注册。首先，您需要使用函数 `apiClientEnroll.continueEnrollment` 创建 `vaulted_token`。由于这是一个异步函数，您可以使用 `try/catch` 来确保正确处理触发的错误。以下示例展示了如何创建 `vaulted_token`：

```javascript theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
const vaultedTokenResponse = await apiClientEnroll.continueEnrollment({
  customer_session: "eec6578e-ac2f-40a0-8065-25b5957f6dd3",
  payment_method: {
    type: "CARD",
    card: {
      detail: {
        expiration_month: 11,
        expiration_year: 25,
        number: "4111111111111111",
        security_code: "123",
        holder_name: "ANDREA B",
        type: "CREDIT"
      }
    },
    customer: {
    }
  }
});
```

`card.detail.type` 是可选的。如果您省略此项，Yuno 会根据卡号检测卡片类型。如果您发送此项，它会随卡片一起保存并用于之后的支付，因此错误的类型可能导致拒绝。

注册新卡后，您将收到 `vaulted_token`。您可以在将来使用它进行支付，而无需再询问客户的卡片信息。以下代码块展示了 `apiClientEnroll.continueEnrollment` 函数的响应示例：

```json theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
{
 "vaulted_token": "9104911d-5df9-429e-8488-ad41abea1a4b",
 "customer": {
   "session": "eec6578e-ac2f-40a0-8065-25b5957f6dd3"
 },
 "status": "ENROLLED"
}
```

> [!NOTE]
>
> **可能的状态值**
>
> `status` 字段可以是以下值之一：
>
> * `CREATED`
> * `EXPIRED`
> * `REJECTED`
> * `READY_TO_ENROLL`
> * `ENROLL_IN_PROCESS`
> * `UNENROLL_IN_PROCESS`
> * `IN_PROCESS`
> * `ENROLLED`
> * `DECLINED`
> * `CANCELED`
> * `ERROR`
> * `UNENROLLED`

> [!NOTE]
>
> **演示应用**
>
> 除了提供的代码示例外，您还可以访问[演示应用](https://github.com/yuno-payments/yuno-sdk-web)，获取 Yuno SDK 的完整实现（从代码库克隆）。

## 错误处理

在您的应用中处理 SDK 返回的错误，例如注册失败或验证错误。有关 HTTP 状态码和响应代码，请参阅 API 参考中的[状态和响应代码](https://docs.y.uno/reference/getting-started/response-codes.md)。

## 保持更新

请访问[更新日志](https://docs.y.uno/changelog/web.md#v1-6-0)，了解最新的 SDK 更新和版本历史。


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