Agent API Test Interface

ID: 1 Tên: - Đã nạp: - Ví: - Chiết khấu: -
API Documentation

Hướng dẫn sử dụng Agent API

Lưu ý: Frontend-agent đang sử dụng backend-api để gọi API. Tất cả các request đều được gửi qua https://api-proxy.gizadata.vn/

1. Cấu hình

Trước khi sử dụng, cần cấu hình trong file config.php:

define('BACKEND_PROXY_URL', 'https://api-proxy.gizadata.vn/');
define('AGENT_ID', '1');
define('AGENT_API_KEY', 'your_api_key_here');

2. Cách gọi API

Có 2 cách để gọi API:

Cách 1: Sử dụng PHP Class (AgentAPI)

<?php
require_once 'agent_api.php';

$api = new AgentAPI($agent_id, $api_key);

// Xác thực
$result = $api->authenticate();

// Lấy số dư
$result = $api->getBalance();

// Tạo VPS
$result = $api->createVPS(
    customer: [],
    hostname: 'vps1.example.com',
    planid: 1,
    billingcycle: 'monthly',
    password: 'SecurePassword123',
    os: 'ubuntu-22.04',
    quantity: 1
);

Cách 2: Gọi trực tiếp qua HTTP (JavaScript/Frontend)

Frontend gọi API qua file setup.php với format JSON:

fetch('', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
        action: 'authenticate'
    })
})
.then(response => response.json())
.then(data => console.log(data));

3. Danh sách Actions

Authentication & Balance

Action Mô tả Parameters Response
authenticate Xác thực agent Không có Thông tin agent (ID, tên, số dư, discount...)
get_balance Lấy số dư ví Không có Số dư hiện tại

VPS

Action Mô tả Parameters
create_vps Tạo VPS mới hostname (string),
planid (int),
billingcycle (string: monthly/quarterly/semiannually/annually),
password (string),
os (string),
quantity (int, default: 1)
vps_action Thực hiện action trên VPS service_id (int),
vps_action (string: start/stop/reboot/poweroff/reinstall/changehostname/changepassword/vnc),
os_name (string, nếu action = reinstall),
rootpass (string, nếu action = reinstall),
new_hostname (string, nếu action = changehostname),
new_password (string, nếu action = changepassword)
get_vps_status Lấy trạng thái VPS service_id (int, optional - null để lấy tất cả)

Hosting

Action Mô tả Parameters
create_hosting Tạo Hosting mới domain (string),
planid (int),
billingcycle (string),
password (string)
get_hosting_login_info Lấy thông tin đăng nhập Hosting service_id (int)

Email

Action Mô tả Parameters
create_email Tạo Email mới (cPanel account) domain (string, required),
planid (int, required),
billingcycle (string, required: monthly/quarterly/semiannually/annually),
password (string, required, min 8 chars)
Lưu ý: Nếu password không đủ mạnh (thiếu uppercase/lowercase/numbers/symbols), hệ thống sẽ tự động generate password mạnh
get_email_login_info Lấy thông tin đăng nhập Email service_id (int, required)
Lưu ý về Email:
  • Hệ thống sẽ tự động kiểm tra xem domain đã có cPanel account chưa. Nếu có, sẽ sử dụng account đó.
  • Nếu domain chưa có account, hệ thống sẽ tạo cPanel account mới với username random.
  • Mỗi gói Email sẽ có cPanel account riêng trên server.
  • User sẽ tự tạo email accounts qua cPanel sau khi account được tạo.
  • Tiền chỉ được trừ khi cPanel account được tạo thành công (status = Active).

Key

Action Mô tả Parameters
create_key Tạo Key mới product_id (int),
quantity (int, default: 1),
months (int, default: 1)

Danh sách & Thông tin

Action Mô tả Parameters
get_public_plans Lấy danh sách gói dịch vụ công khai Không có
get_customers Lấy danh sách customers page (int), per_page (int)
get_services Lấy danh sách services page (int),
per_page (int),
service_type (string, optional: vps/hosting/email/key)
get_transactions Lấy lịch sử giao dịch page (int), per_page (int)
get_package_os_templates Lấy danh sách OS templates cho package package_id (int)

4. Format Response

Tất cả API đều trả về JSON với format:

{
    "status": "success" | "error",
    "message": "Mô tả kết quả",
    "data": { ... },
    "http_code": 200
}

Ví dụ Response thành công (VPS):

{
    "status": "success",
    "message": "Tạo VPS thành công",
    "data": {
        "service_id": 123,
        "order_id": 456,
        "invoice_id": 789,
        "hostname": "vps1.example.com",
        "username": "vpsuser",
        "price": 150000,
        "wallet_balance_before": 1000000,
        "wallet_balance_after": 850000,
        "agent_transaction_id": 42
    },
    "http_code": 200
}

Ví dụ Response thành công (Email):

