> ## 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.

# 校验结账会话

> 校验已完成的结账会话并确认积分已入账。

## 概述

支付成功后，可调用此接口确认付款已处理且积分已入账。适用于 Webhook 未及时触达等场景的手动校验。

<Note>
  积分通常由 Stripe Webhook 自动入账。本接口用于边界情况或客户端侧的额外确认。
</Note>

<Note>
  \*\*第三方集成：\*\*请改用 `GET /credits/verify-session/{sessionId}/api` 并以 API Key 鉴权。
</Note>

## 认证

<ParamField header="Authorization" type="string" required>
  JWT 访问令牌（会话鉴权）。格式：`Bearer YOUR_ACCESS_TOKEN`
</ParamField>

## 路径参数

<ParamField path="sessionId" type="string" required>
  `/credits/checkout` 返回的 Stripe Checkout 会话 ID。
</ParamField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X GET https://api.llmgenerator.com/api/v1/credits/verify-session/cs_test_a1b2c3d4e5f6 \
    -H "Authorization: Bearer YOUR_ACCESS_TOKEN"
  ```

  ```javascript JavaScript theme={null}
  const sessionId = 'cs_test_a1b2c3d4e5f6';
  const response = await fetch(
    `https://api.llmgenerator.com/api/v1/credits/verify-session/${sessionId}`,
    {
      headers: {
        'Authorization': 'Bearer YOUR_ACCESS_TOKEN'
      }
    }
  );

  const data = await response.json();
  if (data.success) {
    console.log(`Added ${data.credits} credits`);
  }
  ```

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

  session_id = 'cs_test_a1b2c3d4e5f6'
  response = requests.get(
      f'https://api.llmgenerator.com/api/v1/credits/verify-session/{session_id}',
      headers={'Authorization': 'Bearer YOUR_ACCESS_TOKEN'}
  )
  data = response.json()
  print(f"Verified: {data['credits']} credits")
  ```
</RequestExample>

<ResponseExample>
  ```json Success theme={null}
  {
    "success": true,
    "credits": 500,
    "sessionId": "cs_test_a1b2c3d4e5f6"
  }
  ```

  ```json Payment Not Completed theme={null}
  {
    "success": false,
    "message": "Payment not completed"
  }
  ```
</ResponseExample>

## 响应字段

<ResponseField name="success" type="boolean">
  会话校验是否成功。
</ResponseField>

<ResponseField name="credits" type="integer">
  本次购买对应的积分数。
</ResponseField>

<ResponseField name="sessionId" type="string">
  已校验的会话 ID。
</ResponseField>

## 错误响应

<ResponseField name="400">
  错误请求——支付未完成或会话元数据无效。
</ResponseField>

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

<ResponseField name="403">
  禁止访问——会话不属于当前认证用户。
</ResponseField>

## 幂等性

本接口具有幂等性。若积分已通过 Webhook 入账，不会重复增加；仍会返回成功及对应积分数量。

## 典型流程

1. 用户在 Stripe 完成结账
2. Stripe 将用户重定向到你的 `successUrl`
3. 调用本接口校验并确认积分
4. 向用户展示确认信息
5. 在前端刷新积分余额展示

<Tip>
  虽然可以使用本接口，推荐仍以 Stripe Webhook 为准，并结合 `/credits/balance` 更新界面。
</Tip>

***

## GET /credits/verify-session/{sessionId}/api

使用 API Key 校验结账会话，适合第三方集成。

### 认证

需要 API Key：

```
Authorization: Bearer llmgen_your_api_key_here
```

### 路径参数

同上。

### 响应

同上。

<RequestExample>
  ```bash cURL theme={null}
  curl -X GET https://api.llmgenerator.com/api/v1/credits/verify-session/cs_test_a1b2c3d4e5f6/api \
    -H "Authorization: Bearer llmgen_your_api_key_here"
  ```

  ```javascript JavaScript theme={null}
  const sessionId = 'cs_test_a1b2c3d4e5f6';
  const response = await fetch(
    `https://api.llmgenerator.com/api/v1/credits/verify-session/${sessionId}/api`,
    {
      headers: {
        'Authorization': 'Bearer llmgen_your_api_key_here'
      }
    }
  );

  const data = await response.json();
  if (data.success) {
    console.log(`Added ${data.credits} credits`);
  }
  ```

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

  session_id = 'cs_test_a1b2c3d4e5f6'
  response = requests.get(
      f'https://api.llmgenerator.com/api/v1/credits/verify-session/{session_id}/api',
      headers={'Authorization': 'Bearer llmgen_your_api_key_here'}
  )
  data = response.json()
  print(f"Verified: {data['credits']} credits")
  ```
</RequestExample>
