A summary of the experiences and experience of Java calling micro-letter cash Red envelopes _java

Source: Internet
Author: User
Tags cdata openid

These days to read the next blog about micro-letter payment, look at a lot of people, read the message do not know because the blog is not thin enough or what the situation, most of them I want to source, I think the programmer or need to have such a process of thinking, so not directly to the source code, as the saying goes, "teach people to fish than to teach people to fishing." So want to read the article at the same time also take a moment to personally knock the code. Okay, no more nonsense. This time to share the use of micro-letter cash Red Envelope interface.

The following is a brief introduction to cash red envelopes in the micro-credit development document:

Cash red envelopes, is a micro-letter payment merchant platform to provide one of the marketing tools, online since the vast number of businesses and users love. Through this platform, the merchant can pay the customers cash red envelopes to the micro-letter. After the user receives the red envelope, the money arrives the user micro-letter to pay the change account, and 0 Wallet's other funds have the same use exportation, if the user does not receive, the fund will return to the merchant's micro-credit payment account in 24 hours.

Product meaning

The micro-letter payment Cash red envelopes because of the capital of the bearer of cash, has been favored by users, in recent years, the Spring Festival evening, cash red envelopes play an important role in the day-to-day operation of the merchant's marketing campaign to bring a warm response. In general, cash red envelopes play an important role in including but not limited to the following scenarios:

    • For enterprises to draw new users, consolidate the old user relationship, enhance the user's active degree
    • Combined with ingenious creative ideas, supplemented by red envelopes, to create hot activities, enhance corporate and brand awareness
    • Combined with enterprise operation activities, with red envelopes as prizes, so that your lottery, full delivery and other marketing activities more convenient to carry out
    • At the same time, in addition to marketing, cash red envelopes in the day-to-day operation of the enterprise also plays an important role. Such as: For employees to return benefits, for the supplier rebate, membership points/virtual level cash and so on

To sum up the micro-letter cash Red envelope is a marketing tool, you can pay attention to the public number, registration and other users to increase user stickiness. This time, I'm sharing my experience with the program development.

A requirement for the function of using the micro-letter cash Red Envelope

1 has the micro-letter merchant platform and the secret key certificate is complete

2 merchant flat too need to have sufficient balance to use (not enough to use tenpay recharge from merchant platform)

3 have micro-credit payment development Foundation better

The emphasis and difficulty of the second development

1 micro-letter Signature Algorithm

2 HttpClient and the use of certificates

3 reading of micro-letter documents (HTTPS://PAY.WEIXIN.QQ.COM/WIKI/DOC/API/TOOLS/CASH_COUPON.PHP?CHAPTER=13_4&INDEX=3)

If there is a micro-letter H5 pay or scan the size of the children's shoes to see this part of the document can be said to be a piece of cake, understand it effortlessly, and as long as the knowledge of httpclient is ready

Three direct masturbate Code

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");/refund Interface Httppost.addheader ("Content-type", "application/x-www-form-urlencoded; 
       
      Charset=utf-8 "); 
      SYSTEM.OUT.PRINTLN ("Executing request" + httppost.getrequestline ()); 
   The requested XML needs transcoding for ISO8859-1 encoding, otherwise it is easy to sign the error or the text on the red envelope is wrong   Stringentity reqentity = new Stringentity (New String (Reuqestxml.getbytes (), "iso8859-1")); 
      Set type 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 (); 
 } 
  }

The place to note is here:
The requested XML needs transcoding for ISO8859-1 encoding, otherwise it is easy to sign the error or the text on the red envelope is wrong

Stringentity reqentity = new Stringentity (New String (Reuqestxml.getbytes (), "iso8859-1"));

This place can be said to have almost crashed me. All kinds of test, all kinds of debugging or holding a try the mentality plus go on OK, this may be because httpclient and native httpsconnection in the data transmission of different bar. There's not much research done here.

It's much simpler to call this method directly as follows

public static void Main (String args[]) { 
    try { 
       
      sendredpack ("12828839012016101420", "Receiver's OpenID", "xxx", "100", " 1 "," Congratulations to the rich, more than every year, "" New Year Red Envelopes "," New Year's red envelopes not to rob "," 127.0.0.1 "); 
    } catch (Exception e) { 
      //TODO auto-generated catch block 
      e.printstacktrace (); 
    } 
} 

The message is printed after the red envelopes are sent:

 ttp/1.1 OK Response content length:567 <xml> <return_code><![ Cdata[success]]></return_code> <return_msg><! [cdata[issued successfully]]></return_msg> <result_code><! [cdata[success]]></result_code> <err_code><! [cdata[success]]></err_code> <err_code_des><! [cdata[issued successfully]]></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> 

The above is the entire content of this article, I hope to help you learn, We also hope that we can support the cloud-dwelling community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.