
def create_app():#微信支付1.程序id: 2.商户号id 3.商户号钥匙 4.回调链接app.config['WEIXIN_APP_ID'] = ''app.config['WEIXIN_APP_MCH_ID'] =''app.config['WEIXIN_APP_MCH_KEY'] =''app.config['WEIXIN_NOTIFY_URL'] =''
<script src="js/jquery-3.3.1.js"></script><script src="js/qrcode.min.js"></script>
<div id="div1" style="text-align: center;width: 400px;height: 400px;;border-radius:10px;box-shadow: 4px 4px 18px #c3c3c3;"></br></br></br></br><h1>500点</h1></br></br><button id="order1" class="button0" onclick="creat_order(this)" value="XXXXX">49元下单</button></br></br><div style="height: 200px;width: 200px;margin:0 auto"><div id="qrcode1"></div></div></br><img id="X1" onclick="close_order(this)" src="img/X.png" style="width: 50px;opacity:0"></div>
button点击onclick,creat_order(this)方法
button的value建议用一个自己知道的字符串
div qrcode1 准备二维码
img 点击onclick,close_order(this)方法关闭支付
var interval = '';//下单//点击微信支付按钮function creat_order(obj) {if ($(this).attr('disabled') == 'disabled') {return false;}var $box_btn = $(obj);//这个按钮var $idinfo = $box_btn.attr("id");//这个按钮的idvar $valueinfo = $box_btn.attr("value");//这个按钮的valuevar $box = $box_btn.parent();//这个按钮的divvar $box_X= $box.find("img");//X图片var $qrcodeDiv = $box.find("div").find("div");//qrcode标签div$.ajax({type: "POST",dataType: "json",url: "/cloud/weixinpay",data: {//表单信息bbox:$valueinfo},beforeSend: function () {$box_btn.text("正在生成支付二维码...请稍候");$box.animate({height: '550px'}, "slow");$box_X.animate({opacity: '1'}, "slow");},success: function (data) {if (data.status == "1") {//二维码var qrcode = new QRCode($qrcodeDiv[0],{text: data.qrcode, width: 200, height: 200});$box_btn.text("手机微信扫码支付");//轮询interval = window.setInterval(function () {pay_status(data.order_id)}, 3000);} else {//失败alert("系统错误停止操作");}}});return false;}
//轮询function pay_status(order_id){$.ajax({type: "POST",dataType: "json",url: "/pay/weixinorder_status",//async:false,data:{order_id:order_id},success: function (data) {if (data.status=="已付款"){// 销毁定时任务window.clearInterval(interval);//关闭模态//$('#weixin-pay-model').modal('hide');$("#close-model").click();window.location.href = "/pudding";//跳转}else{console.log("失败,重新");}}});return false;};
//停止按钮function close_order(obj) {var $box_img = $(obj);//X图片var $box = $box_img.parent();//大div$box.animate({height: '400px'}, "slow");$box_img.animate({opacity: '0'}, "slow");// 销毁定时任务window.clearInterval(interval);$('#qrcode1').empty();};
#微信支付#登录-多了个openid@wechat.route('/cloud/weixinpay', methods=['post'])def weixinpay():#判断有没有sessionif session.get('unionid') is not None:#判断有没有stateif session.get('state') is not None:unionid = session.get('unionid')#从浏览器拿session拿unionidopenid = session.get('openid')#从浏览器拿session拿openidheadimgurl = session.get('headimgurl') #从浏览器拿session拿headimgurlnickname = session.get('nickname')#从浏览器拿session拿nicknamestate = session.get('state')#从浏览器拿session拿statesession['unionid'] = unionid#设置session('unionid',unionid)session['nickname'] = nickname#设置session('nickname',nickname)session['headimgurl'] = headimgurl#设置session('headimgurl',headimgurl)session['state'] = state#设置session('state',state)session.permanent = True #开启session时间限制#上面是检验登陆#此处加逻辑##################price = request.form.get('price')bbox = request.form.get('bbox')if bbox == "XXXXX":total_fee = 4900else:return "error"# 1.拿过数据后,进行出处理逻辑# 2.调用统一下单api,这四个参数都是必须的# 初始化pay = WeixinPay(app_id=app.config["WEIXIN_APP_ID"], mch_id=app.config["WEIXIN_APP_MCH_ID"],mch_key=app.config["WEIXIN_APP_MCH_KEY"], notify_url=app.config["WEIXIN_NOTIFY_URL"])out_trade_no = pay.nonce_str#生成订单号ip = request.remote_addr#获取ip地址Wechatdatabase_id = Wechatdatabase.query.filter_by(unionid=unionid).first().idorder_name = total_fee/100order = Wechatdatabaseorder(openid=openid,nickname=nickname,unionid=unionid,out_trade_no=out_trade_no,order_name=order_name,state="未付款",time_end=0,total_fee=total_fee,spbill_create_ip=ip,Wechatdatabase_id=Wechatdatabase_id)db.session.add(order)db.session.commit()print(out_trade_no)try:# 统一支付api,trade_type:支付类型,当为native时,product_id参数必须要有,body为订单内容,out_rade_no:订单号,total_fee:费用,分为单位,attch:附加消息。# pay_info = pay.unified_order(trade_type="NATIVE", product_id=product_id, body="test", out_trade_no=out_trade_no,pay_info = pay.unified_order(trade_type="NATIVE", product_id=62, body="test", out_trade_no=out_trade_no,total_fee=total_fee,attach="other info ")except WeixinError as e:print(e)return make_response(jsonify({"status": 1,"info": pay_info, # 返回给ajax的订单信息,"qrcode": pay_info.code_url,"order_id": out_trade_no,}))else:return ('404')else:return redirect(url_for('wechat.login'))
# 接受微信系统,支付成功回调的方法@wechat.route("/pay/notify", methods=["post", "get"])def pay_notify():"""微信异步通知"""# 微信发过来的数据,xml格式data = request.datadata = pay.to_dict(request.data.decode('utf-8'))pay = WeixinPay(app_id=app.config["WEIXIN_APP_ID"], mch_id=app.config["WEIXIN_APP_MCH_ID"],mch_key=app.config["WEIXIN_APP_MCH_KEY"], notify_url=app.config["WEIXIN_NOTIFY_URL"])if not pay.check(data):return pay.reply("签名验证失败", False)# 处理业务逻辑# 把数据写去Charge模型中去return pay.reply("OK", True)
#我查微信系统@wechat.route("/pay/weixinorder_status", methods=["POST"])def weixinorder_status():#判断有没有sessionif session.get('unionid') is not None:#判断有没有stateif session.get('state') is not None:#从浏览器拿session拿unionidunionid = session.get('unionid')#从浏览器拿session拿unionidheadimgurl = session.get('headimgurl')#从浏览器拿session拿headimgurlnickname = session.get('nickname')#从浏览器拿session拿nicknamestate = session.get('state')#从浏览器拿session拿statesession['unionid'] = unionid#设置session('unionid',unionid)session['nickname'] = nickname #设置session('nickname',nickname)session['headimgurl'] = headimgurl#设置session('headimgurl',headimgurl)session['state'] = state#设置session('state',state)session.permanent = True#开启session时间限制#上面是检验登陆#此处加逻辑##################if request.method == 'POST':# 查询订单pay = WeixinPay(app_id=app.config["WEIXIN_APP_ID"], mch_id=app.config["WEIXIN_APP_MCH_ID"],mch_key=app.config["WEIXIN_APP_MCH_KEY"], notify_url=app.config["WEIXIN_NOTIFY_URL"])order_id = request.form.get('order_id')try:query_info = pay.order_query(out_trade_no=order_id)print("查询信息", query_info)if query_info["result_code"] == "SUCCESS" and query_info["return_code"] == "SUCCESS":print("支付状态", query_info["trade_state"])if query_info["trade_state"] == "SUCCESS":print("支付成功")#更新订单数据out_trade_no = query_info.get("out_trade_no")#微信-订单号time_end = query_info.get("time_end")#微信-支付完成时间order = Wechatdatabaseorder.query.filter_by(out_trade_no=out_trade_no).first()#根据订单号查记录order.state = "已付款" #修改数据order.time_end = time_end #修改支付完成时间#更新用户数据total_fee = int(query_info.get("total_fee"))#自己写#增加点数if total_fee == 4900:add_points = 500else:return "error"#自己写啦#提交操作到数据库db.session.commit()#返回订单状态status=1status = str(Wechatdatabaseorder.query.filter_by(out_trade_no=out_trade_no).first().state)data = {"status": status}return jsonify(data)except WeixinError as e:print(e)else:return ('404')else:return redirect(url_for('wechat.login'))