> ## 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 Checkout 会话。

## POST /credits/checkout

创建 Stripe Checkout 会话以购买积分套餐。需要登录后的 JWT 访问令牌。

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

### 请求体

<ParamField body="packageId" type="string" required>
  要购买的套餐 ID，可用列表见 `/credits/packages`。
</ParamField>

<ParamField body="successUrl" type="string">
  支付成功后的跳转 URL，默认回到控制台并带成功标记。
</ParamField>

<ParamField body="cancelUrl" type="string">
  用户取消支付时的跳转 URL，默认回到控制台并带取消标记。
</ParamField>

### 响应

<ResponseField name="sessionId" type="string">
  Stripe Checkout 会话 ID。
</ResponseField>

<ResponseField name="url" type="string">
  Stripe 结账页 URL，请将用户重定向到此地址完成支付。
</ResponseField>

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

  <Expandable title="package 字段">
    <ResponseField name="id" type="string">
      套餐标识。
    </ResponseField>

    <ResponseField name="name" type="string">
      套餐名称。
    </ResponseField>

    <ResponseField name="credits" type="number">
      套餐内积分数量。
    </ResponseField>

    <ResponseField name="priceCents" type="number">
      价格（美分）。
    </ResponseField>
  </Expandable>
</ResponseField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api.llmgenerator.com/api/v1/credits/checkout \
    -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
          "packageId": "pkg_pro",
          "successUrl": "https://example.com/success?session_id={CHECKOUT_SESSION_ID}",
          "cancelUrl": "https://example.com/cancel"
        }'
  ```

  ```javascript Node.js theme={null}
  const response = await fetch('https://api.llmgenerator.com/api/v1/credits/checkout', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_ACCESS_TOKEN',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      packageId: 'pkg_pro'
    })
  });

  const data = await response.json();
  // Redirect user to data.url
  window.location.href = data.url;
  ```
</RequestExample>

<ResponseExample>
  ```json Success (200) theme={null}
  {
    "sessionId": "cs_test_a1b2c3d4e5f6g7h8i9j0",
    "url": "https://checkout.stripe.com/c/pay/cs_test_a1b2c3d4e5f6g7h8i9j0",
    "package": {
      "id": "pkg_pro",
      "name": "Pro Pack",
      "credits": 500,
      "priceCents": 1999
    }
  }
  ```

  ```json Package Not Found (404) theme={null}
  {
    "message": "Credit package not found"
  }
  ```
</ResponseExample>

***

## POST /credits/checkout/api

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

### 认证

需要 API Key：

```
Authorization: Bearer llmgen_your_api_key_here
```

### 请求体

与 `/credits/checkout` 相同。

### 响应

与 `/credits/checkout` 相同。

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api.llmgenerator.com/api/v1/credits/checkout/api \
    -H "Authorization: Bearer llmgen_your_api_key_here" \
    -H "Content-Type: application/json" \
    -d '{
          "packageId": "pkg_pro",
          "successUrl": "https://yourapp.com/success?session_id={CHECKOUT_SESSION_ID}",
          "cancelUrl": "https://yourapp.com/cancel"
        }'
  ```

  ```javascript Node.js theme={null}
  const response = await fetch('https://api.llmgenerator.com/api/v1/credits/checkout/api', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer llmgen_your_api_key_here',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      packageId: 'pkg_pro',
      successUrl: 'https://yourapp.com/success?session_id={CHECKOUT_SESSION_ID}',
      cancelUrl: 'https://yourapp.com/cancel'
    })
  });

  const data = await response.json();
  // Redirect user to data.url in your app
  ```

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

  response = requests.post(
      'https://api.llmgenerator.com/api/v1/credits/checkout/api',
      headers={
          'Authorization': 'Bearer llmgen_your_api_key_here',
          'Content-Type': 'application/json'
      },
      json={
          'packageId': 'pkg_pro',
          'successUrl': 'https://yourapp.com/success',
          'cancelUrl': 'https://yourapp.com/cancel'
      }
  )

  data = response.json()
  ```
</RequestExample>
