微信瀏覽器喚起微信支付
阿新 • • 發佈:2022-04-22
微信瀏覽器自帶WeixinJSBridge方法直接呼叫就可以喚起微信支付
wxpay(data) { var ua = navigator.userAgent.toLowerCase(); var isWeixin = ua.indexOf("micromessenger") != -1; pay(data).then((res) = > { if (res.success) { console.log(res.data.result); const pay_params = res.data.result;if (isWeixin) { 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 { this.onBridgeReady(pay_params); } } else { // this.goLoginPage (pay_params)} } else { Toast.fail("微信支付調起失敗!"); } }). catch ((err) = > { console.log(err); }); }, onBridgeReady(params) { const pay_params = params; WeixinJSBridge.invoke("getBrandWCPayRequest", { appId: pay_params.appId, //公眾號名稱,由商戶傳入 timeStamp: pay_params.timeStamp, //時間戳,自1970年以來的秒數 nonceStr: pay_params.nonceStr, //隨機串 package: pay_params.package, signType: pay_params.signType, //微信簽名方式: paySign: pay_params.paySign, //微信簽名 }, function(res) { if (res.err_msg == "get_brand_wcpay_request:ok") { Toast.success("支付成功!"); } }); },