{
    "openapi": "3.0.0",
    "info": {
        "title": "Digital Product Buyer API (Cổng Tích Hợp Mua Sản Phẩm Số)",
        "version": "2.0.0",
        "description": "Cổng API kết nối tự động mua sản phẩm số (tài khoản, key bản quyền, dịch vụ số) trực tiếp qua số dư ví khách hàng.\n\n### 🔑 Cách Xác Thực API:\n- Nhấn nút **Authorize** bên trên và nhập API Key của bạn (ví dụ: `tgb_ad06f847be74800a50d01efed04b8a9679ba2c08dc358033`).\n- Lấy API Key cá nhân bằng cách gửi lệnh `/api` trong Telegram Bot hoặc tại trang Quản lý Tài khoản trên Mini App.\n- API Key có thể gửi qua Header `Authorization: Bearer <key>`, Header `X-API-Key: <key>`, hoặc query string `?api_key=<key>`.",
        "contact": {
            "name": "Hỗ Trợ Kỹ Thuật",
            "url": "https://id.taphoaso.net/huong-dan"
        }
    },
    "servers": [
        {
            "url": "https://id.taphoaso.net",
            "description": "Máy chủ hiện tại"
        }
    ],
    "components": {
        "securitySchemes": {
            "BearerAuth": {
                "type": "http",
                "scheme": "bearer",
                "bearerFormat": "API Key",
                "description": "Nhập API Key của bạn (ví dụ: `tgb_ad06f847be74800a50d01efed04b8a9679ba2c08dc358033`)"
            },
            "ApiKeyAuth": {
                "type": "apiKey",
                "in": "header",
                "name": "X-API-Key",
                "description": "Nhập API Key trong Header X-API-Key"
            },
            "QueryApiKey": {
                "type": "apiKey",
                "in": "query",
                "name": "api_key",
                "description": "Truyền API Key trực tiếp qua URL query parameter"
            }
        }
    },
    "security": [
        {
            "BearerAuth": []
        },
        {
            "ApiKeyAuth": []
        },
        {
            "QueryApiKey": []
        }
    ],
    "paths": {
        "/api/v2/telegram-buyer/products": {
            "get": {
                "tags": [
                    "Sản Phẩm Số (Products)"
                ],
                "summary": "Liệt kê danh sách sản phẩm số hiện có",
                "description": "Trả về toàn bộ sản phẩm đang kích hoạt, phân loại danh mục, đơn giá VNĐ và số lượng tồn kho khả dụng.",
                "responses": {
                    "200": {
                        "description": "Danh sách sản phẩm thành công",
                        "content": {
                            "application/json": {
                                "example": {
                                    "success": true,
                                    "status": "success",
                                    "total": 2,
                                    "currency": "VND",
                                    "data": [
                                        {
                                            "id": 12,
                                            "title": "Tài khoản ChatGPT Plus 1 Tháng",
                                            "slug": "tai-khoan-chatgpt-plus",
                                            "category_id": 3,
                                            "category_name": "AI & Tools",
                                            "type": "account",
                                            "price": 150000,
                                            "currency": "VND",
                                            "stock": 45,
                                            "in_stock": true,
                                            "min_quantity": 1,
                                            "max_quantity": 45,
                                            "short_description": "ChatGPT Plus dùng riêng, kích hoạt sẵn",
                                            "description": "Bảo hành full thời gian sử dụng 30 ngày.",
                                            "cover_image": "https://id.taphoaso.net/assets/default_cover.png"
                                        }
                                    ]
                                }
                            }
                        }
                    },
                    "401": {
                        "description": "API Key không hợp lệ hoặc thiếu"
                    }
                }
            }
        },
        "/api/v2/telegram-buyer/purchase": {
            "post": {
                "tags": [
                    "Mua Hàng & Đơn Hàng (Purchase)"
                ],
                "summary": "Mua sản phẩm số tự động bằng số dư ví",
                "description": "Tự động kiểm tra số dư ví, trừ tiền, trích xuất kho hàng và trả về thông tin tài khoản / key bản quyền ngay lập tức trong JSON response.",
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "required": [
                                    "product_id",
                                    "quantity"
                                ],
                                "properties": {
                                    "product_id": {
                                        "type": "integer",
                                        "description": "Mã ID sản phẩm cần mua",
                                        "example": 12
                                    },
                                    "quantity": {
                                        "type": "integer",
                                        "description": "Số lượng muốn mua (mặc định: 1)",
                                        "example": 1
                                    },
                                    "api_key": {
                                        "type": "string",
                                        "description": "API Key của bạn (nếu không truyền qua Header)",
                                        "example": "tgb_ad06f847be74800a50d01efed04b8a9679ba2c08dc358033"
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Mua hàng và xuất kho thành công",
                        "content": {
                            "application/json": {
                                "example": {
                                    "success": true,
                                    "status": "completed",
                                    "order_id": 1042,
                                    "order_number": "API-20260915-A1B2C3",
                                    "product_id": 12,
                                    "product_title": "Tài khoản ChatGPT Plus 1 Tháng",
                                    "quantity": 1,
                                    "unit_price": 150000,
                                    "total_amount": 150000,
                                    "currency": "VND",
                                    "remaining_balance": 350000,
                                    "delivered_items": [
                                        "user_example@gmail.com|Password123#|2FA:JBSWY3DPEHPK3PXP"
                                    ],
                                    "delivered_data_text": "user_example@gmail.com|Password123#|2FA:JBSWY3DPEHPK3PXP",
                                    "created_at": "2026-09-15 21:55:00"
                                }
                            }
                        }
                    },
                    "400": {
                        "description": "Hết hàng hoặc tham số không hợp lệ"
                    },
                    "401": {
                        "description": "API Key không hợp lệ"
                    },
                    "402": {
                        "description": "Số dư ví không đủ"
                    }
                }
            }
        },
        "/api/v2/telegram-buyer/balance": {
            "get": {
                "tags": [
                    "Tài Khoản & Ví (Wallet)"
                ],
                "summary": "Kiểm tra số dư ví khả dụng",
                "description": "Lấy thông tin số dư ví VNĐ và USD hiện tại của tài khoản kết nối.",
                "responses": {
                    "200": {
                        "description": "Lấy số dư thành công",
                        "content": {
                            "application/json": {
                                "example": {
                                    "success": true,
                                    "status": "success",
                                    "customer_id": 88,
                                    "telegram_id": "123456789",
                                    "name": "Nguyễn Văn A",
                                    "balance_vnd": 500000,
                                    "balance_usdt": 20,
                                    "currency": "VND"
                                }
                            }
                        }
                    }
                }
            }
        },
        "/api/v2/telegram-buyer/orders": {
            "get": {
                "tags": [
                    "Mua Hàng & Đơn Hàng (Purchase)"
                ],
                "summary": "Lấy lịch sử đơn hàng đã mua",
                "description": "Liệt kê danh sách các đơn hàng đã mua cùng nội dung tài khoản/mã key đã nhận.",
                "parameters": [
                    {
                        "name": "limit",
                        "in": "query",
                        "description": "Số lượng đơn hàng muốn lấy (mặc định 20, tối đa 100)",
                        "required": false,
                        "schema": {
                            "type": "integer",
                            "default": 20
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Lấy danh sách đơn hàng thành công"
                    }
                }
            }
        },
        "/api/v2/telegram-buyer/order-detail": {
            "get": {
                "tags": [
                    "Mua Hàng & Đơn Hàng (Purchase)"
                ],
                "summary": "Xem chi tiết một đơn hàng cụ thể",
                "description": "Truy vấn chi tiết đơn hàng bằng `order_id` hoặc mã `order_number`.",
                "parameters": [
                    {
                        "name": "order_id",
                        "in": "query",
                        "description": "Mã ID đơn hàng",
                        "required": false,
                        "schema": {
                            "type": "integer"
                        }
                    },
                    {
                        "name": "order_number",
                        "in": "query",
                        "description": "Mã đơn dạng chuỗi (ví dụ: API-20260915-A1B2C3)",
                        "required": false,
                        "schema": {
                            "type": "string"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Lấy chi tiết đơn hàng thành công"
                    }
                }
            }
        }
    }
}