hand
_1_1_21
4
python3.X - Web - Flask1.1.1
共46篇
python3.X - Web - Flask1.1.1
返回栏目
0k
0.3k
1k
6k
0.6k
0.8k
0.3k
0.1k
0.2k
0.8k
0.6k
0.6k
0.1k
0.5k
0.2k
2k
0.3k
5k
2k
2k
1k
0.3k
1k
0.1k
1k
1k
0.5k
2k
2k
0.7k
1k
2k
0.1k
1k
0.1k
2k
2k
0.9k
5k
4k
1k
1k
3k
1k
0k
0k
返回python3.X - Web - Flask1.1.1栏目
作者:
贺及楼
成为作者
更新日期:2021-11-07 11:20:50
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");//这个按钮的id
var $valueinfo = $box_btn.attr("value");//这个按钮的value
var $box = $box_btn.parent();//这个按钮的div
var $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():
#判断有没有session
if session.get('unionid') is not None:
#判断有没有state
if session.get('state') is not None:
unionid = session.get('unionid')#从浏览器拿session拿unionid
openid = session.get('openid')#从浏览器拿session拿openid
headimgurl = session.get('headimgurl') #从浏览器拿session拿headimgurl
nickname = session.get('nickname')#从浏览器拿session拿nickname
state = session.get('state')#从浏览器拿session拿state
session['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 = 4900
else:
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().id
order_name = total_fee/100
order = 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.data
data = 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():
#判断有没有session
if session.get('unionid') is not None:
#判断有没有state
if session.get('state') is not None:
#从浏览器拿session拿unionid
unionid = session.get('unionid')#从浏览器拿session拿unionid
headimgurl = session.get('headimgurl')#从浏览器拿session拿headimgurl
nickname = session.get('nickname')#从浏览器拿session拿nickname
state = session.get('state')#从浏览器拿session拿state
session['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 = 500
else:
return "error"
#自己写啦
#提交操作到数据库
db.session.commit()
#返回订单状态status=1
status = 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'))
python3.X - Web - Flask1.1.1
整章节共46节
快分享给你的小伙伴吧 ~