diff --git a/__pycache__/wechat_pay_worker.cpython-310.pyc b/__pycache__/wechat_pay_worker.cpython-310.pyc index e5e7f403..72949414 100644 Binary files a/__pycache__/wechat_pay_worker.cpython-310.pyc and b/__pycache__/wechat_pay_worker.cpython-310.pyc differ diff --git a/wechat_pay_worker.py b/wechat_pay_worker.py index a16574a7..239acccb 100644 --- a/wechat_pay_worker.py +++ b/wechat_pay_worker.py @@ -15,12 +15,15 @@ 返回值: 成功返回 0,失败返回 1 - 输出 JSON 格式的结果 + 输出 JSON 格式的结果(到 stdout) + 调试信息输出到 stderr """ import sys import json import os +import io +from contextlib import redirect_stdout # 添加当前目录到路径 sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) @@ -28,21 +31,36 @@ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) def create_order(order_no, total_fee, body, product_id=None): """创建微信支付订单""" + # 捕获所有 stdout 输出(wechat_pay.py 的调试信息) + debug_output = io.StringIO() + try: - from wechat_pay import create_wechat_pay_instance + with redirect_stdout(debug_output): + from wechat_pay import create_wechat_pay_instance - wechat_pay = create_wechat_pay_instance() - result = wechat_pay.create_native_order( - order_no=order_no, - total_fee=float(total_fee), - body=body, - product_id=product_id - ) + wechat_pay = create_wechat_pay_instance() + result = wechat_pay.create_native_order( + order_no=order_no, + total_fee=float(total_fee), + body=body, + product_id=product_id + ) + # 调试信息输出到 stderr + debug_info = debug_output.getvalue() + if debug_info: + print(debug_info, file=sys.stderr) + + # JSON 结果输出到 stdout print(json.dumps(result, ensure_ascii=False)) return result.get('success', False) except Exception as e: + # 调试信息输出到 stderr + debug_info = debug_output.getvalue() + if debug_info: + print(debug_info, file=sys.stderr) + print(json.dumps({ 'success': False, 'error': f'{type(e).__name__}: {str(e)}' @@ -52,16 +70,28 @@ def create_order(order_no, total_fee, body, product_id=None): def query_order(order_no): """查询订单状态""" - try: - from wechat_pay import create_wechat_pay_instance + debug_output = io.StringIO() - wechat_pay = create_wechat_pay_instance() - result = wechat_pay.query_order(order_no=order_no) + try: + with redirect_stdout(debug_output): + from wechat_pay import create_wechat_pay_instance + + wechat_pay = create_wechat_pay_instance() + result = wechat_pay.query_order(order_no=order_no) + + # 调试信息输出到 stderr + debug_info = debug_output.getvalue() + if debug_info: + print(debug_info, file=sys.stderr) print(json.dumps(result, ensure_ascii=False)) return result.get('success', False) except Exception as e: + debug_info = debug_output.getvalue() + if debug_info: + print(debug_info, file=sys.stderr) + print(json.dumps({ 'success': False, 'error': f'{type(e).__name__}: {str(e)}' @@ -71,10 +101,19 @@ def query_order(order_no): def check_config(): """检查微信支付配置""" - try: - from wechat_pay import check_wechat_pay_ready + debug_output = io.StringIO() + + try: + with redirect_stdout(debug_output): + from wechat_pay import check_wechat_pay_ready + + is_ready, msg = check_wechat_pay_ready() + + # 调试信息输出到 stderr + debug_info = debug_output.getvalue() + if debug_info: + print(debug_info, file=sys.stderr) - is_ready, msg = check_wechat_pay_ready() result = { 'success': is_ready, 'message': msg @@ -84,6 +123,10 @@ def check_config(): return is_ready except Exception as e: + debug_info = debug_output.getvalue() + if debug_info: + print(debug_info, file=sys.stderr) + print(json.dumps({ 'success': False, 'error': f'{type(e).__name__}: {str(e)}'