Loading

04.订单支付回调-修改订单状态

1.订单支付回调

1.1 goods/urls.py中注册路由

urlpatterns = [ 
     path('peyment/', views.PayMentView.as_view()),  # /goods/getpayurl/ 订单支付回调
]

1.2 goods/views.py 中写视图函数

from decimal import Decimal
from course.models import *
from rest_framework.views import APIView  # 业务使用APIVIW
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response

class PayMentView(APIView):
    permission_classes = (AllowAny,)  # 用户权限

    def post(self, request):
        # 1.获取了支付宝返回的数据
        data = request.data
        # 2.
        sign = data.pop('sign')
        print('sign', sign)
        '''
        "sign":"Sv......kEoobbA==",
        '''
        order = Orders.objects.get(order_id=data['out_trade_no'])
        order.trade_no = data['trade_no']
        order.pay_time = data['timestamp']
        order.status = 2
        order.save()  # 保存
        # 3.处理用户购买的课程流程
        # 3.1给UserCourse 表增加购买课程
        # goods=order.goods
        user = order.user
        course = order.goods.course
        UserCourse.objects.create(user=user, course=course)
        return Response({"code": 0, "msg": "购买成功"})
posted @ 2020-10-15 21:52  就学45分钟  阅读(544)  评论(0编辑  收藏  举报