微信企业支付接口:。
本文说下如果联通小程序和公众号来实现发红包。
首先你需要一个公众号和小程序。在小程序中发送必须先关注公众号。这样你就可以使用公众号的红包接口给用户发送红包。
原理:
附上参数对象代码:
//创建参数MappublicstaticSortedMapObject,ObjectgetWithdrawParam(Stringopenid,intvalue){SortedMapObject,Objectparameters=newTreeMapObject,Object();parameters.put("mch_appid",PropertyUtil.getInstance().getProperty("wx.appid"));//公众号appidparameters.put("mchid",PropertyUtil.getInstance().getProperty("wx.mchid"));//绑定的商户号parameters.put("nonce_str",StringUtil.getRandomString(16));随机字符串16位parameters.put("partner_trade_no",RandomStringUtils.randomAlphanumeric(20));//商户订单号parameters.put("openid",openid);//用户open_idparameters.put("check_name","NO_CHECK");//是否对用户实名认证,NO_CHECK表示否。parameters.put("amount",value);//金额parameters.put("desc","红包");//描述信息parameters.put("spbill_create_ip",PropertyUtil.getInstance().getProperty("wx.requestUrl"));//Ip地址,这个IP必须在公众号的白名单中,否则会失败!!!returnparameters;}//创建sign,参数为UTF-8编码和上面的参数Map
publicstaticStringcreateSign(StringcharacterEncoding,SortedMapString,Objectparameters){StringBuffersb=newStringBuffer();Setes=parameters.entrySet();Iteratorit=es.iterator();while(it.hasNext()){Map.Entryentry=(Map.Entry)it.next();Stringk=(String)entry.getKey();Objectv=entry.getValue();if(null!=v&&!"".equals(v)&&!"sign".equals(k)&&!"key".equals(k)){sb.append(k+"="+v+"&");}}sb.append("key="+PropertyUtil.getInstance().getProperty("wx.key"));Stringsign=Md5Util.md5Encode(sb.toString(),characterEncoding).toUpperCase();returnsign;}将上面得到的sign放到Map中,param.put("sign",sign);
将Map转换为XML
publicstaticStringgetRequestXml(SortedMapString,Objectparam){StringBuffersb=newStringBuffer();sb.append("xml");Setes=parameters.entrySet();Iteratorit=es.iterator();while(it.hasNext()){Map.Entryentry=(Map.Entry)it.next();Stringk=""+entry.getKey();Stringv=""+entry.getValue();if("attach".equalsIgnoreCase(k)||"body".equalsIgnoreCase(k)){sb.append(""+k+""+"![CDATA["+v+"]]/"+k+"");}else{sb.append(""+k+""+v+"/"+k+"");}}sb.append("/xml");returnsb.toString();}发送post请求到https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack,参数为上面的XML即可。
要实现上面的红包功能,最好还有个定时任务,可以把公众号的用户列表和用户数据实时新增到数据库中,以便根据唯一的union_id查找对应的两个open_id.
附上同步代码
获取access_token,最好和公众号共享一个,因为access_token每天限定获取2000次,每次过期时间为2小时,发生变动时的5分钟内新旧access_token都可以使用。
publicStringgetAccessToken(){StringtokenUrl=PropertyUtil.getInstance().getProperty("wx.tokenURL")+"?appid="+PropertyUtil.getInstance().getProperty("red.wx.appid")+"&secret="+PropertyUtil.getInstance().getProperty("red.wx.appSecret")+"&grant_type=client_credential";JSONObjecttokenResult=JSONObject.parseObject(HttpUtils.get(tokenUrl));if(tokenResult.getString("errmsg")!=null){log.info("获取AccessToken失败,请检查失败原因,{}",tokenResult.getString("errmsg"));returnnull;}log.info("tokenResult="+tokenResult.toString());Stringaccess_token=tokenResult.getString("access_token");stringRedisTemplate.opsForValue().set(redisKey,access_token,10060,TimeUnit.SECONDS);returnaccess_token;}有了access_token就可以获取用户列表了,限定一次最多获取10000个,可以根据next_openid设置起始位置。
地址:https://api.weixin.qq.com/cgi-bin/user/get,参数access_token和next_openid
StringuserListUrl=PropertyUtil.getInstance().getProperty("wx.userListURL")+"?access_token="+access_token+"&next_openid="+next_openid;JSONObjectuserListResult=JSONObject.parseObject(HttpUtils.get(userListUrl))得到open_id集合就可以继续获取用户详细信息,可以单独也可以批量,批量最多100个一次。
批量获取地址:https://api.weixin.qq.com/cgi-bin/user/info/batchget,参数user_list(100个用户的openid)和access_token代码就不加了,基本操作。
在分享个按指定大小,分隔集合,将集合按规定个数分为n个部分的方法,用来分隔10000个open_id为100的list,方便批量获取publicstaticListListStringsplitList(ListStringlist,intlen){if(list==null||list.size()==0||len1){returnnull;}ListListStringresult=newArrayListListString();intsize=list.size();intcount=(size+len-1)/len;for(inti=0;icount;i++){ListStringsubList=list.subList(ilen,((i+1)lensize?size:len(i+1)));result.add(subList);}returnresult;}好了,具体就这么多了,详细的可以根据开发文档和本文参考对照完成。













