# 列出购买记录

Operation / SDK: `listPurchases` · `list_purchases` · `ListPurchases`

分页列出购买记录及其状态。

更新于 2026-11-06

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

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

## 请求

- **凭据**: Server Key，权限 `billing:read`。只在后端调用。
- **SDK**: JavaScript `createLongplayServer().listPurchases` · Python `list_purchases` · Go `ListPurchases`
- **成功状态**: HTTP 200

响应为示例数据。

**cURL: request.sh**

```bash
curl -X POST "$LONGPLAY_API_URL/api/v1/purchases/list" \
  -H "Authorization: Bearer $LONGPLAY_SERVER_KEY" \
  -H "X-Indiana-Protocol: 0.10" \
  -H "Content-Type: application/json" \
  -d '{
  "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d",
  "limit": 20
}'
```

**TypeScript: listPurchases.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 listPurchases() {
  return longplay.listPurchases({
    customerId: "8b258f8b-4228-40fa-8d24-5b16428e686d",
    limit: 20,
  });
}
```

**请求体: request.json**

```json
{
  "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d",
  "limit": 20
}
```

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

```json
{
  "requestId": "44f5fcb8-6ed8-41b8-818f-7571941c5fd5",
  "scope": {
    "projectId": "df5426d6-b335-403c-8f3e-b0f73d2e3511",
    "environment": "sandbox"
  },
  "purchases": {
    "items": [
      {
        "id": "367c366c-4b1c-43be-8afd-da55210a0415",
        "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d",
        "entityId": "3c287fb0-fbf3-4e56-8a3b-0cad92605411",
        "productId": "1Example",
        "productVersionId": "8c489a1a-5542-44fe-8e80-da2f6a319d01",
        "name": "Pro",
        "provider": "stripe",
        "createdAt": "2026-10-03T12:00:00.000Z",
        "status": "active",
        "orderId": "1c5f6225-fd1a-4972-8a9d-b546c2158aed",
        "transactionId": "e888bc71-3de0-425a-89b3-3d92a686b67d",
        "currency": "USD",
        "amountMinor": "1"
      }
    ],
    "nextCursor": {
      "createdAt": "2026-10-03T12:00:00.000Z",
      "id": "82072b10-12fb-46f6-83a9-1e51025445ed"
    }
  }
}
```

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

## 请求体参数

**application/json**

- `customerId` (uuid, optional): Customer 的 UUID。
- `entityId` (uuid, optional): 当前客户范围内的 Entity ID；只用于已配置的 Entity 和许可 Feature。
- `productId` (string, optional): Product 的 ID。 minLength: 1 · maxLength: 256
- `provider` (enum, optional): 支付渠道，取值见允许值。 Values: `"stripe"`, `"apple"`, `"google"`, `"merchant"`, `"free"`.
- `status` (enum, optional): 状态，取值见允许值。 Values: `"trial"`, `"active"`, `"past_due"`, `"ended"`, `"refunded"`.
- `cursor` (object, optional): 上一页返回的不透明游标；不要修改或跨客户复用。
- `cursor.createdAt` (date-time, required): 记录创建时的 ISO 8601 UTC 时间。
- `cursor.id` (uuid, required): 上一页 `nextCursor` 中的 `id`；请原样传回整个 `nextCursor` 对象。
- `limit` (integer, optional): 一页返回的最大记录数量，受契约上限限制。 min: 1 · max: 100

<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"`.
- `purchases` (object, required): 符合筛选条件的一次性 Product 购买的一页结果，按时间倒序；用 `nextCursor` 读取下一页。
- `purchases.items` (array, required): 列表项。 maxItems: 100
- `purchases.items.id` (uuid, required): 该列表项的 ID。在订阅和购买列表中为会员 ID（`membershipId`）；在订阅冲突列表中为被阻塞订单的 ID（可作为 `orderId` 使用）；在发票模板列表中为模板 ID 字符串（不一定是 UUID，可作为 `invoice_template_id` 使用），它不是分页游标。
- `purchases.items.customerId` (uuid, required): Customer 的 UUID。
- `purchases.items.entityId` (uuid | null, required): 当前客户范围内的 Entity ID；只用于已配置的 Entity 和许可 Feature。
- `purchases.items.productId` (string, required): Product 的 ID。 minLength: 1 · maxLength: 256
- `purchases.items.productVersionId` (uuid, required): Product Version 的 ID。
- `purchases.items.name` (string, required): 展示名称，不作为唯一标识。 minLength: 1 · maxLength: 120
- `purchases.items.provider` (enum, required): 支付渠道，取值见允许值。 Values: `"stripe"`, `"apple"`, `"google"`, `"merchant"`, `"free"`.
- `purchases.items.createdAt` (date-time, required): 记录创建时的 ISO 8601 UTC 时间。
- `purchases.items.status` (enum, required): 状态，取值见允许值。 Values: `"active"`, `"ended"`, `"refunded"`.
- `purchases.items.orderId` (uuid | null, required): 订单的 ID。
- `purchases.items.transactionId` (uuid | null, required): 原消费返回的账本交易 UUID，退款必须引用这个记录。
- `purchases.items.currency` (enum | null, required): 已发布价格支持的币种代码。
- `purchases.items.amountMinor` (string | null, required): 金额，以最小货币单位计（如美分）。
- `purchases.nextCursor` (object | null, required): 下一页游标；null 表示结束。原样传回，改筛选条件时清空。
- `purchases.nextCursor.createdAt` (date-time, required): 记录创建时的 ISO 8601 UTC 时间。
- `purchases.nextCursor.id` (uuid, required): 本页最后一行的 ID。读取下一页时，原样传回整个 `nextCursor` 对象。

<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 | 凭据没有 `billing: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#commerce)
