美文网首页
基于Flask实现支付宝接口调用(python3.6)

基于Flask实现支付宝接口调用(python3.6)

作者: python与数据分析 | 来源:发表于2019-12-04 16:08 被阅读0次

    参考链接:https://github.com/fzlee/alipay
    支付宝沙箱环境:https://openhome.alipay.com/platform/appDaily.htm?tab=account
    手机网站支付:https://docs.open.alipay.com/203/107090/

    业务场景:实现租房小程序,用户下单和订单保存功能

    1、安装以下包:
    pip install python-alipay-sdk --upgrade
    
    2、flask实现
    # coding:utf-8
    from ihome.base import db
    from . import api
    from ihome.utils.commons import login_required
    from ihome.models import Order
    from flask import g, current_app, jsonify, request
    from ihome.utils.response_code import RET
    from alipay import AliPay
    from ihome import constants
    
    
    @api.route("/orders/<int:order_id>/payment", methods=["POST"])
    @login_required
    def order_pay(order_id):
        """发起支付宝支付"""
        user_id = g.user_id
        try:
            order = Order.query.filter(Order.id == order_id, Order.user_id == user_id,
                                       Order.status == "WAIT_PAYMENT").first_or_404()
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.DBERR, errmsg="数据库异常")
    
        if order is None:
            return jsonify(errno=RET.NODATA, errmsg="订单数据有误")
        app_private_key_string = open(r"D:\ihome\v1\keys\app_private_key.pem").read()
        alipay_public_key_string = open(r"D:\ihome\v1\keys\alipay_public_key.pem").read()
        alipay = AliPay(
            appid="你的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=False
        )
        order_string = alipay.api_alipay_trade_wap_pay(
            out_trade_no=order.id,
            total_amount=str(order.amount/100.0),
            subject="爱家 %s" % order.id,
            return_url="http://127.0.0.1:8000/payComplete.html",
            notify_url=None
        )
        pay_url = constants.ALIPAY_URL_PREFIX + order_string
        return jsonify(errno=RET.OK, errmsg="OK", data={"pay_url": pay_url})
    
    
    @api.route("/order/payment", methods=["PUT"])
    def save_order_payment_result():
        """保存订单支付结果"""
        data = request.form.to_dict()
        # sign must be poped out
        signature = data.pop("sign")
        app_private_key_string = open(r"D:\ihome\v1\keys\app_private_key.pem").read()
        alipay_public_key_string = open(r"D:\ihome\ihome\v1\keys\alipay_public_key.pem").read()
    
        alipay = AliPay(
            appid="你的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=False
        )
    
        success = alipay.verify(data, signature)
    
        if success:
            order_id = data.get("out_trade_no")
            trade_no = data.get("trade_no")
            try:
                with db.auto_commit():
                    order = Order.query.get(order_id)
                    order.status = "WAIT_COMMENT"
                    order.trade_no = trade_no
                    db.session.add(order)
                    print('更新成功!')
            except Exception as e:
                current_app.logger.error(e)
    
        return jsonify(errno=RET.OK, errmsg="OK")
    
    
    3、遇到问题

    ①在支付测试中,记得保证每次的out_trade_no不同,如果重复会报“操作频繁,请稍后重试”

    相关文章

      网友评论

          本文标题:基于Flask实现支付宝接口调用(python3.6)

          本文链接:https://www.haomeiwen.com/subject/kiqlgctx.html