当前位置: 移动技术网 > IT编程>开发语言>Java > java调用微信现金红包接口的心得与体会总结

java调用微信现金红包接口的心得与体会总结

2019年07月22日  | 移动技术网IT编程  | 我要评论

dr.g,光泽县教育局,海门市爱美阁美容中心

这几天看了下之前写的有关微信支付的博客,看的人还是挺多的,看了下留言不知道是因为博客写的不够细还是什么情况,大多都找我要源码,我觉得吧程序员还是需要有这么一个思考的过程,因此没直接给源码,俗话说“授人以鱼不如授人以渔”。因此希望看文章的同时也花一点时间自己亲自敲一敲代码。好了废话不多说这次来分享微信现金红包接口的使用。

下面是微信开发文档对现金红包的介绍:

现金红包,是微信支付商户平台提供的营销工具之一,上线以来深受广大商户与用户的喜爱。商户可以通过本平台向微信支付用户发放现金红包。用户领取红包后,资金到达用户微信支付零钱账户,和零钱包的其他资金有一样的使用出口;若用户未领取,资金将会在24小时后退回商户的微信支付账户中。

产品意义

微信支付现金红包因资金的承载方式为现金,一直以来深受用户的青睐,近年来的春晚中,现金红包都扮演着重要的角色;在日常运营中也为商户的营销活动带来热烈的反响。总的来说,现金红包在包括但不仅限于以下场景中发挥着重要意义:

  • ◆ 为企业拉取新用户、巩固老用户关系、提升用户活跃度
  • ◆ 结合巧妙的创意点子,辅以红包点缀,打造火爆的活动,提升企业与品牌知名度
  • ◆ 结合企业运营活动,以红包作为奖品,使你的抽奖、满送等营销活动更便利进行
  • ◆ 同时,除了营销之外,现金红包在企业日常的运营中也扮演着重要角色。如:为员工返福利、为供应商返利、会员积分/虚拟等级兑现等等

综上所述微信现金红包是一种营销工具,可以通过关注公众号、注册等给用户发放增加用户粘性。这次着重从程序开发方面分享我的心得体会

一  使用微信现金红包功能需具备的条件

1 拥有微信商户平台且秘钥证书齐全

2 商户平太需要有足够的余额可供使用(不够可以从商户平台使用财付通充值)

3 有微信支付开发基础更佳

二 开发的重点和难点

1 微信签名算法

2 httpclient以及证书的使用

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(); 
    } 
  } 

需要注意的地方是下面这里:
//请求的xml需转码为iso8859-1编码,否则易出现签名错误或红包上的文字显示有误

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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网