调用支付宝接口完成下单流程(沙箱环境)

以下流程都是使用沙箱环境完成,不是正式环境。使用drf后台和vue前端

支付宝一些注意事项和文档链接地址:

# 1、在沙箱环境下实名认证:https://openhome.alipay.com/platform/appDaily.htm?tab=info

# 2、电脑网站支付API:https://docs.open.alipay.com/270/105898/

# 3、完成RSA密钥生成:https://docs.open.alipay.com/291/105971

# 4、在开发中心的沙箱应用下设置应用公钥:填入生成的公钥文件中的内容

# 5、Python支付宝开源框架:https://github.com/fzlee/alipaypip install python-alipay-sdk --upgrade

# 7、公钥私钥设置
"""
# alipay_public_key.pem
-----BEGIN PUBLIC KEY-----
支付宝公钥
-----END PUBLIC KEY-----

# app_private_key.pem
-----BEGIN RSA PRIVATE KEY-----
用户私钥
-----END RSA PRIVATE KEY-----
"""

# 8、支付宝链接
"""
开发:https://openapi.alipay.com/gateway.do
沙箱:https://openapi.alipaydev.com/gateway.do
"""

支付宝流程图

 

 

 

 

 

调用支付宝接口完成下单流程(沙箱环境)

 

 

 aliapy二次封装包

安装aliapy

pip install python-alipay-sdk --upgrade

目录结构:(大致)

libs
    ├── iPay                              # aliapy二次封装包
    │   ├── __init__.py                 # 包文件
    │   ├── keys                        # 密钥文件夹
    │   │   ├── alipay_public_key.pem      # 支付宝公钥
    │   │   └── app_private_key.pem      # 应用私钥
    └── └── settings.py                  # 应用配置  

调用支付宝接口完成下单流程(沙箱环境)

 

 

 settings.py   (只有APP_ID需要改成自己的,其他按照默认的就行)

import os
# 支付宝应用id
APP_ID = '2016093000631831'
# 默认异步回调的地址,通常设置None就行
APP_NOTIFY_URL = None
# 应用私钥文件路径
APP_PRIVATE_KEY_PATH = os.path.join(os.path.dirname(__file__), 'keys', 'app_private_key.pem')
# 支付宝公钥文件路径
ALIPAY_PUBLIC_KEY_PATH = os.path.join(os.path.dirname(__file__), 'keys', 'alipay_public_key.pem')
# 签名方式
SIGN_TYPE = 'RSA2'
# 是否是测试环境 - 是否是支付宝沙箱
DEBUG = True
# 支付连接
DEV_PAY_URL = 'https://openapi.alipaydev.com/gateway.do?'
PROD_PAY_URL = 'https://openapi.alipay.com/gateway.do?'

__init__.py

from alipay import AliPay
from .settings import *
# 对外提供支付对象
alipay = AliPay(
    appid=APP_ID,
    app_notify_url=APP_NOTIFY_URL,
    app_private_key_path=APP_PRIVATE_KEY_PATH,
    alipay_public_key_path=ALIPAY_PUBLIC_KEY_PATH,
    sign_type=SIGN_TYPE,
    debug=DEBUG
)

# 对外提供的支付链接前缀
pay_url = DEV_PAY_URL if DEBUG else PROD_PAY_URL

aliapy_public_key.pem  (放支付宝公钥)

生成支付宝公钥的方法:

一、点击下面链接

https://docs.open.alipay.com/291/105971

1.先下载工具安装 2.生成应用公钥和私钥,我们只需要使用应用公钥,复制

二、点击下面链接到沙箱环境

https://openhome.alipay.com/platform/appDaily.htm

调用支付宝接口完成下单流程(沙箱环境)

 

 

只需要设置这个密钥,然后到里面粘贴你上一步中复制的应用公钥就能获取到支付宝公钥 

-----BEGIN PUBLIC KEY-----
支付宝公钥
-----END PUBLIC KEY-----

调用支付宝接口完成下单流程(沙箱环境)

 

 

 app_private_key.pem(应用私钥)

-----BEGIN RSA PRIVATE KEY-----
应用私钥
-----END RSA PRIVATE KEY-----

应用私钥在上面下载的工具中生成的。

补充配置系统的配置文件settings.py

# 前后台base_url
UP_BASE_URL = 'http://127.0.0.1:8080'
END_BASE_URL = 'http://127.0.0.1:8000'

# alipay回调接口配置
# 上线后必须换成官网地址
# 同步回调的接口(get),前后台分离时一般设置前台页面url
RETURN_URL = UP_BASE_URL + '/pay/success'
# 异步回调的接口(post),一定设置为后台服务器接口
NOTIFY_URL = END_BASE_URL + '/order/success/'

