Chuyển tới nội dung chính

Thêm cấu hình Webhook mới

Webhook code là gì?

code là khóa định danh dùng để truy xuất cấu hình webhook từ bảng tenant_webhook. Khi cần gửi webhook, service dùng code để tìm bản ghi tương ứng và lấy thông tin: endpoint, auth config, timeout, retry policy.

// Tìm webhook config bằng code
tenantWebhookRepository.findByTenantIdAndCodeAndStatus(tenantId, "NEBULA_SIGNAL_UPDATE", TenantWebhookStatus.ACTIVE);

Code hiện có

CodeChức năng
NEBULA_SIGNAL_UPDATEGửi thông báo tín hiệu giao dịch Nebula (ticker, type, volume, date)

Ví dụ code có thể thêm

CodeChức năng
ORDER_STATUS_UPDATEGửi thông báo khi trạng thái lệnh thay đổi
PORTFOLIO_CHANGEGửi thông báo khi danh mục thay đổi
PRICE_ALERTGửi thông báo cảnh báo giá

Các trường của TenantWebhook

Bảng: tenant_webhook

TrườngKiểuBắt buộcMô tả
idLong (auto)-ID tự tăng
tenant_idLongID của tenant
codeStringKhóa định danh chức năng webhook. Ví dụ: NEBULA_SIGNAL_UPDATE
nameStringKhôngTên mô tả để quản lý. Ví dụ: "Nebula Signal Webhook"
endpointTextURL endpoint của tenant nhận webhook. Ví dụ: https://api.partner.com/webhook
timeoutIntegerKhôngTimeout (ms). Mặc định: 30000 (30s)
retry_policyIntegerKhông1 = EXPONENTIAL_BACKOFF (60s, 120s, …), 2 = IMMEDIATE (10s). Mặc định: 1
statusInteger1 = ACTIVE, 2 = INACTIVE, 3 = SUSPENDED
tenant_auth_config_idsText (JSON)KhôngDanh sách ID auth config. Ví dụ: [1, 2]

Ràng buộc duy nhất: (tenant_id, code) — mỗi tenant chỉ có một config cho mỗi code.


Các trường của TenantAuthConfig

Bảng: tenant_auth_config

TrườngKiểuBắt buộcMô tả
idLong (auto)-ID tự tăng
tenant_idLongID của tenant
nameStringKhôngTên mô tả. Ví dụ: "API Key cho partner X"
auth_typeInteger1 = API_KEY, 2 = BASIC, 3 = HMAC
auth_valueString (encrypted)Giá trị xác thực (được mã hóa trong DB)
auth_locationText (JSON)Cấu hình vị trí và cách áp dụng auth

Chi tiết auth_value theo loại auth

LoạiGiá trị auth_value
API_KEYChuỗi API key. Ví dụ: "sk-abc123xyz"
BASICusername:password. Ví dụ: "admin:secret123"
HMACSecret key. Ví dụ: "hmac-secret-key-abc123"

Chi tiết auth_location theo loại auth

API_KEY

Gửi API key trong header:

{
"to": "header",
"key": "X-API-KEY"
}

Gửi API key trong query parameter:

{
"to": "query",
"key": "apiKey"
}
TrườngMô tả
toVị trí: "header" hoặc "query"
keyTên header hoặc tên query param

BASIC

{
"to": "header",
"key": "Authorization"
}

Hệ thống tự động Base64 encode auth_value và gửi: Authorization: Basic {base64}

HMAC

Gửi chữ ký trong header:

{
"to": "header",
"key": "X-Signature",
"algorithm": "SHA256",
"include": [
"method",
"path",
"query",
"body"
],
"recvWindow": 5000
}

Gửi chữ ký trong query parameter:

{
"to": "query",
"key": "signature",
"algorithm": "SHA256",
"include": [
"method",
"path",
"query",
"body"
],
"recvWindow": 5000
}
TrườngMô tảMặc định
toVị trí chữ ký: "header" hoặc "query"-
keyTên header hoặc query param chứa chữ ký-
algorithmThuật toán HMAC: "SHA256""SHA256"
includeCác thành phần tham gia payload ký["method", "path", "query", "body"]
recvWindowThời gian hiệu lực chữ ký (ms)5000

Các giá trị include hỗ trợ:

Giá trịMô tả
methodHTTP method (luôn là POST)
path / urlURL path (ví dụ: /webhook/receive)
queryQuery parameters (sắp xếp theo key, không bao gồm signature)
bodyRequest body (JSON đã minify)

Khi to = "query", hệ thống tự động thêm timestamprecvWindow vào query params trước khi tính chữ ký.


Các bước thêm webhook mới

Bước 1: Tạo auth config

INSERT INTO tenant_auth_config (tenant_id, name, auth_type, auth_value, auth_location)
VALUES (
123, -- tenant_id
'API Key cho Partner X', -- name
1, -- auth_type: 1=API_KEY
'sk-abc123xyz', -- auth_value (sẽ được encrypt)
'{"to":"header","key":"X-API-KEY"}' -- auth_location
);
-- Giả sử id = 10

Bước 2: Tạo webhook config

