微信退款接口

来源:互联网 发布:游戏美工设计师 编辑:程序博客网 时间:2024/04/28 18:42
private Object wxRefund(HttpServletRequest request, String id) throws Exception{
       /*-----  1.生成预支付订单需要的的package数据-----*/
    OrderMain orderMain = orderMainService.get(id);
    int price=(new Double(orderMain.getTotalamount()*100)).intValue();
       Result<Map<String, Object>> result = new Result<Map<String, Object>>();
       TreeMap paramMap = new TreeMap();
       paramMap.put("appid", PaymentConst.WECHAT_APPID);  
       paramMap.put("mch_id", PaymentConst.WECHAT_MCHID);  
       paramMap.put("nonce_str", Util.getRandomString(30, null));  
       paramMap.put("op_user_id", PaymentConst.WECHAT_MCHID);  
       paramMap.put("out_trade_no", orderMain.getNumber());  
       paramMap.put("out_refund_no", orderMain.getNumber());  
       paramMap.put("total_fee",price);  
       paramMap.put("refund_fee", price);  
       /*----2.根据package生成签名sign---- */
       paramMap.put("sign",Signature.createSign("UTF-8", paramMap));
       System.out.println(Signature.createSign("UTF-8", paramMap));
       /*----3.拼装需要提交到微信的数据xml---- */
       /* try {
            ----4.读取证书文件,这一段是直接从微信支付平台提供的demo中copy的,所以一般不需要修改---- 
            KeyStore keyStore  = KeyStore.getInstance("PKCS12");
            System.out.println("32158586586786875867754654643535465463");
            FileInputStream instream = new FileInputStream(new File(PaymentConst.SSLCERT_PATH));
            try {
            System.out.println("32158586586786875867754654643535460");
                keyStore.load(instream, PaymentConst.WECHAT_MCHID.toCharArray());
            }catch(Exception e){
            System.out.println("32158586586786875867754654643535465461");
               e.printStackTrace();
            } 
            finally {
                instream.close();
            }
            System.out.println("32158586586786875867754654643535465462");
            // Trust own CA and all self-signed certs
            SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, PaymentConst.WECHAT_MCHID.toCharArray()).build();
            // Allow TLSv1 protocol only
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,new String[] { "TLSv1" },null,
                    SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
            CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
            System.out.println("32158586586786875867754654643535465463");*/
            /*----5.发送数据到微信的退款接口---- */
            String url="https://api.mch.weixin.qq.com/secapi/pay/refund";
            String response = HttpClientUtil.executeBySslPost(url,new String( XMLUtils.simpleMapToXml(paramMap).getBytes(), "UTF-8"));
    System.out.println("预支付返回结果:"+response);
    Map<String, String> resultParams = getWechatXmlToMap(response);
           // logger.info(jsonStr);
    if (PaymentConst.WECHAT_STATUSCODE_FAIL.equals(resultParams.get("return_code"))){
                throw new MessageException(resultParams.get("return_msg"));
            }else{
            if (PaymentConst.WECHAT_STATUSCODE_FAIL.equals(resultParams.get("result_code"))){
            throw new MessageException(resultParams.get("return_msg"));
                }
            } 
    /* }catch (Exception e) {
   
           }*/


        return result;
   }
原创粉丝点击