• 05.创建订单并生成支付链接接口



    创建订单并生成支付链接接口


    1.创建订单并生成支付链接接口

    1.1 goods/urls.py中注册路由

    urlpatterns = [
    	path('getpayurl/', views.PayUrlView.as_view()), # /goods/getpayurl/
    ]
    

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

    from rest_framework.views import APIView
    from rest_framework.permissions import AllowAny, IsAuthenticated
    from rest_framework.response import Response
    from .models import Goods,Orders,GoodsCourse
    from goods.utils import get_order_id, get_pay_url, alipay
    from decimal import Decimal
    
    class PayUrlView(APIView):
    	permission_classes = (IsAuthenticated,)
        def post(self, request):
        # 1. 获取课程id,获取购买途径(普通购买, 促销购买) 获取用户
        goods_id = request.data.get('goods_id')
        goods = Goods.objects.get(id=goods_id)
        user = request.user
        # 2. 下定单
        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,status=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}})
    
    

    1.3 goods/utils.py中生成随机订单函数和生成支付URL函数

    # -*- coding: utf-8 -*-
    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="RSA2",
        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
    
    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中配置支付相关参数

    • 请求地址
    http://192.168.56.100:8888/goods/getpayurl
    
    • 携带参数
    {
    	"goods_id":1
    }
    
    • 返回数据
    {
    "code":0,
    "msg":"下单成功",
    "data":{
        "pay_url":"https://openapi.alipaydev.com/gateway.do?app_id=2016101800716047&biz_content=%7B%22subject%22%3A%22%5Cu5b9e%5Cu9a8c%5Cu697c%5Cu8ba2%5Cu5355%3ASYL2020101216322149584%2C+%5Cu4ef7%5Cu683c%3A99.00%22%2C%22out_trade_no%22%3A%22SYL2020101216322149584%22%2C%22total_amount%22%3A%2299.00%22%2C%22product_code%22%3A%22FAST_INSTANT_TRADE_PAY%22%7D&charset=utf8&method=alipay.trade.page.pay&return_url=http%3A%2F%2F127.0.0.1%3A8888%2Fpayment%2Fcallback%2F&sign_type=RSA2×tamp=2020-10- 12+16%3A32%3A21&version=1.0&sign=qIM7yrjYgvqOn2DCYK4rBgtwqDnZ%2BTwf89axXQPMzk67FOOHH18YtnoROi4ml5i5w%2Fd%2FtD2RsYyxU5Jeatxw9Skz%2B9HQCAYfmQriYDo5fz2hOrC6Ydx92z5%2BB5o%2BUIh29EAE8JS5MhUOOO8T3r9NtzmQTOs0kSWpnaXjX4S7l9Z83li76MG83H18jBodpWofgzcTTDdPsBnaHmeeJjGg9LKgSDS37ZKP9GE79asVn8S9w49qVmTPzZGdKIZxyLf2K44EllnGxhlDLFRlGLYJOlE%2Bxo1hr3IKH40WoYh3fkmv2%2FEDnl0OtNXHt5HRUH4uZ2k38BNf1ebrODRqWRg1eQ%3D%3D"
    	}
    }
    
  • 相关阅读:
    【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树
    【BZOJ4196】[Noi2015]软件包管理器 树链剖分
    【BZOJ4698】Sdoi2008 Sandy的卡片 后缀数组+RMQ
    【BZOJ4278】[ONTAK2015]Tasowanie 后缀数组
    mysql中使用concat例子
    SAP basis 常用事物
    推和敲
    踏和走
    下一个该你啦
    长城:恐惧的纪念碑
  • 原文地址:https://www.cnblogs.com/xiangnuan/p/13823477.html
Copyright © 2020-2023  润新知