INSERT INTO tenant_webhook (tenant_id, code, name, endpoint, timeout, retry_policy, status, tenant_auth_config_ids)
VALUES (
123, -- tenant_id
'ORDER_STATUS_UPDATE', -- code: khóa định danh
'Order Status Webhook', -- name
'https://api.partner.com/webhook/order', -- endpoint
30000, -- timeout (ms)
1, -- retry_policy: 1=EXPONENTIAL_BACKOFF
1, -- status: 1=ACTIVE
'[10]' -- tenant_auth_config_ids: liên kết auth config
);

Bước 3: Sử dụng trong code

Xem Hướng dẫn sử dụng Webhook để lấy config và gọi WebhookService.sendWebhook().


Ví dụ cấu hình đầy đủ theo loại auth

Ví dụ 1: API_KEY (header)

-- Auth config
INSERT INTO tenant_auth_config (tenant_id, name, auth_type, auth_value, auth_location)
VALUES (123, 'API Key', 1, 'sk-abc123xyz', '{"to":"header","key":"X-API-KEY"}');

-- Webhook config
INSERT INTO tenant_webhook (tenant_id, code, name, endpoint, timeout, retry_policy, status, tenant_auth_config_ids)
VALUES (123, 'MY_WEBHOOK', 'My Webhook', 'https://api.partner.com/webhook', 30000, 1, 1, '[1]');

Request gửi đi:

POST https://api.partner.com/webhook
Headers:
Content-Type: application/json
X-API-KEY: sk-abc123xyz
Body: {...payload...}

Ví dụ 2: BASIC

-- Auth config
INSERT INTO tenant_auth_config (tenant_id, name, auth_type, auth_value, auth_location)
VALUES (123, 'Basic Auth', 2, 'admin:secret123', '{"to":"header","key":"Authorization"}');

-- Webhook config
INSERT INTO tenant_webhook (tenant_id, code, name, endpoint, timeout, retry_policy, status, tenant_auth_config_ids)
VALUES (123, 'MY_WEBHOOK', 'My Webhook', 'https://api.partner.com/webhook', 30000, 1, 1, '[1]');

Request gửi đi:

POST https://api.partner.com/webhook
Headers:
Content-Type: application/json
Authorization: Basic YWRtaW46c2VjcmV0MTIz
Body: {...payload...}

Ví dụ 3: HMAC (header)

-- Auth config
INSERT INTO tenant_auth_config (tenant_id, name, auth_type, auth_value, auth_location)
VALUES (123, 'HMAC Auth', 3, 'hmac-secret-key-abc123',
'{"to":"header","key":"X-Signature","algorithm":"SHA256","include":["method","path","body"],"recvWindow":5000}');

-- Webhook config
INSERT INTO tenant_webhook (tenant_id, code, name, endpoint, timeout, retry_policy, status, tenant_auth_config_ids)
VALUES (123, 'MY_WEBHOOK', 'My Webhook', 'https://api.partner.com/webhook', 30000, 1, 1, '[1]');

Request gửi đi:

POST https://api.partner.com/webhook
Headers:
Content-Type: application/json
X-Signature: a1b2c3d4e5f6... (HMAC-SHA256 signature)
Body: {...payload...}

Ví dụ 4: HMAC (query)

-- Auth config
INSERT INTO tenant_auth_config (tenant_id, name, auth_type, auth_value, auth_location)
VALUES (123, 'HMAC Query Auth', 3, 'hmac-secret-key-abc123',
'{"to":"query","key":"signature","algorithm":"SHA256","include":["method","path","query","body"],"recvWindow":5000}');

-- Webhook config
INSERT INTO tenant_webhook (tenant_id, code, name, endpoint, timeout, retry_policy, status, tenant_auth_config_ids)
VALUES (123, 'MY_WEBHOOK', 'My Webhook', 'https://api.partner.com/webhook', 30000, 1, 1, '[1]');

Request gửi đi:

POST https://api.partner.com/webhook?recvWindow=5000&signature=a1b2c3...&timestamp=1711612800000
Headers:
Content-Type: application/json
Body: {...payload...}

Ví dụ 5: Nhiều auth config

-- API Key auth
INSERT INTO tenant_auth_config (tenant_id, name, auth_type, auth_value, auth_location)
VALUES (123, 'API Key', 1, 'sk-abc123', '{"to":"header","key":"X-API-KEY"}');
-- Giả sử id = 10

-- HMAC auth
INSERT INTO tenant_auth_config (tenant_id, name, auth_type, auth_value, auth_location)
VALUES (123, 'HMAC Signature', 3, 'secret-key',
'{"to":"header","key":"X-Signature","algorithm":"SHA256","include":["method","path","body"]}');
-- Giả sử id = 11

-- Webhook config với 2 auth config
INSERT INTO tenant_webhook (tenant_id, code, name, endpoint, timeout, retry_policy, status, tenant_auth_config_ids)
VALUES (123, 'MY_WEBHOOK', 'My Webhook', 'https://api.partner.com/webhook', 30000, 1, 1, '[10, 11]');

Request gửi đi:

POST https://api.partner.com/webhook
Headers:
Content-Type: application/json
X-API-KEY: sk-abc123
X-Signature: a1b2c3d4e5f6...
Body: {...payload...}