Os exemplos deste artigo compartilham o código de pagamento do aplicativo Java WeChat para sua referência. O conteúdo específico é o seguinte
importar java.io.ioException; importar java.io.unsupportEdEncodingException; importar java.util.random; importar org.apache.http.parseException; importação org.apache.http.client.clientProtocolexception; importpache.httpache.client.client.clientProtocomception; importpache.httpactp.http.client.cientProtocomcepth; org.apache.http.entity.stringentity; importar org.apache.http.impl.client.defaulthttpClient; importar org.apache.http.util.entityutils; import org.json.jsonarray; public.json.jsonml; "WX0378BF81ABFE3D26"; // Definir estático privado final Int mch_id = 1252196606; // Definir a String final estática privada api_key = "B8B9C2BBE92D57CC38FDE49745056167"; // SET privado "http://www.xxx.com/weixin_notify_url.jsp" ;//set estático privado final string trade_type =" app "; // public static void main (string [] args) {posttowechat (" 545545 "test", 0.01); postToWeChat("5455545", "Chinese", 0.01);//It will fail} /** * Submit to WeChat* * @param out_trade_no * Order number of your own system* @param body * Title* @param money * Amount * @return */ private static JSONObject postToWeChat(String out_trade_no, String body, double money) { StringBuilder xml = new StringBuilder (); String nonce_str = getRandomString (32); String ip = "127.0.0.1"; // O IP do cliente lida com o próprio JsonObject jsonObject jso = new jsonObject (); String prepay_id = "", sinal = ""; tente {string weixinMoney = new java.text.decimalformat ("#"). formato (dinheiro * 100); // weChat é dividido em unidades, por isso precisa ser multiplicado por 100 xml.append ("Appid ="). Append (Append) .Append ("& body ="). "UTF-8")); xml.append ("& mch_id ="). Append (mch_id) .append ("& nonce_str ="). Append (noce_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); sinal = new util (). md5purity (xml.toString ()). touppercase (); // A classe de criptografia de assinatura de criptografia md5 é resolvida por si mesma e não será colocada em xml.delete (0, xml.length ()); xml.append ("<xml>"); xml.append ("<ppid>"). Append (Appid) .Append ("</ppid>"); xml.append ("<body>"). Appender (corpo) .append ("</body>"); xml.append ("<mch_id>"). Append (mch_id) .append ("</mch_id>"); xml.Append ("<Nonce_Str>"). Append (nonce_str) .Append ("</Nonce_Str>"); xml.Append ("<TOnify_Url>"). Append (notify_url) .append ("</otify_url>"); xml.append ("<sir_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 ("</ototal_fee>"); xml.append ("<ercred_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 (entidade); JsonArray ChildNodes = jsonml.tojsonObject (entityutils.toString (new DefaUrTHTPClient (). 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 ("pré -PAY_ID")) {prepay_id = js.getjsonArray ("ChildNodes"). getString (0); quebrar; }}} Catch (UnsupportEdEncodingException e) {E.PrintStackTrace (); } catch (parseException e) {e.printStackTrace (); } catch (clientprotocolexception e) {e.printStackTrace (); } catch (ioexception e) {e.printStackTrace (); } jso.put ("sinal", sinal); JSO.put ("Appid", Appid); jso.put ("não -cest", nonce_str); jso.put ("pacote", "sinal = wxpay"); JSO.put ("PartnerID", mch_id); JSO.put ("pré -anyid", preparar_id); JSO.put ("Timestamp", System.CurrentTimeMillis ()); retornar JSO; } / ** * Indica o comprimento da string gerada * * @Param Comprimento * @return * / String estática privada getRandomString (int length) {String base = "abcdefghijklmnopqrststwxyz0123456789"; Aleatório aleatório = novo aleatório (); StringBuffer sb = new StringBuffer (); for (int i = 0; i <comprimento; i ++) {int number = aleatom.nextInt (base.length ()); sb.append (base.charat (número)); } return sb.toString (); }}O exposto acima é tudo sobre este artigo, espero que seja útil para todos aprenderem a programação Java.