Les exemples de cet article partagent le code de paiement de l'application Java WeChat pour votre référence. Le contenu spécifique est le suivant
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.httpost; 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.json.jsonObject; "WX0378BF81ABFE3D26"; // Définir private static final int mch_id = 1252196606; // définir la chaîne finale statique privée api_key = "b8b9c2bbe92d57cc38fde49745056167"; // définir la chaîne finale privée nottify_url = "http://www.xxx.com/weixin_notify_url.jsp" ;//set privé static final String prorade_type =" app "; // public static void main (string [] args) {posttowechat (" 5455545 "," test ", 0,01); Posttowechat ("5455545", "chinois", 0.01); // il échouera} / ** * Soumettre à WeChat * * @param out_trade_no * Numéro de commande de votre propre système * @param Body * Title * @param Money * Montant * @return * / Private Static JsonObject PostTowechat (String Out_Trade_No, String Bodil = new StringBuilder (); String nonce_str = getRandomString (32); String ip = "127.0.0.1"; // Le client IP gère JSONObject lui-même jsonObject JSO = new JSONObject (); String prepay_id = "", signe = ""; Try {String weixinmoney = new Java.Text.decimalformat ("#"). Format (Money * 100); // wechat est divisé en unités afin qu'il soit multiplié par 100 xml.append ("appid ="). APPEND (AppID) .APPEND ("& body ="). "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 ("& troty_type ="). APPEND (TRADE_TYPE) .APPEND ("& key ="). APPEND (API_KEY); signe = new util (). md5purity (xml.tostring ()). touppercase (); // la classe de cryptage de signature de cryptage MD5 est résolu par lui-même et il ne sera pas mis sur xml.delete (0, xml.length ()); xml.append ("<xml>"); xml.append ("<pid>"). append (appid) .append ("</pid>"); xml.append ("<body>"). append (body) .append ("</gody>"); 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 ("<RADADE_TYPE>"). APPEND (TRADE_TYPE) .APPEND ("</romate_type>"); Xml.APPEND ("<Sign>"). APPEND (Sign) .APPEND ("</ Sign>"); xml.append ("</xml>"); Httppost post = new httppost ("https://api.mch.weixin.qq.com/pay/UnifiedOrder"); Entity StronEntity = new StringEntity (xml.toString (), "utf-8"); entity.setConTentencoding ("UTF-8"); entity.setContentType ("text / xml"); post.SetEntity (entité); JSONArray ChildNodes = JSONML.TojsonObject (entityUtils.tostring (new defaulthTTPClient (). Execute (post) .getEntity (), "utf-8")). GetJsonArray ("childNodes"); System.out.println (ChildNodes); int len = childnodes.length () - 1; pour (int i = len; i> -1; i--) {jsonObject js = childnodes.getjsonObject (i); if (js.Get ("tagname"). equals ("prepay_id")) {prepay_id = jsgetjsonArray ("childNodes"). getString (0); casser; }}} catch (UnpaipportDenCcodingException e) {e.printStackTrace (); } catch (parseException e) {e.printStackTrace (); } catch (clientProtoCoLexception e) {e.printStackTrace (); } catch (ioException e) {e.printStackTrace (); } jso.put ("signe", signe); jso.put ("appid", appid); jso.put ("noncestr", nonce_str); jso.put ("package", "signe = wxpay"); jso.put ("partenaired", mch_id); jso.put ("prepayid", prepare_id); JSO.put ("TimeStamp", System.Currenttimemillis ()); retourner JSO; } / ** * Indique la longueur de la chaîne générée * * @param longueur * @return * / private static String getrandomString (int longueur) {String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Aléatoire aléatoire = nouveau aléatoire (); StringBuffer sb = new StringBuffer (); pour (int i = 0; i <longueur; i ++) {int SB.APPEND (Base.Charat (numéro)); } return sb.toString (); }}Ce qui précède concerne cet article, j'espère qu'il sera utile pour tout le monde d'apprendre la programmation Java.