Java 微信商家打款到零钱(旧版本接口)
旧版微信支付接口要求请求时携带证书请求
构建请求参数
/*** 付款到零钱** @param withdrawalDto dto撤军* @return {@link Map }<{@link String }, {@link Object }>* @throws Exception 异常* @Author chen 2023-07-27 15:04*/private Map<String, Object> payToUser(WithdrawalDto withdrawalDto) throws Exception {log.info("发起微信提现零钱请求");Map<String, String> paramMap = new HashMap<>();// 公众账号appidparamMap.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下,某用户的openidparamMap.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<String, Object> 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<String, String> param, String key) {try {//签名步骤一:按字典排序参数List<Object> list = new ArrayList<>(param.keySet());Object[] ary = list.toArray();Arrays.sort(ary);list = Arrays.asList(ary);StringBuilder str = new StringBuilder();for (Object o : list) {str.append(o).append("=").append(param.get(String.valueOf(o))).append("&");}//签名步骤二:加上keystr.append("key=").append(key);//步骤三:加密并大写return DigestUtil.md5Hex(str.toString(), "utf-8").toUpperCase();} catch (Exception e) {log.error("微信签名失败:{}", e.getMessage());return null;}}
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 = "";// 指定读取证书格式为PKCS12KeyStore 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 certsSSLContext 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的SSLSocketFactorytry (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;}