Article From:

1. Preparation work

1、Development platform and SDK Download

WeChat open platform

Download SDK

WeChat pays Demo Downloads


2、Create applications

Create mobile application acquisitionAppID  AppSecret

To get the payment function, developer qualification is also required. The cost of 300RMB/ seems to be the only fee to be charged in the open platform.


3、AndroidMainfestFile modification


<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"/>


Two. Business process

The main interaction between merchant system and WeChat payment system is as follows:

Step 1: users choose goods in merchant APP, submit orders, select WeChat payment.

Step 2: the merchant background receives the user payment form and calls the unified payment interface of WeChat payment. See [unify API].

Step 3: unify the next interface to return to the normal prepay_id, and then regenerate the signature according to the signature specification, then transmit the data to APP. The name of the field is appId, partnerId, prepayId, nonceStr, timeStam.P, package. Note: the value format of package is Sign=WXPay

Step 4: the merchant APP transfers the WeChat payment. API see this section [app end development steps]

Step 5: the merchant receives the payment notice in the background. API see [notification of payment results to API]

Step 6: the merchant returns the payment results in the background. API see [query order API]


Three. Development

1、Registration application

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


2、Invoke payment to unify the single interface, and return to PrepayID

private class GetPrepayIdTask extends AsyncTask<Void, Void, Map<String,String>> {
        private ProgressDialog dialog;
        protected void onPreExecute() {
            dialog =, getString(R.string.app_tip), getString(R.string.getting_prepayid));
        protected void onPostExecute(Map<String,String> result) {
            if (dialog != null) {
        protected void onCancelled() {
        protected Map<String,String>  doInBackground(Void... params) {
            String url = String.format("");
            String entity = genProductArgs();
            byte[] buf = Util.httpPost(url, entity);
            String content = new String(buf);
            Log.e("orion", content);
            Map<String,String> xml=decodeXml(content);
            return xml;
Setting unified single parameter:

private String genProductArgs() {
        StringBuffer xml = new StringBuffer();
        try {
            String  nonceStr = genNonceStr();
           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", ""));
            packageParams.add(new BasicNameValuePair("out_trade_no",genOutTradNo()));
            packageParams.add(new BasicNameValuePair("spbill_create_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, "genProductArgs fail, ex = " + e.getMessage());
            return null;


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

//Setting the parameters of the PayReq requestPrivate 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 ());ItList< NameValuePair> signParams =New LinkedList< NameValuePair> ();SignParams.add (New BasicNameValuePair ("appid", req.a)PpId))SignParams.add (New BasicNameValuePair ("noncestr", req.nonceStr));SignParams.add (New BasicNameValuePair ("package", req.packageValue));SignParams.add (New BasicNameValue)Pair ("partnerid", req.partnerId));SignParams.add (New BasicNameValuePair ("prepayid", req.pre)PayId))SignParams.add (New BasicNameValuePair ("timestamp", req.timeStamp));ItReq.sIgn = genAppSign (signParams);Sb.append ("sign\n" +req.sign+ "\n\n");Show.setText (sb.t)OString ());Log.e ("Orion", signParams.toString ());    }


//Send requestMsgApi.sendReq (req);


4、Processing result callback

Implementation of the IWXAPIEventHandler interface of WeChat API

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

Processing request callbacks in onResp (BaseResp RESP) of IWXAPIEventHandler

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


Leave a Reply

Your email address will not be published. Required fields are marked *