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

# Lite SDK（Web 注册）

> 在 Web 上使用 Lite 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 Lite Web SDK 的注册功能。

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

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

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

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

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

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

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

在您的 JavaScript 应用中提供有效的 `PUBLIC_API_KEY`，以初始化 Yuno SDK：

```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 步：启动注册流程

使用以下配置，为您的客户提供轻量且易用的注册体验：

```javascript theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
await yuno.mountEnrollmentLite({
  customerSession: "438413b7-4921-41e4-b8f3-28a5a0141638",
  renderMode: {
    type: "element",
    elementSelector: "#root",
  },
  countryCode: "US",
  language: "en",
  showLoading: true,
  issuersFormEnable: true,
  onLoading: (args) => console.log(args),
  card: {
    type: "extends",
    styles: "",
    texts: {},
  },
  texts: {},
  enrollmentStatus(data) {
    console.log("Enrollment status:", data);
  },
  async error(error, data) {
    console.error("An error occurred:", error);
    await yuno.hideLoader();
  },
});
```

使用 `mountEnrollmentLite` 时，请指定用于处理注册的 callback。您还可以使用 `texts` 对象自定义界面。

### 参数

使用以下选项配置 Lite 注册：

| 参数 | 说明 |
| - | - |
| `customerSession` | 指当前注册的[客户会话](https://docs.y.uno/reference/customer-sessions-enrollment/create-customer-session.md)。 |
| `elementSelector` | 用于渲染注册表单的 HTML 元素。请在 `renderMode: { type: "element", elementSelector }` 中传入它；顶层的 `elementSelector` 会被忽略，表单将以模态框形式打开。 |
| `countryCode` | 此参数指定要为其设置注册的国家/地区代码。 |
| `language` | 注册表单的语言。 |
| `showLoading` | 控制注册过程中 Yuno 加载/加载动画页面的可见性。 |
| `onLoading` | 用于接收注册过程中服务器调用或加载事件的通知，必须提供。 |
| `issuersFormEnable` | 启用发卡机构表单（例如银行列表）。 |
| `card` | 定义卡片表单的配置。 |
| `texts` | 允许您设置自定义按钮文本。 |
| `enrollmentStatus` | 注册流程结束时调用的 callback。接收 `{ vaultedToken, status }`。 |
| `error` | 注册过程中出现错误时调用的 callback。 |
| `onChange` | 报告注册表单是否已填写完整。接收 `{ form: { isValid } }`。自 v1.10.11 起可用。 |

> [!NOTE]
>
> **状态值**
>
> 常见的状态值包括 `ENROLLED`、`DECLINED`、`CANCELED` 和 `ERROR`。

## 使用 element 渲染模式注册 PayPal

> [!NOTE]
>
> 自 Web SDK **1.10.13** 起可用。

默认情况下，`mountEnrollmentLite` 会打开 Yuno 模态框来注册 PayPal。购物者点击您的 PayPal 按钮后会看到模态框，并需要再次点击 PayPal。使用 element 渲染模式时，PayPal 按钮会直接渲染在您的页面上，购物者只需点击一次即可完成注册。

### 工作原理

在您的页面中添加一个空容器，然后将 `renderMode: { type: 'element', elementSelector }` 传给 `mountEnrollmentLite`。SDK 会在该容器内渲染真正的 PayPal 按钮，并在 PayPal 加载完成之前显示加载骨架屏。

PayPal 按钮会取代您自己的按钮，请勿将您的按钮放在它的前面。如果您不传入 `renderMode`，模态框的行为将与之前完全相同。

### 集成步骤

1. 移除您现有的 PayPal 按钮。
2. 在 PayPal 按钮应显示的位置添加一个空的 `<div>`。
3. 传入 `renderMode`，其中包含 `type: 'element'` 和指向该 `<div>` 的 `elementSelector`。

```html theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
<div id="paypal-enrollment"></div>
```

```javascript theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
await yuno.mountEnrollmentLite({
  customerSession: "438413b7-4921-41e4-b8f3-28a5a0141638",
  countryCode: "US",
  language: "en",
  renderMode: {
    type: "element",
    elementSelector: "#paypal-enrollment",
  },
  enrollmentStatus(data) {
    console.log("Enrollment status:", data);
  },
  error(error, data) {
    console.error("An error occurred:", error);
  },
});
```

除非您设置了 `width`，否则按钮会占据容器 100% 的宽度。

### 自定义 PayPal 按钮

可选。将 `externalButtons.paypal` 传给 `mountEnrollmentLite` 以设置按钮样式，所用属性与 checkout 中的 [PayPal 快捷按钮](https://docs.y.uno/zh-hans/docs/sdks/resources/references/web.md#paypal)相同。默认值与 checkout 不同，因此下面完整列出。

```javascript theme={"theme":{"light":"github-dark","dark":"github-dark-dimmed"}}
await yuno.mountEnrollmentLite({
  customerSession: "438413b7-4921-41e4-b8f3-28a5a0141638",
  countryCode: "US",
  language: "en",
  renderMode: {
    type: "element",
    elementSelector: "#paypal-enrollment",
  },
  externalButtons: {
    paypal: {
      color: "blue",
      shape: "pill",
      label: "paypal",
      height: 48,
      width: 300,
      locale: "en_US",
    },
  },
  // ... rest of config
});
```

| 属性 | 类型 | 默认值 | 说明 |
| - | - | - | - |
| `color` | `'gold'` \| `'blue'` \| `'silver'` \| `'white'` \| `'black'` | 由 PayPal 决定 | 按钮背景颜色 |
| `shape` | `'rect'` \| `'pill'` \| `'sharp'` | `'rect'` | 按钮形状 |
| `label` | `'paypal'` \| `'checkout'` \| `'buynow'` \| `'pay'` \| `'installment'` | 由 PayPal 决定 | 按钮标签文本 |
| `height` | `number` | `48` | 按钮高度，单位为 px（有效范围：25 到 55） |
| `width` | `number` | 容器宽度的 100% | 外层容器宽度，单位为 px |
| `locale` | `string` | 由 PayPal 根据购物者的浏览器决定 | 按钮语言，采用 PayPal 的 `language_COUNTRY` 格式（例如 `en_US`、`es_ES`、`pt_BR`）。请参阅 PayPal 的[支持的区域设置](https://developer.paypal.com/sdk/js/configuration/#locale)。 |

### 结果

* 购物者点击 PayPal 按钮，在 PayPal 弹出窗口中批准，SDK 完成注册。
* `enrollmentStatus` 会收到状态为 `ENROLLED` 的 `vaultedToken`。
* 如果购物者关闭弹出窗口，`error` 会收到 `CANCELED_BY_USER`。

## KCP KakaoPay 和 PAYCO 注册（Web）

> [!NOTE]
>
> KakaoPay 桌面端注册和 KCP 移动端 POST 重定向自 Web SDK **1.11.27** 起可用。PAYCO 桌面端注册自 Web SDK **1.11.43** 起受支持。

Yuno Lite Web SDK 支持在桌面浏览器上通过 KCP 注册 KakaoPay（`KAKAO_PAY`）和 PAYCO（`KCP_PAYCO`），并支持在移动浏览器上为 KCP 注册自动执行 HTTP POST 表单重定向。

### 桌面端注册流程

在桌面浏览器上，KakaoPay 和 PAYCO 注册会在 Lite 注册组件内渲染一个操作按钮。

#### 工作原理

1. **脚本加载与 callback 设置**：注册组件挂载时，SDK 会声明 KCP callback 处理程序（`window.m_Completepayment`），然后从 `script_url`（`https://*.kcp.co.kr`）验证并加载 KCP 的脚本包。
2. **用户操作**：购物者点击操作按钮。SDK 准备订单表单并打开 KCP 授权窗口（`KCP_Pay_Execute_Web`）。
3. **完成与提交**：购物者在 KCP 中完成操作后，KCP 会使用结果（`res_cd`、`res_msg`、`enc_data`、`enc_info`、`tran_cd`）调用 SDK 的 callback，SDK 会通过隐藏表单提交将结果发送到 backend 的 `result_url`（`https://*.y.uno` 或您的白标 API 主机）。当配置为 `RENDER_POPUP` 时，POST 提交会在弹出窗口内进行，而不会导航父页面。
4. **清理**：卸载注册组件会关闭所有已打开的 KCP 窗口，并恢复之前已存在的 `window.m_Completepayment` 处理程序。

