旧版微信支付接口要求请求时携带证书请求
构建请求参数
/**
* 付款到零钱
*
* @param withdrawalDto dto撤军
* @return {@link Map }<{@link String }, {@link Object }>
* @throws Exception 异常
* @Author chen 2023-07-27 15:04
*/
private Map payToUser(WithdrawalDto withdrawalDto) throws Exception {
log.info("发起微信提现零钱请求");
Map paramMap = new HashMap<>();
// 公众账号appid
paramMap.put("mch_appid", wechatConfig.getAppId());
// 微信支付分配的商户号
paramMap.put("mchid", wechatConfig.getMchId());
// 随机字符串,不长于32位。
paramMap.put("nonce_str", RandomUtil.randomString(16));
// 商户订单号,需保持唯一性
paramMap.put("partner_trade_no", withdrawalDto.getSerialNo());
// 商户appid下,某用户的openid
paramMap.put("openid", withdrawalDto.getCardNo());
//校验用户姓名选项 NO_CHECK:不校验真实姓名 FORCE_CHECK:强校验真实姓名
paramMap.put("check_name", "FORCE_CHECK");
//收款用户姓名,如果check_name设置为FORCE_CHECK,则必填用户真实姓名
paramMap.put("re_user_name", withdrawalDto.getIdCardName());
// 企业付款金额,金额必须为整数 单位为分
//元转分
BigDecimal applyMoney = withdrawalDto.getApplyMoney();
BigDecimal amount = applyMoney.multiply(BigDecimal.valueOf(100));
paramMap.put("amount", String.valueOf(amount.intValue()));
// 企业付款描述信息
paramMap.put("desc", "钱包提现");
// 根据微信签名规则,生成签名
paramMap.put("sign", WechatUtils.sign(paramMap, wechatConfig.getMchApiKey()));
log.info("请求参数:{}", paramMap);
// 把参数转换成XML数据格式
String xmlParam = XmlUtil.mapToXmlStr(paramMap, "xml");
String resXml = WechatUtils.httpsSSLPost(wechatConfig.getWithdrawalHost(), wechatConfig.getMchId(), xmlParam);
Map resultMap = XmlUtil.xmlToMap(resXml);
log.info("响应报文:{}", resultMap);
return resultMap;
}
签名
/**
* 微信支付签名sign
*
* @param param
* @param key
* @return {@link String }
* @Author chen 2023-07-27 14:37
*/
public static String sign(Map param, String key) {
try {
//签名步骤一:按字典排序参数
List
http 请求工具类--携带证书请求
证书路径为相对路径(放在 resources 目录下):youPath/cert.p12
/**
* https ssl post
*
* @param url url
* @param mchId
* @param params 参数个数
* @return {@link String }
* @throws Exception 异常
* @Author chen 2023-07-27 14:45
*/
public static String httpsSSLPost(String url, String mchId, String params) throws Exception {
String text = "";
// 指定读取证书格式为PKCS12
KeyStore keyStore = KeyStore.getInstance(CERT_TYPE);
// 读取本机存放的PKCS12证书文件
try (InputStream stream = ResourceUtil.getStream(CERT_P12_PATH)) {
// 指定PKCS12的密码(商户ID)
keyStore.load(stream, mchId.toCharArray());
}
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mchId.toCharArray()).build();
// Allow TLSv1 protocol only
// 指定TLS版本
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{TLS}, null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
// 设置httpclient的SSLSocketFactory
try (CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build()) {
HttpPost post = new HttpPost(url);
StringEntity s = new StringEntity(params, "utf-8");
s.setContentType(CONTENT_TYPE);
post.setEntity(s);
HttpResponse res = httpclient.execute(post);
HttpEntity entity = res.getEntity();
text = EntityUtils.toString(entity, "utf-8");
}
return text;
}