会员积分改版V6 修复订单异步时区校验BUG
This commit is contained in:
@@ -217,11 +217,30 @@ async def wechat_callback(request: Request, db: AsyncSession = Depends(get_db)):
|
||||
total_amount = amount_info.get("total", 0) / 100 # 转换为元
|
||||
|
||||
if order_no:
|
||||
await process_payment_success_by_order_no(db, order_no, transaction_id, total_amount)
|
||||
logger.info(
|
||||
f"WeChat callback processed: order_no={order_no}, "
|
||||
f"transaction_id={transaction_id}, amount={total_amount}"
|
||||
)
|
||||
try:
|
||||
processed = await process_payment_success_by_order_no(
|
||||
db, order_no, transaction_id, total_amount
|
||||
)
|
||||
if processed:
|
||||
logger.info(
|
||||
f"WeChat callback processed: order_no={order_no}, "
|
||||
f"transaction_id={transaction_id}, amount={total_amount}"
|
||||
)
|
||||
else:
|
||||
logger.warning(
|
||||
f"WeChat payment success not fulfilled locally: order_no={order_no}, "
|
||||
f"transaction_id={transaction_id}, amount={total_amount}"
|
||||
)
|
||||
except Exception as e:
|
||||
# 微信已经通过验签、解密并明确通知支付成功。
|
||||
# 本地履约异常属于项目内部故障:回滚本地事务,但仍向微信返回 SUCCESS,
|
||||
# 后续由 pending 主动查单重试或日志人工对账处理。
|
||||
await db.rollback()
|
||||
logger.exception(
|
||||
f"WeChat payment fulfillment error: order_no={order_no}, "
|
||||
f"transaction_id={transaction_id}, error={e}"
|
||||
)
|
||||
return {"code": "SUCCESS", "message": "OK"}
|
||||
|
||||
# 处理退款回调
|
||||
elif event_type == "REFUND.SUCCESS":
|
||||
@@ -402,19 +421,26 @@ async def cancel_order(
|
||||
if order.status != "pending":
|
||||
raise HTTPException(status_code=400, detail=f"订单状态为{order.status},无法取消")
|
||||
|
||||
# If it's an Alipay or WeChat order, call close API first
|
||||
# Alipay/WeChat must confirm remote close before local cancellation.
|
||||
# If close fails or the remote state is uncertain, keep the local order
|
||||
# pending so a later gateway query can still discover a successful payment.
|
||||
db_configs = await _get_payment_configs(db)
|
||||
closed = True
|
||||
if order.payment_method == "alipay":
|
||||
try:
|
||||
await _close_alipay_order(db, order, db_configs)
|
||||
except Exception as e:
|
||||
logger.exception(f"Failed to close Alipay order {order_no}: {e}")
|
||||
closed = await _close_alipay_order(db, order, db_configs)
|
||||
elif order.payment_method == "wechat":
|
||||
try:
|
||||
from app.services.payment import _close_wechat_order
|
||||
await _close_wechat_order(db, order, db_configs)
|
||||
except Exception as e:
|
||||
logger.exception(f"Failed to close WeChat order {order_no}: {e}")
|
||||
from app.services.payment import _close_wechat_order
|
||||
closed = await _close_wechat_order(db, order, db_configs)
|
||||
|
||||
if not closed:
|
||||
logger.warning(
|
||||
f"ORDER_CANCEL_CLOSE_PENDING order_no={order_no} "
|
||||
f"user={current_user.id} method={order.payment_method}"
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail="支付渠道暂未确认订单关闭,请稍后重试",
|
||||
)
|
||||
|
||||
order.status = "cancelled"
|
||||
if order.upgrade_period_ids_json:
|
||||
|
||||
Reference in New Issue
Block a user