Android WeChat payment SDK development, android payment sdk

Source: Internet
Author: User

Android payment SDK development, android payment sdk

I. Preparations

1. Download the development platform and SDK

Open Platform

Https://open.weixin.qq.com

Download SDK

Download payment Demo

Http://pay.weixin.qq.com/wiki/doc/api/app.php? Chapter = 11_1

 

2. Create an application

Create a mobile appAppIDAppSecret

Developer qualification certification is required to obtain the payment function. The fee of RMB/year seems to be the only one to be charged on the open platform...

 

3. AndroidMainfest File Modification

Permission

<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/><uses-permission android:name="android.permission.READ_PHONE_STATE"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

  

Ii. Business Process

Description of interaction between the merchant System and the payment system:

Step 1: select products in the merchant APP, submit the order, and select pay.

Step 2: the merchant's background receives the user's payment order and calls the unified order payment interface. See [Unified order API ].

Step 3: The unified order interface returns the normal prepay_id, and then re-generates the signature according to the signature specification, the data is transmitted to the APP. The fields involved in the signature are appId, partnerId, prepayId, nonceStr, timeStamp, and package. Note: The package Value format is Sign = WXPay.

Step 4: The Merchant APP starts the payment. For more information about APIs, see [app development steps]

Step 5: The Merchant's background receives the payment notification. For more information about the api, see [payment result notification API]

Step 6: query the payment result at the merchant's background ., For api details, see [query order API]

 

Iii. Development

1. register an application

final IWXAPI msgApi = WXAPIFactory.createWXAPI(this, null);msgApi.registerApp(Constants.APP_ID);

  

2. Call the unified payment order interface and return the PrepayID

private class GetPrepayIdTask extends AsyncTask<Void, Void, Map<String,String>> {         private ProgressDialog dialog;         @Override        protected void onPreExecute() {            dialog = ProgressDialog.show(PayActivity.this, getString(R.string.app_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\n");            show.setText(sb.toString());            resultunifiedorder=result;        }         @Override        protected void onCancelled() {            super.onCancelled();        }         @Override        protected Map<String,String>  doInBackground(Void... params) {            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);            Map<String,String> xml=decodeXml(content);            return xml;        }    }
Set unified Order Parameters:
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", "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", "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;        }      }

  

3. Set the parameters of the PayReq request and send the payment request

// Set the 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 partition List <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 ());}

 

// Send the request msgApi. sendReq (req );

 

4. processing result callback

Implement the IWXAPIEventHandler interface of the API

Api. handleIntent (getIntent (), new IWXAPIEventHandler );

Process the request callback in the onResp (BaseResp resp) of IWXAPIEventHandler

@ Override public void onResp (BaseResp resp) {Log. d (TAG, "onPayFinish, errCode =" + resp. errCode); if (resp. getType () = ConstantsAPI. COMMAND_PAY_BY_WX) {// handle the callback AlertDialog of PayReq. 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 ();}}

  

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.