<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>微信支付</title>
</head>
<body>
<button id="btn" style="width:200px,height: 200px;" onclick="pay()">支付</button>
<script>
//获取支付结果
function onBridgeReady() {
var data=
{
"timeStamp":"1539754896588",
"package":"prepay_id=wx17134136728417a78df255d42332351216",
"paySign":"8A1FC464952FF8DF70D399F132049BC0",
"appId":"wx34284214d7a79ba1",
"signType":"MD5",
"nonceStr":"26c74a560172571a0f43a56ca0220c55"
};
WeixinJSBridge.invoke('getBrandWCPayRequest', data, function (res) {
if (res.err_msg == "get_brand_wcpay_request:ok") {
alert("支付成功", 'middle')
} else if (res.err_msg == "get_brand_wcpay_request:cancel") {
alert("支付取消", 'middle')
} else {
alert("支付失败", 'middle')
}
//可以在这里打印支付返回的信息
});
};
//发起支付
function pay() {
//可以在此处查看是否发起支付
if (typeof WeixinJSBridge == "undefined") {
if (document.addEventListener) {
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
} else if (document.attachEvent) {
document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
}
} else {
onBridgeReady();
}
};
</script>
</body>
</html>