首先,我也是翻了度娘,csdn上面的一些大佬的貼子!!!!居然發現沒有一個能跑的,啊啊啊啊。
在我幾近崩潰的邊緣,我嘗試了一種方法!!啊啊啊最後居然成功了,當然非常感謝那位大佬的代碼!!!!
現在,我來說一下可能出現的額問題!!!
傳回簽名錯誤!!簽名錯誤!!!簽名錯誤!!
關于這個問題,我真的想說,這真的是個巨坑!!!!!誰能想到最後不是修改代碼???而是
更改商戶的key!!改key!!!改key!!!重設!!!(不要問我為什麼!!親自測試,有效!!)
說實話,這個問題真的弄的我是焦頭爛額,心力憔悴,一時間真的是不想改代碼,console.log()。最後百度這個問題,發現有幾種可能,然後最後是無可奈何花落去,就最後姑且試一試吧!
沒想到!!!第二天早上,老闆改了商戶的資訊,key,我成了!!!!!(我發誓一定要造福人類!!趕忙就來寫部落格了)
好吧!!!話不多說,直接幹貨!!!
https://developers.weixin.qq.com/community/develop/article/doc/0004c4a50a03107eaa79f03cc56c13
以上是大佬的代碼,注意key和商戶參數這兩個就行,其他的直接複制,就是這麼完美!!!
代碼前提:隻需要替換兩個與自己相關的參數key和mch_id
1、小程式開通微信支付成功,去公衆平台(https://mp.weixin.qq.com/);
成功後可以知道自己的mch_id,即商戶号。
2、去這裡:商戶平台(https://pay.weixin.qq.com/),擷取key = API密鑰
(如果報錯!!!簽名錯誤!!!一定去重設key 可能就是你的密碼太過于簡單和規則了!!!!)雖然可能問題的解決辦法不唯一,但是我的實際經曆告訴你,一定要去試一試
,對了,,不要兩次設定相同的key!!!!!千萬不要,,,,不然死的更慘!!永不找不出錯誤!!!

小程式端:
testWxCloudPay: function () {
wx.cloud.callFunction({
name: 'getPay',
// data: {body:"body",attach:"attach",total_fee:1}, // 可傳入相關參數。
success: res => {
console.log(res.result)
if (!res.result.appId) return
wx.requestPayment({
...res.result,
success: res => {
console.log(res)
}
})
}
})
},
雲函數getPay:
const key = "ABC...XYZ" //換成你的商戶key,32位
const mch_id = "1413092000" //換成你的商戶号
//以下全部照抄即可
const cloud = require('wx-server-sdk')
const rp = require('request-promise')
const crypto = require('crypto')
cloud.init()
function getSign(args) {
let sa = []
for (let k in args) sa.push(k + '=' + args[k])
sa.push('key=' + key)
return crypto.createHash('md5').update(sa.join('&'), 'utf8').digest('hex').toUpperCase()
}
function getXml(args) {
let sa = []
for (let k in args) sa.push('<' + k + '>' + args[k] + '</' + k + '>')
sa.push('<sign>' + getSign(args) + '</sign>')
return '<xml>' + sa.join('') + '</xml>'
}
exports.main = async(event, context) => {
const wxContext = cloud.getWXContext()
const appId = appid = wxContext.APPID
const openid = wxContext.OPENID
const attach = 'attach'
const body = 'body'
const total_fee = 1
const notify_url = "https://mysite.com/notify"
const spbill_create_ip = "118.89.40.200"
const nonceStr = nonce_str = Math.random().toString(36).substr(2, 15)
const timeStamp = parseInt(Date.now() / 1000) + ''
const out_trade_no = "otn" + nonce_str + timeStamp
const trade_type = "JSAPI"
const xmlArgs = {
appid,
attach,
body,
mch_id,
nonce_str,
notify_url,
openid,
out_trade_no,
spbill_create_ip,
total_fee,
trade_type
}
let xml = (await rp({
url: "https://api.mch.weixin.qq.com/pay/unifiedorder",
method: 'POST',
body: getXml(xmlArgs)
})).toString("utf-8")
if (xml.indexOf('prepay_id') < 0) return xml
let prepay_id = xml.split("<prepay_id><![CDATA[")[1].split("]]></prepay_id>")[0]
let payArgs = {
appId,
nonceStr,
package: ('prepay_id=' + prepay_id),
signType: 'MD5',
timeStamp
}
return {
...payArgs,
paySign: getSign(payArgs)
}
}
packge.json:
{
"name": "getPay",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "zfe",
"license": "ISC",
"dependencies": {
"wx-server-sdk": "latest",
"crypto": "^1.0.1",
"request-promise": "^4.2.2"
}
}
最後,祝福大家都能成功!脫離苦海哈哈哈哈哈
ps:代碼原創為連接配接位址的大佬。