支付宝获取支付接口 (回调接口)
1.1 syl/setings.py中配置支付相关参数
ALIPAY_APPID = '2016101800716047' # 沙箱环境中alipay应用ID ALIPAY_DEBUG = True ALIPAY_URL = 'https://openapi.alipaydev.com/gateway.do' # alipay沙箱环 境支付宝网管 ALIPAY_RETURN_URL = 'http://127.0.0.1:8888/payment/callback/' # 支付完成后支 付宝回调我们应用的地址
1.2 goods/utils.py中生成随机订单函数和生成支付URL函数
# -*- coding: utf-8 -*- from user.models import * class EmailAuthBackend: def authenticate(self, request, username=None, password=None): try: user=User.objects.get(username=username) except Exception as e: user = None if not user: try: user = User.objects.get(email=username) except Exception as e: user = None if user and user.check_password(password): return user else: return None def get_user(self, user_id): try: return User.objects.get(pk=user_id) except User.DoesNotExist: return None def jwt_response_payload_handler(token, user=None, request=None, role=None): """ 自定义jwt认证成功返回数据 : token 返回的jwt :user 当前登录的用户信息[对象] : request 当前本次客户端提交过来的数据 :role 角色 """ # permission_classes = (MyPermission,) # # 自定义认证类, 自定义会覆盖全局配置 # permission_classe=[IsAuthenticated] # 接口中加权限 if user.first_name: name = user.first_name else: name = user.username return { 'authenticated': 'true', 'id': user.id, "role": role, 'name': name, 'username': user.username, 'email': user.email, 'token': token, } # # 自定义权限类
1.3 goods/views.py 中写视图函数
from django.shortcuts import render from rest_framework.views import APIView from rest_framework.permissions import AllowAny,IsAuthenticated from rest_framework.response import Response from .models import Goods,Order,GoodsCourse from rest_framework import viewsets from decimal import Decimal from goods.utils import get_order_id, get_pay_url,alipay from .serliazer import * # Create your views here. class PayUrlView(APIView): permission_classes = (IsAuthenticated,) def post(self,request): goods_id=request.data.get('goods_id') print(goods_id) goods=Goods.objects.get(id=goods_id) user=request.user order_id = get_order_id() if user.vip.vip_type == '1': # 普通会员 goods_price = goods.price * Decimal('0.80').quantize(Decimal('0.00')) elif user.vip.vip_type == '2': # 高级会员 goods_price = goods.price * Decimal('0.60').quantize(Decimal('0.00')) else: # 普通用户 goods_price=goods.price goods_price = Decimal(goods_price).quantize(Decimal('0.00')) order = Order(user=user, goods=goods, order_id=order_id, pay_method=1,total_amount=goods_price) order.save() print(order.total_amount,type(order.total_amount)) # 3. 根据订单 生成支付链接 subject="实验楼订单:%s, 价格:%s" % (order.order_id, order.total_amount) pay_url=get_pay_url(order.order_id, order.total_amount, subject) # 4. 返回链接 return Response({"code": 0, "msg": "下单成功", "data": {"pay_url": pay_url}})
class PeyMentVIew(APIView): def post(self,request): data=request.data sign=data.pop('sign') order=Order.objects.get(order_id=data['out_trade_no']) order.trade_no=data['trade_no'] order.pay_time=data['timestamp'] order.status = 2 order.save() user=order.user course=order.goods.course GoodsCourse.objects.create(user=user,course=course) return Response({"code":0,"msg":"sussful"})
1.4 goods/urls.py中注册路由
urlpatterns = [ path('getpayurl/', views.PayUrlView.as_view()), # /goods/getpayurl/ ]