微信支付之企业付款(提现,理赔,退款)(java)

/**
	 * 提现操作
	 * @throws Exception 
	 */
	@RequestMapping("doCash")
	public String doTravelScoreCash(HttpServletRequest request) throws Exception {

         //微信企业付款 接口url     https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers

		String company_pay_url = ConstantUtil.COMPANY_PAY_URL;

		String openid = (String) this.getSession().getAttribute("customerOpenId");//用户
        String mch_appid = ConstantUtil.APPID;
        String mch_id = ConstantUtil.MCH_ID;
        String device_info = ConstantUtil.DEVICE_INFO;
        String nonce_str = WXPayUtil.generateNonceStr();
        String partner_trade_no = DateUtils.fmtTime14Date(new Date());//商户订单号
        String check_name = "NO_CHECK";//校验用户姓名选项 //	NO_CHECK:不校验真实姓名        FORCE_CHECK:强校验真实姓名
        String re_user_name = "王小王";//收款用户真实姓名 //如果check_name设置为FORCE_CHECK,则必填用户真实姓名
        String amount = "1";//企业付款总金额数(分)
        String desc = "旅游基金";//企业付款描述信息
        String spbill_create_ip = request.getRemoteAddr();//调用接口的终端ip
		
        Map<String, String> data = new HashMap<String, String>();
        data.put("mch_appid", mch_appid); //商户号
        data.put("mchid",mch_id);//注意这里没下划线   ,我都不想说什么了,微信支付的时候这里是带下划线的,
        data.put("device_info",device_info);
        data.put("nonce_str",nonce_str); 
        data.put("partner_trade_no",partner_trade_no); 
        data.put("openid",openid);
        data.put("check_name",check_name);
        data.put("re_user_name",re_user_name);
        data.put("amount",amount);
        data.put("desc",desc);
        data.put("spbill_create_ip",spbill_create_ip);
        String sign = WXPayUtil.generateSignature(data,ConstantUtil.APPKEY);
        data.put("sign",sign);
        
        //***********************企业付款请求*********************************
        KeyStore keyStore  = KeyStore.getInstance("PKCS12");
        String certPath = "C://apiclient_cert.p12";
        FileInputStream instream = new FileInputStream(new File(certPath));
        try {
            keyStore.load(instream,mch_id.toCharArray());
        } finally {
            instream.close();
        }
        // Trust own CA and all self-signed certs
        SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mch_id.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();
        try {


        	HttpPost httpPost = new HttpPost(company_pay_url);
            httpPost.addHeader("Connection", "keep-alive");
            httpPost.addHeader("Accept", "*/*");
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            httpPost.addHeader("Host", "api.mch.weixin.qq.com");
            httpPost.addHeader("X-Requested-With", "XMLHttpRequest");
            httpPost.addHeader("Cache-Control", "max-age=0");
            httpPost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
            String dataXML = WXPayUtil.mapToXml(data);
            httpPost.setEntity(new StringEntity(dataXML, "UTF-8"));
            
            System.out.println("执行请求" + httpPost.getRequestLine());


            CloseableHttpResponse response = httpclient.execute(httpPost);
            try {
                HttpEntity entity = response.getEntity();


                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());//相应状态码
                if (entity != null) {
                    System.out.println("响应内容长度: " + entity.getContentLength());
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent(),"UTF-8"));
                    String text;
                    while ((text = bufferedReader.readLine()) != null) {
                        System.out.println(text);
                    }
                   
                }
                EntityUtils.consume(entity);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
        
		
        return "redirect:"+Global.getAdminPath()+"/home/index.do";//进入提现进度页面
	}

猜你喜欢

转载自blog.csdn.net/u014596302/article/details/79485601