Giới thiệu
API Webhooks của SePay cho phép bạn quản lý các webhook để nhận thông báo thời gian thực về giao dịch. Webhook là một cách hiệu quả để tự động hóa quy trình thanh toán, giúp hệ thống của bạn nhận được thông báo ngay khi có giao dịch mới.
Để sử dụng API này, bạn cần có các quyền tương ứng trong phạm vi (scope) của Access Token: webhook:read để xem, webhook:write để tạo/cập nhật, và webhook:delete để xóa webhook.
Các Endpoints
API Webhooks cung cấp các endpoints sau:
/api/v1/webhooks/api/v1/webhooks/{id}/api/v1/webhooks/api/v1/webhooks/{id}/api/v1/webhooks/{id}Lấy danh sách webhooks
https://my.sepay.vn/api/v1/webhooksAuthorization: Bearer {YOUR_ACCESS_TOKEN}
Endpoint này trả về danh sách webhooks của công ty bạn. Bạn có thể lọc kết quả theo nhiều tiêu chí khác nhau.
Quyền yêu cầu:
- Scope:
webhook:read - Quyền người dùng: Webhooks (Xem danh sách webhooks)
Tham số truy vấn:
curl -G "https://my.sepay.vn/api/v1/webhooks" \-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \--data-urlencode "active=1" \--data-urlencode "page=1" \--data-urlencode "limit=20"
{
"status": "success",
"data": [
{
"id": 23,
"bank_account_id": 19,
"name": "Tích hợp với shop online",
"event_type": "In_only",
"authen_type": "HMAC_SHA256",
"webhook_url": "https://example.com/webhook/payment",
"is_verify_payment": true,
"skip_if_no_code": true,
"prefix_filters": [],
"retry_conditions": { "non_2xx_status_code": 0 },
"only_va": true,
"va_mode": "list",
"bank_mode": "single",
"alert_threshold": 3,
"alert_event_types": ["failure", "recovery"],
"active": true,
"created_at": "2025-02-15 14:23:56",
"secret_key": "whsec_a7c3b4e5f6a7b8c9d0e1f2a3b4c5d6e7",
"request_content_type": "Json",
"bank_sub_account_ids": [25, 26]
},
{
"id": 22,
"bank_account_id": 0,
"name": "Webhook toàn công ty",
"event_type": "All",
"authen_type": "OAuth2.0",
"webhook_url": "https://crm.example.com/webhook/transactions",
"is_verify_payment": false,
"skip_if_no_code": false,
"prefix_filters": ["SEVQR", "ORDER"],
"retry_conditions": { "non_2xx_status_code": 0 },
"only_va": false,
"va_mode": "all",
"bank_mode": "multi",
"alert_threshold": null,
"alert_event_types": null,
"active": true,
"created_at": "2025-02-10 09:45:32",
"oauth2_client_id": "client_id_example",
"oauth2_client_secret": "client_secret_example",
"oauth2_access_token_url": "https://crm.example.com/oauth/token"
}
],
"meta": {
"pagination": {
"total": 5,
"per_page": 20,
"current_page": 1,
"last_page": 1
}
}
}Lấy chi tiết webhook
https://my.sepay.vn/api/v1/webhooks/{id}Authorization: Bearer {YOUR_ACCESS_TOKEN}
Endpoint này trả về thông tin chi tiết của một webhook dựa trên ID.
Quyền yêu cầu:
- Scope:
webhook:read - Quyền người dùng: Webhooks (Xem danh sách webhooks)
Tham số đường dẫn:
curl -X GET "https://my.sepay.vn/api/v1/webhooks/{id}" \-H "Authorization: Bearer YOUR_ACCESS_TOKEN"
{
"status": "success",
"data": {
"id": 23,
"bank_account_id": 19,
"name": "Tích hợp với shop online",
"event_type": "In_only",
"authen_type": "Api_Key",
"webhook_url": "https://example.com/webhook/payment",
"is_verify_payment": true,
"skip_if_no_code": true,
"prefix_filters": [],
"retry_conditions": {
"non_2xx_status_code": 0
},
"only_va": true,
"va_mode": "list",
"bank_mode": "single",
"alert_threshold": null,
"alert_event_types": null,
"active": true,
"created_at": "2025-02-15 14:23:56",
"api_key": "a7c3b4e5f6a7b8c9d0e1f2a3b4c5d6e7",
"request_content_type": "Json",
"bank_sub_account_ids": [25, 26]
}
}Tạo webhook mới
https://my.sepay.vn/api/v1/webhooksAuthorization: Bearer {YOUR_ACCESS_TOKEN}
Content-Type: application/json
Endpoint này cho phép bạn tạo một webhook mới để nhận thông báo về giao dịch.
Quyền yêu cầu:
- Scope:
webhook:write - Quyền người dùng: Webhooks (Thêm webhooks)
Tham số yêu cầu:
Thêm tham số theo kiểu xác thực:
Api_Key
api_key(bắt buộc): API Key, tối đa 1000 ký tựrequest_content_type(bắt buộc):Json,multipart_form-data, hoặcapplication_x-www-form-urlencoded
HMAC_SHA256 (khuyến nghị)
secret_key(bắt buộc): Secret Key, tối đa 500 ký tự. SePay ký payload bằng HMAC-SHA256 và gửi chữ ký trong headerX-SePay-Signature, kèmX-SePay-Timestamp.request_content_type(bắt buộc):Json,multipart_form-data, hoặcapplication_x-www-form-urlencoded
Hướng dẫn xác minh chữ ký xem tại tài liệu SePay Webhooks, mục HMAC-SHA256.
OAuth2.0
oauth2_access_token_url(bắt buộc): URL lấy access token. Phải là URL public.oauth2_client_id(bắt buộc): Client IDoauth2_client_secret(bắt buộc): Client Secret
No_Authen
request_content_type(bắt buộc):Json,multipart_form-data, hoặcapplication_x-www-form-urlencoded
curl -X POST "https://my.sepay.vn/api/v1/webhooks" \-H "Content-Type: application/json" \-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \-d '{"bank_account_id": 19,"name": "Tích hợp với shop online","event_type": "In_only","authen_type": "Api_Key","webhook_url": "https://example.com/webhook/payment","is_verify_payment": 1,"skip_if_no_code": 1,"active": 1,"only_va": 1,"bank_sub_account_ids": [25, 26],"retry_conditions": {"non_2xx_status_code": 1},"api_key": "a7c3b4e5f6a7b8c9d0e1f2a3b4c5d6e7","request_content_type": "Json"}'
{
"status": "success",
"message": "Webhook created successfully",
"data": { "id": 23 }
}Cập nhật webhook
https://my.sepay.vn/api/v1/webhooks/{id}Authorization: Bearer {YOUR_ACCESS_TOKEN}
Content-Type: application/json
Endpoint này cho phép bạn cập nhật thông tin của một webhook hiện có.
Quyền yêu cầu:
- Scope:
webhook:write - Quyền người dùng: Webhooks (Sửa webhooks)
Tham số đường dẫn:
Tham số yêu cầu: giống khi tạo webhook, nhưng tất cả đều tùy chọn. Chỉ gửi các trường muốn thay đổi.
Lưu ý:
- Webhook dùng
bank_mode=multihoặcbank_mode=allkhông đổi đượcbank_account_idqua API. Hãy chỉnh qua giao diện web. - Gửi
bank_account_id=0phải kèmbank_mode=all.
curl -X PATCH "https://my.sepay.vn/api/v1/webhooks/{id}" \-H "Content-Type: application/json" \-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \-d '{"active": 0,"skip_if_no_code": 0}'
{
"status": "success",
"message": "Webhook updated successfully",
"data": { "active": 0, "skip_if_no_code": 0 }
}Xóa webhook
https://my.sepay.vn/api/v1/webhooks/{id}Authorization: Bearer {YOUR_ACCESS_TOKEN}
Endpoint này cho phép bạn xóa một webhook.
Quyền yêu cầu:
- Scope:
webhook:delete - Quyền người dùng: Webhooks (Xóa webhooks)
Tham số đường dẫn:
curl -X DELETE "https://my.sepay.vn/api/v1/webhooks/{id}" \-H "Authorization: Bearer YOUR_ACCESS_TOKEN"
HTTP/1.1 204 No Content
Ví dụ: webhook HMAC-SHA256
{
"bank_account_id": 19,
"name": "Webhook HMAC",
"event_type": "In_only",
"authen_type": "HMAC_SHA256",
"webhook_url": "https://example.com/webhook/payment",
"secret_key": "whsec_a1b2c3d4e5f6...",
"request_content_type": "Json",
"is_verify_payment": 1
}
SePay ký payload bằng HMAC-SHA256 và gửi chữ ký trong header X-SePay-Signature, kèm X-SePay-Timestamp. Cách xác minh xem ở tài liệu SePay Webhooks, mục HMAC-SHA256.
Ví dụ: webhook nhiều tài khoản
{
"bank_account_ids": [19, 20, 21],
"bank_mode": "multi",
"name": "Webhook nhiều tài khoản",
"event_type": "In_only",
"authen_type": "Api_Key",
"webhook_url": "https://example.com/webhook/payment",
"api_key": "a7c3b4e5f6a7b8c9d0e1f2a3b4c5d6e7",
"request_content_type": "Json",
"is_verify_payment": 1
}
Áp dụng cho toàn bộ tài khoản của công ty:
{
"bank_mode": "all",
"name": "Webhook toàn công ty",
"event_type": "All",
"authen_type": "HMAC_SHA256",
"webhook_url": "https://example.com/webhook/all",
"secret_key": "whsec_...",
"request_content_type": "Json",
"is_verify_payment": 1
}
Ví dụ: bật cảnh báo
Bật cảnh báo khi webhook lỗi liên tiếp và khi webhook phục hồi, gửi tới các kênh đã tạo trong trang Webhooks (tab Kênh cảnh báo):
{
"bank_account_id": 19,
"name": "Webhook có cảnh báo",
"event_type": "In_only",
"authen_type": "HMAC_SHA256",
"webhook_url": "https://example.com/webhook/payment",
"secret_key": "whsec_...",
"request_content_type": "Json",
"is_verify_payment": 1,
"alert_threshold": 3,
"alert_event_types": ["failure", "recovery"],
"alert_channel_ids": [5, 7]
}
Mã lỗi
400validation_errorDữ liệu đầu vào không hợp lệ (ví dụ: `webhook_url` không public, `bank_account_id=0` thiếu `bank_mode=all`).
401unauthorizedToken không hợp lệ hoặc hết hạn.
403forbiddenKhông có quyền truy cập tài nguyên này.
404not_foundKhông tìm thấy webhook.
500delete_failedXóa webhook thất bại. Gọi lại an toàn.