API Webhooks
Tài liệu về cách sử dụng API webhooks thông qua OAuth2 trong SePay.
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/webhooksLấy danh sách webhooks với các tùy chọn lọc
/api/v1/webhooks/{id}Lấy thông tin chi tiết về một webhook cụ thể
/api/v1/webhooksTạo webhook mới
/api/v1/webhooks/{id}Cập nhật thông tin webhook
/api/v1/webhooks/{id}Xóa webhook
Lấy danh sách webhooks
/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": "Api_Key",
"webhook_url": "https://example.com/webhook/payment",
"is_verify_payment": true,
"skip_if_no_code": true,
"retry_conditions": {
"non_2xx_status_code": 0
},
"only_va": true,
"active": true,
"created_at": "2025-02-15 14:23:56",
"api_key": "a7c3b4e5f6a7b8c9d0e1f2a3b4c5d6e7",
"request_content_type": "Json",
"bank_sub_account_ids": [25, 26]
},
{
"id": 22,
"bank_account_id": 18,
"name": "Tích hợp với CRM",
"event_type": "All",
"authen_type": "OAuth2.0",
"webhook_url": "https://crm.example.com/webhook/transactions",
"is_verify_payment": false,
"skip_if_no_code": false,
"retry_conditions": {
"non_2xx_status_code": 0
},
"only_va": false,
"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
/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,
"retry_conditions": {
"only_va": true,
"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
/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ố cho từng kiểu xác thực:
OAuth2.0:
oauth2_access_token_url(bắt buộc) – URL để lấy access tokenoauth2_client_id(bắt buộc) – Client IDoauth2_client_secret(bắt buộc) – Client Secret
Api_Key:
api_key(bắt buộc) – API Key dùng để xác thựcrequest_content_type(bắt buộc) – Kiểu nội dung yêu cầu (Json, multipart_form-data)
No_Authen:
request_content_type(bắt buộc) – Kiểu nội dung yêu cầu (Json, multipart_form-data)
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"}'
{
"message": "Thêm WebHooks thành công",
"id": 23
}Cập nhật webhook
/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: Tham số yêu cầu giống như khi tạo webhook, nhưng tất cả đều là tùy chọn. Bạn chỉ cần cung cấp các tham số cần thay đổi.
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}'
{
"message": "Cập nhật WebHooks thành công",
"id": "23"
}Xóa webhook
/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"
{
"message": "Xóa WebHooks thành công"
}Mã lỗi
Dưới đây là các mã lỗi có thể gặp khi sử dụng API webhooks:
400validation_errorLỗi xác thực dữ liệu đầu vào
401unauthorizedToken không hợp lệ hoặc hết hạn
403forbiddenKhông có quyền truy cập vào tài nguyên này
404not_foundKhông tìm thấy webhook