支付模块的数据库models设计

order/models.py

from django.db import models

# 订单表:
from django.db import models
from utils.model import BaseModel
from user.models import User
from course.models import Course


class Order(models.Model):
    """订单模型"""
    status_choices = (
        (0, '未支付'),
        (1, '已支付'),
        (2, '已取消'),
        (3, '超时取消'),
    )
    pay_choices = (
        (1, '支付宝'),
        (2, '微信支付'),
    )
    subject = models.CharField(max_length=150, verbose_name="订单标题")
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="订单总价", default=0)
    out_trade_no = models.CharField(max_length=64, verbose_name="订单号", unique=True)
    trade_no = models.CharField(max_length=64, null=True, verbose_name="流水号")
    order_status = models.SmallIntegerField(choices=status_choices, default=0, verbose_name="订单状态")
    pay_type = models.SmallIntegerField(choices=pay_choices, default=1, verbose_name="支付方式")
    pay_time = models.DateTimeField(null=True, verbose_name="支付时间")
    user = models.ForeignKey(User, related_name='user_orders', on_delete=models.DO_NOTHING, db_constraint=False,
                             verbose_name="下单用户")
    class Meta:
        db_table = "luffy_order"
        verbose_name = "订单记录"
        verbose_name_plural = "订单记录"

    def __str__(self):
        return "%s - ¥%s" % (self.subject, self.total_amount)

    @property
    def courses(self):
        data_list = []
        for item in self.order_courses.all():
            data_list.append({
                "id": item.id,
                "course_name": item.course.name,
                "real_price": item.real_price,
            })

        return data_list


class OrderDetail(BaseModel):
    """订单详情"""
    order = models.ForeignKey(Order, related_name='order_courses', on_delete=models.CASCADE, db_constraint=False,
                              verbose_name="订单")
    course = models.ForeignKey(Course, related_name='course_orders', on_delete=models.CASCADE, db_constraint=False,
                               verbose_name="课程")
    price = models.DecimalField(max_digits=6, decimal_places=2, verbose_name="课程原价")
    real_price = models.DecimalField(max_digits=6, decimal_places=2, verbose_name="课程实价")

    class Meta:
        db_table = "luffy_order_detail"
        verbose_name = "订单详情"
        verbose_name_plural = "订单详情"

    def __str__(self):
        return "%s订单(%s)" % (self.course.name, self.order.out_trade_no)

后台接口路由设计

from django.urls import path
from . import views
urlpatterns = [
    path('pay/', views.PayAPIView.as_view()),
    path('success/', views.SuccessAPIView.as_view()),
]

订单序列化模块设计serializers

from rest_framework import serializers
from . import models
class OrderModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Order
        fields = ('name', 'total_price', 'out_trade_no', 'pay_type', 'user')
        extra_kwargs = {
            'pay_type': {
                'required': True
            },
            'total_price': {
                'required': True
            },
        }

支付接口生成支付链接(在后台生成支付链接的参数,在前台拼接成支付路径,然后跳转到该支付宝支付界面)

使用场景:

比如在一个订单页面,点击提交订单支付,前端把需要的参数返回给后台,到PayAPIView视图类

import time
from rest_framework.views import APIView
from utils.response import APIResponse
from libs.iPay import alipay
from . import authentications, serializers
from rest_framework.permissions import IsAuthenticated
from django.conf import settings
# 获取前台 商品名、价格,产生 订单、支付链接
class PayAPIView(APIView):
    authentication_classes = [authentications.JWTAuthentication]
    permission_classes = [IsAuthenticated]
    def post(self, request, *args, **kwargs):
        # 前台提供:商品名、总价、支付方式
        request_data = request.data
        # 后台产生:订单号、用户
        out_trade_no = '%d' % time.time() * 2
        request_data['out_trade_no'] = out_trade_no
        request_data['user'] = request.user.id

        # 反序列化数据,用于订单生成前的校验
        order_ser = serializers.OrderModelSerializer(data=request_data)
        if order_ser.is_valid():
            # 生成订单,订单默认状态为:未支付
            order = order_ser.save()
            # 支付链接的参数
            order_string = alipay.api_alipay_trade_page_pay(
                subject=order.subject,
                out_trade_no=order.out_trade_no,
                total_amount='%.2f' % order.total_amount,
                return_url=settings.RETURN_URL,
                notify_url=settings.NOTIFY_URL
            )
            # 形成支付链接:alipay._gateway根据字符环境DEBUG配置信息,决定是沙箱还是真实支付环境
            pay_url = '%s?%s' % (alipay._gateway, order_string)
            return APIResponse(0, 'ok', pay_url=pay_url)


        return APIResponse(1, 'no ok', results=order_ser.errors)

