微信支付回调debug

This commit is contained in:
ahao 2025-03-13 10:39:42 +08:00
parent 1ff60433a3
commit 6d651c4833

View File

@ -277,59 +277,97 @@ async def order_pay(request: OrderPayRequest):
@router.post("/paystatus/") @router.post("/paystatus/")
async def wxpay_notify(request: Request): async def notify(request: Request):
# 获取请求的 body 数据
body = await request.body()
# 假设 wxpay.callback 是一个同步函数
result = wxpay.callback(request.headers, body)
if result and result.get('event_type') == 'TRANSACTION.SUCCESS':
resp = result.get('resource')
appid = resp.get('appid')
mchid = resp.get('mchid')
out_trade_no = resp.get('out_trade_no')
transaction_id = resp.get('transaction_id')
trade_type = resp.get('trade_type')
trade_state = resp.get('trade_state')
trade_state_desc = resp.get('trade_state_desc')
bank_type = resp.get('bank_type')
attach = resp.get('attach')
success_time = resp.get('success_time')
payer = resp.get('payer')
amount = resp.get('amount').get('total')
print(amount)
print(success_time)
print(payer)
print(attach)
print(bank_type)
print(trade_state)
print(trade_state_desc)
print(trade_type)
print(transaction_id)
print(out_trade_no)
return JSONResponse(content={'code': 'SUCCESS', 'message': '成功'}, status_code=200)
else:
return JSONResponse(content={'code': 'FAILED', 'message': '失败'}, status_code=500)
# async def wxpay_notify(request: Request):
# connection = None # connection = None
# cursor = None # cursor = None
try: # try:
# 获取回调数据 # # 获取回调数据
headers = dict(request.headers) # headers = dict(request.headers)
data = await request.body() # data = await request.body()
#
# 解密回调数据 # # 解密回调数据
result = wxpay.callback(headers, data) # result = wxpay.callback(headers, data)
if result and result.get('event_type') == 'TRANSACTION.SUCCESS': # if result and result.get('event_type') == 'TRANSACTION.SUCCESS':
print("收到支付成功信息") # print("收到支付成功信息")
print(result) # print(result)
out_trade_no = result.get('out_trade_no') # out_trade_no = result.get('out_trade_no')
print(out_trade_no) # print(out_trade_no)
transaction_id = result.get('transaction_id') # transaction_id = result.get('transaction_id')
print(transaction_id) # print(transaction_id)
#
#
# 获取数据库连接 # # 获取数据库连接
# connection = get_connection() # # connection = get_connection()
# cursor = connection.cursor(dictionary=True) # # cursor = connection.cursor(dictionary=True)
# # #
# # 查询订单并锁定 # # # 查询订单并锁定
# cursor.execute(""" # # cursor.execute("""
# SELECT order_id, payable_price # # SELECT order_id, payable_price
# FROM orders # # FROM orders
# WHERE out_trade_no = %s # # WHERE out_trade_no = %s
# FOR UPDATE""", (out_trade_no,)) # # FOR UPDATE""", (out_trade_no,))
# order = cursor.fetchone() # # order = cursor.fetchone()
# # #
# cursor.execute(""" # # cursor.execute("""
# UPDATE orders SET # # UPDATE orders SET
# order_status = 'completed', # # order_status = 'completed',
# payment_method = 'wechat', # # payment_method = 'wechat',
# wx_transaction_id = %s, # # wx_transaction_id = %s,
# settlement_time = NOW() # # settlement_time = NOW()
# WHERE order_id = %s""", # # WHERE order_id = %s""",
# (transaction_id, order['order_id'])) # # (transaction_id, order['order_id']))
# # #
# # #
# connection.commit() # # connection.commit()
#
# 返回微信要求的成功响应 # # 返回微信要求的成功响应
return JSONResponse(content={"code": "SUCCESS", "message": "OK"}) # return JSONResponse(content={"code": "SUCCESS", "message": "OK"})
#
except Exception as e: # except Exception as e:
logging.error(f"回调处理异常: {str(e)}", exc_info=True) # logging.error(f"回调处理异常: {str(e)}", exc_info=True)
# if connection: # # if connection:
# connection.rollback() # # connection.rollback()
return JSONResponse(content={"code": "FAIL", "message": "系统错误"}, status_code=500) # return JSONResponse(content={"code": "FAIL", "message": "系统错误"}, status_code=500)
# finally: # # finally:
# if cursor: # # if cursor:
# cursor.close() # # cursor.close()
# if connection: # # if connection:
# connection.close() # # connection.close()