Android realizes micro-credit payment function _android

Source: Internet
Author: User
Tags md5 string format stringbuffer

To develop the Android app micro-credit payment function, you need to complete three steps: The first step is to generate a pre-payment order, a second step to generate a micro-credit payment parameter, and a third pace of micro-mail app payment. In addition to the need to audit through the app application, but also need to obtain the micro-letter payment interface permissions, and then get the corresponding merchant number, API key, which is indispensable, and in the app micro-letter payment to use

Get merchant number, API key

In the micro-credit open platform to see the audit through the app application, whether to apply for payment function, if applied, login micro-credit Payment | Merchant platform: http://pay.weixin.qq.com view the corresponding merchant number, API key

"Application for micro-credit payment interface

"Login to the merchant platform

"View Merchant number

"Get the API Key

Step one: Generate a pre-payment order

Store the commodity information, merchant information in Basicnamevaluepair, then store it in the list, and construct it into an XML string format, the interface provided by the Post method:https://api.mch.weixin.qq.com/ Pay/unifiedorder sends the data, receives the return information, obtains prepay_id the advance payment order number

To generate an order request

Private String Genproductargs () {stringbuffer xml = new StringBuffer (); 
 
 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", "Weixin")); 
 Packageparams. Add (New Basicnamevaluepair ("mch_id", constants.mch_id)); 
 Packageparams.add (New Basicnamevaluepair ("Nonce_str", Noncestr)); 
 Packageparams.add (New Basicnamevaluepair ("Notify_url", "http://121.40.35.3/test")); 
 Packageparams.add (New Basicnamevaluepair ("Out_trade_no", Genouttradno ()); 
 Packageparams.add (New Basicnamevaluepair ("Spbill_create_ip", "127.0.0.1")); 
 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, "Genproductargs fail, ex =" + E.getmessage ()); 
 return null; 
 } 
 
 }

"Get the pre-payment order number

String url = String.Format ("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); 
 

Step two: Generate the micro-credit payment parameters

AppID corresponding to the application of the app Appid,partnerid corresponding merchant number, Prepayid corresponding to the first step of the advance payment order, Packagevalue value Sign=wxpay,noncestr is a series of random numbers, Timestamp generated timestamp, sign corresponding to app signature

"Payment parameter Configuration

 private void Genpayreq () {req.appid = constants.app_id; 
 Req.partnerid = constants.mch_id; 
 Req.prepayid = Resultunifiedorder.get ("prepay_id"); 
 Req.packagevalue = "Sign=wxpay"; 
 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"); 
 
 Show.settext (Sb.tostring ()); 
 
 LOG.E ("Orion", Signparams.tostring ()); } 

"Noncestr random number

Private String Gennoncestr () { 
 Random Random = new Random (); 
 Return Md5.getmessagedigest (string.valueof (Random.nextint (10000)) 
 . GetBytes ()); 
  

"Timestamp time stamp

Private Long Gentimestamp () {return 
 System.currenttimemillis ()/1000; 
 } 

"App Signature

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 ()) 
 . toUpperCase (); 
 LOG.E ("Orion", appsign); 
 return appsign; 
 } 

The third step: to adjust the micro-letter app Payment

Before paying, need handset to install the micro-letter client, if not, will prompt to download installs the micro-letter, then the payment, adds the following configuration in the Androidmanifest.xml file

<activity 
 android:name= ". Act. Payactivity " 
 android:exported=" true " 
 android:launchmode=" Singletop "> 
 <intent-filter> 
 <action android:name= "Android.intent.action.VIEW"/> 
 
 <category android:name= " Android.intent.category.DEFAULT "/> 
 
 <data android:scheme=" wxf2f565574a968187 "/> 
 </ Intent-filter> 
 </activity> 

"The micro-letter payment

private void Sendpayreq () { 
 
 msgapi.registerapp (constants.app_id); 
 Msgapi.sendreq (req); 
 } 

Receive micro-letter return results

Rewrite the Onresp (Baseresp) method in an activity that implements the Iwxapieventhandler interface to receive the micro-letter results

"Receive the payment result

public void Onresp (Baseresp resp) { 
 log.d (TAG, "onpayfinish, Errcode =" + Resp.errcode); 
 
 if (resp.gettype () = = constantsapi.command_pay_by_wx) { 
 Alertdialog.builder Builder = new Alertdialog.builder (this ); 
 Builder.settitle (r.string.app_tip); 
 Builder.setmessage (GetString (r.string.pay_result_callback_msg, 
  resp.errstr + "; code=" + string.valueof ( Resp.errcode))); 
 Builder.show (); 
 } 
  

This article has been organized into the "Android micro-credit Payment Tutorial Summary", "Android micro-credit Development tutorial Summary," Welcome to learn to read.

The above is the entire content of this article, I hope to help you learn.

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.