ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

2021-7-10 微信退款java实现

2021-07-10 15:58:19  阅读:169  来源: 互联网

标签:refund 10 return String map 微信 put java out


接着上一篇微信H5支付后来。

 @ResponseBody
    @GetMapping("refund")
    public String Refund(@RequestParam("orderId")String orderId,@RequestParam("refundId")String refundId) throws Exception {
            Integer total_fee = 1;   //付款金额
            Integer refund_fee = 1;  //退款的金额
            SortedMap<String,String> map = new TreeMap<>();
            String appid = weChatConfig.appid;
            String mch_id = weChatConfig.merId;
            String nonce_str =CommonUtils.generateUUID();
            String out_refund_no = refundId;//UtilUuId.get18UUID();
            map.put("appid", appid);
            map.put("mch_id", mch_id);
            map.put("nonce_str", nonce_str);
            map.put("sign_type", "MD5");
          //  map.put("transaction_id", ""); //微信支付单号 和 商家订单号 2选一
            map.put("out_trade_no",orderId );
            map.put("out_refund_no",out_refund_no);
            map.put("total_fee", total_fee.toString());
            map.put("refund_fee", refund_fee.toString());
            String sign = WXPayUtil.createSign(map, weChatConfig.key);
            map.put("sign",sign);

            String xml = WXPayUtil.mapToXml(map);
            log.info("payXml: "+xml);
            String createOrderURL = "https://api.mch.weixin.qq.com/secapi/pay/refund";//微信统一下单接口
            try {
                //预下单 获取接口地址
                Map map1 = weChatPayService.doRefund(mch_id,createOrderURL, xml);//注意了退款.p12证书在这里面配置,官方文档没有怎么详细说明,所以很多同学很头大。
                String return_code = (String) map1.get("return_code");
                String return_msg = (String) map1.get("return_msg");
                System.out.println(return_msg);
                if ("SUCCESS".equals(return_code) && "OK".equals(return_msg)) {
                    return "ok";//JsonResult.ok("success").put("out_refund_no", out_refund_no);
                } else {
                    return "---------统一退单接口出错";//JsonResult.error("统一退单接口出错");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return "统一退单接口出错";//JsonResult.error("统一退单接口出错");
            }

    }
    /**
     *
     * @param mchId 商户ID
     * @param url 请求URL
     * @param data 退款参数
     * @return
     * @throws Exception
     */
    public  Map doRefund(String mchId, String url, String data) throws Exception {
        /**
         * 注意PKCS12证书 是从微信商户平台-》账户设置-》 API安全 中下载的
         */
        KeyStore keyStore = KeyStore.getInstance("PKCS12");//证书格式
        //这里自行实现我是使用数据库配置将证书上传到了服务器可以使用 FileInputStream读取本地文件
        FileInputStream fis=new FileInputStream("E:\\WXCertUtil\\cert\\apiclient_cert.p12");
        try {
            //这里写密码..默认是你的MCHID
            keyStore.load(fis, mchId.toCharArray());
        } finally {
            fis.close();
        }
        SSLContext sslcontext = SSLContexts.custom()
                .loadKeyMaterial(keyStore, mchId.toCharArray())
                .build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                sslcontext,
                SSLConnectionSocketFactory.getDefaultHostnameVerifier());
        CloseableHttpClient httpclient = HttpClients.custom()
                .setSSLSocketFactory(sslsf)
                .build();
        try {
            HttpPost httpost = new HttpPost(url);
            httpost.setEntity(new StringEntity(data, "UTF-8"));
            CloseableHttpResponse response = httpclient.execute(httpost);
            System.out.println("---------------------------------------------------");
            try {
                HttpEntity entity = response.getEntity();
                //接受到返回信息
                String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
                EntityUtils.consume(entity);
                System.out.println(jsonStr);
                Map<String, String> map =  WXPayUtil.xmlToMap(jsonStr);
                return map;
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }

标签:refund,10,return,String,map,微信,put,java,out
来源: https://blog.csdn.net/qq_42729674/article/details/118635807

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有