Android integrated micro-credit payment function _android

Source: Internet
Author: User
Tags locale md5 md5 encryption uikit

Ready to work here will not say, including signing and application AppID, attached to the micro-letter open Platform App Development steps, do not understand the students can refer to here:

Https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5

The above steps are very detailed, here mainly said to pay attention to the issue. According to the above document, the Merchant server generates the payment order, first invokes the unified next single API to generate the prepayment, obtains after the prepay_id the parameter to sign again transmits to the app to initiate the payment.

The relevant code is as follows:

/** * Merchant Server generates payment orders, first invoke the unified next single API (see section 7th) to generate a prepaid order, obtain the prepay_id and then sign the parameters again to the app to initiate payment. 
  *///Product description String BODY = "iphone6s"; 
  Random string nonce_str = resourceutil.createrandomstring (32); 
  Notification address String Notify_url = "http://www.weixin.qq.com/wxpay/pay.php"; 
  Merchant Order number String Out_trade_no = Resourceutil.generateouttradeno (32); 
   
  Total amount (unit cent) int total_fee = 1; 
   
  String url = "Https://api.mch.weixin.qq.com/pay/unifiedorder"; String sign = SIGNUTIL.SIGNBYMD5 ("appid=" + constants.app_id + "&body=" + body + "&mch_id=" + constants.mch_i D + "&nonce_str=" + nonce_str + "¬ify_url=" + Notify_url + "&out_trade_no=" + out_trade_no + "&SPBILL_CR" eate_ip=127.0.0.1 "+" &total_fee= "+ Total_fee +" &trade_type=app "+" &key= "+ constants.key). ToUpperCase 
   
  (Locale.getdefault ()); Parameter passed String entity = "<xml><appid>" + constants.app_id + "</appid><mch_id>" + Constants in XML format . mch_id + "</mch_id>&Lt;nonce_str> "+ nonce_str +" </nonce_str><sign> "+ sign +" </sign><body> "+ Body +" </b Ody><out_trade_no> "+ out_trade_no +" </out_trade_no><total_fee> "+ Total_fee +" </total_fee& gt;<spbill_create_ip>127.0.0.1</spbill_create_ip><notify_url>http://www.weixin.qq.com/wxpay/  
   
  Pay.php</notify_url><trade_type>app</trade_type></xml> "; 
  LOG.D ("entity", entity); 
  Paybutton.setenabled (FALSE); 
   
  Toast.maketext (Payactivity.this, "Get order ...", Toast.length_short). Show (); 
  byte[] buf = util.httppost (URL, entity); 
   if (buf!= null && buf.length > 0) {String content = new string (BUF); 
    
   LOG.D ("Get Server Pay params:", content); 
   
   Orderresult Orderresult = resourceutil.parsexml (New Bytearrayinputstream (Content.getbytes ())); if (! Textutils.equals (Orderresult.getreturncode (), "SUCCESS")) {Toast.maketext (Payactivity.this, Orderresult.getreturnmsg (), Toast.length_short). Show (); 
   Return } if (! Textutils.equals (Orderresult.getresultcode (), "SUCCESS")) {Toast.maketext (Payactivity.this, 
    Orderresult.geterrordesc (), Toast.length_short). Show (); 
   Return 
   //The order is successful, the payment payreq request = new Payreq (); 
   Request.appid = constants.app_id; 
   Request.partnerid = constants.mch_id; 
   Request.prepayid = Orderresult.getprepayid (); 
   Request.packagevalue = "Sign=wxpay"; 
    
   Request.noncestr = Nonce_str; 
   String TimeStamp = string.valueof (System.currenttimemillis ()/1000); 
   Request.timestamp = TimeStamp; Request.sign = SIGNUTIL.SIGNBYMD5 ("appid=" + constants.app_id + "&noncestr=" + nonce_str + "&package=Sign=WXPay" + "&partnerid=" + constants.mch_id + "&prepayid=" + Orderresult.getprepayid () + "xtamp=" + TimeStamp + " 
    
   ; key= "+ Constants.key). toUpperCase (Locale.getdefault ()); 
    
   Api.sendreq (Request); 
  Paybutton.setenabled (TRUE);  } 
 } 
});

