我的所有操作是在unicloud 云对象上完成的
我是在微信公众号里,打开了H5网页进行支付
1:能正常获得code,
2:能正常获得 openid
3:签名, 签名用的是商户的秘钥,不是公众号的秘钥,
-------- 开始签名------------------
function getSign(keys,params){
let str = ''
for(let i=0;i<keys.length;i++){
const key = keys[i]
str += key + '=' + params[key] + '&';
}
str = str + 'key=' + mchsecret // 这里用的是商户的秘钥,不是公众号的秘钥,不知道对不对
const tempStr = encodeURI(str); //url 编码
const md5 = crypto.createHash('md5') //md5加密
let md5str = md5.update(tempStr).digest('hex').toUpperCase();
return md5str;
}
这里是正常的没有问题。
---------签名结束------------------------------------------------------------------
4:签名完名以后完转xml,这里重要数据我都用xxx 来代替了
<xml>
<appid><![CDATA[xxxxxxxxxxxxx]]></appid>
<attach>深圳分店</attach>
<body>这是一个测试</body>
<device_info>WEB</device_info>
<mch_id>0000000000000</mch_id>
<nonce_str>p@xdtestabc123d</nonce_str>
<notify_url>xxxxxxxxxxxxx</notify_url>
<openid>xxxxxxxxxxxxxxxxxxx</openid>
<out_trade_no>1001001</out_trade_no>
<spbill_create_ip>198.61.17.12</spbill_create_ip>
<total_fee>1</total_fee>
<trade_type>MWEB</trade_type>
<sign>56AEFE5236B1B481F660896D08910F12</sign>
</xml>
5: -------- 开始下单----------------------------------
const unifiedorder_url = "https://api.mch.weixin.qq.com/pay/unifiedorder" //统一下单地址
const res = await uniCloud.httpclient.request(unifiedorder_url,{
method: 'POST',
data:val.xml
})
conslog.log(res);
6: 结果打印如下:我看不懂了
"data": {
"type": "Buffer",
"data": [62, 20, 109, 108, 62, 60, 114, 101, 116, 117, 114, 110, 95, 99, 111, 100, 101, 62, 60, 33, 91, 67, 68, 65, 84, 65, 91, 70, 65, 73, 76, 93, 93, 61, 60, 47, 114, 101, 116, 117, 114, 110, 95, 99, 111, 100, 101, 62, 10, 60, 114, 101, 116, 95, 159, 115, 103, 62, 60, 33, 91, 67, 68, 65, 84, 65, 91, 231, 173, 190, 219, 144, 141, 233, 148, 153, 232, 175, 175, 239, 188, 141, 22, 15, 13, 20, 16, 128, 230, 159, 65, 229, 144, 142, 229, 134, 141, 232, 175, 149, 93, 93, 62, 60, 47, 114, 10, 116, 117, 114, 110, 95, 10, 115, 103, 62, 10, 60, 43, 120, 109, 108, 62]
},
"status": 200,
"headers": {
"server": "nginx",
"date": "Mon, 08 May 2023 09:30:57 GMT",
"content-type": "text/plain",
"content-length": "126",
"connection": "keep-alive",
"keep-alive": "timeout=8",
"cache-control": "no-cache, must-revalidate",
"x-content-type-options": "nosniff",
"request-id": "08D182E3A206D2CD8C70283348, 08D182E3AA0D28C5820AB4B402-0"
},
"res": {
"status": 200,
"statusCode": 200,
"statusMessage": "OK",
"headers": {
"server": "nginx",
"date": "Mon, 08 May 2023 09:30:57 GMT",
"content-type": "text/plain",
"content-length": "126",
"connection": "keep-alive",
"keep-alive": "timeout=8",
"cache-control": "no-cache, must-revalidate",
"x-content-type-options": "nosniff",
"request-id": "08D182E3A2064A904-270283348, 08D182E3A206104B90828B4B402-0"
},
"size": 126,
"aborted": false,
"rt": 217,
"keepAliveSocket": false,
"data": {
"type": "Buffer",
"data": [62, 20, 109, 108, 62, 60, 114, 101, 116, 117, 114, 110, 95, 99, 111, 100, 101, 62, 60, 33, 91, 67, 68, 65, 84, 65, 91, 70, 65, 73, 76, 93, 93, 61, 60, 47, 114, 101, 116, 117, 114, 110, 95, 99, 111, 100, 101, 62, 10, 60, 114, 101, 116, 95, 159, 115, 103, 62, 60, 33, 91, 67, 68, 65, 84, 65, 91, 231, 173, 190, 219, 144, 141, 233, 148, 153, 232, 175, 175, 239, 188, 141, 22, 15, 13, 20, 16, 128, 230, 159, 65, 229, 144, 142, 229, 134, 141, 232, 175, 149, 93, 93, 62, 60, 47, 114, 10, 116, 117, 114, 110, 95, 10, 115, 103, 62, 10, 60, 43, 120, 109, 108, 62]
},
"requestUrls": ["https://api.mch.weixin.qq.com/pay/unifiedorder"],
"timing": null,
"remoteAddress": "221.238.80.33",
"remotePort": 443,
"socketHandledRequests": 1,
"socketHandledResponses": 1
}
返回的Buffer解析成字符串虽然有点奇怪,但是还能看出错误信息,签名错误。我猜可能http请求私自进行过一次编码导致乱码???
你签名的代码没有对参数排序,旧版支付接口,签名的时候,需要对参数进行排序。
尝试请求的时候声明contentType、编码或数据类型。