Various pitfalls encountered during development in those years
I have been unable to talk about the API, but it's really a bit of a spam,
The difficulties encountered in development and API development are more profound than all the routines, and it is almost heartless.
Let's provide you with a code about enterprise payment. Let's just move on to the Code:
Private Logger logger = Logger. getLogger (getClass ());
// Developer ID
Private String AppID = "";
// Merchant ID
Private String mchId = "";
// Payment key
Private static String key = "";
// Unified withdrawal address
Private String UNIFIED_FEEDBACK_URL = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers ";
// Check the user name Option
Private String check_name = "NO_CHECK ";
// Check the user name Option
Private String send_name = "";
// Enterprise payment description
Private String desc = "";
@ Autowired
Private UserService userService;
@ RequestMapping ("/pay ")
@ ResponseBody
Public boolean pay (HttpServletRequest request, Integer userid) throws Exception {
Userinfo user = UserUtils. getUserById (request, userid );
Boolean falg = true;
SortedMap <Object, Object> parameters = new TreeMap <Object, Object> ();
StringBuffer param = new StringBuffer ();
Param. append ("mch_appid =" + AppID );
Param. append ("& mchid =" + mchId );
Param. append ("& nonce_str =" + MD5Util. MD5Encode (String. valueOf (new Random (). nextInt (10000), "UTF-8"). toUpperCase ());
String currTime = TenpayUtil. getCurrTime ();
String strTime = currTime. substring (8, currTime. length ());
String strRandom = TenpayUtil. buildRandom (4) + "";
Param. append ("& partner_trade_no =" + strTime + strRandom );
Param. append ("& openid =" + user. getOpenid ());
Param. append ("& check_name =" + check_name );
Param. append ("& re_user_name =" + user. getName (); // payee name
Int amount = (int) (10 * Math. random () + 100 );
// Int amount = (int) (Math. random () * 100 + 100 );
Request. getSession (). setAttribute (Constants. REDPACKET, amount );
Param. append ("& amount =" + amount );
Param. append ("& desc =" + desc );
InetAddress inetAddress = InetAddress. getLocalHost ();
String ip = inetAddress. getHostAddress ();
Param. append ("& spbill_create_ip =" + ip );
String [] params = param. toString (). split ("&");
Arrays. sort (params );
Param = new StringBuffer ();
For (String p: params ){
String [] value = p. split ("= ");
Parameters. put (value [0], value [1]);
Param. append (value [0] + "=" + value [1] + "&");
}
String sign = WeixinPaySign. createSign ("UTF-8", parameters, key );
Parameters. put ("sign", sign );
String requestXML = XMLUtil. getRequestXml (parameters );
Logger.info ("requestXML:" + requestXML );
CloseableHttpClient httpclient = ClientCustomSSL. ssl ();
Logger.info ("httpclient:" + httpclient );
HttpPost httpPost = new HttpPost (UNIFIED_FEEDBACK_URL );
HttpPost. setEntity (new StringEntity (requestXML, "UTF-8 "));
System. out. println ("executing request" + httpPost. getRequestLine ());
CloseableHttpResponse response = httpclient.exe cute (httpPost );
HttpEntity entity = response. getEntity ();
InputStreamReader reader = new InputStreamReader (entity. getContent (), "UTF-8 ");
Char [] buff = new char [1024];
Int length = 0;
StringBuffer strxml = new StringBuffer ();
While (length = reader. read (buff ))! =-1 ){
Strxml. append (new String (buff, 0, length ));
System. out. println (new String (buff, 0, length ));
}
// Httpclient. close ();
Httpclient. getConnectionManager (). shutdown ();
// Parse the passed xml
Document document = incluenthelper. parseText (strxml. toString ());
// Get the xml root element
Element root = document. getRootElement ();
// Obtain all child nodes of the root element
List <Element> elementList = root. elements ();
For (Element e: elementList ){
// Result_code business
If ("return_code". equals (e. getName ())&&! "SUCCESS". equals (e. getText ())){
Falg = false;
}
If ("result_code". equals (e. getName ())&&! "SUCCESS". equals (e. getText ())){
Falg = false;
}
}
Response. close ();
Return falg;
}
// Security certificate address
Public static String certPath = "cert/apiclient_cert.p12 ";
// Merchant ID
Private static String mchId = "";
/**
* Public Method for certificate verification
* @ Description
* @ Param certPath: Certificate path
* @ Param mchid: merchant id
* @ Return
* @ Throws Exception
* @ Author Jobs
*/
Public static CloseableHttpClient ssl () throws Exception {
KeyStore keyStore = KeyStore. getInstance ("PKCS12 ");
System. out. println ("keyStore:" + keyStore );
FileInputStream instream = new FileInputStream (new File (certPath ));
System. out. println ("instream:" + instream );
Try {
KeyStore. load (instream, mchId. toCharArray ());
} Finally {
Instream. close ();
}
// Trust your CA and all self-signed certificates
SSLContext sslcontext = SSLContexts. custom (). loadKeyMaterial (keyStore, mchId. toCharArray (). build ();
// Only the TLSv1 protocol is allowed
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory (sslcontext, new String [] {"TLSv1"}, null, SSLConnectionSocketFactory. BROWSER_COMPATIBLE_HOSTNAME_VERIFIER );
CloseableHttpClient httpclient = HttpClients. custom (). setSSLSocketFactory (sslsf). build ();
Return httpclient;
}
/**
* @ Note: Convert request parameters to strings in xml format
*/
Public static String getRequestXml (SortedMap <Object, Object> parameters ){
StringBuffer sb = new StringBuffer ();
Sb. append ("<xml> ");
Set <Entry <Object, Object> es = parameters. entrySet ();
Iterator <Entry <Object, Object> it = es. iterator ();
While (it. hasNext ()){
Map. Entry <Object, Object> entry = (Map. Entry <Object, Object>) it. next ();
String k = (String) entry. getKey ();
String v = entry. getValue (). toString ();
If ("attach". inclusignorecase (k) | "body". inclusignorecase (k) | "sign". inclusignorecase (k )){
Sb. append ("<" + k + ">" + "<! [CDATA ["+ v +"]> </"+ k +"> ");
} Else {
Sb. append ("<" + k + ">" + v + "</" + k + "> ");
}
}
Sb. append ("</xml> ");
Return sb. toString ();
}
Export research this enterprise payment is also a variety of traps, but fortunately these traps have survived
* Note: The main code segment is the first section. The subsequent sections are tool classes that can be found online.
Write down these hopes to help those who have been trapped