|
@@ -931,7 +931,7 @@ class AccountService:
|
|
|
enterprise_id: str,
|
|
enterprise_id: str,
|
|
|
) -> str | bool | None:
|
|
) -> str | bool | None:
|
|
|
"""查询单笔转账详情并更新本地记录
|
|
"""查询单笔转账详情并更新本地记录
|
|
|
- 优先调 fund.trans.common.query,无权限时改用 consume.detail.query(用 fund_order_id 查)
|
|
|
|
|
|
|
+ 优先调 fund.trans.common.query,无权限时改用 consume.detail.query(用 order_no 当 pay_no 查)
|
|
|
返回: 新状态str / False(无权限) / None(失败/无变化)
|
|
返回: 新状态str / False(无权限) / None(失败/无变化)
|
|
|
"""
|
|
"""
|
|
|
from sqlalchemy import select, update as sa_update
|
|
from sqlalchemy import select, update as sa_update
|
|
@@ -939,11 +939,10 @@ class AccountService:
|
|
|
|
|
|
|
|
from app.core.alipay import AlipayClient
|
|
from app.core.alipay import AlipayClient
|
|
|
|
|
|
|
|
- # 先查本地记录,获取 fund_order_id
|
|
|
|
|
|
|
+ # 先查本地记录
|
|
|
tf_stmt = select(TransferModel).where(TransferModel.out_biz_no == out_biz_no)
|
|
tf_stmt = select(TransferModel).where(TransferModel.out_biz_no == out_biz_no)
|
|
|
tf_result = await auth.db.execute(tf_stmt)
|
|
tf_result = await auth.db.execute(tf_stmt)
|
|
|
local_transfer = tf_result.scalar_one_or_none()
|
|
local_transfer = tf_result.scalar_one_or_none()
|
|
|
- fund_order_id = local_transfer.fund_order_id if local_transfer else None
|
|
|
|
|
|
|
|
|
|
# — 方案A: fund.trans.common.query —
|
|
# — 方案A: fund.trans.common.query —
|
|
|
try:
|
|
try:
|
|
@@ -985,9 +984,10 @@ class AccountService:
|
|
|
except ImportError:
|
|
except ImportError:
|
|
|
pass
|
|
pass
|
|
|
|
|
|
|
|
- # — 方案B: consume.detail.query(用 fund_order_id 当 pay_no 查) —
|
|
|
|
|
- if not fund_order_id:
|
|
|
|
|
- log.warning(f"无 fund_order_id 可用于查询: out_biz_no={out_biz_no}")
|
|
|
|
|
|
|
+ # — 方案B: consume.detail.query(用 order_no 当 pay_no 查) —
|
|
|
|
|
+ order_no = local_transfer.order_no if local_transfer else None
|
|
|
|
|
+ if not order_no:
|
|
|
|
|
+ log.warning(f"无 order_no 可用于查询: out_biz_no={out_biz_no}")
|
|
|
return False
|
|
return False
|
|
|
|
|
|
|
|
try:
|
|
try:
|
|
@@ -1002,7 +1002,7 @@ class AccountService:
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
model = AlipayCommerceEcConsumeDetailQueryModel()
|
|
model = AlipayCommerceEcConsumeDetailQueryModel()
|
|
|
- model.pay_no = fund_order_id
|
|
|
|
|
|
|
+ model.pay_no = order_no
|
|
|
model.enterprise_id = enterprise_id
|
|
model.enterprise_id = enterprise_id
|
|
|
|
|
|
|
|
request = AlipayCommerceEcConsumeDetailQueryRequest()
|
|
request = AlipayCommerceEcConsumeDetailQueryRequest()
|
|
@@ -1018,22 +1018,23 @@ class AccountService:
|
|
|
result.parse_response_content(response)
|
|
result.parse_response_content(response)
|
|
|
|
|
|
|
|
if not result.is_success():
|
|
if not result.is_success():
|
|
|
|
|
+ sub_code = getattr(result, 'sub_code', '') or ''
|
|
|
sub_msg = getattr(result, 'sub_msg', '') or ''
|
|
sub_msg = getattr(result, 'sub_msg', '') or ''
|
|
|
- log.warning(f"consume.detail.query 查询失败: out_biz_no={out_biz_no}, err={sub_msg}")
|
|
|
|
|
- return False
|
|
|
|
|
|
|
+ # 权限不足
|
|
|
|
|
+ if '权限' in sub_msg or 'NO_PERMISSION' in sub_code:
|
|
|
|
|
+ return False
|
|
|
|
|
+ log.warning(f"consume.detail.query 查无记录: out_biz_no={out_biz_no}, err={sub_msg}")
|
|
|
|
|
+ return None
|
|
|
|
|
|
|
|
consume_info = getattr(result, 'consume_info', None)
|
|
consume_info = getattr(result, 'consume_info', None)
|
|
|
if not consume_info:
|
|
if not consume_info:
|
|
|
- return False
|
|
|
|
|
|
|
+ return None
|
|
|
|
|
|
|
|
- # 从消费详情中提取转账状态
|
|
|
|
|
consume_type = getattr(consume_info, 'consume_type', '')
|
|
consume_type = getattr(consume_info, 'consume_type', '')
|
|
|
if consume_type != "TRANSFER":
|
|
if consume_type != "TRANSFER":
|
|
|
return None
|
|
return None
|
|
|
|
|
|
|
|
- # consume.detail.query 返回的 consume_info 没有直接的状态字段
|
|
|
|
|
- # 但如果有 notify_reason 可以判断
|
|
|
|
|
- notify_reason = getattr(consume_info, 'notify_reason', '')
|
|
|
|
|
|
|
+ notify_reason = getattr(consume_info, 'notify_reason', '') or ''
|
|
|
if 'SUCCESS' in notify_reason.upper():
|
|
if 'SUCCESS' in notify_reason.upper():
|
|
|
new_status = "SUCCESS"
|
|
new_status = "SUCCESS"
|
|
|
elif 'FAIL' in notify_reason.upper():
|
|
elif 'FAIL' in notify_reason.upper():
|
|
@@ -1043,7 +1044,7 @@ class AccountService:
|
|
|
|
|
|
|
|
update_data = {"status": new_status}
|
|
update_data = {"status": new_status}
|
|
|
pay_no = getattr(consume_info, 'pay_no', None)
|
|
pay_no = getattr(consume_info, 'pay_no', None)
|
|
|
- if pay_no:
|
|
|
|
|
|
|
+ if pay_no and pay_no != order_no:
|
|
|
update_data["order_no"] = pay_no
|
|
update_data["order_no"] = pay_no
|
|
|
|
|
|
|
|
upd = sa_update(TransferModel).where(
|
|
upd = sa_update(TransferModel).where(
|
|
@@ -1054,7 +1055,7 @@ class AccountService:
|
|
|
return new_status
|
|
return new_status
|
|
|
|
|
|
|
|
except ImportError:
|
|
except ImportError:
|
|
|
- log.warning("consume.detail.query 不可用")
|
|
|
|
|
|
|
+ log.warning("consume.detail.query SDK 不可用")
|
|
|
return False
|
|
return False
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
log.warning(f"consume.detail.query 异常: out_biz_no={out_biz_no}, err={e}")
|
|
log.warning(f"consume.detail.query 异常: out_biz_no={out_biz_no}, err={e}")
|