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(