1.创建订单并生成支付链接接口
1.1 goods/urls.py中注册路由
from django.urls import path from . import views urlpatterns = [ path(‘getpayurl/‘,views.PayUrlView.as_view()), path(‘peyment/‘,views.PayMentView.as_view()) ]
1.2 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,Orders from decimal import Decimal from goods.utils import get_order_id, get_pay_url,alipay # Create your views here. class PayUrlView(APIView): permission_classes = (IsAuthenticated,) def post(self,request): goods_id=request.data.get(‘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 = Orders(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}}) from course.models import UserCourse class PayMentView(APIView): permission_classes = (AllowAny,) def post(self,request): data=request.data sign=data.pop(‘sign‘) 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() user=order.user course=order.goods.course UserCourse.objects.create(user=user,course=course) return Response({"code":0,"msg":"购买成功"})
1.3 goods/utils.py中生成随机订单函数和生成支付URL函数
import datetime import os import random from alipay import AliPay from syl import settings app_private_key_path = os.path.join(settings.BASE_DIR, "apps/goods/keys/app_private_key.pem") alipay_public_key_path = os.path.join(settings.BASE_DIR, "apps/goods/keys/alipay_public_key.pem") with open(app_private_key_path) as f: app_private_key_string = f.read() with open(alipay_public_key_path) as f: alipay_public_key_string = f.read() alipay = AliPay( appid=settings.ALIPAY_APPID, app_notify_url=None, # 默认回调url app_private_key_string=app_private_key_string, alipay_public_key_string=alipay_public_key_string, # app_private_key_path=app_private_key_path, # alipay_public_key_path=alipay_public_key_path, sign_type="RSA", debug=settings.ALIPAY_DEBUG ) def get_pay_url(out_trade_no, total_amount, subject): order_string = alipay.api_alipay_trade_page_pay( out_trade_no=out_trade_no, total_amount=str(total_amount), subject=subject, return_url=settings.ALIPAY_RETURN_URL, ) # 响应登录支付宝连接 # 真实环境电脑网站支付网关:https://openapi.alipay.com/gateway.do? + order_string # 沙箱环境电脑网站支付网关:https://openapi.alipaydev.com/gateway.do? + order_string alipay_url = settings.ALIPAY_URL + "?" + order_string return alipay_url alipay_url = settings.ALIPAY_URL + "?" + order_string return alipay_url def get_order_id(): """ SYL202008241212121200005/24 生成订单号: 格式: SYL + 年月日时分秒 + 5位随机数 :return: """ d = datetime.datetime.now() base = ‘SYL‘ time_str = ‘%04d%02d%02d%02d%02d%02d‘ % (d.year, d.month, d.day, d.hour, d.minute, d.second) rand_num = str(random.randint(10000, 99999)) return base + time_str + rand_num
1.4 syl/setings.py中配置支付相关参数
# 支付宝配置 ALIPAY_APPID = ‘2016102600762709‘ # 沙箱环境中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/‘ # 支付完成后支 付宝回调我们应用的地址
原文:https://www.cnblogs.com/xiaoxiamiaichiyu/p/13828574.html