java微信退款以及微信支付的发送请求util


    /**
     * 微信退款
     * @param orderNo 订单编号
     * @param refundAmount 退款金额
     * @return 微信返回结果 return_code 为 ‘SUCCESS’ 表示成功
     * @throws Exception
     */
    public Map<String,String> wxRefund(String orderNo,double refundAmount) throws Exception{
        Date now = new Date();
        String outRefundNo = orderNo+"-"+now.getTime();
        String resundUrl = "https://api.mch.weixin.qq.com/secapi/pay/refund";
        Map<String,String> paramMap = new HashMap<>(8);
        String totalFee = WXPayUtil.getMoney(refundAmount+"");
        String refundFee = WXPayUtil.getMoney(refundAmount+"");
        paramMap.put("appid",WXConstans.APPID);
        paramMap.put("mch_id",WXConstans.MCHID);
        paramMap.put("nonce_str",WXPayUtil.generateNonceStr());
        paramMap.put("out_trade_no",orderNo);
        paramMap.put("out_refund_no",outRefundNo);
        paramMap.put("total_fee",totalFee);
        paramMap.put("refund_fee",refundFee);
        paramMap.put("sign_type","MD5");
//        paramMap.put("notify_url", "退款回调url,必须是外网能访问的,可以有,也可以没有");
        String signStr = WXPayUtil.generateSignature(paramMap, WXConstans.PATERNER_KEY);
        paramMap.put("sign",signStr);
        String paramXml = WXPayUtil.mapToXml(paramMap);
        String perpayXml = WXPayUtil.sendPost(resundUrl, paramXml,true);
        System.out.println(perpayXml);
        Map<String, String> map = WXPayUtil.xmlToMap(perpayXml);
        return map;
    }
  /**
     * 发送post请求
     * @param url  post请求链接
     * @param data post请求数据
     * @param useCert  是否需要证书
     * @return 调用post请求后的结果(字符串)。
     */
    public static String sendPost(String url, String data, boolean useCert) throws Exception {
        BasicHttpClientConnectionManager connectionManager;
        if (useCert) {
            // 证书
            char[] password = WXConstans.MCHID.toCharArray();
            String path = "微信证书服务器地址,.p12文件";
            InputStream certStream = new FileInputStream(new File(path));
            KeyStore ks = KeyStore.getInstance("PKCS12");
            ks.load(certStream, password);

            // 实例化密钥库 & 初始化密钥工厂
            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(ks, password);

            // 创建 SSLContext
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(kmf.getKeyManagers(), null, new SecureRandom());

            SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
                    sslContext,
                    new String[]{"TLSv1"},
                    null,
                    new DefaultHostnameVerifier());

            connectionManager = new BasicHttpClientConnectionManager(
                    RegistryBuilder.<ConnectionSocketFactory>create()
                            .register("http", PlainConnectionSocketFactory.getSocketFactory())
                            .register("https", sslConnectionSocketFactory)
                            .build(),
                    null,
                    null,
                    null
            );
        } else {
            connectionManager = new BasicHttpClientConnectionManager(
                    RegistryBuilder.<ConnectionSocketFactory>create()
                            .register("http", PlainConnectionSocketFactory.getSocketFactory())
                            .register("https", SSLConnectionSocketFactory.getSocketFactory())
                            .build(),
                    null,
                    null,
                    null
            );
        }


        HttpClient httpClient = HttpClientBuilder.create()
                .setConnectionManager(connectionManager)
                .build();
        HttpPost httpPost = new HttpPost(url);
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(8000).setConnectTimeout(6000).build();
        httpPost.setConfig(requestConfig);

        StringEntity postEntity = new StringEntity(data, "UTF-8");
        httpPost.addHeader("Content-Type", "text/xml");
        httpPost.addHeader("User-Agent", WXPayConstants.USER_AGENT);
        httpPost.setEntity(postEntity);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        return EntityUtils.toString(httpEntity, "UTF-8");

    }

PS:send post  微信支付也能用这个 微信支付的文章因为写的人很多我就不放出来了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值