本文小編為大家詳細(xì)介紹“微信小程序中支付功能開(kāi)發(fā)錯(cuò)誤怎么解決”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“微信小程序中支付功能開(kāi)發(fā)錯(cuò)誤怎么解決”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。
創(chuàng)新互聯(lián)主營(yíng)高陽(yáng)網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶APP開(kāi)發(fā)公司,高陽(yáng)h5小程序制作搭建,高陽(yáng)網(wǎng)站營(yíng)銷推廣歡迎高陽(yáng)等地區(qū)企業(yè)咨詢業(yè)務(wù)流程在這里大家看文檔的時(shí)候可以看到。第一個(gè)坑,獲取用戶的openid,參數(shù)一定要拼在url連接上,否則會(huì)報(bào){"errcode":40013,"errmsg":"invalid appid, hints: [ req_id: iil1ba0504ns86 ]"}錯(cuò)誤
onLoad: function () { var that = this wx.login({ success: function (res) { if (res.code) { //發(fā)起網(wǎng)絡(luò)請(qǐng)求 wx.request({ url: '/tupian/20230522/jscode2session method: "POST", success: function (res) { that.setData({ openid: res.data.openid }) } }) } else { console.log('獲取用戶登錄態(tài)失??!' + res.errMsg) } } }); }
第二個(gè)坑,支付統(tǒng)一下單接口,簽名這個(gè)坑是比較多人遇到問(wèn)題的這個(gè)是MD5加密經(jīng)常和簽名工具里面的加密簽名不一樣
簽名加密工具地址:/tupian/20230522/404.html>簽名加密的時(shí)候要轉(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 += " "" + appid + " " //appid formData += "" + attach + " " //附加數(shù)據(jù) formData += "" + body + "" //標(biāo)題 formData += "" + mch_id + " " //商戶號(hào) formData += "" + nonce_str + " " //隨機(jī)字符串,不長(zhǎng)于32位。 formData += "" + notify_url + " " //異步接收微信支付結(jié)果通知的回調(diào)地址 formData += "" + openid + " " //用戶Id formData += "" + out_trade_no + " " //商戶訂單號(hào) formData += "" + spbill_create_ip + " " formData += "" + total_fee + " " //金額 formData += "" + trade_type + " " //公共號(hào)支付 formData += "" + sign + " "http://簽名 formData += "
返回?cái)?shù)據(jù)解析xml
//請(qǐng)求統(tǒng)一下單接口 wx.request({ url: "/tupian/20230522/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)用支付了,這里也有幾個(gè)小坑,第一就是appId很多寫成appid就不行了,第二個(gè)就是preoatid 的參數(shù)格式要寫對(duì)prepay_id=wx2017011711060194dccf725232155886323 第三個(gè)就是調(diào)用支付的時(shí)候報(bào)支付簽名錯(cuò)誤,也需要到簽名接口查看簽名是否一致,查看參數(shù)是否是對(duì)的,調(diào)用微信支付的時(shí)候必須加上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'); } }); } }) }
讀到這里,這篇“微信小程序中支付功能開(kāi)發(fā)錯(cuò)誤怎么解決”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。