{
    "openapi": "3.1.0",
    "info": {
        "title": "Ship Xu Tự Động Open API",
        "version": "1.2.0",
        "summary": "REST JSON API đặt đơn Ship Xu tự động (Auto-Radar), Đơn Lớn/Login, đối soát đơn và đồng bộ web con.",
        "description": "Tài liệu chuẩn OpenAPI 3.1 cho hệ thống Ship Xu Tự Động.\n\n- **Xác thực:** Gửi `Authorization: Bearer <API_KEY>` trong header HTTP (không đặt API Key trên query URL).\n- **Chống tạo đơn trùng (Idempotency):** Luôn sinh `request_key` (32 ký tự hex) trước khi gọi `POST /api/ship-xu.php?action=create`.\n- **Timeout lấy mã phòng:** Tối đa **15 giây** cho cả web mẹ (VNMOD) và web con (gọi web mẹ). Quá 15 giây chưa có mã phòng sẽ tự hủy yêu cầu giữ chỗ và trả HTTP `409` với `reason: \"server_busy\"` (ví không bị trừ tiền).\n- **Rate Limit:** 20 req/phút/tài khoản cho `create`; 120 req/phút cho các endpoint đọc (`quote`, `history`, `reconcile`, `capacity`, `public_capacity`, `profile`)."
    },
    "servers": [
        {
            "url": "https://hzstore.vn",
            "description": "Production / Current Server"
        }
    ],
    "tags": [
        {
            "name": "Public",
            "description": "Endpoint công khai: danh mục gói Xu, luồng trống và báo giá"
        },
        {
            "name": "Orders",
            "description": "Tạo đơn Ship Xu, tra cứu lịch sử và đối soát đơn"
        },
        {
            "name": "Account",
            "description": "Thông tin số dư ví & tích hợp web con"
        }
    ],
    "components": {
        "securitySchemes": {
            "bearerAuth": {
                "type": "http",
                "scheme": "bearer",
                "bearerFormat": "API_KEY",
                "description": "API Key 64 ký tự hex lấy từ trang Hồ sơ hoặc Tài liệu API."
            }
        },
        "schemas": {
            "ServerOffer": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "integer",
                        "example": 1
                    },
                    "name": {
                        "type": "string",
                        "example": "Tiêu chuẩn"
                    },
                    "mode": {
                        "type": "string",
                        "enum": [
                            "standard",
                            "login_run"
                        ],
                        "example": "standard"
                    },
                    "enabled": {
                        "type": "boolean",
                        "example": true
                    },
                    "price_per_million": {
                        "type": "integer",
                        "example": 4000
                    },
                    "ordering_available": {
                        "type": "boolean",
                        "example": true
                    }
                }
            },
            "Quote": {
                "type": "object",
                "properties": {
                    "server_id": {
                        "type": "integer",
                        "example": 1
                    },
                    "server_name": {
                        "type": "string",
                        "example": "Tiêu chuẩn"
                    },
                    "mode": {
                        "type": "string",
                        "enum": [
                            "standard",
                            "login_run"
                        ],
                        "example": "standard"
                    },
                    "amount_millions": {
                        "type": "integer",
                        "example": 4
                    },
                    "price_per_million": {
                        "type": "integer",
                        "example": 4000
                    },
                    "total_vnd": {
                        "type": "integer",
                        "example": 16000
                    },
                    "server_accepts_orders": {
                        "type": "boolean",
                        "example": true
                    }
                }
            },
            "WalletSummary": {
                "type": "object",
                "properties": {
                    "balance": {
                        "type": "number",
                        "example": 263436
                    },
                    "held_balance": {
                        "type": "number",
                        "example": 0
                    },
                    "available_balance": {
                        "type": "number",
                        "example": 263436
                    }
                }
            },
            "OrderSummary": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "integer",
                        "example": 1024
                    },
                    "order_code": {
                        "type": [
                            "string",
                            "null"
                        ],
                        "example": "DH_A1B2C3D4"
                    },
                    "server_id": {
                        "type": "integer",
                        "example": 1
                    },
                    "server_name": {
                        "type": "string",
                        "example": "Tiêu chuẩn"
                    },
                    "order_mode": {
                        "type": "string",
                        "enum": [
                            "standard",
                            "login_run"
                        ],
                        "example": "standard"
                    },
                    "amount_millions": {
                        "type": "integer",
                        "example": 4
                    },
                    "delivered_millions": {
                        "type": "number",
                        "example": 4
                    },
                    "total_vnd": {
                        "type": "integer",
                        "example": 16000
                    },
                    "status": {
                        "type": "string",
                        "enum": [
                            "pending",
                            "processing",
                            "completed",
                            "failed",
                            "refunded"
                        ]
                    },
                    "payment_state": {
                        "type": "string",
                        "enum": [
                            "held",
                            "paid",
                            "released"
                        ]
                    },
                    "room_id": {
                        "type": [
                            "string",
                            "null"
                        ],
                        "example": "123456"
                    },
                    "match_id": {
                        "type": [
                            "string",
                            "null"
                        ],
                        "example": "MATCH-98765"
                    },
                    "created_at": {
                        "type": "string",
                        "example": "2026-12-01 14:30:00"
                    }
                }
            },
            "ErrorResponse": {
                "type": "object",
                "properties": {
                    "error": {
                        "type": "string",
                        "example": "order_creation_failed"
                    },
                    "reason": {
                        "type": "string",
                        "example": "server_busy"
                    },
                    "next_request_key": {
                        "type": "string",
                        "example": "0123456789abcdef0123456789abcdef"
                    }
                }
            }
        }
    },
    "paths": {
        "/api/ship-xu.php?action=catalog": {
            "get": {
                "tags": [
                    "Public"
                ],
                "operationId": "getCatalog",
                "summary": "Lấy danh sách máy chủ, bảng giá và gói Xu",
                "description": "Trả về danh sách máy chủ đang mở, các gói Đơn Thường (4M–20M), thông số Đơn Lớn/Login (10M–1B) và trạng thái nhận đơn tổng thể.",
                "parameters": [
                    {
                        "name": "for_child",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "string",
                            "enum": [
                                "1"
                            ]
                        },
                        "description": "Dành riêng cho web con (yêu cầu Bearer token của tài khoản web con đã cấp quyền trong SHIP_XU_CHILD_USER_IDS)."
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Danh mục dịch vụ Ship Xu",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "properties": {
                                        "servers": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/components/schemas/ServerOffer"
                                            }
                                        },
                                        "packages": {
                                            "type": "array",
                                            "items": {
                                                "type": "integer"
                                            },
                                            "example": [
                                                4,
                                                8,
                                                12,
                                                16,
                                                20
                                            ]
                                        },
                                        "ordering_available": {
                                            "type": "boolean"
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "429": {
                        "description": "Vượt quá 120 req/phút/IP"
                    }
                }
            }
        },
        "/api/ship-xu.php?action=public_capacity": {
            "get": {
                "tags": [
                    "Public"
                ],
                "operationId": "getPublicCapacity",
                "summary": "Kiểm tra số luồng Auto-Radar còn trống",
                "parameters": [
                    {
                        "name": "server_id",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "integer",
                            "default": 1,
                            "minimum": 1,
                            "maximum": 3
                        },
                        "description": "ID máy chủ cần kiểm tra (mặc định 1)."
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Số luồng trống hiện tại",
                        "content": {
                            "application/json": {
                                "example": {
                                    "remaining_threads": 12,
                                    "stale": false
                                }
                            }
                        }
                    },
                    "429": {
                        "description": "Vượt giới hạn 120 req/phút/IP"
                    }
                }
            }
        },
        "/api/ship-xu.php?action=quote": {
            "get": {
                "tags": [
                    "Public"
                ],
                "operationId": "getQuote",
                "summary": "Báo giá chính xác trước khi tạo đơn",
                "description": "Gửi thêm `Authorization: Bearer <API_KEY>` để nhận đơn giá đã áp dụng chiết khấu CTV/Đại lý.",
                "security": [
                    [],
                    {
                        "bearerAuth": []
                    }
                ],
                "parameters": [
                    {
                        "name": "server_id",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "integer",
                            "default": 1
                        },
                        "description": "1–2 cho Đơn Thường, 3 cho Đơn Lớn/Login."
                    },
                    {
                        "name": "amount_millions",
                        "in": "query",
                        "required": true,
                        "schema": {
                            "type": "integer",
                            "example": 4
                        },
                        "description": "Số triệu Xu (4, 8, 12, 16, 20 hoặc 10–1000 cho login_run)."
                    },
                    {
                        "name": "mode",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "string",
                            "enum": [
                                "standard",
                                "login_run"
                            ],
                            "default": "standard"
                        },
                        "description": "Chế độ đặt đơn."
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Báo giá hợp lệ",
                        "content": {
                            "application/json": {
                                "example": {
                                    "quote": {
                                        "server_id": 1,
                                        "server_name": "Tiêu chuẩn",
                                        "mode": "standard",
                                        "amount_millions": 4,
                                        "price_per_million": 4000,
                                        "total_vnd": 16000,
                                        "server_accepts_orders": true
                                    },
                                    "ordering_available": true
                                }
                            }
                        }
                    },
                    "409": {
                        "description": "Máy chủ tạm đóng (`server_unavailable`)"
                    },
                    "422": {
                        "description": "Gói Xu hoặc máy chủ không hợp lệ"
                    }
                }
            }
        },
        "/api/ship-xu.php?action=create": {
            "post": {
                "tags": [
                    "Orders"
                ],
                "operationId": "createOrder",
                "summary": "Tạo đơn Ship Xu (Đơn Thường & Đơn Lớn/Login)",
                "description": "Khóa giá bằng `total_vnd` từ `quote` và chống tạo trùng bằng `request_key` (32 ký tự hex). Với Đơn Thường, hệ thống lấy mã phòng tối đa **15 giây**; quá 15 giây chưa có mã phòng sẽ tự hủy yêu cầu giữ chỗ và trả `409` (`reason: \"server_busy\"`).",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/x-www-form-urlencoded": {
                            "schema": {
                                "type": "object",
                                "required": [
                                    "server_id",
                                    "amount_millions",
                                    "total_vnd",
                                    "request_key",
                                    "guide_acknowledged"
                                ],
                                "properties": {
                                    "mode": {
                                        "type": "string",
                                        "enum": [
                                            "standard",
                                            "login_run"
                                        ],
                                        "default": "standard"
                                    },
                                    "server_id": {
                                        "type": "integer",
                                        "example": 1
                                    },
                                    "amount_millions": {
                                        "type": "integer",
                                        "example": 4
                                    },
                                    "total_vnd": {
                                        "type": "integer",
                                        "example": 16000,
                                        "description": "Phải khớp chính xác `quote.total_vnd`."
                                    },
                                    "request_key": {
                                        "type": "string",
                                        "pattern": "^[0-9a-f]{32}$",
                                        "example": "0123456789abcdef0123456789abcdef"
                                    },
                                    "guide_acknowledged": {
                                        "type": "string",
                                        "enum": [
                                            "1"
                                        ],
                                        "example": "1"
                                    },
                                    "player_id": {
                                        "type": "string",
                                        "pattern": "^[0-9]{4,32}$",
                                        "description": "Bắt buộc khi `mode=login_run`."
                                    },
                                    "login_provider": {
                                        "type": "string",
                                        "enum": [
                                            "garena",
                                            "google"
                                        ],
                                        "description": "Bắt buộc khi `mode=login_run`."
                                    },
                                    "character_name": {
                                        "type": "string",
                                        "minLength": 1,
                                        "maxLength": 32,
                                        "description": "Bắt buộc khi `mode=login_run`."
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "201": {
                        "description": "Tạo đơn thành công và đã có mã phòng (hoặc đã tiếp nhận đơn Login)",
                        "content": {
                            "application/json": {
                                "example": {
                                    "order": {
                                        "order_code": "DH_A1B2C3D4",
                                        "status": "processing",
                                        "payment_state": "paid",
                                        "room_id": "987654"
                                    },
                                    "replayed": false,
                                    "radar_dispatch": "confirmed",
                                    "balance": 247436,
                                    "held_balance": 0,
                                    "available_balance": 247436,
                                    "next_request_key": "fedcba9876543210fedcba9876543210"
                                }
                            }
                        }
                    },
                    "200": {
                        "description": "Yêu cầu trùng `request_key` hợp lệ (`replayed: true`)"
                    },
                    "202": {
                        "description": "Đơn đang chờ đối soát"
                    },
                    "401": {
                        "description": "Thiếu hoặc sai Bearer API Key"
                    },
                    "409": {
                        "description": "Từ chối tạo đơn: `insufficient_balance`, `price_changed`, `order_limit`, `request_conflict`, hoặc `order_creation_failed` với `reason: \"server_busy\" | \"capacity_unavailable\" | \"backend_stopped\"`.",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/ErrorResponse"
                                }
                            }
                        }
                    },
                    "422": {
                        "description": "Tham số không hợp lệ (`invalid_order`)"
                    },
                    "429": {
                        "description": "Vượt giới hạn 20 đơn/phút/tài khoản"
                    },
                    "503": {
                        "description": "Hệ thống đang bảo trì (`maintenance`)"
                    }
                }
            }
        },
        "/api/ship-xu.php?action=history": {
            "get": {
                "tags": [
                    "Orders"
                ],
                "operationId": "listOrders",
                "summary": "Lấy lịch sử đơn hàng & số dư khả dụng",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "parameters": [
                    {
                        "name": "limit",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "integer",
                            "enum": [
                                5,
                                10,
                                50,
                                100
                            ],
                            "default": 10
                        }
                    },
                    {
                        "name": "status",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "string",
                            "enum": [
                                "pending",
                                "processing",
                                "completed",
                                "failed",
                                "refunded"
                            ]
                        }
                    },
                    {
                        "name": "code",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "string"
                        },
                        "description": "Tìm theo tiền tố mã đơn."
                    },
                    {
                        "name": "before",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "integer"
                        },
                        "description": "Con trỏ phân trang (`next_cursor` từ trang trước)."
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Danh sách đơn và thông tin ví",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "properties": {
                                        "orders": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/components/schemas/OrderSummary"
                                            }
                                        },
                                        "next_cursor": {
                                            "type": [
                                                "integer",
                                                "null"
                                            ]
                                        },
                                        "wallet": {
                                            "$ref": "#/components/schemas/WalletSummary"
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "401": {
                        "description": "Sai/thiếu Bearer API Key"
                    }
                }
            }
        },
        "/api/ship-xu.php?action=history&reconcile=1": {
            "get": {
                "tags": [
                    "Orders"
                ],
                "operationId": "reconcileOrder",
                "summary": "Đối soát chi tiết 1 đơn theo mã đơn hoặc request_key",
                "description": "Truyền **đúng 1 trong 2** tham số `code` hoặc `request_key` để kiểm tra trạng thái thật sau khi mạng gián đoạn.",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "parameters": [
                    {
                        "name": "code",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "string"
                        },
                        "description": "Mã đơn (`order_code`)."
                    },
                    {
                        "name": "request_key",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "string",
                            "pattern": "^[0-9a-f]{32}$"
                        },
                        "description": "Mã chống trùng 32 ký tự hex đã dùng khi tạo đơn."
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Trả về snapshot đầy đủ của đơn (`order`, `room_id`, `room_expires_in`, `phase`, `match_id`, `settled`, `needs_review`)."
                    },
                    "404": {
                        "description": "Không tìm thấy đơn (`order_not_found`)"
                    },
                    "409": {
                        "description": "Đơn đã bị từ chối khi tạo (`order_creation_failed` + `reason`)"
                    }
                }
            }
        },
        "/api/profile.php": {
            "get": {
                "tags": [
                    "Account"
                ],
                "operationId": "getProfile",
                "summary": "Lấy thông tin tài khoản và số dư ví",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Thông tin tài khoản",
                        "content": {
                            "application/json": {
                                "example": {
                                    "status": "success",
                                    "msg": "Lấy dữ liệu thành công!",
                                    "data": [
                                        {
                                            "username": "tai_khoan_mau",
                                            "money": "263436"
                                        }
                                    ]
                                }
                            }
                        }
                    },
                    "401": {
                        "description": "API key không hợp lệ"
                    }
                }
            }
        },
        "/api/ship-xu.php?action=capacity": {
            "get": {
                "tags": [
                    "Account"
                ],
                "operationId": "getDetailedCapacity",
                "summary": "Telemetry luồng chi tiết (chỉ Admin / Web con được duyệt)",
                "description": "Yêu cầu Bearer token của Admin hoặc tài khoản web con đã khai báo trong `SHIP_XU_CHILD_USER_IDS` trên web mẹ.",
                "security": [
                    {
                        "bearerAuth": []
                    }
                ],
                "parameters": [
                    {
                        "name": "server_id",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "integer",
                            "default": 1
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Chi tiết công suất máy chủ (`capacity`, `updated_at`, `stale`)"
                    },
                    "401": {
                        "description": "Không đủ quyền truy cập (`login_required`)"
                    },
                    "503": {
                        "description": "Chưa có dữ liệu công suất (`capacity_unavailable`)"
                    }
                }
            }
        }
    }
}