java调用微信现金红包接口的心得与体会总结
这几天看了下之前写的有关微信支付的博客,看的人还是挺多的,看了下留言不知道是因为博客写的不够细还是什么情况,大多都找我要源码,我觉得吧程序员还是需要有这么一个思考的过程,因此没直接给源码,俗话说“授人以鱼不如授人以渔”。因此希望看文章的同时也花一点时间自己亲自敲一敲代码。好了废话不多说这次来分享微信现金红包接口的使用。 下面是微信开发文档对现金红包的介绍: 现金红包,是微信支付商户平台提供的营销工具之一,上线以来深受广大商户与用户的喜爱。商户可以通过本平台向微信支付用户发放现金红包。用户领取红包后,资金到达用户微信支付零钱账户,和零钱包的其他资金有一样的使用出口;若用户未领取,资金将会在24小时后退回商户的微信支付账户中。 产品意义 微信支付现金红包因资金的承载方式为现金,一直以来深受用户的青睐,近年来的春晚中,现金红包都扮演着重要的角色;在日常运营中也为商户的营销活动带来热烈的反响。总的来说,现金红包在包括但不仅限于以下场景中发挥着重要意义:
综上所述微信现金红包是一种营销工具,可以通过关注公众号、注册等给用户发放增加用户粘性。这次着重从程序开发方面分享我的心得体会 一 使用微信现金红包功能需具备的条件 1 拥有微信商户平台且秘钥证书齐全 2 商户平太需要有足够的余额可供使用(不够可以从商户平台使用财付通充值) 3 有微信支付开发基础更佳 二 开发的重点和难点 1 微信签名算法 2 httpclient以及证书的使用 3 微信文档的阅读(https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3) 如果有微信h5支付或扫码支付的童鞋看这一部分的文档可以说是小菜一碟,理解起来不费吹灰之力,同时只要掌握httpclient的知识就万事俱备了 三 直接撸代码 public static void sendRedPack(String mch_billno,String openId,String send_name,String total_fee,String total_num,String wishing,String act_name,String remark,String ip) throws Exception{ String non=PayCommonUtil.CreateNoncestr(); SortedMap<Object,Object> p = new TreeMap<Object,Object>(); p.put("nonce_str",non); p.put("mch_billno",mch_billno); p.put("mch_id",ConfigUtil.MCH_ID); p.put("wxappid",ConfigUtil.APPID); p.put("re_openid",openId); p.put("total_amount",total_fee); p.put("total_num","1"); p.put("client_ip","127.0.0.1"); p.put("act_name",act_name); p.put("send_name",send_name); p.put("wishing",wishing); p.put("remark",remark); String sign = PayCommonUtil.createSign("UTF-8",p); System.out.println(sign); p.put("sign",sign); String reuqestXml = PayCommonUtil.getRequestXml(p); KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(new File(ConfigUtil.CERT_PATH)); try { keyStore.load(instream,ConfigUtil.MCH_ID.toCharArray()); } finally { instream.close(); } SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore,ConfigUtil.MCH_ID.toCharArray()).build(); 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("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack");// 退款接口 httpPost.addHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); System.out.println("executing request" + httpPost.getRequestLine()); //请求的xml需转码为iso8859-1编码,否则易出现签名错误或红包上的文字显示有误 StringEntity reqEntity = new StringEntity(new String(reuqestXml.getBytes(),"ISO8859-1")); // 设置类型 httpPost.setEntity(reqEntity); CloseableHttpResponse response = httpclient.execute(httpPost); try { HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + 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(); } } 需要注意的地方是下面这里: StringEntity reqEntity = new StringEntity(new String(reuqestXml.getBytes(),"ISO8859-1")); 这个地方可以说把我弄得差点崩溃了各种试,各种调试还是抱着试一试的心态加上去就OK了,这个可能是因为httpclient和原生的HttpsConnection在数据传输上的不同吧。这里没做过多的研究。 调用这个方法就更简单了直接像下面这样 public static void main(String args[]){ try { sendRedPack("12828839012016101420","接收者的openid","xxx","100","1","恭喜发财,年年有余","新年红包","新年红包还不快抢","127.0.0.1"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } 红包发送后打印的信息如下: TTP/1.1 200 OK Response content length: 567 <xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[发放成功]]></return_msg> <result_code><![CDATA[SUCCESS]]></result_code> <err_code><![CDATA[SUCCESS]]></err_code> <err_code_des><![CDATA[发放成功]]></err_code_des> <mch_billno><![CDATA[12828839012016101421]]></mch_billno> <mch_id><![CDATA[1282883901]]></mch_id> <wxappid><![CDATA[xxxxx]]></wxappid> <re_openid><![CDATA[xxxx]]></re_openid> <total_amount>100</total_amount> <send_listid><![CDATA[1000041701201610143000090813093]]></send_listid> </xml> 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。 (编辑:甘南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |