修复了订单状态的验签问题
This commit is contained in:
@@ -178,14 +178,20 @@ def verify_notify_params(params, club_id=None):
|
|||||||
cfg = get_alipay_config(club_id)
|
cfg = get_alipay_config(club_id)
|
||||||
sign = params.get('sign', '')
|
sign = params.get('sign', '')
|
||||||
if not sign:
|
if not sign:
|
||||||
|
logger.warning('[ALIPAY_VERIFY_DEBUG] sign 为空')
|
||||||
return False
|
return False
|
||||||
sign_type = params.get('sign_type', '')
|
sign_type = params.get('sign_type', '')
|
||||||
if sign_type and sign_type.upper() != 'RSA2':
|
if sign_type and sign_type.upper() != 'RSA2':
|
||||||
|
logger.warning(f'[ALIPAY_VERIFY_DEBUG] sign_type 非 RSA2: {sign_type}')
|
||||||
return False
|
return False
|
||||||
# 回调验签:排除 sign 和 sign_type(支付宝官方规则)
|
# 回调验签:排除 sign 和 sign_type(支付宝官方规则)
|
||||||
sorted_keys = sorted(k for k in params if k not in ('sign', 'sign_type'))
|
sorted_keys = sorted(k for k in params if k not in ('sign', 'sign_type'))
|
||||||
sign_content = '&'.join(f'{k}={params[k]}' for k in sorted_keys if params[k] != '')
|
sign_content = '&'.join(f'{k}={params[k]}' for k in sorted_keys if params[k] != '')
|
||||||
return verify_rsa2(sign_content, sign, cfg['ALIPAY_PUBLIC_KEY'])
|
logger.warning(f'[ALIPAY_VERIFY_DEBUG] sign_content(前300)={sign_content[:300]}')
|
||||||
|
logger.warning(f'[ALIPAY_VERIFY_DEBUG] sign(前50)={sign[:50]}')
|
||||||
|
result = verify_rsa2(sign_content, sign, cfg['ALIPAY_PUBLIC_KEY'])
|
||||||
|
logger.warning(f'[ALIPAY_VERIFY_DEBUG] verify_rsa2 result={result}')
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
# ==================== wap.pay 跳转 URL ====================
|
# ==================== wap.pay 跳转 URL ====================
|
||||||
|
|||||||
@@ -422,28 +422,43 @@ class AlipayPayNotifyView(WechatPayNotifyView):
|
|||||||
"""
|
"""
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
try:
|
try:
|
||||||
|
# 详细记录原始请求(调试回调是否到达)
|
||||||
|
logger.warning(f"[ALIPAY_NOTIFY_DEBUG] 收到回调, method={request.method}, content_type={request.content_type}")
|
||||||
|
logger.warning(f"[ALIPAY_NOTIFY_DEBUG] POST keys={list(request.POST.keys())}")
|
||||||
|
|
||||||
# 支付宝回调为 form-urlencoded
|
# 支付宝回调为 form-urlencoded
|
||||||
params = dict(request.POST) if request.POST else {}
|
# 注意:必须用 .dict() 而不是 dict()!
|
||||||
|
# dict(request.POST) 会返回 {key: [value_list]}(值是列表)
|
||||||
|
# request.POST.dict() 返回 {key: value}(值是字符串),符合验签需求
|
||||||
|
params = request.POST.dict() if request.POST else {}
|
||||||
if not params:
|
if not params:
|
||||||
logger.warning("支付宝回调: 收到空数据")
|
logger.warning("[ALIPAY_NOTIFY_DEBUG] 支付宝回调: 收到空数据")
|
||||||
return HttpResponse('fail', content_type='text/plain')
|
return HttpResponse('fail', content_type='text/plain')
|
||||||
|
|
||||||
out_trade_no = params.get('out_trade_no', '')
|
out_trade_no = params.get('out_trade_no', '')
|
||||||
trade_no = params.get('trade_no', '')
|
trade_no = params.get('trade_no', '')
|
||||||
trade_status = params.get('trade_status', '')
|
trade_status = params.get('trade_status', '')
|
||||||
|
|
||||||
logger.info(f"支付宝回调: out_trade_no={out_trade_no}, trade_no={trade_no}, trade_status={trade_status}")
|
logger.warning(f"[ALIPAY_NOTIFY_DEBUG] out_trade_no={out_trade_no}, trade_no={trade_no}, trade_status={trade_status}")
|
||||||
|
logger.warning(f"[ALIPAY_NOTIFY_DEBUG] params keys={list(params.keys())}")
|
||||||
|
|
||||||
# 验签
|
# 验签
|
||||||
from jituan.services.alipay_pay import verify_notify_params, club_id_from_order
|
from jituan.services.alipay_pay import verify_notify_params, club_id_from_order
|
||||||
notify_club_id = club_id_from_order(out_trade_no)
|
notify_club_id = club_id_from_order(out_trade_no)
|
||||||
if not verify_notify_params(params, notify_club_id):
|
logger.warning(f"[ALIPAY_NOTIFY_DEBUG] notify_club_id={notify_club_id}")
|
||||||
logger.warning(f"支付宝验签失败: out_trade_no={out_trade_no}")
|
|
||||||
|
verify_result = verify_notify_params(params, notify_club_id)
|
||||||
|
logger.warning(f"[ALIPAY_NOTIFY_DEBUG] verify_result={verify_result}")
|
||||||
|
|
||||||
|
if not verify_result:
|
||||||
|
logger.warning(f"[ALIPAY_NOTIFY_DEBUG] 支付宝验签失败: out_trade_no={out_trade_no}")
|
||||||
return HttpResponse('fail', content_type='text/plain')
|
return HttpResponse('fail', content_type='text/plain')
|
||||||
|
|
||||||
# 处理支付结果
|
# 处理支付结果
|
||||||
if trade_status in ('TRADE_SUCCESS', 'TRADE_FINISHED'):
|
if trade_status in ('TRADE_SUCCESS', 'TRADE_FINISHED'):
|
||||||
|
logger.warning(f"[ALIPAY_NOTIFY_DEBUG] 调用 _handle_success")
|
||||||
self._handle_success(out_trade_no, trade_no, None)
|
self._handle_success(out_trade_no, trade_no, None)
|
||||||
|
logger.warning(f"[ALIPAY_NOTIFY_DEBUG] _handle_success 完成")
|
||||||
else:
|
else:
|
||||||
logger.info(f"支付宝支付状态非成功: {trade_status}, out_trade_no={out_trade_no}")
|
logger.info(f"支付宝支付状态非成功: {trade_status}, out_trade_no={out_trade_no}")
|
||||||
|
|
||||||
@@ -451,7 +466,7 @@ class AlipayPayNotifyView(WechatPayNotifyView):
|
|||||||
return HttpResponse('success', content_type='text/plain')
|
return HttpResponse('success', content_type='text/plain')
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"支付宝回调处理异常: {str(e)}")
|
logger.error(f"[ALIPAY_NOTIFY_DEBUG] 支付宝回调处理异常: {str(e)}")
|
||||||
logger.error(traceback.format_exc())
|
logger.error(traceback.format_exc())
|
||||||
return HttpResponse('fail', content_type='text/plain')
|
return HttpResponse('fail', content_type='text/plain')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user