# 加载 Offering 及其 Product 可购买状态

Operation / SDK: `resolveOffering` · `resolve_offering` · `ResolveOffering`

解析已发布的 Offering Version、候选 Product 与可购买状态。

更新于 2026-11-06

[网页版本](https://www.longplayai.com/zh-hans/docs/api/resolve-offering)

<a id="request"></a>

## 请求

- **凭据**: Server Key（传 `customerId`）或 Customer Token，权限 `entitlements:read`。
- **SDK**: JavaScript `createLongplayServer().resolveOffering` · JavaScript `createLongplay().resolveOffering` · Python `resolve_offering` · Go `ResolveOffering`
- **成功状态**: HTTP 200

响应为示例数据。

**cURL: request.sh**

```bash
curl -X POST "$LONGPLAY_API_URL/api/v1/offerings/resolve" \
  -H "Authorization: Bearer $LONGPLAY_SERVER_KEY" \
  -H "X-Indiana-Protocol: 0.10" \
  -H "Content-Type: application/json" \
  -d '{
  "selector": {
    "type": "version",
    "versionId": "2d393025-cac1-4672-8ea7-31dada869245"
  },
  "commerce": {
    "provider": "stripe",
    "currency": "USD"
  },
  "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d"
}'
```

**TypeScript: resolveOffering.ts**

```ts
import { createLongplayServer } from "@tinkerlandai/longplay-sdk/server";

const longplay = createLongplayServer({
  baseUrl: process.env.LONGPLAY_API_URL!,
  apiKey: process.env.LONGPLAY_SERVER_KEY!,
  scope: { projectId: process.env.LONGPLAY_PROJECT_ID!, environment: "sandbox" },
});

export async function resolveOffering() {
  return longplay.resolveOffering({
    selector: {
      type: "version",
      versionId: "2d393025-cac1-4672-8ea7-31dada869245",
    },
    commerce: {
      provider: "stripe",
      currency: "USD",
    },
    customerId: "8b258f8b-4228-40fa-8d24-5b16428e686d",
  });
}
```

**请求体: request.json**

```json
{
  "selector": {
    "type": "version",
    "versionId": "2d393025-cac1-4672-8ea7-31dada869245"
  },
  "commerce": {
    "provider": "stripe",
    "currency": "USD"
  },
  "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d"
}
```

**响应示例: response.json**

```json
{
  "requestId": "44f5fcb8-6ed8-41b8-818f-7571941c5fd5",
  "scope": {
    "projectId": "df5426d6-b335-403c-8f3e-b0f73d2e3511",
    "environment": "sandbox"
  },
  "offering": {
    "id": "2bd9a6d1-333e-46ea-8850-4480295cc169",
    "offeringId": "2bd9a6d1-333e-46ea-8850-4480295cc169",
    "version": 1,
    "document": {
      "lookupKey": "1Example",
      "displayName": "1Example",
      "items": [
        {
          "type": "product",
          "productId": "2fd314d6-0f91-46cc-8882-ae8b3cc3bd21"
        }
      ],
      "metadata": {}
    }
  },
  "candidates": [
    {
      "productId": "1Example",
      "productVersionId": "1Example",
      "name": "Pro",
      "action": "purchase",
      "membershipId": null,
      "currency": "USD",
      "amountMinor": "1",
      "interval": "free",
      "intervalCount": 1
    }
  ],
  "availability": [
    {
      "productVersionId": "8c489a1a-5542-44fe-8e80-da2f6a319d01",
      "available": true,
      "reason": null
    }
  ],
  "check": null
}
```

<a id="parameters"></a>

## 请求体参数

**application/json**

- `processContext` (object, optional): Funnel 已受理的商业调用上下文，包含原 Run、Customer 绑定和操作回执；调用方仍须提供有效的 Customer 凭据。
- `processContext.runId` (uuid, required): Run 的 ID。
- `processContext.bindingId` (uuid, required): 该调用受理时固定的 Run-to-Customer 绑定 ID；须与 Run、回执及当前授权一致。
- `processContext.receiptId` (uuid, required): 回执的 ID。
- `funnelId` (uuid, optional): Funnel 的 ID。
- `funnelVersionId` (uuid, optional): Funnel 版本的 ID。
- `selector` (object | object | object | object, required): 选择 Offering 或 Paywall 的条件。
- `selector.type` ("current" | "id" | "lookup_key" | "version", required): 选择方式。Offering：`current`（环境中设为当前的 Offering）、`id` 和 `lookup_key` 解析为该 Offering 已发布的 Version，`version` 固定到指定的 Offering Version；Paywall：`paywall` 解析为其已发布的 Version，`version` 固定到指定的 Paywall Version；Page、Popup、Banner：`resource` 解析资源当前发布的 Version，`version` 固定到已发布 Version。
- `selector.id` (uuid, optional): Offering 选择器中为要解析的 Offering UUID。Page、Popup、Banner 中为资源或已发布 Version 的 UUID，由 selector.type 决定；已归档或未发布的资源返回未找到。
- `selector.lookupKey` (string, optional): 稳定的查找键。 minLength: 1 · maxLength: 200
- `selector.versionId` (uuid, optional): 不可变 product_versions.id，可筛选历史版本。
- `customerId` (string, optional): Customer 的 UUID。用 Server Key 调用时指定 Customer；Customer Token 调用时省略。 minLength: 1
- `commerce` (object, required): 支付渠道与币种，例如 `{ "provider": "stripe", "currency": "USD" }`。
- `commerce.provider` (enum, required): 支付渠道，取值见允许值。 Values: `"stripe"`, `"apple"`, `"google"`, `"merchant"`.
- `commerce.currency` (enum, required): 已发布价格支持的币种代码。 Values: `"USD"`, `"EUR"`, `"CNY"`, `"GBP"`, `"JPY"`, `"CAD"`, `"AUD"`, `"NZD"`, `"CHF"`, `"HKD"`, `"SGD"`, `"KRW"`, `"INR"`, `"BRL"`, `"MXN"`, `"TWD"`, `"THB"`, `"IDR"`, `"MYR"`, `"PHP"`, `"VND"`, `"ZAR"`, `"AED"`, `"SAR"`, `"SEK"`, `"NOK"`, `"DKK"`, `"PLN"`, `"CZK"`, `"HUF"`, `"ILS"`, `"TRY"`, `"RON"`.
- `commerce.intent` ("subscription_change", optional): 操作意图，取值见允许值。
- `requirement` (object, optional): 要检查的需求：Feature、数量和计价属性。
- `requirement.internal_feature_id` (uuid, required): Feature 的 ID。
- `requirement.quantity` (string, required): 数量，十进制字符串，避免浮点误差。 pattern: ^(0|[1-9]\d{0,11})(\.\d{1,6})?$
- `requirement.properties` (object, required): 服务端校验的计价属性，用于匹配维度和倍率。
- `requirement.entityId` (uuid, optional): 当前客户范围内的 Entity ID；只用于已配置的 Entity 和许可 Feature。

<a id="response"></a>

## 响应字段

成功响应都包含 `requestId`（本次请求 ID，排查时提供）和 `scope`（App 与环境）。

**HTTP 200 · application/json**

- `requestId` (uuid, required): 服务端为这次 HTTP 请求生成的 UUID，排查时提供；它不是你的业务 requestId。
- `scope` (object, required): 响应实际所属的 App 和环境。Header 只能核对，不能切换 Key 范围。
- `scope.projectId` (string, required): 凭据绑定的 App ID。 minLength: 1
- `scope.environment` (enum, required): 独立数据范围：sandbox 或 live。 Values: `"sandbox"`, `"live"`.
- `offering` (object, required): Offering，字段见下。
- `offering.id` (uuid, required): Offering 的 ID。
- `offering.offeringId` (uuid, required): Offering 的 ID。
- `offering.version` (integer, required): 版本号。 max: 9007199254740991
- `offering.document` (object, required): 已发布的 schemaVersion 3 设计。缓存命中时可为 null；直接 HTTP 调用方须用匹配缓存还原。
- `offering.document.lookupKey` (string, required): 稳定的查找键。 minLength: 1 · maxLength: 200
- `offering.document.displayName` (string, required): 展示名称。 minLength: 1 · maxLength: 1500
- `offering.document.items` (array, required): 列表项。 maxItems: 20
- `offering.document.items.type` ("product" | "group", required): `product` 为叶子项，通过 `productId` 引用一个 Product Version；`group` 为仅在本 Offering 文档内有效的命名分组，带 `key`、`name` 和嵌套的 `items`（根以下最多三层）。
- `offering.document.items.productId` (uuid, optional): Product 的 ID。
- `offering.document.items.key` (string, optional): 该项的键。在 Offering 的 `group` 项中是分组键（以字母开头，最多 64 个字母、数字、`_` 或 `-`），在 Offering 内唯一；在发票明细项中用于标识该发票行。 pattern: ^[a-zA-Z][a-zA-Z0-9_-]{0,63}$
- `offering.document.items.name` (string, optional): 展示名称，不作为唯一标识。 minLength: 1 · maxLength: 80
- `offering.document.items.items` (array, optional): 列表项。 maxItems: 20
- `offering.document.metadata` (object, required): 自定义键值数据。
- `candidates` (array, required): 候选 Product。 maxItems: 20
- `candidates.productId` (string, required): Product 的 ID。 minLength: 1
- `candidates.productVersionId` (string, required): Product Version 的 ID。 minLength: 1
- `candidates.name` (string, required): 展示名称，不作为唯一标识。 minLength: 1 · maxLength: 120
- `candidates.action` (enum, required): 本次业务动作。不同动作的目标字段和数量要求不同，见请求结构与示例。 Values: `"purchase"`, `"upgrade"`, `"addon"`, `"topup"`.
- `candidates.membershipId` (string | null, required): 会员的 ID。
- `candidates.featureQuantities` (object, optional): 按内部 Feature ID 记录的预付 Feature 数量。
- `candidates.currency` (enum, required): 已发布价格支持的币种代码。 Values: `"USD"`, `"EUR"`, `"CNY"`, `"GBP"`, `"JPY"`, `"CAD"`, `"AUD"`, `"NZD"`, `"CHF"`, `"HKD"`, `"SGD"`, `"KRW"`, `"INR"`, `"BRL"`, `"MXN"`, `"TWD"`, `"THB"`, `"IDR"`, `"MYR"`, `"PHP"`, `"VND"`, `"ZAR"`, `"AED"`, `"SAR"`, `"SEK"`, `"NOK"`, `"DKK"`, `"PLN"`, `"CZK"`, `"HUF"`, `"ILS"`, `"TRY"`, `"RON"`.
- `candidates.amountMinor` (string, required): 金额，以最小货币单位计（如美分）。 pattern: ^(0|[1-9]\d{0,8})$
- `candidates.interval` (enum, required): 周期，取值见允许值。 Values: `"free"`, `"week"`, `"month"`, `"year"`, `"one_time"`.
- `candidates.intervalCount` (integer, required): 收费周期的次数：week 最多 156、month 最多 36、year 最多 3，省略为 1；free 和 one_time 不接受。额度重置周期独立。 min: 1 · max: 156
- `availability` (array, required): 可购买状态。 maxItems: 20
- `availability.productVersionId` (uuid, required): Product Version 的 ID。
- `availability.available` (boolean, required): 可用数量。
- `availability.reason` (enum | null, required): 拒绝原因或账本操作说明；结合 accepted / 状态判断结果。
- `check` (object | null, required): 权益检查结果。
- `check.internal_feature_id` (string, required): Feature 的 ID。 minLength: 1
- `check.name` (string, required): 展示名称，不作为唯一标识。 minLength: 1 · maxLength: 120
- `check.kind` (enum, required): 类型，取值见允许值。 Values: `"access"`, `"metered"`, `"credit"`.
- `check.quantity` (string, required): 数量，十进制字符串，避免浮点误差。 pattern: ^(0|[1-9]\d{0,11})(\.\d{1,6})?$
- `check.allowed` (boolean, required): 当前评估时是否允许访问此 Feature。仅用于展示，业务操作仍需后端授权。
- `check.observation` (object, optional): 本次检查的权威来源、版本和观察时间；商户判定时必须返回。
- `check.observation.subject` (object, required): 本次权益判定对应的 App、环境和 Customer；须与原请求一致。
- `check.observation.subject.scope` (object, required): 响应实际所属的 App 和环境。Header 只能核对，不能切换 Key 范围。
- `check.observation.subject.customerId` (uuid, required): Customer 的 UUID。
- `check.observation.authority` (enum, required): 给出这次判定的权威方。 Values: `"longplay"`, `"merchant"`.
- `check.observation.observedAt` (date-time, required): 时间（ISO 8601）。
- `check.observation.sourceRevision` (string, required): 权威数据的版本，用来判断结果是否过期。 minLength: 1 · maxLength: 128
- `check.observation.requirementDigest` (string, required): 原检查要求的摘要；响应必须和原要求一致。 pattern: ^[a-f0-9]{64}$
- `check.reason` (enum | null, required): 拒绝原因或账本操作说明；结合 accepted / 状态判断结果。
- `check.preview` (object | null, required): 访问被拒绝且请求带有 `commerce` 时的购买建议。Customer 持有该 Feature（或为其供额的积分 Feature）的余额时 `scenario` 为 `usage_limit`，否则为 `feature_flag`。`candidates` 按顺序列出最多 20 个授予该 Feature 或积分 Feature 的 Product，按 `commerce.currency` 计价：先是 Customer 未持有、相对其当前主 Product 属于升级的主 Product（未持有主 Product 时为全部主 Product），再是其未持有的附加 Product 和一次性附加 Product。在该币种下没有单一固定价格的 Product 不会列出；传入 `paywallId` 或通过 Paywall、Offering 检查时只列出其展示的 Product，因此 `candidates` 可能为空。`allowed` 为 `true` 或请求未带 `commerce` 时为 `null`。
- `check.preview.scenario` (enum, required): 场景，取值见允许值。 Values: `"feature_flag"`, `"usage_limit"`.
- `check.preview.candidates` (array, required): 候选 Product。 maxItems: 20
- `check.preview.candidates.productId` (string, required): Product 的 ID。 minLength: 1
- `check.preview.candidates.productVersionId` (string, required): Product Version 的 ID。 minLength: 1
- `check.preview.candidates.name` (string, required): 展示名称，不作为唯一标识。 minLength: 1 · maxLength: 120
- `check.preview.candidates.action` (enum, required): 本次业务动作。不同动作的目标字段和数量要求不同，见请求结构与示例。 Values: `"purchase"`, `"upgrade"`, `"addon"`, `"topup"`.
- `check.preview.candidates.membershipId` (string | null, required): 会员的 ID。
- `check.preview.candidates.featureQuantities` (object, optional): 按内部 Feature ID 记录的预付 Feature 数量。
- `check.preview.candidates.currency` (enum, required): 已发布价格支持的币种代码。 Values: `"USD"`, `"EUR"`, `"CNY"`, `"GBP"`, `"JPY"`, `"CAD"`, `"AUD"`, `"NZD"`, `"CHF"`, `"HKD"`, `"SGD"`, `"KRW"`, `"INR"`, `"BRL"`, `"MXN"`, `"TWD"`, `"THB"`, `"IDR"`, `"MYR"`, `"PHP"`, `"VND"`, `"ZAR"`, `"AED"`, `"SAR"`, `"SEK"`, `"NOK"`, `"DKK"`, `"PLN"`, `"CZK"`, `"HUF"`, `"ILS"`, `"TRY"`, `"RON"`.
- `check.preview.candidates.amountMinor` (string, required): 金额，以最小货币单位计（如美分）。 pattern: ^(0|[1-9]\d{0,8})$
- `check.preview.candidates.interval` (enum, required): 周期，取值见允许值。 Values: `"free"`, `"week"`, `"month"`, `"year"`, `"one_time"`.
- `check.preview.candidates.intervalCount` (integer, required): 收费周期的次数：week 最多 156、month 最多 36、year 最多 3，省略为 1；free 和 one_time 不接受。额度重置周期独立。 min: 1 · max: 156

<a id="errors"></a>

## 错误

失败时返回错误对象，按 `error.code` 与 `error.recovery` 处理。下表是这个接口的凭据、作用域和幂等字段决定的错误；视请求内容还可能返回 `NOT_FOUND`（引用的资源不存在）、`NOT_CONFIGURED`（所需的集成未连接）或 `PAYLOAD_TOO_LARGE`。有些拒绝（如余额不足）不是错误，而是成功响应里的 `reason`。

**可能的错误**

| code | HTTP | 何时 | recovery |
| --- | --- | --- | --- |
| `INVALID_REQUEST` | 400 | 请求不符合本页的参数。缺少或无法解析协议头时分别返回 `UNSUPPORTED_PROTOCOL_VERSION` 或 `INVALID_PROTOCOL_VERSION`。 | `fix_request` |
| `UNAUTHENTICATED` | 401 | 凭据缺失、无效、过期或已撤销。 | `authenticate` |
| `FORBIDDEN` | 403 | 凭据没有 `entitlements:read` 权限。 | `check_permissions` |
| `SCOPE_MISMATCH` | 403 | `X-Indiana-Project` 或 `X-Indiana-Environment` 与凭据所属的 App 和环境不一致。 | `check_scope` |
| `RATE_LIMITED` | 429 | 请求过多；按 `Retry-After` 等待后用同一请求重试。 | `retry_same_request` |
| `SERVICE_UNAVAILABLE` | 503 | 服务暂时不可用；用同一请求重试。 | `retry_same_request` |
| `INTERNAL_ERROR` | 500 | 结果未知；稍后重试。 | `recover_operation` |

- [错误码与恢复](https://www.longplayai.com/zh-hans/docs/api/errors.md)
- [请求约定](https://www.longplayai.com/zh-hans/docs/api.md)
- [全部接口](https://www.longplayai.com/zh-hans/docs/api/operations.md#access)
