正在加载搜索…

正在加载搜索…

跳至内容

API v2 仅供现有集成使用。新集成请使用 API v3。

查看此页的 API v3 版本
产品与订阅创建结账会话

使用目录商品创建结账会话

了解如何使用您在仪表板中管理的目录商品创建结账会话。

概述

虚拟物品和组合包在仪表板中都作为目录商品管理。创建结账会话时,您通过 id 引用目录商品及其中一个价格方案。名称、图片和价格均取自您的目录,因此请求中无需包含价格。

前提条件

在使用目录商品创建结账会话之前,请确保您已:

  1. API 凭证:从 Tokenz 仪表板获取的密钥。
  2. 目录设置:在仪表板中发布要销售的目录商品,且至少有一个已上架的价格方案。
  3. 目录商品与价格方案 id:目录商品的 id(catalogitem_…)及其价格方案的 id(priceplan_…)。两者都可从目录商品端点获取。测试密钥返回测试商品,其 id 以 _t 结尾。

使用目录商品创建结账会话

每个目录商品在 itemDetails 中添加一个条目。每个条目包含一个 catalogItem 对象,三个字段均为必填:

  • catalogItemId:目录商品的 id。
  • pricePlanId:商品 pricePlans 中某个方案的 id。商品的 defaultPricePlanId 是优先显示的方案。
  • quantity:销售数量,至少为 1。

虚拟物品和组合包的引用方式相同。

单个目录商品

bash
curl --request POST \
  --url https://api.tokenz.one/v2/checkoutsession \
  --header 'Authorization: Bearer secret_test_YOUR_KEY_HERE' \
  --header 'Content-Type: application/json' \
  --data '{
    "itemDetails": [
      {
        "catalogItem": {
          "catalogItemId": "catalogitem_7Gp1KlY3wSz_t",
          "pricePlanId": "priceplan_4Tm8XuQ5pFg_t",
          "quantity": 2
        }
      }
    ],
    "customerInfo": {},
    "successUrl": "https://yourdomain.com/success",
    "pendingUrl": "https://yourdomain.com/pending",
    "cancelUrl": "https://yourdomain.com/cancel",
    "locale": "zh_CN"
  }'

多个目录商品

一个结账会话可以包含多个目录商品,例如一个组合包加一个虚拟物品:

bash
curl --request POST \
  --url https://api.tokenz.one/v2/checkoutsession \
  --header 'Authorization: Bearer secret_test_YOUR_KEY_HERE' \
  --header 'Content-Type: application/json' \
  --data '{
    "itemDetails": [
      {
        "catalogItem": {
          "catalogItemId": "catalogitem_8Hq2LmZ4xTa_t",
          "pricePlanId": "priceplan_3Rk9VwP2nDe_t",
          "quantity": 1
        }
      },
      {
        "catalogItem": {
          "catalogItemId": "catalogitem_7Gp1KlY3wSz_t",
          "pricePlanId": "priceplan_4Tm8XuQ5pFg_t",
          "quantity": 3
        }
      }
    ],
    "customerInfo": {},
    "successUrl": "https://yourdomain.com/success",
    "pendingUrl": "https://yourdomain.com/pending",
    "cancelUrl": "https://yourdomain.com/cancel",
    "locale": "zh_CN"
  }'

实现示例

javascript
import fetch from 'node-fetch';

async function createCheckoutSession() {
  const response = await fetch('https://api.tokenz.one/v2/checkoutsession', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${process.env.TOKENZ_SECRET_KEY}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      itemDetails: [
        {
          catalogItem: {
            catalogItemId: "catalogitem_7Gp1KlY3wSz_t",
            pricePlanId: "priceplan_4Tm8XuQ5pFg_t",
            quantity: 2
          }
        }
      ],
      customerInfo: {},
      successUrl: "https://yourdomain.com/success",
      pendingUrl: "https://yourdomain.com/pending",
      cancelUrl: "https://yourdomain.com/cancel",
      locale: "zh_CN"
    })
  });

  const session = await response.json();

  // 将用户重定向到结账页面
  return session.url;
}

响应

API 返回一个结账会话对象:

json
{
  "id": "checkoutsession_12345678QsZ",
  "object": "checkoutsession",
  "url": "https://checkout.tokenz.one/12345678QsZ",
  "cancelUrl": "https://yourdomain.com/cancel",
  "pendingUrl": "https://yourdomain.com/pending",
  "successUrl": "https://yourdomain.com/success",
  "order": {
    "id": "order_1xdzo1aZRGB",
    "object": "order",
    "status": "requiresPayment",
    "amount": {
      "amount": 398,
      "currency": "USD"
    },
    "items": [
      {
        "id": "item_1xdzo1ZpRcT",
        "detail": {
          "product": {
            "price": {
              "amount": 199,
              "currency": "USD"
            },
            "quantity": 2,
            "label": "100 Gems",
            "description": "",
            "images": [
              "https://example.com/images/gems-100.png"
            ],
            "sku": "gems-100",
            "taxCategory": "DIGITAL_GOODS_AND_SERVICES",
            "catalogItemId": "catalogitem_7Gp1KlY3wSz_t",
            "pricePlanId": "priceplan_4Tm8XuQ5pFg_t"
          }
        }
      }
    ],
    "test": true,
    "createdAt": "2026-11-20T04:32:04.696Z",
    "updatedAt": "2026-11-20T04:32:04.696Z"
  },
  "createdAt": "2026-11-20T04:32:04.374Z",
  "updatedAt": "2026-11-20T04:32:04.374Z",
  "test": true,
  "customerInfo": {},
  "locale": "zh_CN",
  "preferApplePay": false,
  "preferGooglePay": false
}

order.items 中每个 product 都带有定价来源的 catalogItemId 和 pricePlanId。请用它们判断要发放的内容。

错误

包含目录商品条目的请求可能因以下错误而失败:

  • 400 itemDetails.catalogItem.not-supported:您的账户使用 Payment Gateway 模式,该模式不支持目录商品。
  • 400 pricePlan.recurring-not-supported:在不创建订阅的结账会话中使用 RECURRING 方案。
  • 400 pricePlan.one-time-not-supported:在创建订阅的结账会话中使用 ONE_TIME 方案。
  • 404 entity.not-found:找不到引用的目录商品或价格方案。

最佳实践

  1. 从 API 读取 id:从目录商品端点获取目录商品和价格方案的 id,不要硬编码
  2. 错误处理:为上述错误实现适当的错误处理
  3. 使用 Webhook:设置 webhook 以接收支付状态的实时更新
  4. 充分测试:上线前使用测试模式验证您的集成

后续步骤