Skip to content

Payment Schema

Schema payment chứa 1 model quản lý cấu hình endpoint webhook để gửi các sự kiện thanh toán và giao dịch đến hệ thống bên ngoài.

Nguồn: packages/core/src/models/schemas/payment/

WebhookConfig

Đăng ký endpoint webhook với lọc loại sự kiện, header tùy chỉnh, và cấu hình retry.

Các Cột

CộtKiểuRàng buộcMô tả
idtextPKSnowflake ID
nametextNOT NULLTên webhook
urltextNOT NULLURL endpoint webhook
event_typestext[]NOT NULLMảng loại sự kiện để lắng nghe
statustextNOT NULL, DEFAULT ACTIVATED, indexedTrạng thái webhook
headersjsonbDEFAULT {}Header HTTP tùy chỉnh (ví dụ: token xác thực)
metadatajsonbDEFAULT { timeoutMs: 30000, maxRetries: 3 }Cấu hình retry và timeout
created_attimestamptzThời gian tạo
modified_attimestamptzThời gian sửa đổi cuối

Lưu ý: WebhookConfig sử dụng generateIdColumnDefs({ id: { dataType: 'string' } }) + generateTzColumnDefs() trực tiếp (không có deletedAt hoặc hỗ trợ xóa mềm).

Giá trị Trạng thái

Trạng tháiGiá trịMô tả
ACTIVATED201_ACTIVATEDWebhook đang hoạt động và sẽ nhận sự kiện
DEACTIVATED401_DEACTIVATEDWebhook tạm dừng, sự kiện sẽ không được gửi
ARCHIVED405_ARCHIVEDWebhook đã lưu trữ và vô hiệu hóa vĩnh viễn

Các trạng thái được định nghĩa trong WebhookConfigStatuses (từ packages/core/src/common/constants.ts).

Schema Metadata

Trường metadata được xác thực bởi một Zod schema:

typescript
export const WebhookConfigMetadataSchema = z.object({
  timeoutMs: z.number().int().min(1000).max(300_000).default(30_000),
  maxRetries: z.number().int().min(0).max(10).default(3),
});
TrườngKiểuPhạm viMặc địnhMô tả
timeoutMsinteger1.000 -- 300.00030.000Timeout yêu cầu HTTP tính bằng mili giây
maxRetriesinteger0 -- 103Số lần thử lại tối đa khi thất bại

Loại Sự kiện

Mảng event_types lọc những sự kiện nào kích hoạt webhook này. Các loại sự kiện MQ-Pay phổ biến bao gồm:

Sự kiệnMô tả
mq-pay:transaction.createdGiao dịch thanh toán mới được tạo
mq-pay:transaction.settledGiao dịch đã thanh toán đầy đủ
mq-pay:transaction.cancelledGiao dịch đã hủy
mq-pay:attempt.createdLần thử thanh toán được khởi tạo
mq-pay:attempt.sentLần thử thanh toán đã gửi đến nhà cung cấp
mq-pay:attempt.successLần thử thanh toán thành công
mq-pay:attempt.failedLần thử thanh toán thất bại
mq-pay:attempt.expiredLần thử thanh toán hết hạn
mq-pay:attempt.cancelledLần thử thanh toán đã hủy
mq-pay:refund.successHoàn tiền thành công
mq-pay:refund.failedHoàn tiền thất bại

Gửi Webhook

WebhookDispatcherService (trong @nx/payment) xử lý việc gửi sự kiện:

  • Gửi fire-and-forget với retry theo backoff lũy thừa
  • Header tùy chỉnh từ trường headers được bao gồm trong mỗi yêu cầu
  • Giới hạn timeout và retry được đọc từ trường metadata
  • Chỉ các webhook ACTIVATED mới nhận sự kiện

Sơ đồ Thực thể

Tài liệu Liên quan

Proprietary and Confidential. Unauthorized copying, distribution, or use of this software is strictly prohibited.