fix(pay): 支付回调通知响应
This commit is contained in:
@@ -9,7 +9,7 @@ from xml.parsers.expat import ExpatError
|
||||
|
||||
import xmltodict
|
||||
from asyncer import asyncify
|
||||
from fastapi import APIRouter, Request, HTTPException, status, Depends
|
||||
from fastapi import APIRouter, Request, HTTPException, status, Depends, Response
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from core.base import schema as base_schema
|
||||
@@ -93,11 +93,20 @@ async def create_order(
|
||||
return {'data': {'payment_params': payment_params, 'order_id': order.order_id}}
|
||||
|
||||
|
||||
@pay_router.post('/wechat/notify', summary='处理支付结果通知', response_model=base_schema.BaseResponse)
|
||||
@pay_router.post('/wechat/notify', summary='处理支付结果通知')
|
||||
async def wechat_notify(
|
||||
request: Request,
|
||||
db_session: AsyncSession = Depends(get_async_session)
|
||||
):
|
||||
def generate_response():
|
||||
xml_response = """
|
||||
<xml>
|
||||
<return_code><![CDATA[SUCCESS]]></return_code>
|
||||
<return_msg><![CDATA[OK]]></return_msg>
|
||||
</xml>
|
||||
"""
|
||||
return Response(content=xml_response.strip(), media_type="application/xml")
|
||||
|
||||
body = await request.body()
|
||||
logger.debug(body.decode())
|
||||
try:
|
||||
@@ -113,13 +122,12 @@ async def wechat_notify(
|
||||
|
||||
local_sign = wechat_pay_api.generate_sign(params)
|
||||
if sign != local_sign:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail='签名验证失败'
|
||||
)
|
||||
return generate_response()
|
||||
|
||||
# 处理支付结果
|
||||
db_crud = CRUD(db_session, Order)
|
||||
order = await db_crud.get(filters={'order_id': params['out_trade_no']})
|
||||
if order and order.status.value not in ['ok', 'fail']:
|
||||
record = {
|
||||
'order_id': params['out_trade_no'],
|
||||
'transaction_id': params['transaction_id']
|
||||
@@ -129,18 +137,20 @@ async def wechat_notify(
|
||||
'notify_total_fee': params['total_fee'],
|
||||
'status': base_schema.OrderStatusType.PAID
|
||||
})
|
||||
result = {'status': 200, 'message': 'OK'}
|
||||
await asyncify(add_task)(
|
||||
actions_after_payment,
|
||||
notify_params=params,
|
||||
task_id=record['order_id'],
|
||||
)
|
||||
else:
|
||||
result = {'status': 400, 'message': '支付失败'}
|
||||
record.update({
|
||||
'status': base_schema.OrderStatusType.FAIL,
|
||||
})
|
||||
|
||||
await db_crud.update(data=record, filters={'order_id': record['order_id']})
|
||||
|
||||
return result
|
||||
await db_crud.update(
|
||||
data=record,
|
||||
filters={
|
||||
'order_id': record['order_id'], 'status': base_schema.OrderStatusType.WAIT_PAYMENT
|
||||
}
|
||||
)
|
||||
return generate_response()
|
||||
|
||||
@@ -24,7 +24,7 @@ pycryptodome
|
||||
xmltodict
|
||||
fastapi-limiter
|
||||
asyncer
|
||||
|
||||
apscheduler
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user