优化斗鱼充值日志与配置

This commit is contained in:
yml2213
2026-08-14 11:27:53 +08:00
parent 0bf69c1240
commit e2a45b7893
13 changed files with 193 additions and 52 deletions
+32 -8
View File
@@ -116,6 +116,28 @@ class FishFinRechargeClient:
"""生成单行 JSON 调试文本,避免日志被控制字符截断。"""
return json.dumps(value, ensure_ascii=False, separators=(",", ":"), default=str)
@classmethod
def _debug_payload(cls, payload: Mapping[str, Any]) -> dict[str, Any]:
"""调试记录只保留协议字段,签名值和密钥相关内容统一脱敏。"""
safe = dict(payload)
if "sign" in safe:
safe["sign"] = "<redacted>"
return safe
@staticmethod
def _response_value(payload: Mapping[str, Any], *keys: str) -> Any:
"""兼容供应商将订单字段放在根节点、data 或 result 节点。"""
sources = [payload]
for key in ("data", "result"):
value = payload.get(key)
if isinstance(value, Mapping):
sources.append(value)
for source in sources:
for key in keys:
if source.get(key) is not None:
return source[key]
return None
def verify_response_sign(self, payload: Mapping[str, Any], method: str) -> bool:
"""校验带 sign 的响应;供应商未返回 sign 时由调用方决定是否接受。"""
received = str(payload.get("sign") or "").strip().lower()
@@ -153,10 +175,12 @@ class FishFinRechargeClient:
trace_event.update({
"url": url,
"content_type": "application/json",
# 调试模式按用户要求保留完整协议内容,包含 sign 和 AppSecret。
"json_body": request_params,
"json_body": self._debug_payload(request_params),
"sign_params": sign_params,
"sign_source": f"{sign_query}{method.upper()}{self.config.app_secret}",
# 不把可重放签名原文或 AppSecret 写入日志,只记录待签名串摘要。
"sign_source_digest": hashlib.sha256(
f"{sign_query}{method.upper()}".encode("utf-8")
).hexdigest()[:12],
})
self.trace(trace_event)
try:
@@ -179,9 +203,10 @@ class FishFinRechargeClient:
"http_status": response.status_code,
"code": payload.get("code"),
"message": payload.get("msg") or payload.get("message") or "",
"keys": sorted(payload.keys()),
"data_keys": sorted((payload.get("data") or {}).keys()) if isinstance(payload.get("data"), dict) else [],
"result_keys": sorted((payload.get("result") or {}).keys()) if isinstance(payload.get("result"), dict) else [],
"out_order_id": self._response_value(payload, "out_order_id", "outOrderId"),
"order_id": self._response_value(payload, "order_id", "orderId"),
"order_status": self._response_value(payload, "order_status", "orderStatus"),
"fail_reason": self._response_value(payload, "fail_reason", "failReason"),
}
if self.config.debug:
trace_event.update({
@@ -190,8 +215,7 @@ class FishFinRechargeClient:
for key, value in response.headers.items()
if key.lower() in {"content-type", "x-request-id", "request-id"}
},
"response_body": payload,
"response_text": response.text,
"response_body": self._debug_payload(payload),
})
self.trace(trace_event)
return payload