> ## Documentation Index
> Fetch the complete documentation index at: https://docs.clinkbill.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 接入 Stripe SPT 支付

> 接收 Clink 传入的 Stripe Shared Payment Token，并在你自己的 Stripe 账户完成扣款。

Stripe Shared Payment Token（SPT）让使用 Clink Agent Pay 的 Agent 可以直接为你平台上的订单付款，用户无需跳转到结账页。Clink 向 Stripe 申请一个只有你的 Stripe 账户能使用、且以订单金额为上限的 SPT，并传给你提供的接口；你的服务端在自己的 Stripe 账户完成扣款。

## 流程说明

```mermaid theme={null}
sequenceDiagram
  participant U as 用户
  participant C as Clink
  participant S as Stripe
  participant M as 你的服务端

  U->>C: 让 Agent 代为购买
  C->>M: 创建订单
  M-->>C: 订单号、最终应付金额、币种
  C->>S: 申请签发 SPT（你的 Profile ID、用户支付方式、金额上限）
  S-->>C: spt_xxx
  C->>M: 将 SPT 传给你的接口
  M->>S: 使用 SPT 创建并确认 PaymentIntent
  S-->>M: 扣款结果
  M-->>C: SUCCEEDED / PROCESSING / FAILED

  opt 结果为 PROCESSING
    C->>M: 查询支付状态
    M-->>C: 最终状态
  end
```

| 参与方 | 职责 |
| - | - |
| Clink | 使用你的 Stripe Profile ID、用户的支付方式、订单金额与币种向 Stripe 申请签发 SPT，将 SPT 传给你的接口，并跟进仍在处理中的支付。 |
| 你 | 提供 Stripe Profile ID，为每笔订单返回最终应付金额与币种，提供接收 SPT 的接口，通过 Stripe 完成扣款并返回结果。 |

## 开始前

