个人开发通过第三方接口实现支付宝微信支付:https://www.paysapi.com/
开发文档: https://www.paysapi.com/docpay
需要用户表,订单表,商品表
class Course(models.Model): title = models.TextField(max_length=100) video_url = models.URLField() cover_url = models.URLField() price = models.FloatField() duration = models.IntegerField() # 持续时间 秒 profile = models.TextField() # 课程简介 pub_time = models.DateTimeField(auto_now_add=True) category = models.ForeignKey(Category, related_name="course", on_delete=models.DO_NOTHING) # DO_NOTHING django不作任何操作。 teacher = models.ForeignKey(Teacher, related_name="course", on_delete=models.DO_NOTHING) class CourseOrder(models.Model): amount = models.FloatField() course = models.ForeignKey(Course, on_delete=models.DO_NOTHING, related_name='order') buyer = models.ForeignKey(User, on_delete=models.DO_NOTHING) statuse = models.SmallIntegerField() # 1:代表未支付, 2:代表支付成功 istype = models.SmallIntegerField(default=0) # 1:支付宝支付, 2:代表微信支付, 0:代表未知 pub_time = models.DateTimeField(auto_now_add=True)
def order_key(request):
""" 生成key值,发送给前端,前端发送key值+其它参数给接口 https://pay.bbbapi.com """ uid = '94dc532695baa99e16e01bc0' token = '6e110f92abcb11040ba153967847b7a6' price = request.POST.get('price') # 商品价格 istype = request.POST.get('istype') # 支付方式 orderid = request.POST.get('orderid') # 订单id goodsname = request.POST.get('goodsname') # 商品名字 orderuid = str(request.user.id) # 用户id notify_url = request.build_absolute_uri(reverse('course:notify_url')) # 支付成功前端跳转url return_url = request.POST.get('return_url') # 支付成功,返回参数给该接口。 详情见开发文档 key = md5((goodsname + istype + notify_url + orderid + orderuid + price + return_url + token + uid).encode('utf-8')).hexdigest() print(uid, price, notify_url, return_url, orderid, orderuid, goodsname, key) return restful.result(data={'key': key}) @csrf_exempt def notify_url(request):
""" 用户支付成功,第三方支付接口返回参数 """ print(request.POST, '???????') try: order_id = request.POST.get('orderid') CourseOrder.objects.filter(id=order_id).update(statuse=2) # 默认statuse=1,未支付。 2:代表已支付 except: pass return restful.ok() # 需要返回状态码200