注意事项:
1.v2版本提现需要用到证书(证书的密码就是你的mchid)
2.发送请求的时候需要setSSLSocketFactory
1.组装好提现需要的参数
Map<String,Object> params = new HashMap<String,Object>(10);
params.put("mch_appid","申请商户号的appid或商户号绑定的appid");
params.put("mchid","");
params.put("nonce_str","随机生成一个字符串");
params.put("partner_trade_no","提现用到的单号和订单号的道理一样");
params.put("openid","");
params.put("check_name","NO_CHECK");
params.put("amount","单位是分,最少提现一块,当天最多5000");
params.put("desc","备注");
String sign = getSign(params);
params.put("sign",sign );
//得到请求的参数
String xmlStr = XmlUtil.mapToXmlStr(convert, "xml");
//对参数进行加密
public String getSign(Map<String, Object> convert) {
//转换成key=value&key1=value1
String urlMap = WxUtil.sortMap(convert,"你的商户key");
//加密
return SecureUtil.md5(urlMap).toUpperCase();
}
2.发送请求
我使用的是hutool里的HttpRequest 我直接粘源码啦,有懂的小伙伴可以讲一下HttpRequest .setSSLSocketFactory,我也不太懂害怕误导了大家。我保证复制上就能用。
try {
String responseXml = HttpRequest.post("微信提现的链接")
.setSSLSocketFactory(SSLSocketFactoryBuilder
.create()
.setProtocol(SSLSocketFactoryBuilder.TLSv1)
.setKeyManagers(WxUtil.getKeyManager("你的商户id"))
.setSecureRandom(new SecureRandom())
.build()
//上面的组装好的参数
).body(xmlStr)
.execute()
.body();
//返回结果
Map<String, Object> responseMap = XmlUtil.xmlToMap(responseXml);
if (!WxUtil.isSuccess(response)) {
throw new ApiException("提现失败,请稍后重试");
}
//提现成功--你的逻辑部分
return responseMap ;
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("catch到的错误[{}]",e.getMessage());
throw new ApiException("提现失败,请稍后重试");
}
WxUtil.getKeyManager();,这里我直接放到resource下了,你也可以指定你的路径,比如:FileInputStream fileInputStream = new FileInputStream(“你的证书路径”);
/**
* certPass:你的商户id
*/
public static KeyManager[] getKeyManager(String certPass) throws Exception {
//加载证书
ClassPathResource cp = new ClassPathResource("resource下的证书路径");
KeyStore clientStore = KeyStore.getInstance("PKCS12");
clientStore.load(cp.getInputStream(), certPass.toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(clientStore, certPass.toCharArray());
return kmf.getKeyManagers();
}
至此已经可以提现啦。测试的话最少提现一块。