这篇文章就不写那些外面都有的代码,就写一个遇到的坑吧,在httpclient请求商家转账到零钱的api时,微信总是返回商户证书序列号有误。请使用签名私钥匹配的证书序列号,在网上找了各种帖子也没找到方法,然后在自己实践中发现,原来是微信生成的生成的序列号和证书序列号都是需要传进去的,这里就上一个httpclient的代码,其他的代码其他博客都有,我就不搬运了
/**
* 发起批量转账API 批量转账到零钱
*
* @param requestUrl
* @param requestJson 组合参数
* @param wechatPayserialNo 商户证书序列号
* @param mchID4M 商户号
* @param privatekeypath 商户私钥证书路径
* @return
*/
public static String postTransBatRequest(
String requestUrl,
String requestJson,
String wechatPayserialNo, //这里是wx的jar包生成的序列号 certificate.getSerialNumber().toString(16).toUpperCase();
String wechatPayserialNo2, //这里是商户证书的序列号
String mchID4M,
String privatekeypath) {
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
HttpEntity entity = null;
try {
//商户私钥证书
HttpPost httpPost = new HttpPost(requestUrl);
// NOTE: 建议指定charset=utf-8。低于4.4.6版本的HttpCore,不能正确的设置字符集,可能导致签名错误
httpPost.addHeader("Content-Type", "application/json");
httpPost.addHeader("Accept", "application/json");
//"55E551E614BAA5A3EA38AE03849A76D8C7DA735A");
httpPost.addHeader("Wechatpay-Serial", wechatPayserialNo);
//-------------------------核心认证 start-----------------------------------------------------------------
String strToken = VechatPayV3Util.getToken("POST",
"/v3/transfer/batches",
requestJson,mchID4M,wechatPayserialNo2, privatekeypath);
log.error("微信转账token "+strToken);
// 添加认证信息
httpPost.addHeader("Authorization",
"WECHATPAY2-SHA256-RSA2048" + " "
+ strToken);
//---------------------------核心认证 end---------------------------------------------------------------
httpPost.setEntity(new StringEntity(requestJson, "UTF-8"));
//发起转账请求
response = httpclient.execute(httpPost);
entity = response.getEntity();//获取返回的数据
log.info("-----getHeaders.Request-ID:"+response.getHeaders("Request-ID"));
return EntityUtils.toString(entity,"UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭流
}
return null;
}