说一下没有子商户,只有商户号,就是用的以前的支付接口,这是云开发没有后端代码那个notify_url不写还不行,
不知道哪里有问题,
签名在微信工具里验证通过,调用下单接口就报签名错误,希望会的人答疑解惑一下,谢谢啦
const mch_id = 'xxx ';
const key = 'xxxxx';
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)
console.log(sa.join('&'))
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>')
let axml = '<xml>' + sa.join('') + '</xml>'
console.log("最后签名:",axml)
return axml
}
function getNonceStr(){
var chars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
var nums="";
for(var i=0;i<32;i++){
var id = parseInt(Math.random()*61);
nums+=chars[id];
}
nums= nums.toLowerCase()
return nums ;
}
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
const appId = appid = wxContext.APPID
console.log("appid是:", appid, appId)
console.log("key是:", key)
const openid = wxContext.OPENID
const body = event.msg;
const total_fee = event.totalFee;
const notify_url = "http://127.0.0.1"
const spbill_create_ip = "127.0.0.1"
const fee_type = "CNY"
const nonceStr = nonce_str = getNonceStr()
const timeStamp = parseInt(Date.now() / 1000) + ''
const out_trade_no = event.outTradeNo;
const trade_type = "JSAPI"
const sign_type = "MD5"
const xmlArgs = {
appid,
body,
fee_type,
mch_id,
nonce_str,
notify_url,
openid,
out_trade_no,
sign_type,
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")
console.log("签名是:",xml)
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)
}
}
直接使用这个sdk https://wechatpay.js.org/
<xml><return_code><![CDATA[FAIL]]></return_code>
<return_msg><![CDATA[签名错误,请检查后再试]]></return_msg>
</xml>