优化斗鱼充值

This commit is contained in:
yml2213
2026-08-13 21:43:27 +08:00
parent 902e669614
commit 0bf69c1240
19 changed files with 450 additions and 266 deletions
+25 -13
View File
@@ -740,7 +740,7 @@ class DouyuBatchRunner:
@classmethod
def _supplier_order_status(cls, payload: dict) -> int | None:
"""提取供应商订单状态,文档约定 0-4。"""
return cls._to_int(cls._supplier_value(payload, "order_status", "orderStatus"))
return cls._to_int(cls._supplier_value(payload, "order_status", "orderStatus", "supplier_order_status"))
@classmethod
def _supplier_message(cls, payload: dict) -> str:
@@ -768,13 +768,20 @@ class DouyuBatchRunner:
result: dict,
) -> int | None:
"""轮询供应商直充订单至结束状态。"""
order_no = str(result["customer_order_no"])
order_no = str(result["out_order_id"])
deadline = time.monotonic() + DOUYU_PAYMENT_POLL_SECONDS
poll_count = 0
result["payment_polling"] = True
while not self._stop.is_set() and time.monotonic() <= deadline:
try:
payload = client.query_order(customer_order_no=order_no)
# 回调可能已在另一个数据库会话中结束订单,刷新后直接使用其结果。
db.refresh(task)
if task.status in {"success", "failed"}:
callback_result = task.result if isinstance(task.result, dict) else result
result.update(callback_result)
result["payment_polling"] = False
return self._supplier_order_status(callback_result)
payload = client.query_order(order_no)
code = self._to_int(self._supplier_value(payload, "code"))
status = self._supplier_order_status(payload)
poll_count += 1
@@ -2505,10 +2512,12 @@ class DouyuBatchRunner:
if not charge_account:
raise FishFinRechargeError("账号缺少斗鱼 UID,无法发起供应商直充")
# task.id 是唯一且稳定的商户单号来源,重试时不会创建不同的供应商订单。
# task.id 是唯一且稳定的外部订单号来源,重试时不会创建不同的供应商订单。
order_no = f"DYGF{task.id}"
# customer_price 是用户选择的充值面值;goodsFaceValue=0.993 是供货成本,不能作为支付金额。
customer_price = Decimal(amount)
task.supplier_out_order_id = order_no
db.commit()
# pay_amount 是用户选择的充值面值;goodsFaceValue=0.993 是供货成本,不能作为支付金额。
pay_amount = Decimal(amount)
def trace(event: dict) -> None:
"""将脱敏供应商协议信息输出到任务日志,便于线上联调。"""
stage = event.get("stage")
@@ -2517,8 +2526,8 @@ class DouyuBatchRunner:
self._push_log(
"info",
"供应商直充请求 "
f"path={event.get('path')} uid={params.get('charge_account')} "
f"buy_num={params.get('buy_num')} customer_price={params.get('customer_price')} "
f"path={event.get('path')} out_order_id={params.get('out_order_id')} "
f"buy_num={params.get('buy_num')} pay_amount={params.get('pay_amount')} "
f"product_id={params.get('product_id')} types={event.get('parameter_types')} "
f"sign_digest={event.get('sign_digest')}",
)
@@ -2550,22 +2559,25 @@ class DouyuBatchRunner:
client = FishFinRechargeClient(FishFinRechargeConfig.from_env(), trace=trace)
order_payload = client.create_order(
charge_account=charge_account,
buy_num=amount,
customer_price=customer_price,
customer_order_no=order_no,
pay_amount=pay_amount,
out_order_id=order_no,
product_id=product_id,
recharge_arg=[{"templateName": template_name, "templateVal": charge_account}],
order_type=0,
notify_url=client.config.notify_url,
)
code = self._to_int(self._supplier_value(order_payload, "code"))
status = self._supplier_order_status(order_payload)
result = {
"recharge_channel": "supplier_api",
"customer_order_no": order_no,
"out_order_id": order_no,
"order_id": self._supplier_value(order_payload, "order_id", "orderId"),
"charge_account": charge_account,
"buy_num": amount,
"product_id": product_id,
"customer_price": format(customer_price.normalize(), "f"),
"pay_amount": format(pay_amount.normalize(), "f"),
"order_type": 0,
"supplier_code": code,
"supplier_order_status": status,
"supplier_order": self._supplier_result(order_payload),