vue支付部分代码

buy() {
                // 检测登录状态
                let token = this.token;
                window.console.log(token);
                if (!token) {
                    this.$message("请先登录");
                    return false
                }
                this.$axios({
                    url: this.$settings.base_url + '/order/pay/',
                    method: 'post',
                    headers: {  // 需要登录后访问的接口,默认认证信息就有authorization
                        authorization: `jwt ${token}`,
                    },
                    //后端向前端传递数据
                    data: {
                        name:this.goods_name ,  //商品名
                        total_price: this.total_price,   //总价格
                        pay_type: 0  //支付方式
                    }  
                }).then(response => {
                    let pay_url = response.data.pay_url;  //后台拼接访问支付宝支付的路径
                    // js自身链接发生跳转
                    window.open(pay_url, '_self');  //跳转到支付界面,输入支付宝用户和密码
                }).catch(() => {
                    this.$message("订单生成失败!");
                })
            },

支付成功之后需要返回一个页面,所以我们需要绑定一个自定义的支付成功页面

在项目的settings.py中我们已经设定了

RETURN_URL :同步回调接口get,一般是前台页面的url

NOTIFY_URL:异步回调接口post,一般是后台服务器接口(这个路径和前端访问后端的路径设置需要一致)

下面我们开始前后台回调页面的设计

前台回调接口的页面,设计路径

{
    path: '/pay/success',
    name: 'pay-success',
    component: PaySuccess
},

PaySuccess.vue

调用支付宝接口完成下单流程(沙箱环境)
<template>
    <div class="pay-success">
        <Header/>
        <div class="main">
            <div class="title">
                <div class="success-tips">
                    <p class="tips">您已成功购买 1 门课程!</p>
                </div>
            </div>
            <div class="order-info">
                <p class="info"><b>订单号:</b><span>{{ result.out_trade_no }}</span></p>
                <p class="info"><b>交易号:</b><span>{{ result.trade_no }}</span></p>
                <p class="info"><b>付款时间:</b><span><span>{{ result.timestamp }}</span></span></p>
            </div>
            <div class="study">
                <span>立即学习</span>
            </div>
        </div>
        <Footer/>
    </div>
</template>

<script>
    import Header from "@/components/Header"
    import Footer from "@/components/Footer"

    export default {
        name: "Success",
        data() {
            return {
                result: {},
            };
        },
        created() {
            // 判断登录状态
            let token = this.$cookies.get('token');
            if (!token) {
                this.$message.error('非法请求');
                this.$router.go(-1)
            }


            localStorage.this_nav = '/';
            if (!location.search.length) return;
            let params = location.search.substring(1);
            let items = params.length ? params.split('&') : [];
            //逐个将每一项添加到args对象中
            for (let i = 0; i < items.length; i++) {
                let k_v = items[i].split('=');
                //解码操作,因为查询字符串经过编码的
                let k = decodeURIComponent(k_v[0]);
                let v = decodeURIComponent(k_v[1]);
                this.result[k] = v;
                // this.result[k_v[0]] = k_v[1];
            }
            // console.log(this.result);

            // 把地址栏上面的支付结果,转发给后端
            this.$axios({
                url: this.$settings.base_url + '/order/success/' + location.search,
                method: 'patch',
                headers: {
                    Authorization: token
                }
            }).then(response => {
                console.log(response.data);
            }).catch(() => {
                console.log('支付结果同步失败');
            })
        },
        components: {
            Header,
            Footer,
        }
    }
</script>

<style scoped>

    .main {
        padding: 60px 0;
        margin: 0 auto;
        width: 1200px;
        background: #fff;
    }

    .main .title {
        display: flex;
        -ms-flex-align: center;
        align-items: center;
        padding: 25px 40px;
        border-bottom: 1px solid #f2f2f2;
    }

    .main .title .success-tips {
        box-sizing: border-box;
    }

    .title img {
        vertical-align: middle;
        width: 60px;
        height: 60px;
        margin-right: 40px;
    }

    .title .success-tips {
        box-sizing: border-box;
    }

    .title .tips {
        font-size: 26px;
        color: #000;
    }


    .info span {
        color: #ec6730;
    }

    .order-info {
        padding: 25px 48px;
        padding-bottom: 15px;
        border-bottom: 1px solid #f2f2f2;
    }

    .order-info p {
        display: -ms-flexbox;
        display: flex;
        margin-bottom: 10px;
        font-size: 16px;
    }

    .order-info p b {
        font-weight: 400;
        color: #9d9d9d;
        white-space: nowrap;
    }

    .study {
        padding: 25px 40px;
    }

    .study span {
        display: block;
        width: 140px;
        height: 42px;
        text-align: center;
        line-height: 42px;
        cursor: pointer;
        background: #ffc210;
        border-radius: 6px;
        font-size: 16px;
        color: #fff;
    }
