首先,我也是翻了度娘,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:代码原创为连接地址的大佬。