Android payment
========================================================== =
Preparation:
1. The imported libs package libammsdk. jar;
2. Use debug_keystore in weixinDemo for testing;
3. Pay attention that the application must pass the review and the Key values are correct. The values in the payment Demo are as follows:
// Public platform id; private String app_wx_appid = WxConstants. app_wx_appid; // private String app_wx_secret_key = "secret" agreed by the open platform and merchants; // private String app_wx_parent_key = "secret" agreed by the merchant module of the public platform and the merchant "; // The payment key private String app_wx_pay_key = "yellow" agreed by the merchant module of the public platform and the merchant; // The Merchant id applied by the merchant to caifu */private String app_tx_parent_key = "1900000109 ";
========================================================== =
Based on the Payment Demo, the payment is divided into three steps:
Step 1: Obtain the accessToken and use the accessToken value in step 2;
private class GetAccessTokenTask extends AsyncTask
{@Overrideprotected WxGetAccessTokenResult doInBackground(Void... params) {WxGetAccessTokenResult result = getAccessToken();return result;}@Overrideprotected void onPostExecute(WxGetAccessTokenResult result) {if (result.localRetCode == WxLocalRetCode.ERR_OK) {GetPrepayIdTask getPrepayId = new GetPrepayIdTask();getPrepayId.execute(result);}}}
Resolution Server Response
private WxGetAccessTokenResult getAccessToken() {WxGetAccessTokenResult result = new WxGetAccessTokenResult();String url = String.format(api_get_access_token, "client_credential",app_wx_appid, app_wx_secret_key);byte[] buf = WeixinUtil.httpGet(url);if (buf == null || buf.length == 0) {result.localRetCode = WxLocalRetCode.ERR_HTTP;return result;}String content = new String(buf);result.parseFrom(content);return result;}Step 2: Post the assembled product parameters to the server based on the accesstoken value of step 1.
private class GetPrepayIdTask extends AsyncTask
{@Overrideprotected WxGetPrepayIdResult doInBackground(WxGetAccessTokenResult... params) {WxGetPrepayIdResult result = getPrepayId(params[0]);return result;}@Overrideprotected void onPostExecute(WxGetPrepayIdResult result) {if (result.localRetCode == WxLocalRetCode.ERR_OK) {sendPayReq(result);}}}
Assembly Parameters
private WxGetPrepayIdResult getPrepayId(WxGetAccessTokenResult accessTokenResult) {String url = String.format(api_get_preorder_id,accessTokenResult.accessToken);String entity = appSign.getWxPrepayAppSign();WxGetPrepayIdResult result = new WxGetPrepayIdResult();byte[] buf = WeixinUtil.httpPost(url, entity);if (buf == null || buf.length == 0) {result.localRetCode = WxLocalRetCode.ERR_HTTP;return result;}String content = new String(buf);result.parseFrom(content);return result;}Post to server
private void sendPayReq(WxGetPrepayIdResult result) {PayReq req = new PayReq();req.appId = app_wx_appid;req.partnerId = app_tx_parent_key;req.prepayId = result.prepayId;req.nonceStr = appSign.getNoncestr();req.timeStamp = appSign.getTimestamp();req.packageValue = "Sign=" + appSign.getPackageSign();List
signParams = new LinkedList
();signParams.add(new BasicNameValuePair("appid", req.appId));signParams.add(new BasicNameValuePair("appkey", app_wx_pay_key));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 = WeixinUtil.genSign(signParams);wxRequest.sendReq(req);}
Step 3: Create a package wxapi under the project and create a class named WXPayEntryActivity as the payment result. However, the final result is subject to the returned result of the server, policy_url:
Package net. sourceforge. simcpux. wxapi; public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {@ Overridepublic 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 ("Payment result"); builder. setMessage ("Payment result" + String. valueOf (resp. errCode); builder. show ();}}}========================================================== =For the moment, I did not expect other things to be said. Let's take a look at the effect first.
1. In the package structure, note the class name that receives the returned results;
2. Assemble data. The rules are described in this document.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink">
VcD4KPHByZSBjbGFzcz0 = "brush: java;"> // package _ field generation method // package generation method: //) after all input parameters are sorted in ascending order (lexicographically) according to the field name ASCII code, the format of the URL key-value pair is used (that is, key1 = value1 & key2 = value2 ...) Concatenate the string into string1; // B) obtain the stringSignTemp string by key = partnerKey at the end of string1, perform md5 operations on stringSignTemp, and convert all the characters in the string to uppercase, obtain the signValue of sign. // C) perform urlencode transcoding on the values of all key-value pairs in string1, resplice them into strings according to step a, and get string2. For js frontend programs, you must use the encodeURIComponent function for urlencode encoding (note! During urlencode, convert the space to % 20 instead of + ). // D) concatenate sign = signValue into string1 to obtain the final package string. // App_signature generation method: // A) signature fields include: appid, appkey, noncestr, package, timestamp, and traceid // B) after all parameters to be signed are sorted in ascending order (lexicographically) according to the field name ASCII code, the format of the URL key-value pair is used (that is, key1 = value1 & key2 = value2 ...) Concatenate the string into string1. Note: All parameter names are lowercase characters. // C) use the string1 signature algorithm. Both field names and Field Values use the original values without URL escaping. The specific signature algorithm is sha1.
3. corresponding payment Interface