diff --git a/backend/app/routers/user_order.py b/backend/app/routers/user_order.py index 6820686..13dbf68 100644 --- a/backend/app/routers/user_order.py +++ b/backend/app/routers/user_order.py @@ -277,59 +277,97 @@ async def order_pay(request: OrderPayRequest): @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 # cursor = None - try: - # 获取回调数据 - headers = dict(request.headers) - data = await request.body() - - # 解密回调数据 - result = wxpay.callback(headers, data) - if result and result.get('event_type') == 'TRANSACTION.SUCCESS': - print("收到支付成功信息") - print(result) - out_trade_no = result.get('out_trade_no') - print(out_trade_no) - transaction_id = result.get('transaction_id') - print(transaction_id) - - - # 获取数据库连接 - # connection = get_connection() - # cursor = connection.cursor(dictionary=True) - # - # # 查询订单并锁定 - # cursor.execute(""" - # SELECT order_id, payable_price - # FROM orders - # WHERE out_trade_no = %s - # FOR UPDATE""", (out_trade_no,)) - # order = cursor.fetchone() - # - # cursor.execute(""" - # UPDATE orders SET - # order_status = 'completed', - # payment_method = 'wechat', - # wx_transaction_id = %s, - # settlement_time = NOW() - # WHERE order_id = %s""", - # (transaction_id, order['order_id'])) - # - # - # connection.commit() - - # 返回微信要求的成功响应 - return JSONResponse(content={"code": "SUCCESS", "message": "OK"}) - - except Exception as e: - logging.error(f"回调处理异常: {str(e)}", exc_info=True) - # if connection: - # connection.rollback() - return JSONResponse(content={"code": "FAIL", "message": "系统错误"}, status_code=500) - # finally: - # if cursor: - # cursor.close() - # if connection: - # connection.close() + # try: + # # 获取回调数据 + # headers = dict(request.headers) + # data = await request.body() + # + # # 解密回调数据 + # result = wxpay.callback(headers, data) + # if result and result.get('event_type') == 'TRANSACTION.SUCCESS': + # print("收到支付成功信息") + # print(result) + # out_trade_no = result.get('out_trade_no') + # print(out_trade_no) + # transaction_id = result.get('transaction_id') + # print(transaction_id) + # + # + # # 获取数据库连接 + # # connection = get_connection() + # # cursor = connection.cursor(dictionary=True) + # # + # # # 查询订单并锁定 + # # cursor.execute(""" + # # SELECT order_id, payable_price + # # FROM orders + # # WHERE out_trade_no = %s + # # FOR UPDATE""", (out_trade_no,)) + # # order = cursor.fetchone() + # # + # # cursor.execute(""" + # # UPDATE orders SET + # # order_status = 'completed', + # # payment_method = 'wechat', + # # wx_transaction_id = %s, + # # settlement_time = NOW() + # # WHERE order_id = %s""", + # # (transaction_id, order['order_id'])) + # # + # # + # # connection.commit() + # + # # 返回微信要求的成功响应 + # return JSONResponse(content={"code": "SUCCESS", "message": "OK"}) + # + # except Exception as e: + # logging.error(f"回调处理异常: {str(e)}", exc_info=True) + # # if connection: + # # connection.rollback() + # return JSONResponse(content={"code": "FAIL", "message": "系统错误"}, status_code=500) + # # finally: + # # if cursor: + # # cursor.close() + # # if connection: + # # connection.close()