微信小程序支付申請,微信小程序支付開發(fā)詳解
https://pay.weixin.qq.com/wiki/d ... chapter=7_4&index=2 業(yè)務(wù)流程在這里大家看文檔的時候可以看到。第一個坑,獲取用戶的openid,參數(shù)一定要拼在url連接上,否則會報{"errcode":40013,"errmsg":"invalid appid, hints: [ req_id: iil1ba0504ns86 ]"}錯誤
onLoad: function () {
var that = this
wx.login({
success: function (res) {
if (res.code) {
//發(fā)起網(wǎng)絡(luò)請求
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wxaacf22345345cfc7162fe3&secret=83ebd41c3e6f34a49b3a34578063434548ff3f71&js_code=' + res.code + '&grant_type=authorization_code',
method: "POST",
success: function (res) {
that.setData({
openid: res.data.openid
})
}
})
} else {
console.log('獲取用戶登錄態(tài)失敗!' + res.errMsg)
}
}
});
}
復(fù)制代碼
第二個坑,支付統(tǒng)一下單接口,簽名這個坑是比較多人遇到問題的這個是MD5加密經(jīng)常和簽名工具里面的加密簽名不一樣
簽名加密工具地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=20_1
簽名加密的時候要轉(zhuǎn)成utf-8,加密我用自己的接口進(jìn)行加密的 digest.update(data.getBytes("utf-8"));
// 統(tǒng)一下單接口獲取sign(簽名)
paysignjsapi: function (appid, attach, body, mch_id, nonce_str, notify_url, openid, out_trade_no, spbill_create_ip, total_fee, trade_type, key) {
var self = this;
//加密簽名
wx.request({
url: 'http://localhost:8080/XinXingWXApi/wxXcxApi/Md5Encrypt.do',
method: 'GET',
data: {
appid: appid,
attach: attach,
body: body,
mch_id: mch_id,
nonce_str: nonce_str,
notify_url: notify_url,
openid: openid,
out_trade_no: out_trade_no,
spbill_create_ip: spbill_create_ip,
total_fee: total_fee,
trade_type: trade_type,
key: key
},
//統(tǒng)一下單
success: function (res) {
var sign = res.data.strMd5
var formData = "
formData += "
formData += "
formData += "" + body + "" //標(biāo)題
formData += "
formData += "
formData += "
formData += "
formData += "
formData += "
formData += "
formData += "
formData += "
formData += ""
復(fù)制代碼
返回數(shù)據(jù)解析xml
//請求統(tǒng)一下單接口
wx.request({
url: "https://api.mch.weixin.qq.com/pay/unifiedorder",
method: 'POST',
data: formData,
success: function (data) {
wx.request({
url: "http://localhost:8080/XinXingWXApi/wxXcxApi/xmlAnalyze.do?strXml=" + data.data,
method: 'POST',
success: function (res) {
var pk = 'prepay_id=' + res.data.prepayId;
var timeStamp = self.createTimeStamp();
//獲取支付簽名,并支付
self.getsignType(appid, timeStamp, nonce_str, pk, "MD5", key);
}
})
}
})
}
});
}
第三就是調(diào)用支付了,這里也有幾個小坑,第一就是appId很多寫成appid就不行了,第二個就是preoatid 的參數(shù)格式要寫對prepay_id=wx2017011711060194dccf725232155886323 第三個就是調(diào)用支付的時候報支付簽名錯誤,也需要到簽名接口查看簽名是否一致,查看參數(shù)是否是對的,調(diào)用微信支付的時候必須加上appId
getsignType: function (appid, timeStamp, nonce_str, pk, signType, key) {
var that = this;
wx.request({
url: "http://localhost:8080/XinXingWXApi/wxXcxApi/getSignType.hn",
method: 'GET',
data: {
appId: appid,
timeStamp: timeStamp,
nonceStr: nonce_str,
pk: pk,
signType: signType,
key: key
},
success: function (res) {
console.log(res.data.paySign)
var paySign = res.data.paySign
//調(diào)用微信支付
wx.requestPayment({
'appId': appid,
'timeStamp': timeStamp,
'nonceStr': nonce_str,
'package': pk,
'signType': 'MD5',
'paySign': paySign,
'success': function (res) {
console.log(res);
console.log('success');
},
'fail': function (res) {
console.log(res);
console.log('fail');
},
'complete': function (res) {
// console.log(res);
console.log('complete');
}
});
}
})
}
第二部分:如何開通一個小商店