公司是微服务架构 用docker 容器部署项目的配置文件都是做过配置的(这个配置比较复杂很多机器每次新增文件都得重新配置),于是想了个办法把微信支付证书扔云服务上去读取
/*** 代理ip地址 */@Value("${proxyHost:}")private String proxyHost;/*** 是否使用代理 开关*/@Value("${proxySwitch:}")private boolean proxySwitch;/*** 证书路径 如:.p1*/@Value("${certificateUrl:}")private String certificateUrl ;/*** 商户id */@Value("${hId:}")private String mchId;public static File downLoad(String path) {String[] temp = path.split("/");temp = temp[temp.length - 1].split("\.");File file = new File(temp[0] + System.currentTimeMillis() + "." + temp[1]);try {URL url = new URL(path);HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setConnectTimeout(3 * 1000);InputStream stream = InputStream();byte[] buffer = new byte[1024];int len;ByteArrayOutputStream bos = new ByteArrayOutputStream();while ((len = ad(buffer)) != -1) {bos.write(buffer, 0, len);}bos.close();byte[] data = ByteArray();FileOutputStream fos = new FileOutputStream(file);fos.write(data);fos.close();stream.close();} catch (Exception e) {("ImportExcel---downLoad()",e);}return file;}/*** 开始退款业务* @param xmlParam* @return* @throws Exception*/public String weixingPost(String url ,String xmlParam) throws Exception {log.info("============proxyHost : "+proxyHost+ "====proxySwitch:"+proxySwitch);String jsonStr= "";try {KeyStore clientStore = Instance("PKCS12");log.info("===========证书位置:"+certificateUrl );
// FileInputStream instream = new FileInputStream(certificateUrl);File file = downLoad(certificateUrl);FileInputStream instream = new FileInputStream(file);try {// 指定PKCS12的密码(商户ID)clientStore.load(instream, CharArray());} finally {instream.close();} SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(clientStore, CharArray()).build();// 指定TLS版本SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, DefaultHostnameVerifier());// 设置httpclient的SSLSocketFactoryCloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();try {HttpPost httpost = new HttpPost(url); // 设置响应头信息// 设置代理信息if (proxySwitch) {HttpHost proxy = new HttpHost(proxyHost,80 , "http");// 设置请求和传输超时时间RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).setProxy(proxy).build();httpost.setConfig(requestConfig);} // 参数httpost.setEntity(new StringEntity(xmlParam, "UTF-8"));log.info("===========xmlParam:"+xmlParam );CloseableHttpResponse response = ute(httpost);try {HttpEntity entity = Entity();jsonStr = Entity(), "UTF-8");sume(entity);log.info("=========jsonStr:"+jsonStr);} finally {response.close();}} finally {httpclient.close();}} catch (Exception e) {log.info("==================",e);throw new Exception(e);}return jsonStr;}
1、对接微信支付时用的是特约商户的子账号 ,退款的证书 要用父账号的
2、上述代码中有使用代理,不需要的朋友可以注释或设置proxySwitch = false
本文发布于:2024-01-30 14:47:50,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170659726920772.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |