2026-03-01 00:41:38 +08:00
# Sub2API Admin API: Payment Integration
2026-03-01 00:41:38 +08:00
2026-03-01 00:41:38 +08:00
This document describes the minimum Admin API surface for external payment systems (for example, sub2apipay) to complete balance top-up and reconciliation.
2026-03-01 00:57:26 +08:00
2026-03-01 00:41:38 +08:00
## Base URL
2026-03-01 00:57:26 +08:00
2026-03-01 00:41:38 +08:00
- Production: `https://<your-domain>`
- Beta: `http://<your-server-ip>:8084`
2026-03-01 00:57:26 +08:00
2026-03-01 00:41:38 +08:00
All endpoints below use:
2026-03-01 00:57:26 +08:00
2026-03-01 00:41:38 +08:00
- Header: `x-api-key: admin-<64hex>` (recommended for server-to-server)
- Header: `Content-Type: application/json`
2026-03-01 00:57:26 +08:00
2026-03-01 00:41:38 +08:00
Note: Admin JWT is also accepted by admin routes, but machine-to-machine integration should use admin API key.
2026-03-01 00:57:26 +08:00
2026-03-01 00:41:38 +08:00
## 1) Create + Redeem in One Step
2026-03-01 00:57:26 +08:00
`POST /api/v1/admin/redeem-codes/create-and-redeem`
2026-03-01 00:41:38 +08:00
Purpose:
- Atomically create a deterministic redeem code and redeem it to a target user.
- Typical usage: called after payment callback succeeds.
Required headers:
2026-03-01 00:57:26 +08:00
- `x-api-key`
- `Idempotency-Key`
2026-03-01 00:41:38 +08:00
Request body:
2026-03-01 00:57:26 +08:00
```json
{
"code": "s2p_cm1234567890",
"type": "balance",
"value": 100.0,
"user_id": 123,
"notes": "sub2apipay order: cm1234567890"
}
```
2026-03-01 00:41:38 +08:00
Rules:
2026-03-01 00:57:26 +08:00
2026-03-01 00:41:38 +08:00
- `code` : external deterministic order-mapped code.
- `type` : currently recommended `balance` .
- `value` : must be `> 0` .
- `user_id` : target user id.
2026-03-01 00:57:26 +08:00
2026-03-01 00:41:38 +08:00
Idempotency semantics:
2026-03-01 00:57:26 +08:00
2026-03-01 00:41:38 +08:00
- Same `code` , same `used_by` user: return `200` (idempotent replay).
- Same `code` , different `used_by` user: return `409` conflict.
- Missing `Idempotency-Key` : return `400` (`IDEMPOTENCY_KEY_REQUIRED` ).
2026-03-01 00:41:38 +08:00
2026-03-01 00:41:38 +08:00
Example:
2026-03-01 00:41:38 +08:00
```bash
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"
}'
```
2026-03-01 00:41:38 +08:00
## 2) Query User (Optional Pre-check)
2026-03-01 00:41:38 +08:00
`GET /api/v1/admin/users/:id`
2026-03-01 00:41:38 +08:00
Purpose:
- Check whether target user exists before payment finalize/retry.
Example:
2026-03-01 00:41:38 +08:00
```bash
curl -s "${BASE}/api/v1/admin/users/123" \
-H "x-api-key: ${KEY}"
```
2026-03-01 00:41:38 +08:00
## 3) Balance Adjustment (Existing Interface)
2026-03-01 00:41:38 +08:00
`POST /api/v1/admin/users/:id/balance`
2026-03-01 00:41:38 +08:00
Purpose:
- Existing reusable admin interface for manual correction.
- Supports `set` , `add` , `subtract` .
2026-03-01 00:41:38 +08:00
Request body example (`subtract` ):
2026-03-01 00:41:38 +08:00
2026-03-01 00:41:38 +08:00
```json
{
"balance": 100.0,
"operation": "subtract",
"notes": "manual correction"
}
```
2026-03-01 00:41:38 +08:00
Example:
2026-03-01 00:41:38 +08:00
```bash
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"
}'
```
2026-03-01 00:41:38 +08:00
## 4) Error Handling Recommendations
2026-03-01 00:57:26 +08:00
2026-03-01 00:41:38 +08:00
- Persist upstream payment result independently from recharge result.
- Mark payment success immediately after callback verification.
- If recharge fails after payment success, keep order retryable by admin operation.
- For retry, always reuse deterministic `code` + new `Idempotency-Key` .
## 5) Suggested `doc_url` Setting
Sub2API already supports `doc_url` in system settings.
2026-03-01 00:57:26 +08:00
2026-03-01 00:41:38 +08:00
Recommended values:
2026-03-01 00:41:38 +08:00
- View URL: `https://github.com/Wei-Shaw/sub2api/blob/main/ADMIN_PAYMENT_INTEGRATION_API.md`
2026-03-01 00:41:38 +08:00
- Direct download URL: `https://raw.githubusercontent.com/Wei-Shaw/sub2api/main/ADMIN_PAYMENT_INTEGRATION_API.md`