From 3fcc7a1608fd0485baaf905773f37d9eddbd47c5 Mon Sep 17 00:00:00 2001 From: xingc Date: Tue, 6 May 2025 14:07:59 +0800 Subject: [PATCH] =?UTF-8?q?fix(pay):=20=E4=BC=98=E5=8C=96=E6=94=AF?= =?UTF-8?q?=E4=BB=98=E5=9B=9E=E8=B0=83=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/routers/pay/routes.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/core/routers/pay/routes.py b/core/routers/pay/routes.py index 1718250..9913b7b 100644 --- a/core/routers/pay/routes.py +++ b/core/routers/pay/routes.py @@ -5,6 +5,8 @@ # @Author : xingc # @Desc : import logging +import xml +from xml.parsers.expat import ExpatError import xmltodict from fastapi import APIRouter, Request, HTTPException, status, Depends, BackgroundTasks @@ -90,11 +92,17 @@ async def wechat_notify( ): body = await request.body() logger.debug(body.decode()) - result = xmltodict.parse(body.decode()) + try: + result = xmltodict.parse(body.decode()) + except ExpatError as e: + raise HTTPException(status.HTTP_422_UNPROCESSABLE_ENTITY, detail='无法处理的实体') from e # 验证签名 - params = result['xml'] - sign = params.pop('sign') + params = result.get('xml', {}) + sign = params.pop('sign', None) + if sign is None: + raise HTTPException(status.HTTP_422_UNPROCESSABLE_ENTITY, detail='无法处理的实体') + local_sign = wechat_pay_api.generate_sign(params) if sign != local_sign: raise HTTPException(