Full is the pit, because the server lazy let customer service to write unified order, the server only gave the URL of the notification. Micro-letter Payment demo does not unify the order of the Code.
Read this article before reading: Https://pay.weixin.qq.com/wiki/doc/api/app/app.PHP?chapter=9_1
Step-by-Step to the first according to the unified parameters of the Introduction tool:
1. Get to current IP:
<span style= "FONT-SIZE:14PX;" >public String getlocalipaddress () {try {for (enumeration<networkinterface> en = networkinterface.g Etnetworkinterfaces (); En.hasmoreelements ();
) {NetworkInterface intf = en.nextelement (); for (enumeration<inetaddress> enumipaddr = intf.getinetaddresses (); enumipaddr.hasmoreelements ();)
{InetAddress inetaddress = enumipaddr.nextelement ();
if (!inetaddress.isloopbackaddress ()) {return inetaddress.gethostaddress (). toString ();
The catch (SocketException ex) {} return null; Private String Getwifiip () {//Get WiFi service Wifimanager Wifimanager = (Wifimanager) getsystemservice (context.
Wifi_service);
Determine if WiFi opens if (!wifimanager.iswifienabled ()) {wifimanager.setwifienabled (true);
} wifiinfo Wifiinfo = Wifimanager.getconnectioninfo ();
int ipaddress = wifiinfo.getipaddress (); String IP = inttoip (ipaddress);
return IP;
Private String Inttoip (int i) {return (I & 0xFF) + "." + ((I >> 8) & 0xFF) + "." + ((i >>) & 0xFF) + "."
+ (I >> & 0xFF); } </span>
2. Random order number generation test you can generate random numbers on your own:
<span style= "FONT-SIZE:14PX;" >private String Genouttradno () {
Random Random = new Random ();
Return Md5.getmessagedigest (string.valueof (Random.nextint (10000)). GetBytes ());
3. Signature tool:
<span style= "FONT-SIZE:14PX;" >private String genappsign (list<namevaluepair> params) {
StringBuilder sb = new StringBuilder ();
for (int i = 0; i < params.size (); i++) {
sb.append (params.get (i) getName ());
Sb.append (' = ');
Sb.append (Params.get (i). GetValue ());
Sb.append (' & ');
}
Sb.append ("key=");
Sb.append (Constants.api_key);
This.sb.append ("Sign str\n" +sb.tostring () + "\ n");
String appsign = Md5.getmessagedigest (Sb.tostring (). GetBytes ());
LOG.E ("Orion", appsign);
return appsign;
Pretty much. Now we need to generate parameters parameter requirements that are passed in XML format:
<span style= "FONT-SIZE:14PX;"
>private String Genproductargs () {stringbuffer xml = new StringBuffer ();
String IP = GETWIFIIP ();
if (IP = = "" && IP = = "") {IP = getlocalipaddress ();
try {String noncestr = Gennoncestr ();
Xml.append ("</xml>");
list<namevaluepair> packageparams = new linkedlist<namevaluepair> ();
Packageparams.add (New Basicnamevaluepair ("AppID", constants.app_id));
Packageparams.add (New Basicnamevaluepair ("Body", "APP pay Test"));
Packageparams.add (New Basicnamevaluepair ("mch_id", constants.mch_id));
Packageparams.add (New Basicnamevaluepair ("Nonce_str", Noncestr));
Packageparams.add (New Basicnamevaluepair ("Notify_url", Configutil.notify_url));
Packageparams.add (New Basicnamevaluepair ("Out_trade_no", Genouttradno ());
Packageparams.add (New Basicnamevaluepair ("Spbill_create_ip", IP)); Packageparams.add (New Basicnamevaluepair ("Total_fee"," 1 "));
Packageparams.add (New Basicnamevaluepair ("Trade_type", "APP"));
String sign = genpackagesign (packageparams);
Packageparams.add (New Basicnamevaluepair ("sign", sign));
String xmlstring =toxml (packageparams);
return xmlstring;
catch (Exception e) {log.e ("TAG", "fail, ex =" + E.getmessage ());
return null;
}}</span>
Among them ToXml:
<span style= "FONT-SIZE:14PX;" >private String toXml (list<namevaluepair> params) {
StringBuilder sb = new StringBuilder ();
Sb.append ("<xml>");
for (int i = 0; i < params.size (); i++) {
sb.append ("<" +params.get (i). GetName () + ">");
Sb.append (Params.get (i). GetValue ());
Sb.append ("</" +params.get (i) getName () + ">");
Sb.append ("</xml>");
LOG.E ("Orion", Sb.tostring ());
return sb.tostring ();
The parameters are passed, according to the document instructions, we need to use post to hang connection URL address: https://api.mch.weixin.qq.com/pay/unifiedorder:
<span style= "FONT-SIZE:14PX;" > Private Class Getprepayidtask extends Asynctask<void, Void, map<string,string>> {private Progres
Sdialog Dialog; @Override protected void OnPreExecute () {dialog = Progressdialog.show (Payactivity.this, getString (R.string.ap
P_tip), getString (R.string.getting_prepayid));
} @Override protected void OnPostExecute (map<string,string> result) {if (dialog!= null) {
Dialog.dismiss ();
} sb.append ("prepay_id\n" +result.get ("prepay_id") + "\ n");
Resultunifiedorder=result;
} @Override protected void oncancelled () {super.oncancelled (); } @Override protected map<string,string> doinbackground (Void ... params) {String URL = string.f
Ormat ("Https://api.mch.weixin.qq.com/pay/unifiedorder");
String entity = Genproductargs ();
LOG.E ("Orion", entity); byte[] buf = util.httppost (URL, entity);
String content = new string (BUF);
LOG.E ("Orion", content);
Map<string,string> xml=decodexml (content);
return XML;
}}</span>
where Decodexml is:
<span style= "FONT-SIZE:14PX;" >public map<string,string> decodexml (String content) {try {map<string, string> xml = new Ha
Shmap<string, string> ();
Xmlpullparser parser = Xml.newpullparser ();
Parser.setinput (content) (new StringReader);
int event = Parser.geteventtype ();
while (event!= xmlpullparser.end_document) {String nodename=parser.getname ();
Switch (event) {case XmlPullParser.START_DOCUMENT:break;
Case XmlPullParser.START_TAG:if ("xml". Equals (nodename) ==false) {//Instantiate student Object
Xml.put (Nodename,parser.nexttext ());
} break;
Case XmlPullParser.END_TAG:break;
event = Parser.next ();
} return XML;
catch (Exception e) {log.e ("Orion", E.tostring ());
return null;
}</span>
is completed, the second part is to the micro-credit payment transmission to adjust the micro-letter payment parameters (see the specific parameters of the document):
<span style= "FONT-SIZE:14PX;"
>private void Genpayreq () {req.appid = constants.app_id;
Req.partnerid = constants.mch_id;
Req.prepayid = Resultunifiedorder.get ("prepay_id");
Req.packagevalue = "prepay_id=" +resultunifiedorder.get ("prepay_id");
Req.noncestr = Gennoncestr ();
Req.timestamp = string.valueof (Gentimestamp ());
list<namevaluepair> signparams = new linkedlist<namevaluepair> ();
Signparams.add (New Basicnamevaluepair ("AppID", Req.appid));
Signparams.add (New Basicnamevaluepair ("Noncestr", Req.noncestr));
Signparams.add (New Basicnamevaluepair ("package", Req.packagevalue));
Signparams.add (New Basicnamevaluepair ("Partnerid", Req.partnerid));
Signparams.add (New Basicnamevaluepair ("Prepayid", Req.prepayid));
Signparams.add (New Basicnamevaluepair ("timestamp", Req.timestamp));
Req.sign = Genappsign (signparams);
Sb.append ("sign\n" +req.sign+ "n ')";
LOG.E ("Orion", Signparams.tostring ());
}</span>
The third part, the micro-letter payment:
<span style= "FONT-SIZE:14PX;" >private void Sendpayreq () {
msgapi.registerapp (constants.app_id);
Msgapi.sendreq (req);
} </span>
which
<span style= "FONT-SIZE:14PX;" >Constants.APP_ID</span>
is AppID on the developer platform to get
<span style= "FONT-SIZE:14PX;" >
Merchant ID
The callback notification address after payment.
Signed two times, then use the Api--key is the Merchant Platform API security inside customization.
Yes, and there's a time tool to get
<span style= "FONT-SIZE:14PX;" >private long Gentimestamp () {return
System.currenttimemillis ()/1000;
} </span>
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.