java 微信退款 读取远端云服务存储安全证书

背景

公司是微服务架构 用docker 容器部署项目的配置文件都是做过配置的(这个配置比较复杂很多机器每次新增文件都得重新配置),于是想了个办法把微信支付证书扔云服务上去读取

实现


	/**
	 * 代理ip地址   10.0.xx.xx
	 */
	@Value("${proxyHost:}")
	private String proxyHost;

	/**
	 * 是否使用代理 开关
	 */
	@Value("${proxySwitch:}")
	private boolean proxySwitch;
	
	/**
	 * 证书路径  如:https://xxxxx.com.cn/apiclient_cert.p1
	 */
	@Value("${certificateUrl:}")
	private String certificateUrl ;

	/**
	 * 商户id 
	 */
	@Value("${WxPayConfig.mchId:}")
	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 = connection.getInputStream();
            byte[] buffer = new byte[1024];
            int len;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            while ((len = stream.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
            bos.close();
            byte[] data = bos.toByteArray();
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(data);
            fos.close();
            stream.close();
        } catch (Exception e) {
        	log.error("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 = KeyStore.getInstance("PKCS12");
           log.info("===========证书位置:"+certificateUrl  );
//            FileInputStream instream = new FileInputStream(certificateUrl);
            File file =  downLoad(certificateUrl);
            FileInputStream instream = new FileInputStream(file);
            try {
                // 指定PKCS12的密码(商户ID)
                clientStore.load(instream, mchId.toCharArray());
            } finally {
                instream.close();
            } 
            SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(clientStore, mchId.toCharArray()).build();
            // 指定TLS版本
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, null,
                    SSLConnectionSocketFactory.getDefaultHostnameVerifier());
            // 设置httpclient的SSLSocketFactory
            CloseableHttpClient 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 = httpclient.execute(httpost);
                try {
                    HttpEntity entity = response.getEntity();
                      jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
                    EntityUtils.consume(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 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java微信退款V3是指使用Java语言开发的微信支付接口版本V3中的退款功能。微信支付是一种在线支付平台,用户可以通过微信支付完成各种消费,包括购买商品、预定服务等。在某些情况下,用户可能需要退款,例如购买商品后发现有质量问题或者服务未提供等原因。 Java微信退款V3提供了一种方便快捷的方式来实现退款操作。具体流程如下: 1. 创建退款请求:通过Java代码构建一个退款请求对象,包括订单号、退款金额、退款原因等信息。 2. 生成签名:使用微信支付提供的签名算法对退款请求进行签名,确保数据的安全性。 3. 发送退款请求:使用Java的Http请求库向微信支付接口发送退款请求,并将签名、订单信息等参数传递给接口。 4. 处理应答:接收微信接口返回的响应结果,包括退款是否成功、返回的错误信息等。 5. 处理结果:根据接口返回的结果进行相应的处理,如果退款成功,则更新订单状态并通知用户退款完成;如果退款失败,则根据返回的错误信息进行处理。 Java微信退款V3具有以下特点: 1. 安全可靠:使用微信支付提供的签名算法对退款请求进行签名,确保数据传输的安全性。 2. 简单易用:通过Java代码构建退款请求对象,方便快捷地实现退款操作。 3. 实时性好:通过Http请求将退款请求发送给微信支付接口,实时地获取退款结果。 4. 丰富的功能:除了基本的退款功能外,Java微信退款V3还支持退款查询、退款通知等附加功能。 总之,Java微信退款V3是一种方便快捷、安全可靠的退款解决方案,可以方便地实现退款操作,并提供了丰富的功能以满足不同业务需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值