#### 按钮状态和错误恢复

* **按钮禁用**：如果所需的 backend 注册配置（`script_url`、`form_fields` 或 `result_url`）缺失或指向未经授权的主机，按钮会被禁用并显示内联错误消息。
* **脚本加载重试**：如果 KCP 脚本加载失败，按钮会显示内联错误；点击按钮会再次加载 KCP，加载完成后，下一次点击会打开 KCP。
* **关闭弹出窗口后的恢复**：如果购物者未批准就关闭了 KCP 弹出窗口，SDK 会通过 DOM 变更观察器检测到层被移除，并重新启用按钮以便再次尝试。
* **超时恢复**：如果在发送注册结果 10 秒后浏览器仍停留在页面上（通常是由于缺少 CSP form-action 策略而阻止了表单提交），SDK 会清除处理中状态，显示内联错误并重新启用按钮，以便购物者重试。请注意，不会触发任何商户错误 callback。

### KCP 移动端 POST 重定向

在移动浏览器上通过 KCP 注册时，backend 会返回一个 `require_action.redirect` payload，其中包含 `method: "POST"` 以及指向 KCP 主机（`*.kcp.co.kr`）的 `init_url`。

Lite Web SDK 会自动检测 POST 重定向配置，构建一个填入 `form_fields` 的隐藏表单，将其附加到 DOM 中，并提交该表单，将用户直接导航到服务提供商。

### 内容安全策略（CSP）要求

如果您的网站实施了严格的内容安全策略（CSP），请配置以下指令，以允许 KCP 脚本、iframe 覆盖层和表单提交：

| 指令 | 允许的来源 | 说明 |
| - | - | - |
| `script-src` | `https://*.kcp.co.kr` | 允许加载 KCP SDK hub 和辅助脚本（`kcp_spay_hub.js`、`kcp_jquery` 等）。 |
| `frame-src` | `https://*.kcp.co.kr` | 允许渲染 KCP 覆盖层模态框和支付 iframe。 |
| `form-action` | `https://*.kcp.co.kr https://*.y.uno` | 允许通过隐藏表单 POST 导航到 KCP，并将注册结果发送到 Yuno（或您的白标 API 域名）。 |

> [!WARNING]
>
> 如果缺少针对 `*.y.uno` 或 `*.kcp.co.kr` 的 `form-action` 指令，浏览器会在不产生 JavaScript 错误的情况下阻止表单提交，导致注册超时。

## 第 5 步：发起注册（必需）

使用 `await yuno.mountEnrollmentLite()` 挂载 SDK 后，表单将显示在指定的元素中。随后，用户可以输入其支付方式详细信息以完成注册。

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

## 错误处理

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

## 保持更新

访问 [changelog](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.