来源: 阅读:- 2020-03-30 06:46:21
参考链接:https://github.com/fzlee/alipay
支付宝沙箱环境:https://openhome.alipay.com/platform/appDaily.htm?tab=account
手机网站支付:https://docs.open.alipay.com/203/107090/
pip install python-alipay-sdk --upgrade
# coding:utf-8from ihome.base import dbfrom . import apifrom ihome.utils.commons import login_requiredfrom ihome.models import Orderfrom flask import g, current_app, jsonify, requestfrom ihome.utils.response_code import RETfrom alipay import AliPayfrom ihome import constants@api.route("/orders/<int:order_id>/payment", methods=["POST"])@login_requireddef 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")
①在支付测试中,记得保证每次的out_trade_no不同(使用时间戳),如果重复会报“操作频繁,请稍后重试”
(正文已结束)
免责声明及提醒:此文内容为本网所转载企业宣传资讯,该相关信息仅为宣传及传递更多信息之目的,不代表本网站观点,文章真实性请浏览者慎重核实!任何投资加盟均有风险,提醒广大民众投资需谨慎!