Micro-mail app payment Java code _java

Source: Internet
Author: User
Tags md5 encryption stringbuffer

This example for you to share the Java micro-letter app payment code for your reference, the specific contents are as follows

Import java.io.IOException;

Import java.io.UnsupportedEncodingException;

Import Java.util.Random;
Import org.apache.http.ParseException;
Import org.apache.http.client.ClientProtocolException;
Import Org.apache.http.client.methods.HttpPost;
Import org.apache.http.entity.StringEntity;
Import org.apache.http.impl.client.DefaultHttpClient;
Import Org.apache.http.util.EntityUtils;
Import Org.json.JSONArray;
Import Org.json.JSONML;

Import Org.json.JSONObject; public class Test {private static final String AppID = "wx0378bf81abfe3d26";//set private static final int mch_id = 12 52196606;//set private static final String Api_key = "b8b9c2bbe92d57cc38fde49745056167";/set private static final STR ing notify_url = "http://www.xxx.com/weixin_notify_url.jsp";//Set private static final String Trade_type = "APP";//PU
 Blic static void Main (string[] args) {posttowechat ("5455545", "Test", 0.01);   Posttowechat ("5455545", "Chinese", 0.01);/will fail}/** * submit to micro-letter * * * @param out_trade_no *   Your system's order number * @param body * Title * @param Money * Amount * @return/private static Jsonobject Posttowechat (St
 Ring Out_trade_no, String body, double) {StringBuilder XML = new StringBuilder ();
 String nonce_str = getrandomstring (32);
 String IP = "127.0.0.1";//client IP handles Jsonobject JSO = new Jsonobject ();
 String prepay_id = "", sign = ""; try {String Weixinmoney = new Java.text.DecimalFormat ("#"). Format (Money * 100);//micro-letter is divided into units, so multiply the xml.append ("AppID
  = "). Append (AppID). Append (" &body= "). Append (New String (Body.getbytes (" UTF-8 ")," Utf-8 "));
  Xml.append ("&mch_id="). Append (mch_id). Append ("&nonce_str="). Append (NONCE_STR); Xml.append ("¬ify_url="). Append (Notify_url). Append ("&out_trade_no="). Append (Out_trade_no). Append ("&
  Spbill_create_ip= "). Append (IP); Xml.append ("&total_fee="). Append (Weixinmoney). Append ("&trade_type="). Append (Trade_type). Append ("&
  Key= "). Append (Api_key); sign = new Util (). Md5purity (Xml.tostring ()). toUpperCase ()//MD5 Encryption Signature Encryption class own solution will not put up the Xml.delete (0, Xml.length ());
  Xml.append ("<xml>");
  Xml.append ("<appid>"). Append (AppID). Append ("</appid>");
  Xml.append ("<body>"). Append (Body). Append ("</body>");
  Xml.append ("<mch_id>"). Append (mch_id). Append ("</mch_id>");
  Xml.append ("<nonce_str>"). Append (Nonce_str). Append ("</nonce_str>");
  Xml.append ("<notify_url>"). Append (Notify_url). Append ("</notify_url>");
  Xml.append ("<out_trade_no>"). Append (Out_trade_no). Append ("</out_trade_no>");
  Xml.append ("<spbill_create_ip>"). Append (IP). Append ("</spbill_create_ip>");
  Xml.append ("<total_fee>"). Append (Weixinmoney). Append ("</total_fee>");
  Xml.append ("<trade_type>"). Append (Trade_type). Append ("</trade_type>");
  Xml.append ("<sign>"). Append (sign). Append ("</sign>");
  Xml.append ("</xml>"); HttpPost post = new HttpPost ("Https://api.mch.weixin.qq.com/pay/unifieDorder ");
  stringentity entity = new Stringentity (xml.tostring (), "UTF-8");
  Entity.setcontentencoding ("Utf-8");
  Entity.setcontenttype ("Text/xml");
  Post.setentity (entity); Jsonarray childnodes = Jsonml.tojsonobject (entityutils.tostring (New Defaulthttpclient (). Execute (POST). GetEntity (),
  "Utf-8")). Getjsonarray ("ChildNodes");
  System.out.println (childnodes);
  int len = Childnodes.length ()-1;
  for (int i = len; i >-1; i--) {Jsonobject js = childnodes.getjsonobject (i);
   if (Js.get ("TagName"). Equals ("prepay_id")) {prepay_id = Js.getjsonarray ("ChildNodes"). GetString (0);
  Break
 A catch (Unsupportedencodingexception e) {e.printstacktrace ());
 catch (ParseException e) {e.printstacktrace ();
 catch (Clientprotocolexception e) {e.printstacktrace ();
 catch (IOException e) {e.printstacktrace ();
 } jso.put ("sign", sign);
 Jso.put ("AppID", AppID);
 Jso.put ("Noncestr", nonce_str);
 Jso.put ("Package", "Sign=wxpay"); Jso.put ("Partnerid", mch_id);
 Jso.put ("Prepayid", prepay_id);
 Jso.put ("timestamp", System.currenttimemillis ());
 return JSO; /** * Indicates the length of the generated string * * @param lengths * @return/private static String getrandomstring (int length) {string bas
 E = "abcdefghijklmnopqrstuvwxyz0123456789";
 Random Random = new Random ();
 StringBuffer sb = new StringBuffer ();
  for (int i = 0; i < length; i++) {int number = Random.nextint (Base.length ());
 Sb.append (Base.charat (number));
 return sb.tostring ();
 }
}

The above is the entire content of this article, I hope to learn Java program to help you.

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.