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):
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,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)
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,
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,
)
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 = '2016101800716047'
ALIPAY_DEBUG = True
ALIPAY_URL = 'https://openapi.alipaydev.com/gateway.do'
ALIPAY_RETURN_URL = 'http://127.0.0.1:8888/payment/callback/'
2.测试
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%5Cu697
c%5Cu8ba2%5Cu5355%3ASYL2020101216322149584%2C+%5Cu4ef7%5Cu683c%3A99.00%22%2C%22ou
t_trade_no%22%3A%22SYL2020101216322149584%22%2C%22total_amount%22%3A%2299.00%22%2
C%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%2BTwf89axXQPMzk67FO
OHH18YtnoROi4ml5i5w%2Fd%2FtD2RsYyxU5Jeatxw9Skz%2B9HQCAYfmQriYDo5fz2hOrC6Ydx92z5%2
BB5o%2BUIh29EAE8JS5MhUOOO8T3r9NtzmQTOs0kSWpnaXjX4S7l9Z83li76MG83H18jBodpWofgzcTTD
dPsBnaHmeeJjGg9LKgSDS37ZKP9GE79asVn8S9w49qVmTPzZGdKIZxyLf2K44EllnGxhlDLFRlGLYJOlE
%2Bxo1hr3IKH40WoYh3fkmv2%2FEDnl0OtNXHt5HRUH4uZ2k38BNf1ebrODRqWRg1eQ%3D%3D"
}
}