</style>
View Code

后台支付完成订单校验的接口

from . import models
from utils.logging import logger
from rest_framework.response import Response
class SuccessAPIView(APIView):
    # 不能认证,别人支付宝异步回调就进不来了
    # authentication_classes = [authentications.JWTAuthentication]
    # permission_classes = [IsAuthenticated]
    def patch(self, request, *args, **kwargs):
        # 默认是QueryDict类型,不能使用pop方法
        request_data = request.query_params.dict()
        # 必须将 sign、sign_type(内部有安全处理) 从数据中取出,拿sign与剩下的数据进行校验
        sign = request_data.pop('sign')
        result = alipay.verify(request_data, sign)
        if result:  # 同步回调:修改订单状态
            try:
                out_trade_no = request_data.get('out_trade_no')
                order = models.Order.objects.get(out_trade_no=out_trade_no)
                if order.order_status != 1:
                    order.order_status = 1
                    order.save()
            except:
                pass
            return APIResponse(0, '支付成功')
        return APIResponse(1, '支付失败')

    # 支付宝异步回调
    def post(self, request, *args, **kwargs):
        # 默认是QueryDict类型,不能使用pop方法
        request_data = request.data.dict()
        # 必须将 sign、sign_type(内部有安全处理) 从数据中取出,拿sign与剩下的数据进行校验
        sign = request_data.pop('sign')
        result = alipay.verify(request_data, sign)
        # 异步回调:修改订单状态
        if result and request_data["trade_status"] in ("TRADE_SUCCESS", "TRADE_FINISHED" ):
            out_trade_no = request_data.get('out_trade_no')
            logger.critical('%s支付成功' % out_trade_no)
            try:
                order = models.Order.objects.get(out_trade_no=out_trade_no)
                if order.order_status != 1:
                    order.order_status = 1
                    order.save()
            except:
                pass
            # 支付宝八次异步通知,订单成功一定要返回 success
            return Response('success')
        return Response('failed')

 

from django.db import models
# 订单表:from django.db import modelsfrom utils.model import BaseModelfrom user.models import Userfrom course.models import Course

class Order(models.Model):    """订单模型"""    status_choices = (        (0, '未支付'),        (1, '已支付'),        (2, '已取消'),        (3, '超时取消'),    )    pay_choices = (        (1, '支付宝'),        (2, '微信支付'),    )    subject = models.CharField(max_length=150, verbose_name="订单标题")    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="订单总价", default=0)    out_trade_no = models.CharField(max_length=64, verbose_name="订单号", unique=True)    trade_no = models.CharField(max_length=64, null=True, verbose_name="流水号")    order_status = models.SmallIntegerField(choices=status_choices, default=0, verbose_name="订单状态")    pay_type = models.SmallIntegerField(choices=pay_choices, default=1, verbose_name="支付方式")    pay_time = models.DateTimeField(null=True, verbose_name="支付时间")    user = models.ForeignKey(User, related_name='user_orders', on_delete=models.DO_NOTHING, db_constraint=False,                             verbose_name="下单用户")    class Meta:        db_table = "luffy_order"        verbose_name = "订单记录"        verbose_name_plural = "订单记录"
    def __str__(self):        return "%s - ¥%s" % (self.subject, self.total_amount)
    @property    def courses(self):        data_list = []        for item in self.order_courses.all():            data_list.append({                "id": item.id,                "course_name": item.course.name,                "real_price": item.real_price,            })
        return data_list

class OrderDetail(BaseModel):    """订单详情"""    order = models.ForeignKey(Order, related_name='order_courses', on_delete=models.CASCADE, db_constraint=False,                              verbose_name="订单")    course = models.ForeignKey(Course, related_name='course_orders', on_delete=models.CASCADE, db_constraint=False,                               verbose_name="课程")    price = models.DecimalField(max_digits=6, decimal_places=2, verbose_name="课程原价")    real_price = models.DecimalField(max_digits=6, decimal_places=2, verbose_name="课程实价")
    class Meta:        db_table = "luffy_order_detail"        verbose_name = "订单详情"        verbose_name_plural = "订单详情"
    def __str__(self):        return "%s订单(%s)" % (self.course.name, self.order.out_trade_no)

上一篇:【实例:利用Django管理后台管理IP地址】(二)创建后台管理员和自定义的表模型


下一篇:python模块-optparse(解析命令行参数)