微信提现(商户向商家转账)

这个相对于支付要简单一些吧,个人理解,没有支付那么多操作,依旧是ssm框架+maven+tomcat8代码如下:

    /**
	 * 提现
	 * @param openid 小程序需要的用户openid
	 * @param money 依旧要求是整数而且提现微信硬性要求是金额必须大于5元的
	 * @throws ParseException
	 */
	@RequestMapping(value="pay/deposit",produces = "application/json; charset=utf-8")
	@ResponseBody
	 public String deposit(String openid,double money,HttpServletRequest req) throws ParseException{
		JSONObject json = new JSONObject();
		double price=money*100;
		int a=(int)price;
        String ip = new GetIpAddress().getIP(req);
    	
    	String times = System.currentTimeMillis() + "";
        //是否校验用户姓名 NO_CHECK:不校验真实姓名  FORCE_CHECK:强校验真实姓名
        String checkName ="NO_CHECK";
        //等待确认转账金额,ip,openid的来源
        //描述
        String desc = money+"$";
        SortedMap parameters = new TreeMap();
        // 参数:开始生成第一次签名
        parameters.put("mch_appid", 小程序的qppid);
        parameters.put("mchid", 商家号);
        parameters.put("partner_trade_no", 随便什么字符串就可以小于32位自己任意写一个生成规则);
        parameters.put("nonce_str", times);
        parameters.put("openid", openid);
        parameters.put("check_name", "NO_CHECK");
        parameters.put("amount", a);
        parameters.put("spbill_create_ip", ip);
        parameters.put("desc", desc);
        String sign = PayCommonUtil.createSign("UTF-8", parameters);
        parameters.put("sign", sign);
        String xmlInfo = PayCommonUtil.getRequestXml(parameters,"UTF-8");
        try {
            CloseableHttpResponse response =  HttpUtil.Post("https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers", xmlInfo, true);
            String transfersXml = EntityUtils.toString(response.getEntity(), "utf-8");
            Map transferMap = XMLUtil.doXMLParse(transfersXml);
            if (transferMap.size()>0) {
                if (transferMap.get("result_code").equals("SUCCESS") && transferMap.get("return_code").equals("SUCCESS")) {
            		//这里边就是需要成功之后的业务逻辑代码
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.put("mes", "企业支付异常!"+e.getMessage());
            json.put("code", "403");
        }
        return json.toString();
    }

*一定要注意这些参数名,我当初做的时候不知道是怎么弄的参数名错了,个人又是比较粗心的找了好久的错,后来还是别人帮忙找出来的,写提现的接口的时候遇到两个错吧,这是第一个第二个待续。

帮助类:

public static String createSign(String characterEncoding,SortedMap parameters){
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序(升序)
        Iterator it = es.iterator();
        while(it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next();
            String k = (String)entry.getKey();
            Object v = entry.getValue();
            if(null != v && !"".equals(v) 
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        sb.append("key=" + appkey开发平台是有的);
        String sign = MD5.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
        return sign;
    }

public static CloseableHttpResponse Post(String url, String outputEntity, boolean isLoadCert) throws Exception {
         HttpPost httpPost = new HttpPost(url);
         // 得指明使用UTF-8编码,否则到API服务器XML的中文不能被成功识别
         httpPost.addHeader("Content-Type", "text/xml");
         httpPost.setEntity(new StringEntity(outputEntity, "UTF-8"));
         if (isLoadCert) {
             // 加载含有证书的http请求
             return HttpClients.custom().setSSLSocketFactory(CertUtil.initCert()).build().execute(httpPost);
         } else {
             return HttpClients.custom().build().execute(httpPost);
         }
     }

*这就是第二个错的存在就是那个appkey我写成商户号了,这也是没认真看文档的原因吧,文字型东西太枯燥了,文档还是很重要的,下次注意吧

编辑成xml

public class XMLUtil {
    public static Map doXMLParse(String strxml) throws Exception {  
           strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");  
 
           if(null == strxml || "".equals(strxml)) {  
               return null;  
           }  
 
           Map m = new HashMap();  
 
           InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));  
           SAXBuilder builder = new SAXBuilder();  
           Document doc = builder.build(in);  
           Element root = doc.getRootElement();  
           List list = root.getChildren();  
           Iterator it = list.iterator();  
           while(it.hasNext()) {  
               Element e = (Element) it.next();  
               String k = e.getName();  
               String v = "";  
               List children = e.getChildren();  
               if(children.isEmpty()) {  
                   v = e.getTextNormalize();  
               } else {  
                   v = XMLUtil.getChildrenText(children);  
               }  
 
               m.put(k, v);  
           }  
 
           //关闭流  
           in.close();  
 
           return m;  
       }  
 
       /** 
        * 获取子结点的xml 
        * @param children 
        * @return String 
        */  
       public static String getChildrenText(List children) {  
           StringBuffer sb = new StringBuffer();  
           if(!children.isEmpty()) {  
               Iterator it = children.iterator();  
               while(it.hasNext()) {  
                   Element e = (Element) it.next();  
                   String name = e.getName();  
                   String value = e.getTextNormalize();  
                   List list = e.getChildren();  
                   sb.append("<" + name + ">");  
                   if(!list.isEmpty()) {  
                       sb.append(XMLUtil.getChildrenText(list));  
                   }  
                   sb.append(value);  
                   sb.append("");  
               }  
           }  
 
           return sb.toString();  
       }  
       
}

到此就结束了,反正第一次接触就是各种问题,还是自己能力不够的原因吧,虽然弄出来了可能还是有些理解的不对,但是我在网上没找到完整的demo也是很难受的,就想着自己做好了分享出来,也方便自己下次看吧

你可能感兴趣的:(java)