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

# 创建订阅结账

> 创建 Stripe 结账会话以订阅套餐。

## 概述

创建用于订阅套餐的 Stripe Checkout 会话，返回需将用户重定向到的支付 URL。需要 JWT 访问令牌（会话鉴权）。

<Note>
  \*\*第三方集成：\*\*请改用 `POST /subscriptions/checkout/api` 并以 API Key 鉴权。
</Note>

## 认证

<ParamField header="Authorization" type="string" required>
  JWT 访问令牌。格式：`Bearer YOUR_ACCESS_TOKEN`
</ParamField>

## 请求体

<ParamField body="planId" type="string" required>
  要订阅的套餐标识，须为 `starter`、`professional`、`business` 或 `agency` 之一。
</ParamField>

<ParamField body="billingCycle" type="string" default="monthly">
  计费频率：`monthly` 或 `yearly`。
</ParamField>

<ParamField body="successUrl" type="string">
  支付成功后的跳转 URL，默认返回应用控制台。
</ParamField>

<ParamField body="cancelUrl" type="string">
  用户取消支付时的跳转 URL，默认定价页。
</ParamField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api.llmgenerator.com/api/v1/subscriptions/checkout \
    -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
      "planId": "professional",
      "successUrl": "https://yourapp.com/subscription/success",
      "cancelUrl": "https://yourapp.com/pricing"
    }'
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch('https://api.llmgenerator.com/api/v1/subscriptions/checkout', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_ACCESS_TOKEN',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      planId: 'professional',
      successUrl: 'https://yourapp.com/subscription/success',
      cancelUrl: 'https://yourapp.com/pricing'
    })
  });

  const data = await response.json();
  // Redirect user to Stripe checkout
  window.location.href = data.url;
  ```

  ```python Python theme={null}
  import requests

  response = requests.post(
      'https://api.llmgenerator.com/api/v1/subscriptions/checkout',
      headers={
          'Authorization': 'Bearer YOUR_ACCESS_TOKEN',
          'Content-Type': 'application/json'
      },
      json={
          'planId': 'professional',
          'successUrl': 'https://yourapp.com/subscription/success',
          'cancelUrl': 'https://yourapp.com/pricing'
      }
  )
  data = response.json()
  print(f"Redirect to: {data['url']}")
  ```
</RequestExample>

<ResponseExample>
  ```json Response theme={null}
  {
    "sessionId": "cs_test_a1b2c3d4e5f6g7h8i9j0",
    "url": "https://checkout.stripe.com/c/pay/cs_test_a1b2c3d4...",
    "plan": {
      "id": "professional",
      "name": "Professional",
      "monthlyCredits": 3000,
      "priceCents": 1299
    }
  }
  ```
</ResponseExample>

## 响应字段

<ResponseField name="sessionId" type="string">
  Stripe Checkout 会话 ID，可用于后续校验。
</ResponseField>

<ResponseField name="url" type="string">
  用户完成支付的 Stripe 托管结账页 URL。
</ResponseField>

<ResponseField name="plan" type="object">
  所选套餐详情。
</ResponseField>

## 结账流程

1. **创建会话**：调用本接口并传入目标套餐
2. **重定向用户**：跳转到返回的 `url`
3. **支付处理**：用户在 Stripe 完成支付
4. **Webhook**：Stripe 通知你的 Webhook 端点
5. **成功跳转**：用户被重定向到 `successUrl`
6. **校验（可选）**：通过 `/subscriptions/current` 核对订阅状态

## 错误响应

<ResponseField name="400">
  错误请求——套餐 ID 无效，或用户已有活跃订阅。
</ResponseField>

<ResponseField name="401">
  未授权——令牌无效或缺失。
</ResponseField>

<Note>
  若用户已有活跃订阅，更换套餐应使用账单门户（`/subscriptions/portal`）。
</Note>

***

## POST /subscriptions/checkout/api

使用 API Key 创建 Stripe Checkout 会话，适合第三方集成。

### 认证

需要 API Key：

```
Authorization: Bearer llmgen_your_api_key_here
```

### 请求体

与 `/subscriptions/checkout` 相同。

### 响应

与 `/subscriptions/checkout` 相同。

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api.llmgenerator.com/api/v1/subscriptions/checkout/api \
    -H "Authorization: Bearer llmgen_your_api_key_here" \
    -H "Content-Type: application/json" \
    -d '{
      "planId": "professional",
      "successUrl": "https://yourapp.com/subscription/success",
      "cancelUrl": "https://yourapp.com/pricing"
    }'
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch('https://api.llmgenerator.com/api/v1/subscriptions/checkout/api', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer llmgen_your_api_key_here',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      planId: 'professional',
      successUrl: 'https://yourapp.com/subscription/success',
      cancelUrl: 'https://yourapp.com/pricing'
    })
  });

  const data = await response.json();
  // Redirect user to Stripe checkout
  window.location.href = data.url;
  ```

  ```python Python theme={null}
  import requests

  response = requests.post(
      'https://api.llmgenerator.com/api/v1/subscriptions/checkout/api',
      headers={
          'Authorization': 'Bearer llmgen_your_api_key_here',
          'Content-Type': 'application/json'
      },
      json={
          'planId': 'professional',
          'successUrl': 'https://yourapp.com/subscription/success',
          'cancelUrl': 'https://yourapp.com/pricing'
      }
  )
  data = response.json()
  print(f"Redirect to: {data['url']}")
  ```
</RequestExample>
