获取code在微信客户端打开
https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=http://app.xxx.com/App/wxpay/test.do&response_type=code&scope=snsapi_base&state=1#wechat_redirect
根据code获取openid
https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=secret&code=041mY1622LrXx01Gob222hZ7622mY169&grant_type=authorization_code
签名包括这几个字段
signMap.put("appId", );
signMap.put("timeStamp", );
signMap.put("nonceStr", );
signMap.put("signType", "MD5");
signMap.put("package", "prepay_id=" + PrepayId);
然后前端调用
function onBridgeReady(){
WeixinJSBridge.invoke(
'getBrandWCPayRequest', {
"appId" : appId, //公众号名称,由商户传入
"timeStamp": timeStamp, //时间戳,自1970年以来的秒数
"nonceStr" : nonceStr, //随机串
"package" : "prepay_id=" + pg,
"signType" : signType, //微信签名方式:
"paySign" : paySign //微信签名
},
function(res){
if(res.err_msg == "get_brand_wcpay_request:ok" ) {
alert("支付成功");
} // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回 ok,但并不保证它绝对可靠。
}
);
}
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();
}
}