This commit is contained in:
jeremygan2021
2026-02-10 22:31:00 +08:00
parent 15c3954e75
commit 0ea5975c68
3 changed files with 23 additions and 4 deletions

View File

@@ -33,10 +33,14 @@ def get_wechat_pay_client():
""" """
获取微信支付 V3 客户端实例的辅助函数 获取微信支付 V3 客户端实例的辅助函数
""" """
print(f"正在获取微信支付配置...")
wechat_config = WeChatPayConfig.objects.filter(is_active=True).first() wechat_config = WeChatPayConfig.objects.filter(is_active=True).first()
if not wechat_config: if not wechat_config:
print("错误: 数据库中没有激活的 WeChatPayConfig")
return None, "支付配置未找到" return None, "支付配置未找到"
print(f"找到配置: ID={wechat_config.id}, MCH_ID={wechat_config.mch_id}")
# 1. 严格清理所有配置项的空格和换行符 # 1. 严格清理所有配置项的空格和换行符
mch_id = str(wechat_config.mch_id).strip() mch_id = str(wechat_config.mch_id).strip()
appid = str(wechat_config.app_id).strip() appid = str(wechat_config.app_id).strip()
@@ -135,6 +139,10 @@ def pay(request):
微信支付 V3 Native 下单接口 微信支付 V3 Native 下单接口
参数: goodid, quantity, customer_name, phone_number, shipping_address, ref_code 参数: goodid, quantity, customer_name, phone_number, shipping_address, ref_code
""" """
print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] 进入 pay 接口")
print(f"Request Headers: {request.headers}")
print(f"Request Data: {request.data}")
# 1. 获取并验证请求参数 # 1. 获取并验证请求参数
good_id = request.data.get('goodid') good_id = request.data.get('goodid')
quantity = int(request.data.get('quantity', 1)) quantity = int(request.data.get('quantity', 1))
@@ -144,18 +152,25 @@ def pay(request):
ref_code = request.data.get('ref_code') ref_code = request.data.get('ref_code')
if not all([good_id, customer_name, phone_number, shipping_address]): if not all([good_id, customer_name, phone_number, shipping_address]):
return Response({'error': '缺少必要参数: goodid, customer_name, phone_number, shipping_address'}, status=status.HTTP_400_BAD_REQUEST) missing_params = []
if not good_id: missing_params.append('goodid')
if not customer_name: missing_params.append('customer_name')
if not phone_number: missing_params.append('phone_number')
if not shipping_address: missing_params.append('shipping_address')
print(f"支付接口缺少参数: {missing_params}, 接收到的数据: {request.data}")
return Response({'error': f'缺少必要参数: {", ".join(missing_params)}'}, status=status.HTTP_400_BAD_REQUEST)
# 2. 获取支付配置并初始化客户端 # 2. 获取支付配置并初始化客户端
wxpay, error_msg = get_wechat_pay_client() wxpay, error_msg = get_wechat_pay_client()
if not wxpay: if not wxpay:
return Response({'error': error_msg}, status=status.HTTP_400_BAD_REQUEST) print(f"支付配置错误: {error_msg}")
return Response({'error': f'支付配置错误: {error_msg}'}, status=status.HTTP_400_BAD_REQUEST)
# 3. 查找商品和销售员,创建订单 # 3. 查找商品和销售员,创建订单
# ... (此处省略中间逻辑,保持不变) ...
try: try:
product = ESP32Config.objects.get(id=good_id) product = ESP32Config.objects.get(id=good_id)
except ESP32Config.DoesNotExist: except ESP32Config.DoesNotExist:
print(f"商品不存在: {good_id}")
return Response({'error': f'找不到 ID 为 {good_id} 的商品'}, status=status.HTTP_404_NOT_FOUND) return Response({'error': f'找不到 ID 为 {good_id} 的商品'}, status=status.HTTP_404_NOT_FOUND)
salesperson = None salesperson = None

View File

@@ -1,7 +1,8 @@
services: services:
backend: backend:
build: ./backend build: ./backend
command: sh -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000" # 使用 gunicorn 替代 runserver提高稳定性
command: sh -c "python manage.py migrate && gunicorn --bind 0.0.0.0:8000 config.wsgi:application"
volumes: volumes:
- ./backend:/app - ./backend:/app
ports: ports:

View File

@@ -49,9 +49,12 @@ const ProductDetail = () => {
quantity: values.quantity, quantity: values.quantity,
customer_name: values.customer_name, customer_name: values.customer_name,
phone_number: values.phone_number, phone_number: values.phone_number,
// 如果是自提,手动设置地址,否则使用表单中的地址
shipping_address: isPickup ? '线下自提' : values.shipping_address, shipping_address: isPickup ? '线下自提' : values.shipping_address,
ref_code: refCode ref_code: refCode
}; };
console.log('提交订单数据:', orderData); // 调试日志
const response = await nativePay(orderData); const response = await nativePay(orderData);
message.success('订单已创建,请完成支付'); message.success('订单已创建,请完成支付');
navigate(`/payment/${response.data.order_id}`, { navigate(`/payment/${response.data.order_id}`, {