配色: 字号:
Java微信支付-微信红包
2016-12-19 | 阅:  转:  |  分享 
  
Java微信支付-微信红包



本篇文章介绍了Java微信支付-微信红包,可以实现微信公众号发红包功能,具有一定的参考价值,有需要的可以了解一下。



微信红包的使用已经很广泛,本篇文章介绍了微信发红包的实例,需要有认证的公众号,且开通了微信支付,商户平台且开通了现金红包的权限即可。



pay.weixin.qq商户登陆地址。选择查看营销中心的现金红包



pay.weixin.qq/wiki/doc/api/tools/cash_coupon.php?chapter=13_1现金红包的官网文档说明



先看几个图简单的测试。前提需要你去商户平台先充值。不支持预支付。本文只是总结微信现金红包接口的调用与实现。具体要根据自己的业务去实现如何调用该接口。



pay.weixin.qq/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3文档中普通红包有关于所有的讲解。调用必须有商户平台的证书。



需要的参数也都有列出。根据自己需求来决定。





1.java封装一个红包对象



/红包对象@author小帅帅丶@date2016-8-17上午11:12:19@开源中国http://my.oschina.net/xshuai/publicclassRedPackimplementsSerializable{privateStringsign;//根据属性生成的验证privateStringmch_billno;//订单号privateStringmch_id;//商户号privateStringwxappid;//微信appidprivateStringsend_name;//商户名称privateStringre_openid;//用户openidprivateStringtotal_amount;//付款金额privateStringtotal_num;//红包接收人数现金红包只能是1privateStringwishing;//红包祝福语privateStringclient_ip;//调用接口机器的IPprivateStringact_name;//活动名称privateStringremark;//备注privateStringnonce_str;//随机字符串//setget省略}



2.需要用的工具类createBillNo是生成商户订单号官网文档要求如下:



/红包工具类@author小帅帅丶@date2016-8-17上午11:12:19@开源中国http://my.oschina.net/xshuai/publicclassRedPackUtil{/生成商户订单号@parammch_id商户号@paramuserId该用户的userID@return/publicstaticStringcreateBillNo(){//组成:mch_id+yyyymmdd+10位一天内不能重复的数字//10位一天内不能重复的数字实现方法如下://因为每个用户绑定了userId,他们的userId不同,加上随机生成的(10-length(userId))可保证这10位数字不一样Datedt=newDate();SimpleDateFormatdf=newSimpleDateFormat("yyyymmdd");StringnowTime=df.format(dt);intlength=10;returnWXConstants.MCH_ID+nowTime+getRandomNum(length);}/生成特定位数的随机数字@paramlength@return/publicstaticStringgetRandomNum(intlength){Stringval="";Randomrandom=newRandom();for(inti=0;i


3.前面工作很简单需要的证书和商户号有。且商户平台有金额即可测试现金红包接口



RedPackpack=newRedPack(null//第一次为空,RedPackUtil.createBillNo()//商户订单号,"你自己的商户号","公众号的appid","名称","要发送用户的openid","发送金额单位是分例如100则是1元RMB","只能是1","9","127.0.0.1","活动名称","备注","随机字符串");



测试中除了sign为空。其他都可以填充。现在我们生成sign签名;根据pack对象中的参数去生成sign;



具体签名算法pay.weixin.qq/wiki/doc/api/tools/cash_coupon.php?chapter=4_3官网给出的地址



pay.weixin.qq/wiki/tools/signverify/可以在这个测试页面进行对比看加密后是否一致。



Stringsigns=Signature.www.hunanwang.netgetSign(pack);//生成的signset到pack对象中pack.setSign(signs);//将对象转为xml格式微信要求xml格式Stringxml=XmlUtil.objToXml(pack,RedPack.class,"xml");



4.发送红包



RedPackServiceservice=newRedPacService();Stringresult=service.redpackOrder(xml);//请求返回的数据是否成功



publicclassRedPackService{/红包接口地址/privatefinalstaticStringREDP_ORDER_PATH="api.mch.weixin.www.visa158.com/mmpaymkttransfers/sendredpack";/红包需要证书@paramparamXml@return/publicstaticStringredpackOrder(StringparamXml){try{WXBaseServiceservice=newWXBaseService(REDP_ORDER_PATH);returnservice.sendPost(paramXml);}catch(Exceptione){log.error(e.toString());}returnnull;}}/通过Https往APIpostxml数据@paramurlAPI地址@paramxmlObj要提交的XML数据对象@returnAPI回包的实际数据@throwsIOException@throwsKeyStoreException@throwsUnrecoverableKeyException@throwsNoSuchAlgorithmException@throwsKeyManagementException/publicStringsendPost(Stringurl,StringpostDataXML)throwsIOException,KeyStoreException,UnrecoverableKeyException,NoSuchAlgorithmException,KeyManagementException{if(!hasInit){init();}Stringresult=null;HttpPosthttpPost=newHttpPost(url);//解决XStream对出现双下划线的bug//XStreamxStreamForRequestPostData=newXStream(newDomDriver("UTF-8",newXmlFriendlyNameCoder("-_","_")));//将要提交给API的数据对象转换成XML格式数据Post给API//StringpostDataXML=xStreamForRequestPostData.toXML(xmlObj);Util.log("API,POST过去的数据是:");Util.log(postDataXML);//得指明使用UTF-8编码,否则到API服务器XML的中文不能被成功识别StringEntitypostEntity=newStringEntity(postDataXML,"UTF-8");httpPost.addHeader("Content-Type","text/xml");httpPost.setEntity(postEntity);//设置请求器的配置httpPost.setConfig(requestConfig);Util.log("executingrequest"+httpPost.getRequestLine());try{HttpResponseresponse=httpClient.execute(httpPost);HttpEntityentity=response.getEntity();result=EntityUtils.toString(entity,"UTF-8");}catch(ConnectionPoolTimeoutExceptione){log.e("httpgetthrowConnectionPoolTimeoutException(waittimeout)");}catch(ConnectTimeoutExceptione){log.e("httpgetthrowConnectTimeoutException");}catch(SocketTimeoutExceptione){log.e("httpgetthrowSocketTimeoutException");}catch(Exceptione){log.e("httpgetthrowException");}finally{httpPost.abort();}returnresult;}



5.返回的xml看是否成功由于只充值了1元前几天已经测试发送所以返回如下信息



100























献花(0)
+1
(本文系白狐一梦首藏)