* 一个 Stripe 已向卖家开放 SPT 的 Stripe 账户。可用范围和服务条款见 [Stripe SPT 官方文档](https://docs.stripe.com/agentic-commerce/concepts/shared-payment-tokens?agent-seller=seller)。
* 已在 Stripe Dashboard 创建 [Stripe Profile](https://docs.stripe.com/get-started/account/profile)。
* 一个可供 Clink 代用户下单的下单接口或 MCP 工具。

## 1. 提供 Stripe Profile ID

将沙箱环境和生产环境的 Stripe Profile ID（`profile_xxx`）分别提供给 Clink。Clink 用它申请签发 SPT，签发出的 SPT 只授予你的 Stripe 账户。

## 2. 返回最终应付金额与币种

Clink 申请 SPT 时，以订单的最终应付金额作为额度上限。Clink 创建订单时，请返回：

* 订单号
* 最终应付金额，已包含各项费用和优惠
* ISO 4217 币种代码，例如 `USD`

如果实际扣款金额超过你返回的金额，Stripe 会拒绝这笔支付。

## 3. 提供接收 SPT 的接口

提供一个 HTTPS 接口，Clink 拿到 SPT 后调用它。如果 Clink 已经通过 MCP 对接你的平台，也可以改为提供一个 MCP 工具。以下请求和响应格式供参考，具体接口路径、字段命名和鉴权方式在联调时与 Clink 确认。

### 请求示例

```json theme={null}
{
  "orderId": "order_10001",
  "sharedPaymentToken": "spt_example"
}
```

| 字段 | 类型 | 必填 | 说明 |
| - | - | - | - |
| `orderId` | string | 是 | 创建订单时你返回的订单号，字段名沿用你自己的下单接口。 |
| `sharedPaymentToken` | string | 是 | Stripe 签发的 SPT（`spt_xxx`）。 |

请求中不带金额。金额和币种始终以你本地的订单记录为准。

### 响应示例

```json 成功 theme={null}
{
  "orderId": "order_10001",
  "paymentStatus": "SUCCEEDED",
  "paidAmount": 17.41,
  "currency": "USD"
}
```

```json 处理中 theme={null}
{
  "orderId": "order_10001",
  "paymentStatus": "PROCESSING"
}
```

```json 失败 theme={null}
{
  "orderId": "order_10001",
  "paymentStatus": "FAILED",
  "failureCode": "card_declined",
  "failureMessage": "Your card was declined."
}
```

| 字段 | 类型 | 说明 |
| - | - | - |
| `orderId` | string | 请求中的订单号。 |
| `paymentStatus` | string | `SUCCEEDED`（成功）、`PROCESSING`（处理中）或 `FAILED`（失败）。 |
| `paidAmount` | number | 实际扣款金额，以币种的标准单位表示，例如 `17.41`。支付成功时返回。 |
| `currency` | string | ISO 4217 币种代码。支付成功时返回。 |
| `failureCode` | string | 支付失败时返回。透传 Stripe 的 `error.code`；本地校验失败时返回你自己的错误码，例如 `ORDER_NOT_FOUND`、`ORDER_ALREADY_PAID`。 |
| `failureMessage` | string | 失败原因。支付失败时返回。 |

> [!WARNING]
>
> 这个接口只接受来自 Clink 的请求，SPT 只在你的服务端处理，不要把完整的 Token 写入日志。

## 4. 使用 SPT 扣款

校验订单存在且未支付，从订单记录中读取金额和币种，然后带上 SPT 创建并确认 Stripe `PaymentIntent`：

```bash theme={null}
curl https://api.stripe.com/v1/payment_intents \
  -u "$STRIPE_SECRET_KEY:" \
  -H "Stripe-Version: 2026-09-30.preview" \
  -d amount=1741 \
  -d currency=usd \
  -d "payment_method_data[shared_payment_granted_token]=spt_example" \
  -d "metadata[orderId]=order_10001" \
  -d confirm=true
```

* Stripe 的金额以币种最小单位表示，币种代码为小写：`17.41 USD` 对应 `amount=1741` 和 `currency=usd`。
* 这个流程中没有用户可以跳转。如果确认时 Stripe 要求提供 `return_url`，请加上 `automatic_payment_methods[enabled]=true` 和 `automatic_payment_methods[allow_redirects]=never`。
* 在 `metadata` 中写入订单号，便于对账以及在 Webhook 中将 Stripe 支付对应到你的订单。
* SPT 目前是 Stripe 的预览功能，`Stripe-Version` 和参数以最新的 [Stripe SPT 官方文档](https://docs.stripe.com/agentic-commerce/concepts/shared-payment-tokens?agent-seller=seller)为准。

### 映射处理结果

| Stripe 返回 | `paymentStatus` | 处理方式 |
| - | - | - |
| `succeeded` | `SUCCEEDED` | 将订单更新为已支付，返回 `paidAmount` 和 `currency`。 |
| `processing` | `PROCESSING` | 订单保持待支付，后续再更新为最终状态。 |
| `requires_payment_method`、`requires_action`、`canceled`，或接口返回错误 | `FAILED` | 返回 `failureCode` 和 `failureMessage`。 |

这个流程中没有用户在场完成额外的身份验证，因此 `requires_action` 按失败处理。返回 `FAILED` 时，如果 PaymentIntent 仍未结束，请将其取消，避免之后又扣款成功。

Stripe 可能直接返回 `processing`，表示这笔支付还没有最终结果。此时请返回 `PROCESSING`，不要返回 `FAILED`。

## 5. 处理中的支付如何闭环

返回 `PROCESSING` 后，Clink 会查询支付状态，直到拿到最终结果。你需要：

* 将订单更新为最终状态。监听 Stripe Webhook `payment_intent.succeeded` 和 `payment_intent.payment_failed`，或在 Clink 查询时向 Stripe 反查 PaymentIntent。
* 提供一个支付状态查询接口，按订单号返回当前的支付状态。已有的订单或支付状态查询接口可以直接复用。

## 退款与对账

PaymentIntent 确认后，Stripe 会把它当作你账户上的一笔普通支付。退款和对账沿用你现有的 Stripe 流程。

## 沙箱测试

Stripe 提供测试辅助接口，可以直接为你的沙箱账户签发 SPT，方便你在与 Clink 联调前先自测接口：

```bash theme={null}
curl https://api.stripe.com/v1/test_helpers/shared_payment/granted_tokens \
  -u "$STRIPE_SECRET_KEY:" \
  -H "Stripe-Version: 2026-09-30.preview" \
  -d payment_method=pm_card_visa \
  -d "usage_limits[currency]=usd" \
  -d "usage_limits[max_amount]=1741" \
  -d "usage_limits[expires_at]=$(($(date +%s) + 3600))"
```

使用 Stripe 沙箱环境的 Secret Key，然后用返回的 `spt_xxx` 调用你的接口，确认：

* 正常请求只扣款一次，并返回 `SUCCEEDED` 以及金额和币种。
* 对已支付订单再次请求时不会重复扣款。
* 订单不存在时返回 `FAILED`。
* 订单金额超过 `max_amount` 或 Token 已过期时，返回 `FAILED` 并带上 Stripe 的错误码。

## 上线前检查

* 已将生产环境的 Stripe Profile ID 提供给 Clink。
* 接口使用生产环境的 Stripe Secret Key，且密钥只保存在服务端。
* 金额和币种取自本地订单记录。
* 返回 `PROCESSING` 的支付都能更新为最终状态，并已校验 Stripe Webhook 签名。


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