1.创建订单并生成支付链接接口
1.1 goods/urls.py中注册路由
urlpatterns = [
path('getpayurl/',views.PayUrlView.as_view()),
]
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))
#根据订单 生成支付连接
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函数
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,
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中配置支付相关参数
ALIPAY_APPID='2016102600761321' # 沙箱环境中alipay应用ID
ALIPAY_DEBUG=True # 默认False,我们是沙箱,所以改成True(让访问沙箱环境支付宝地址)
ALIPAY_URL='https://openapi.alipaydev.com/gateway.do' #ALIPAY沙箱环境网管
ALIPAY_RETURN_URL='http://127.0.0.1:8888/payment/callback/' #完成支付后回调我们应用的地址