当前位置: 首页 > Web前端 > HTML5

微信、支付宝支付

时间:2023-04-05 13:38:35 HTML5

微信公众号支付通过url重定向获取微信需要的code//获取微信code微信公众号环境函数getCode(){constappId="WeChatappid";//注意一定要转义urlconstREDIRECT_URI=encodeURIComponent(window.location.href);window.location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${REDIRECT_URI}&response_type=code&scope=snsapi_base&state=null#wechat_redirect`);}调用微信支付jssdk//调用微信支付JSAPIfunctiononBridgeReady(data){if(typeofWeixinJSBridge=="undefined"){if(document.addEventListener){document.addEventListener("WeixinJSBridgeReady",onBridgeReady,false);}elseif(document.attachEvent){document.attachEvent("WeixinJSBridgeReady",onBridgeReady);document.attachEvent("onWeixinJSBridgeReady",on}Bridge}else{WeixinJSBridge.invoke("getBrandWCPayRequest",{appId:data.appId,//公众号Name,商户传入timeStamp:data.timeStamp,//时间戳,从1970年开始的秒数nonceStr:data.nonceStr,//随机字符串package:data.package,signType:"MD5",//微信签名Method:paySign:data.paySign,//微信签名},function(res){/***当状态为cancel时,表示用户取消了支付。Toast提示*当状态不是cancel时,跳转到内部支付状态页面*/if(res.err_msg=="get_brand_wcpay_request:cancel"){Toast("PaymentCancellation");}else{//注意微信公众号支付需要前端跳转到支付状态页面window.location.replace();}});}}H5微信支付通过重定向URL连接微信支付。一般是后端返回给前端的地址,需要重定向window.location.replace('后端返回的地址');form表单来对接支付,需要注意不要传入更多的参数,否则会返回错误vartemp=document.createElement("form");temp.action=data.prepay.form_url;临时方法=“发布”;临时.style.display=“无”;letparams=data.prepay;for(letvinparams){if(v!="form_url"){varopt=document.createElement("input");opt.name=v;opt.value=decodeURIComponent(params[v]);temp.appendChild(选择);}}document.body.appendChild(temp);临时提交();