Micro-Credit Payment Java version V3 validation data legality (DEOM) _java

Source: Internet
Author: User
Tags stringbuffer

1.1 Parsing micro-letter callback data

InputStream instream = Request.getinputstream ();
Bytearrayoutputstream Outsteam = new Bytearrayoutputstream ();
byte[] buffer = new byte[1024];
int len = 0;
while (len = instream.read (buffer))!=-1) {
outsteam.write (buffer, 0, Len);
}
Outsteam.close ();
Instream.close ();
/** Gets the Notify_url return XML message of the micro-mail Call/
string result = new String (Outsteam.tobytearray (), "utf-8");

The result is the XML data returned by the micro-trust callback.

1.2 Parsing the XML data returned by the micro-letter

/**
* Incoming micro-letter callback returns XML information
* returns in map form for easy value
* dom4j parse XML, return first-level element key-value pairs. If the first level element has child nodes, the value of this node is null
* @param strxml
* @return
* @throws documentexception
* * @SuppressWarnings ("Rawtypes")
public static sortedmap<string, string> Dom4jxmlparse (String strxml) throws documentexception {
SortedMap <string, string> SMAP = new treemap<string, string> ();
Document doc = Documenthelper.parsetext (strxml);
Element root = Doc.getrootelement ();
for (Iterator iterator = Root.elementiterator (); Iterator.hasnext ();) {
element E = (Element) iterator.next ();
Smap.put (E.getname (), E.gettext ());
}
return SMAP;
}

Returns the ordered map format data, taking the value of Smap.get ("field name") to get the data.

1.3 Verifying the legality of the micro-letter return signature

/**
* Whether the micro-letter V3 signature, the rule is: by parameter name A-Z sort, encountered null parameters do not participate in the signature
* Incoming micro-letter return information after parsing the sortedmap format parameter data
* Verify that the message is a legal message from the micro-letter *
Param SMAP
* @param apikey Set Key
* @return Verification result * *
@SuppressWarnings ("rawtypes") public
static Boolean iswechatsign (sortedmap<string, string> smap,string apikey) {
StringBuffer sb = new StringBuffer (); C11/>set es = Smap.entryset ();
Iterator it = Es.iterator ();
while (It.hasnext ()) {
Map.entry Entry = (map.entry) it.next ();
String k = (string) entry.getkey ();
String v = (string) entry.getvalue ();
if (!) Sign ". Equals (k) && null!= v &&!" ". Equals (v) &&! " Key ". Equals (k)) {
sb.append (k +" = "+ V +" & ");
}
Sb.append ("key=" + apikey);
/** Authenticated Signature */
String sign = Md5util.md5encode (sb.tostring (), "Utf-8"). toUpperCase ();
/** the legal signature returned by the micro-letter/
String validsign = ((String) smap.get ("sign")). toUpperCase ();
return validsign.equals (sign);
}

Personal advice: To verify the legality of the micro-letter signature, you can first determine whether the Return_code and Result_code returned by the micro-letter are success.

The above is a small set to introduce the micro-letter to pay the Java version of the V3 Verify data Legality (DEOM), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.