Relevant parameter description in the document are indicated, I am here nonce_str and out_trade_no are randomly generated word Fu Yi, attached to my tool class, convenient for everyone to reference.

Resourceutil.java

Package Com.xylpay.sdk.pay.uikit; 
Import java.io.IOException; 
Import Java.io.InputStream; 
 
Import Java.util.Random; 
Import Org.xmlpull.v1.XmlPullParser; 
 
Import org.xmlpull.v1.XmlPullParserException; 
 
Import Com.xylpay.sdk.pay.bean.OrderResult; 
 
Import android.util.Xml; public class Resourceutil {/** * randomly generated string * @param length of String * @return random string/public static Stri 
  ng createrandomstring (int length) {String Source = ' 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '; 
  Random Random = new Random (); 
  StringBuilder builder = new StringBuilder (); 
   for (int i = 0; i < length; i++) {int position = Random.nextint (Source.length ()); 
  Builder.append (Source.charat (position)); 
 return builder.tostring (); 
  public static String Generateouttradeno (int n) {StringBuilder builder = new StringBuilder (); 
  Random Random = new Random (); for (int i = 0; i < n; i++) {Builder.append (Random.nextint (10));
  return builder.tostring (); public static Orderresult Parsexml (InputStream is) {//pull parsing Xml data xmlpullparser parser = Xml.newpullparser () 
  ; 
  Orderresult orderresult = null; 
   try {parser.setinput (IS, "UTF-8"); 
   int type = Parser.geteventtype (); 
    while (type!= xmlpullparser.end_document) {switch (type) {case XmlPullParser.START_DOCUMENT:break; 
     Case XmlPullParser.START_TAG:if (Parser.getname (). Equals ("xml")) {Orderresult = new Orderresult (); 
     else if (Parser.getname (). Equals ("Return_code")) {Orderresult.setreturncode (Parser.nexttext ()); 
     else if (Parser.getname (). Equals ("Return_msg")) {orderresult.setreturnmsg (Parser.nexttext ()); 
     else if (Parser.getname (). Equals ("Result_code")) {Orderresult.setresultcode (Parser.nexttext ()); 
     else if (Parser.getname (). Equals ("Err_code_des")) {Orderresult.seterrordesc (Parser.nexttext ()); else if (Parser.getname ().Equals ("prepay_id")) {Orderresult.setprepayid (Parser.nexttext ()); 
    } break; 
    Case XmlPullParser.END_TAG:break; 
   } type = Parser.next (); 
  } catch (Xmlpullparserexception e) {e.printstacktrace (); 
  catch (IOException e) {e.printstacktrace (); 
 return orderresult; 
 } 
}

About the generation of sign, the order of the parameters must be in strict accordance with the above order plus key for MD5 encryption, view the signature specification.
About key description, the key here is to build and then configure the micro-letter open platform, reference merchant payment key key generation and configuration, both sides need to be consistent. In addition, when the order is made, the parameters are passed in XML format.

Finally, attach your own signature algorithm:

Signutil.java

Package Com.xylpay.sdk.pay.uikit; 
 
Import Java.security.MessageDigest; 
Import java.security.NoSuchAlgorithmException; 
 
/** 
 * Created by Jackie on 2016/2/15. 
 * * MD5 Encryption 
/public class Signutil {public 
 static string signByMD5 (string source) { 
  byte[] bytes = null;< c10/>try { 
   MessageDigest digest = messagedigest.getinstance ("MD5"); 
   Digest.update (Source.getbytes ()); Update Digest 
 
   bytes = Digest.digest ();//The hash calculation is done by performing a final operation such as padding. After calling this method, the summary is reset. 
  catch (NoSuchAlgorithmException e) { 
   e.printstacktrace (); 
  } 
 
  StringBuilder builder = new StringBuilder (Bytes.length * 2); 
  for (byte b:bytes) { 
   /** 
    * 0xFF The default is to reshape, a byte and 0xFF to participate in the first byte into the plastic operation/ 
   if ((b & 0xFF) < 0x10) {//If a 0 
    builder.append ("0") is filled in front of 1 digits; 
   } 
 
   Builder.append (integer.tohexstring (b & 0xFF)); 
  } 
 
  return builder.tostring (); 
 } 
 

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.

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.