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