测试中,尝试接入支付宝(沙箱)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from django.urls import path
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from .views import CreateOrderView, WechatPayNotifyView, \
|
||||
from .views import CreateOrderView, WechatPayNotifyView, AlipayPayNotifyView, alipay_pay_page, \
|
||||
PaymentFailView, PaymentVerifyView, DingdanHuoquView, DingdanXiangqingView, JiedanView, \
|
||||
ShangpinLeixingView, ShangjiaPaifaView, ShangjiaDingdanHuoquView, ShangjiaJiesuanView, \
|
||||
ShangjiaDingdanXiangqingView, ShangjiaChufaShenqingView, ShangjiaTuikuanShenqingView, \
|
||||
@@ -18,6 +18,8 @@ urlpatterns = [
|
||||
path('xiadan', CreateOrderView.as_view(), name='下单订单生成接口'),
|
||||
#path('pay-notify/', WechatPayNotifyView.as_view(), name='老板下单微信支付回调'),
|
||||
path('pay-notify/', csrf_exempt(WechatPayNotifyView.as_view()), name='老板下单微信支付回调'),
|
||||
path('alipay-notify/', csrf_exempt(AlipayPayNotifyView.as_view()), name='老板下单支付宝支付回调'),
|
||||
path('alipay-pay/', csrf_exempt(alipay_pay_page), name='支付宝H5中转页'),
|
||||
path('shibai', PaymentFailView.as_view(), name='老板支付失败接口'),
|
||||
path('fucha', PaymentVerifyView.as_view(), name='老板支付成功复查接口'),
|
||||
path('dingdanhuoqu', DingdanHuoquView.as_view(), name='老板订单获取'),
|
||||
|
||||
@@ -5,8 +5,10 @@ Preserves external `from orders.views import X` for orders/urls.py and merchant_
|
||||
from .payment import (
|
||||
PaymentFailView,
|
||||
WechatPayNotifyView,
|
||||
AlipayPayNotifyView,
|
||||
PaymentVerifyView,
|
||||
CreateOrderView,
|
||||
alipay_pay_page,
|
||||
)
|
||||
from .boss_orders import (
|
||||
DingdanHuoquView,
|
||||
@@ -66,7 +68,8 @@ from .merchant_penalty import (
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"PaymentFailView", "WechatPayNotifyView", "PaymentVerifyView", "CreateOrderView",
|
||||
"PaymentFailView", "WechatPayNotifyView", "AlipayPayNotifyView", "PaymentVerifyView", "CreateOrderView",
|
||||
"alipay_pay_page",
|
||||
"DingdanHuoquView", "DingdanXiangqingView", "JiedanView",
|
||||
"DingdanXiangqingView2", "JiedanView2",
|
||||
"ShangpinLeixingView", "ShangjiaPaifaView", "ShangjiaDingdanHuoquView",
|
||||
|
||||
@@ -22,6 +22,7 @@ from django.db.models import F, Q, OuterRef, Subquery
|
||||
from django.core.cache import cache
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import render
|
||||
from django.utils import timezone
|
||||
|
||||
from rest_framework.views import APIView
|
||||
@@ -415,6 +416,60 @@ class WechatPayNotifyView(APIView):
|
||||
return '游戏订单'
|
||||
|
||||
|
||||
class AlipayPayNotifyView(WechatPayNotifyView):
|
||||
"""支付宝支付异步回调接口 - 接收支付宝官方支付结果通知。
|
||||
继承 WechatPayNotifyView 以复用 _handle_success/_handle_failure 履约逻辑。
|
||||
"""
|
||||
def post(self, request):
|
||||
try:
|
||||
# 支付宝回调为 form-urlencoded
|
||||
params = dict(request.POST) if request.POST else {}
|
||||
if not params:
|
||||
logger.warning("支付宝回调: 收到空数据")
|
||||
return HttpResponse('fail', content_type='text/plain')
|
||||
|
||||
out_trade_no = params.get('out_trade_no', '')
|
||||
trade_no = params.get('trade_no', '')
|
||||
trade_status = params.get('trade_status', '')
|
||||
|
||||
logger.info(f"支付宝回调: out_trade_no={out_trade_no}, trade_no={trade_no}, trade_status={trade_status}")
|
||||
|
||||
# 验签
|
||||
from jituan.services.alipay_pay import verify_notify_params, club_id_from_order
|
||||
notify_club_id = club_id_from_order(out_trade_no)
|
||||
if not verify_notify_params(params, notify_club_id):
|
||||
logger.warning(f"支付宝验签失败: out_trade_no={out_trade_no}")
|
||||
return HttpResponse('fail', content_type='text/plain')
|
||||
|
||||
# 处理支付结果
|
||||
if trade_status in ('TRADE_SUCCESS', 'TRADE_FINISHED'):
|
||||
self._handle_success(out_trade_no, trade_no, None)
|
||||
else:
|
||||
logger.info(f"支付宝支付状态非成功: {trade_status}, out_trade_no={out_trade_no}")
|
||||
|
||||
# 支付宝要求返回 "success"(纯文本),否则会重复通知
|
||||
return HttpResponse('success', content_type='text/plain')
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"支付宝回调处理异常: {str(e)}")
|
||||
logger.error(traceback.format_exc())
|
||||
return HttpResponse('fail', content_type='text/plain')
|
||||
|
||||
|
||||
def alipay_pay_page(request):
|
||||
"""支付宝H5中转页 - 中转跳转到支付宝支付,以及支付完成后的返回页。"""
|
||||
pay_url = request.GET.get('pay_url', '')
|
||||
out_trade_no = request.GET.get('out_trade_no', '')
|
||||
|
||||
if pay_url:
|
||||
# 中转模式:自动跳转到支付宝
|
||||
return render(request, 'alipay/pay.html', {'pay_url': pay_url})
|
||||
elif out_trade_no:
|
||||
# 返回模式:支付完成返回
|
||||
return render(request, 'alipay/pay.html', {'out_trade_no': out_trade_no})
|
||||
else:
|
||||
return render(request, 'alipay/pay.html', {})
|
||||
|
||||
|
||||
class PaymentVerifyView(APIView):
|
||||
"""
|
||||
@@ -672,6 +727,7 @@ class CreateOrderView(APIView):
|
||||
beizhu = request.data.get('Remark', '').strip()
|
||||
zhiding = request.data.get('zhiding', '').strip()
|
||||
jine = request.data.get('jine', 0)
|
||||
pay_method = request.data.get('pay_method', 'wechat').strip().lower()
|
||||
|
||||
# 2. 验证必填字段
|
||||
if not shangpin_id:
|
||||
@@ -766,7 +822,8 @@ class CreateOrderView(APIView):
|
||||
laoban_yonghuid = getattr(current_user, 'UserUID', '') or getattr(current_user, 'yonghuid', '')
|
||||
user_openid, club_id = get_payment_openid(request, current_user, club_id)
|
||||
|
||||
if not user_openid:
|
||||
# 支付宝支付不需要 openid
|
||||
if pay_method == 'wechat' and not user_openid:
|
||||
return Response({'code': 10, 'msg': '用户openid不存在', 'data': None})
|
||||
|
||||
# 创建订单主表数据
|
||||
@@ -843,15 +900,23 @@ class CreateOrderView(APIView):
|
||||
PlatformOrderExt.query.create(**pingtaikuozhan_data)'''
|
||||
|
||||
|
||||
# 7. 生成微信支付参数(向微信官方发送请求)
|
||||
pay_params = self.generate_wechat_pay_params(
|
||||
dingdanid=dingdanid,
|
||||
jine=shangpin_jiage,
|
||||
beizhu=getattr(settings, 'WEIXIN_PAY_DESCRIPTION', '星阙订单'),
|
||||
user_ip=self.get_client_ip(request),
|
||||
openid=user_openid,
|
||||
club_id=club_id,
|
||||
)
|
||||
# 7. 根据支付方式生成支付参数
|
||||
if pay_method == 'alipay':
|
||||
pay_params = self.generate_alipay_pay_params(
|
||||
dingdanid=dingdanid,
|
||||
jine=shangpin_jiage,
|
||||
subject=getattr(settings, 'WEIXIN_PAY_DESCRIPTION', '星阙订单'),
|
||||
club_id=club_id,
|
||||
)
|
||||
else:
|
||||
pay_params = self.generate_wechat_pay_params(
|
||||
dingdanid=dingdanid,
|
||||
jine=shangpin_jiage,
|
||||
beizhu=getattr(settings, 'WEIXIN_PAY_DESCRIPTION', '星阙订单'),
|
||||
user_ip=self.get_client_ip(request),
|
||||
openid=user_openid,
|
||||
club_id=club_id,
|
||||
)
|
||||
|
||||
if not pay_params:
|
||||
# 手动抛出异常触发事务回滚
|
||||
@@ -863,6 +928,7 @@ class CreateOrderView(APIView):
|
||||
'msg': '订单创建成功',
|
||||
'data': {
|
||||
'dingdanid': dingdanid,
|
||||
'payMethod': pay_method,
|
||||
'payParams': pay_params
|
||||
}
|
||||
}
|
||||
@@ -1010,5 +1076,25 @@ class CreateOrderView(APIView):
|
||||
except Exception as e:
|
||||
raise e
|
||||
|
||||
def generate_alipay_pay_params(self, dingdanid, jine, subject, club_id=None):
|
||||
"""
|
||||
生成支付宝 wap.pay 跳转 URL。
|
||||
返回前端用于 web-view 加载的支付参数。
|
||||
"""
|
||||
try:
|
||||
from jituan.services.alipay_pay import build_wap_pay_url
|
||||
pay_url = build_wap_pay_url(
|
||||
out_trade_no=dingdanid,
|
||||
amount=jine,
|
||||
subject=subject,
|
||||
club_id=club_id,
|
||||
)
|
||||
return {
|
||||
'pay_url': pay_url,
|
||||
'pay_method': 'alipay',
|
||||
}
|
||||
except Exception as e:
|
||||
raise e
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user