• 【支付宝SDK】沙箱调试,以及遇到的坑


    from rest_framework.views import APIView
    from alipay import AliPay, DCAliPay, ISVAliPay
    from django.http import JsonResponse
    import os
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    import time
    
    class Alipay(APIView):
        authentication_classes = []
    
        # 跳转支付
        def get(self,request):
    
            # 初始化配置
            app_private_key_string = open(BASE_DIR + "/payAPI/应用私钥2048.txt").read()
            alipay_public_key_string = open(BASE_DIR + "/payAPI/支付宝公钥.txt").read()
    
            alipay = AliPay(
                appid="你自己的ID",
                app_notify_url=None,  # 默认回调url
                app_private_key_string=app_private_key_string,
                # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥,
                alipay_public_key_string=alipay_public_key_string,
                sign_type="RSA2",  # RSA 或者 RSA2
                debug = True  # 默认False
            )
    
            # 电脑网站支付,需要跳转到https://openapi.alipay.com/gateway.do? + order_string
            order_string = alipay.api_alipay_trade_page_pay(
                out_trade_no="订单号",
                total_amount=str("总价"),   坑:要转换成字符串类型 否则会报错
                subject="商品标题",
                return_url=None,
                notify_url=None  # 可选, 不填则使用默认notify url
            )
    
            message={}
            message['pay_url'] = 'https://openapi.alipaydev.com/gateway.do?' + order_string
    
            return JsonResponse(message)
    
        # 查询订单支付情况
        def post(self,request):
    
            # 初始化配置
            app_private_key_string = open(BASE_DIR + "/payAPI/应用私钥2048.txt").read()
            alipay_public_key_string = open(BASE_DIR + "/payAPI/支付宝公钥.txt").read()
    
            alipay = AliPay(
                appid="你的APPID",
                app_notify_url=None,  # 默认回调url
                app_private_key_string=app_private_key_string,
                # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥,
                alipay_public_key_string=alipay_public_key_string,
                sign_type="RSA2",  # RSA 或者 RSA2
                debug = True  # 默认False
            )
            message = {}
    
            while True:
                result = alipay.api_alipay_trade_query("20200823")
                print(result)
                print("trade_status",result.get("trade_status"))
    
                #  获取返回结果
                code = result.get("code")
    
                if code == "10000" and result.get("trade_status") == "TRADE_SUCCESS":
                    # 支付成功
                    # 获取支付宝交易号
                    trade_no = result.get('trade_no')
                    print("支付宝交易号",trade_no)
                    # 更新订单状态
                    # 返回结果
                    message['code'] = 200
                    message['message'] = "支付成功"
                    return JsonResponse(message)
    
                elif code == "40004" or (code == "10000" and result.get("trade_status") == "WAIT_BUYER_PAY"):
                    # 等待买家付款
                    time.sleep(5)
                    continue
                else:
                    # 支付出错
                    message['code'] = 444
                    message['message'] = "支付失败"
                    return JsonResponse(message)
    <!DOCTYPE html>
    <html>
    <head>
      <title>Bootstrap 实例</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
      <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
      <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
      <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
    </head>
    <body>
    
    <div class="container">
    
      <button type="button" class="btn btn-primary" id="pay">立即付款</button>
    
    </div>
    
    </body>
    <script>
        $("#pay").click(function () {
            $.ajax({
                url:"alipay",
                type:"GET",
                success:function (data) {
                    window.open(data.pay_url)
                    // 查询结果
                    $.ajax({
                        url:"alipay/result",
                        type:"POST",
                        success:function (data) {
                            if (data.code == 200){
                                alert("支付成功")
                            }else {
                                alert("支付失败")
                            }
                }
            })
    
                }
            })
        })
    </script>
    </html>
    # 支付宝付款测试
        url(r'^$', href_index.Index.as_view()),
        url(r'alipay$',alipay.Alipay.as_view()),   # 查询
        url(r'alipay/result$',alipay.Alipay.as_view()), # 查询结果
  • 相关阅读:
    [转载]#2002 服务器没有响应 (或者本地 MySQL 服务器的套接字没有正确配置
    [转载]使用Cufon技术实现Web自定义字体
    [转载]Fedora14下MySQL、Apache、PHP、phpMyAdmin的安装步聚
    GUID的使用
    访问来源记录
    MongoDB数据库的基本概念
    域登录验证
    sql语法和MongoDB语法的对应关系
    ADO.NET数据集添加虚拟字段
    MongoDB开发常用资源地址
  • 原文地址:https://www.cnblogs.com/wanghong1994/p/13521861.html
Copyright © 2020-2023  润新知