1
This commit is contained in:
@@ -176,8 +176,10 @@ async def wechat_callback(request: Request, db: AsyncSession = Depends(get_db)):
|
||||
|
||||
decrypted_data = json.loads(decrypted_str)
|
||||
|
||||
event_type = body_data.get("event_type", "")
|
||||
|
||||
# 处理支付成功回调
|
||||
if body_data.get("event_type") == "TRANSACTION.SUCCESS":
|
||||
if event_type == "TRANSACTION.SUCCESS":
|
||||
order_no = decrypted_data.get("out_trade_no", "")
|
||||
transaction_id = decrypted_data.get("transaction_id", "")
|
||||
amount_info = decrypted_data.get("amount", {})
|
||||
@@ -190,6 +192,30 @@ async def wechat_callback(request: Request, db: AsyncSession = Depends(get_db)):
|
||||
f"transaction_id={transaction_id}, amount={total_amount}"
|
||||
)
|
||||
|
||||
# 处理退款回调
|
||||
elif event_type == "REFUND.SUCCESS":
|
||||
order_no = decrypted_data.get("out_trade_no", "")
|
||||
refund_id = decrypted_data.get("refund_id", "")
|
||||
refund_status = decrypted_data.get("status", "")
|
||||
|
||||
if order_no and refund_status == "SUCCESS":
|
||||
# 更新订单状态为已退款
|
||||
from app.models import PaymentOrder
|
||||
from sqlalchemy import select
|
||||
|
||||
result = await db.execute(select(PaymentOrder).where(PaymentOrder.order_no == order_no))
|
||||
order = result.scalar_one_or_none()
|
||||
|
||||
if order and order.status == "refunding":
|
||||
order.status = "refunded"
|
||||
order.transaction_id = refund_id
|
||||
await db.commit()
|
||||
|
||||
logger.info(
|
||||
f"WeChat refund callback processed: order_no={order_no}, "
|
||||
f"refund_id={refund_id}, status={refund_status}"
|
||||
)
|
||||
|
||||
return {"code": "SUCCESS", "message": "OK"}
|
||||
except Exception as e:
|
||||
logger.exception(f"WeChat callback processing error: {e}")
|
||||
|
||||
Reference in New Issue
Block a user