后台
先获取code code有效5min
public
string GetCodeUrl(string Appid, string redirect_uri)
{
return
string.Format(
"https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect"
, Appid, redirect_uri);
}
url 接口(接收code方法地址***/a/b)
通过code获取token
access_token
公众号调用各接口时都需使用access_token
通过token 生成 预支付订单
文档代码:
function onBridgeReady(){
WeixinJSBridge.invoke(
'getBrandWCPayRequest', {
"appId":"wx2421b1c4370ec43b", //公众号名称,由商户传入
"timeStamp":"1395712654", //时间戳,自1970年以来的秒数
"nonceStr":"e61463f8efa94090b1f366cccfbbb444", //随机串
"package":"prepay_id=u802345jgfjsdfgsdg888",
"signType":"MD5", //微信签名方式:
"paySign":"70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名
},
function(res){
if(res.err_msg == "get_brand_wcpay_request:ok" ) {} // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回 ok,但并不保证它绝对可靠。
}
);
}
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();
}
项目: 统一下单 - 返回参数 - 下单调起支付
wxPay: function (obj) {
if (typeof WeixinJSBridge == "undefined") {
g.alert('该浏览器不支持微信支付哟!')
if (document.addEventListener) {
document.addEventListener('WeixinJSBridgeReady', g.inPay, false);
} else if (document.attachEvent) {
document.attachEvent('WeixinJSBridgeReady', g.inPay);
document.attachEvent('onWeixinJSBridgeReady', g.inPay);
}
} else {
g.inPay(obj);
}
},
inPay: function (obj) {
var json;
$.ajaxSettings.async = false;
var postUrl;
switch (obj.mark){
case 'course':postUrl='/Api/course/submitOrder';
break;
case 'member':postUrl='/Api/member/buyMember';
break;
default:break;
}
$.post(postUrl, {id:obj.id}, function (res) {
if (res.httpCode == 200) {
var dt = res.data;
json = {
appId: dt.appId
, timeStamp: dt.timeStamp
, nonceStr: dt.nonceStr
, package: dt.package
, signType: dt.signType
, paySign: dt.paySign
}
WeixinJSBridge.invoke(
'getBrandWCPayRequest', {
"appId": json.appId
, "timeStamp": json.timeStamp
, "nonceStr": json.nonceStr
, "package": json.package
, "signType": json.signType
, "paySign": json.paySign
},
function (res) {
if (res.err_msg == "get_brand_wcpay_request:ok") {
g.alert("恭喜你支付成功!");
g.open(localStorage.getItem('url'));
} else {
g.alert("支付失败!");
}
}
);
} else {
g.alert('获取订单失败!')
}
});
}
微信官方文档 :https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6
文档参考 : https://www.zhihu.com/question/63196360
来源:oschina
链接:https://my.oschina.net/u/4363045/blog/4232163