# ADMIN_PAYMENT_INTEGRATION_API
单文件中英双语文档 / Single-file bilingual documentation (Chinese + English)
本文档用于对接外部支付系统(如 sub2apipay)与 Sub2API 的 Admin API,覆盖:
https://<your-domain>http://<your-server-ip>:8084推荐使用:
x-api-key: admin-<64hex>Content-Type: application/jsonIdempotency-Key说明:管理员 JWT 也可访问 admin 路由,但服务间调用建议使用 Admin API Key。
POST /api/v1/admin/redeem-codes/create-and-redeem
用途:原子完成“创建兑换码 + 兑换到指定用户”。
请求头:
x-api-keyIdempotency-Key请求体示例:
{
"code": "s2p_cm1234567890",
"type": "balance",
"value": 100.0,
"user_id": 123,
"notes": "sub2apipay order: cm1234567890"
}
幂等语义:
code 且 used_by 一致:200code 但 used_by 不一致:409Idempotency-Key:400(IDEMPOTENCY_KEY_REQUIRED)curl 示例:
curl -X POST "${BASE}/api/v1/admin/redeem-codes/create-and-redeem" \
-H "x-api-key: ${KEY}" \
-H "Idempotency-Key: pay-cm1234567890-success" \
-H "Content-Type: application/json" \
-d '{
"code":"s2p_cm1234567890",
"type":"balance",
"value":100.00,
"user_id":123,
"notes":"sub2apipay order: cm1234567890"
}'
GET /api/v1/admin/users/:id
curl -s "${BASE}/api/v1/admin/users/123" \
-H "x-api-key: ${KEY}"
POST /api/v1/admin/users/:id/balance
用途:人工补偿 / 扣减,支持 set / add / subtract。
请求体示例(扣减):
{
"balance": 100.0,
"operation": "subtract",
"notes": "manual correction"
}
curl -X POST "${BASE}/api/v1/admin/users/123/balance" \
-H "x-api-key: ${KEY}" \
-H "Idempotency-Key: balance-subtract-cm1234567890" \
-H "Content-Type: application/json" \
-d '{
"balance":100.00,
"operation":"subtract",
"notes":"manual correction"
}'
当 Sub2API 打开 purchase_subscription_url 或用户侧自定义页面 iframe URL 时,会统一追加:
user_idtokentheme(light / dark)lang(例如 zh / en,用于向嵌入页传递当前界面语言)ui_mode(固定 embedded)示例:
https://pay.example.com/pay?user_id=123&token=<jwt>&theme=light&lang=zh&ui_mode=embedded
code,并使用新的 Idempotency-Keydoc_url 配置建议https://github.com/Wei-Shaw/sub2api/blob/main/docs/ADMIN_PAYMENT_INTEGRATION_API.mdhttps://raw.githubusercontent.com/Wei-Shaw/sub2api/main/docs/ADMIN_PAYMENT_INTEGRATION_API.mdThis document describes the minimal Sub2API Admin API surface for external payment integrations (for example, sub2apipay), including:
https://<your-domain>http://<your-server-ip>:8084Recommended headers:
x-api-key: admin-<64hex>Content-Type: application/jsonIdempotency-Key for idempotent endpointsNote: Admin JWT can also access admin routes, but Admin API Key is recommended for server-to-server integration.
POST /api/v1/admin/redeem-codes/create-and-redeem
Use case: atomically create a redeem code and redeem it to a target user.
Headers:
x-api-keyIdempotency-KeyRequest body:
{
"code": "s2p_cm1234567890",
"type": "balance",
"value": 100.0,
"user_id": 123,
"notes": "sub2apipay order: cm1234567890"
}
Idempotency behavior:
code and same used_by: 200code but different used_by: 409Idempotency-Key: 400 (IDEMPOTENCY_KEY_REQUIRED)curl example:
curl -X POST "${BASE}/api/v1/admin/redeem-codes/create-and-redeem" \
-H "x-api-key: ${KEY}" \
-H "Idempotency-Key: pay-cm1234567890-success" \
-H "Content-Type: application/json" \
-d '{
"code":"s2p_cm1234567890",
"type":"balance",
"value":100.00,
"user_id":123,
"notes":"sub2apipay order: cm1234567890"
}'
GET /api/v1/admin/users/:id
curl -s "${BASE}/api/v1/admin/users/123" \
-H "x-api-key: ${KEY}"
POST /api/v1/admin/users/:id/balance
Use case: manual correction with set / add / subtract.
Request body example (subtract):
{
"balance": 100.0,
"operation": "subtract",
"notes": "manual correction"
}
curl -X POST "${BASE}/api/v1/admin/users/123/balance" \
-H "x-api-key: ${KEY}" \
-H "Idempotency-Key: balance-subtract-cm1234567890" \
-H "Content-Type: application/json" \
-d '{
"balance":100.00,
"operation":"subtract",
"notes":"manual correction"
}'
When Sub2API opens purchase_subscription_url or a user-facing custom page iframe URL, it appends:
user_idtokentheme (light / dark)lang (for example zh / en, used to pass the current UI language to the embedded page)ui_mode (fixed: embedded)Example:
https://pay.example.com/pay?user_id=123&token=<jwt>&theme=light&lang=zh&ui_mode=embedded
code for retry, and use a new Idempotency-Keydoc_urlhttps://github.com/Wei-Shaw/sub2api/blob/main/docs/ADMIN_PAYMENT_INTEGRATION_API.mdhttps://raw.githubusercontent.com/Wei-Shaw/sub2api/main/docs/ADMIN_PAYMENT_INTEGRATION_API.md