跳转到主要内容

收款 Webhook

当收款状态发生变更时,A55 向您的 webhook_url 发送 POST 请求,携带 JSON 载荷。


载荷字段

字段类型说明
charge_uuidstring (UUID)收款标识符
statusstring当前状态:confirmederrorrefundedchargeback_requestedchargeback_refunded
transaction_referencestring您的外部引用(如创建时提供)
subscription_uuidstring订阅标识符(如适用)
amountnumber收款金额
currencystringISO 货币代码
created_atstring创建时间戳(ISO 8601)
updated_atstring最后更新时间戳(ISO 8601)

各状态载荷示例

{
"charge_uuid": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"status": "confirmed",
"transaction_reference": "ord_001",
"amount": 100.50,
"currency": "BRL",
"created_at": "2026-03-20T10:00:00Z",
"updated_at": "2026-03-20T10:00:15Z"
}

处理代码

import json, os
from flask import Flask, request, Response

app = Flask(__name__)

@app.route("/webhooks/a55", methods=["POST"])
def handle_charge_webhook():
"""处理收款状态变更 Webhook。"""
raw = request.get_data()
sig = request.headers.get("X-Webhook-Signature", "")
ts = request.headers.get("X-Webhook-Timestamp", "")

if not verify_signature(raw, sig, ts, os.environ["WEBHOOK_SECRET"]):
return Response(status=401)

event = json.loads(raw)
charge_uuid = event["charge_uuid"]
status = event["status"]

if already_processed(charge_uuid, status):
return Response(status=200)

if status == "confirmed":
fulfill_order(charge_uuid)
elif status == "error":
notify_payment_failed(charge_uuid)
elif status == "refunded":
process_refund(charge_uuid)
elif status == "chargeback_requested":
flag_chargeback(charge_uuid)

mark_processed(charge_uuid, status)
return Response(status=200)

最佳实践

实践原因
首先验证 HMAC 签名防止伪造事件
立即返回 200避免超时和重试
在后台队列中处理耗时操作不应阻塞 HTTP 处理器
charge_uuid + status 去重至少一次投递意味着可能收到重复事件
记录原始载荷调试审计追踪
切勿仅依赖重定向 URL 中的状态始终通过 Webhook 确认