# 用优惠码领取优惠

Operation / SDK: `redeemCoupon`

Customer 输入券码兑换优惠。

更新于 2026-11-05

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

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

## 请求

- **凭据**: Customer Token，权限 `offers:resolve`。以 Token 对应的 Customer 身份执行。
- **SDK**: JavaScript `createLongplay().redeemCoupon`
- **成功状态**: HTTP 200

响应为示例数据。

**cURL: request.sh**

```bash
curl -X POST "$LONGPLAY_API_URL/api/v1/offers/redeem" \
  -H "Authorization: Bearer $LONGPLAY_CUSTOMER_TOKEN" \
  -H "X-Indiana-Protocol: 0.10" \
  -H "Content-Type: application/json" \
  -d '{
  "code": "WELCOME10",
  "requestId": "0f58d1f8-069f-4288-8ec1-4ed10af55b45"
}'
```

**请求体: request.json**

```json
{
  "code": "WELCOME10",
  "requestId": "0f58d1f8-069f-4288-8ec1-4ed10af55b45"
}
```

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

```json
{
  "requestId": "44f5fcb8-6ed8-41b8-818f-7571941c5fd5",
  "scope": {
    "projectId": "df5426d6-b335-403c-8f3e-b0f73d2e3511",
    "environment": "sandbox"
  },
  "offer": {
    "id": "1a59cc8f-4bb2-4d16-81df-d804d72b0c61",
    "couponId": "a08847c9-0351-4b7a-8e23-b13019d008b9",
    "status": "issued",
    "issuedAt": "2026-10-03T12:00:00.000Z",
    "expiresAt": "2099-01-01T00:00:00.000Z",
    "coupon": {
      "name": "Pro",
      "discount": {
        "type": "percent",
        "percentOff": 1
      },
      "duration": {
        "type": "once"
      },
      "appliesTo": {
        "type": "all"
      }
    }
  },
  "reason": "claim_disabled"
}
```

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

## 请求体参数

**application/json**

- `code` (string, required): 券码或兑换码。 pattern: ^[A-Z0-9][A-Z0-9_-]{2,31}$
- `requestId` (uuid, required): 你的业务请求 UUID；同一次操作重试复用，新的操作生成新值。

<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"`.
- `offer` (object | null, required): 优惠。
- `offer.id` (uuid, required): 优惠 的 ID。
- `offer.couponId` (uuid, required): 当前 App 和环境中的优惠券 UUID。
- `offer.status` (enum, required): 状态，取值见允许值。 Values: `"issued"`, `"reserved"`, `"redeemed"`, `"revoked"`, `"expired"`.
- `offer.issuedAt` (date-time, required): 时间（ISO 8601）。
- `offer.expiresAt` (date-time, required): 到期时间（ISO 8601，UTC）。
- `offer.coupon` (object, required): 优惠券。
- `offer.coupon.name` (string, required): 展示名称，不作为唯一标识。
- `offer.coupon.discount` (object | object, required): 折扣。
- `offer.coupon.discount.type` ("percent" | "amount", required): 折扣类型：`percent` 按 `percentOff` 的百分比减免；`amount` 减免 `amountOffCents`，以 `currency` 的最小货币单位计。
- `offer.coupon.discount.percentOff` (number, optional): 折扣百分比。 min: 1 · max: 100
- `offer.coupon.discount.amountOffCents` (integer, optional): 减免金额（最小货币单位）。 min: 1 · max: 100000000
- `offer.coupon.discount.currency` (enum, optional): 已发布价格支持的币种代码。 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"`.
- `offer.coupon.duration` (object | object | object, required): 持续时长。
- `offer.coupon.duration.type` ("once" | "repeating" | "forever", required): 适用时长：`once` 仅适用于一张发票，`repeating` 适用 `months` 个月，`forever` 持续适用。
- `offer.coupon.duration.months` (integer, optional): 月数。 min: 1 · max: 36
- `offer.coupon.nativeDiscountPolicy` (enum, optional): Coupon 配置的原生折扣处理策略：replace 为替换，stack_native 为叠加。 Values: `"replace"`, `"stack_native"`.
- `offer.coupon.appliesTo` (object | object | object, required): 适用范围。
- `offer.coupon.appliesTo.type` ("all" | "plans" | "merchandise", required): `all`：优惠券适用于所有 Product；`plans`：仅适用于 `planIds` 中列出的 Product。
- `offer.coupon.appliesTo.planIds` (array, optional): Product 的 ID 列表。 minItems: 1 · maxItems: 50
- `offer.coupon.appliesTo.scope` (object, optional): 响应实际所属的 App 和环境。Header 只能核对，不能切换 Key 范围。
- `reason` (enum | null, required): 拒绝原因或账本操作说明；结合 accepted / 状态判断结果。

<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 | 凭据没有 `offers:resolve` 权限。 | `check_permissions` |
| `SCOPE_MISMATCH` | 403 | `X-Indiana-Project` 或 `X-Indiana-Environment` 与凭据所属的 App 和环境不一致。 | `check_scope` |
| `CONFLICT` | 409 | 同一个 `requestId` 用了不同的输入（`reason: IDEMPOTENCY_CONFLICT`）。 | `resolve_conflict` |
| `RATE_LIMITED` | 429 | 请求过多；按 `Retry-After` 等待后用同一请求重试。 | `retry_same_request` |
| `SERVICE_UNAVAILABLE` | 503 | 服务暂时不可用；用同一请求重试。 | `retry_same_request` |
| `INTERNAL_ERROR` | 500 | 结果未知；先读取原操作，或用原 ID 和原输入重放。 | `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#offers)