{
    "status": "success",
    "message": "Tạo Email thành công",
    "data": {
        "service_id": 123,
        "order_id": 456,
        "invoice_id": 789,
        "domain": "example.com",
        "username": "mail1234",
        "price": 15000,
        "amount_deducted": 15000,
        "service_status": "Active",
        "account_created": true,
        "account_error": null,
        "account_warning": null,
        "discount_amount": 1500,
        "discount_percentage": 10,
        "wallet_balance_before": 1000000,
        "wallet_balance_after": 985000,
        "agent_transaction_id": 42
    },
    "http_code": 200
}

Giải thích các trường:
amount_deducted: Số tiền thực tế đã trừ (chỉ khi account được tạo thành công)
service_status: Trạng thái dịch vụ (Active/Pending)
account_created: true nếu cPanel account được tạo thành công
account_error: Thông báo lỗi nếu tạo account thất bại
account_warning: Cảnh báo (ví dụ: DNS warning nhưng account vẫn được tạo)

Ví dụ Response lỗi:

{
    "status": "error",
    "message": "Số dư không đủ",
    "http_code": 400
}

5. Backend API Actions

Frontend-agent gọi API qua backend-api với các action sau:

  • agent_auth - Xác thực agent
  • agent_balance - Lấy số dư
  • agent_customers - Lấy danh sách customers
  • agent_services - Lấy danh sách services
  • agent_transactions - Lấy lịch sử giao dịch
  • agent_public_plans - Lấy danh sách gói dịch vụ công khai
  • agent_vps_create - Tạo VPS
  • agent_vps_start - Start VPS
  • agent_vps_stop - Stop VPS
  • agent_vps_reboot - Reboot VPS
  • agent_vps_poweroff - Power Off VPS
  • agent_vps_reinstall - Reinstall OS VPS
  • agent_vps_changehostname - Đổi hostname VPS
  • agent_vps_changepassword - Đổi password VPS
  • agent_vps_renew - Gia hạn VPS
  • agent_vps_suspend - Suspend VPS
  • agent_vps_unsuspend - Unsuspend VPS
  • agent_vps_terminate - Terminate VPS
  • agent_vps_status - Lấy trạng thái VPS
  • agent_vps_vnc - Lấy VNC link
  • agent_hosting_create - Tạo Hosting
  • agent_hosting_login_info - Lấy thông tin đăng nhập Hosting
  • agent_email_create - Tạo Email
  • agent_email_login_info - Lấy thông tin đăng nhập Email
  • agent_key_create - Tạo Key
Lưu ý:
  • Tất cả requests đều cần gửi kèm agent_idapi_key trong payload
  • Backend sẽ tự động tạo customer cho agent, không cần gửi customer info
  • API sử dụng POST method với Content-Type: application/json
  • Timeout mặc định: 30 giây

6. Ví dụ sử dụng

Ví dụ 1: Tạo VPS bằng PHP

<?php
require_once 'agent_api.php';

$api = new AgentAPI('1', 'your_api_key_here');

$result = $api->createVPS(
    customer: [],
    hostname: 'vps1.example.com',
    planid: 1,
    billingcycle: 'monthly',
    password: 'SecurePassword123!',
    os: 'ubuntu-22.04',
    quantity: 1
);

if ($result['status'] === 'success') {
    echo "VPS đã được tạo thành công! Service ID: " . $result['data']['service_id'];
} else {
    echo "Lỗi: " . $result['message'];
}

Ví dụ 2: Tạo VPS bằng JavaScript

fetch('', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
        action: 'create_vps',
        hostname: 'vps1.example.com',
        planid: 1,
        billingcycle: 'monthly',
        password: 'SecurePassword123!',
        os: 'ubuntu-22.04',
        quantity: 1
    })
})
.then(response => response.json())
.then(data => {
    if (data.status === 'success') {
        console.log('VPS đã được tạo:', data.data);
    } else {
        console.error('Lỗi:', data.message);
    }
});

Ví dụ 3: Tạo Email bằng JavaScript

fetch('', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
        action: 'create_email',
        domain: 'example.com',
        planid: 1,
        billingcycle: 'monthly',
        password: 'SecurePassword123!'
    })
})
.then(response => response.json())
.then(data => {
    if (data.status === 'success') {
        console.log('Email đã được tạo:', data.data);
        console.log('Service ID:', data.data.service_id);
        console.log('Username:', data.data.username);
        console.log('Account Created:', data.data.account_created);
        if (data.data.account_warning) {
            console.warn('Warning:', data.data.account_warning);
        }
    } else {
        console.error('Lỗi:', data.message);
    }
});

Ví dụ 4: Gọi trực tiếp backend-api (cURL)

curl -X POST "https://api-proxy.gizadata.vn/api.php?action=agent_auth" \
  -H "Content-Type: application/json" \
  -d '{
    "agent_id": "1",
    "api_key": "your_api_key_here"
  }'