原文地址:http://blog.csdn.net/intbird

微信官方文档地址:

t=resource/res_main_tmpl&verify=1&lang=zh_CN" style="font-size:32px">https://open.weixin.qq.com/cgi-bin/frame?t=resource/res_main_tmpl&verify=1&lang=zh_CN


微信官方Demo:http://download.csdn.net/detail/intbird/8476901

@与server交互通信调用,android端仅仅需获取server參数填入微信.

==========================================

1,开启

android微信付费_第1张图片

2,结果(支付和微信分享的Activity要安装api文档上说明的放到指定的包名内),支付结果最好要通知一下server.

android微信付费_第2张图片

3,演示样例

公布使用的签名正确的话取消和成功都能正确通知.

android微信付费_第3张图片


==========================================

@自己測试调用:Android自己请求微信server,一般不会这样搞,仅仅是练习须要这样...

1,导入微信的libs包libammsdk.jar;

2,測试时使用weixinDemo中的debug_keystore;

3,须要注意应用要通过审核,而且几个Key值正确,一下为微信支付Demo中的值:

//微信公众平台id;private String app_wx_appid=WxConstants.app_wx_appid;//微信开放平台和商户约定的密钥private String app_wx_secret_key="db426a9829e4b49a0dcac7b4162da6b6";//微信公众平台商户模块和商户约定的密钥private String app_wx_parent_key="8934e7d15453e97507ef794cf7b0519d";//微信公众平台商户模块和商户约定的支付密钥private String app_wx_pay_key="L8LrMqqeGRxST5reouB0K66CaYAWpqhAVsq7ggKkxHCOastWksvuX1uvmvQclxaHoYd3ElNBrNO2DHnnzgfVG9Qs473M3DTOZug5er46FhuGofumV8H2FVR9qkjSlC5K";// 商家向財付通申请的商家id */private String app_tx_parent_key = "1900000109";    

==========================================

依据微信支付Demo,微信支付分为三步:

第一步,获取accessToken,accessToken值第二步要用;

private class GetAccessTokenTask extends AsyncTask<Void, Void, WxGetAccessTokenResult> {@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);}}}
解析server响应
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;}
第二步,依据第一步的accesstoken值,将 组装的商品參数Post给微信server

private class GetPrepayIdTask extends AsyncTask<WxGetAccessTokenResult, Void, WxGetPrepayIdResult> {@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);}}}
组装參数

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给server
<span style="white-space:pre"></span>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<NameValuePair> signParams = new LinkedList<NameValuePair>();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);}
  

  第三步:在项目下新建一个包wxapi,建立一个类名为WXPayEntryActivity作为接受微信的支付结果,只是终于结果以server的返回为准notify_url:  
package net.sourceforge.simcpux.wxapi;public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{<pre name="code" class="java"><span style="white-space:pre"></span>@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("支付结果");builder.setMessage("支付结果"+String.valueOf(resp.errCode));builder.show();}}
}

  ==========================================  

临时没想到其它想说的,先看个效果

1,包结构,须要注意的就是接收微信返回结果的那个类名;

android微信付费_第4张图片

2,组装数据,规则在文档中有说明

//package_ 字段生成方法//package生成方法://A)对全部传入參数依照字段名的ASCII 码从小到大排序(字典序)后,使用URL 键值对的格式(即key1=value1&key2=value2…)拼接成字符串string1;//B) 在string1 最后拼接上key=partnerKey 得到stringSignTemp 字符串。 并对 stringSignTemp进行md5 运算,再将得到的字符串全部字符转换为大写,得到sign值signValue。

//C)对string1 中的全部键值对中的value 进行urlencode 转码,依照a 步骤又一次拼接成字符串。得到string2。对于js 前端程序。一定要使用函数encodeURIComponent 进行urlencode编码(注意!进行urlencode时要将空格转化为%20而不是+)。//D)将sign=signValue 拼接到string1 后面得到终于的package 字符串。

//app_signature生成方法://A)參与签名的字段包含:appid、appkey、noncestr、package、timestamp以及 traceid //B)对全部待签名參数依照字段名的ASCII 码从小到大排序(字典序)后,使用URL 键值对的格式(即key1=value1&key2=value2…)拼接成字符串string1。 注意:全部參数名均为小写字符//C)对string1 作签名算法,字段名和字段值都採用原始值。不进行URL 转义。详细签名算法为SHA1

android微信付费_第5张图片
3,相应的支付界面

android微信付费_第6张图片

android微信付费_第7张图片


更多相关文章

  1. Android读取服务器图片的三种方法
  2. android制作圆角图片和图片倒影
  3. android 获取本地图片或音乐
  4. android传递图片和图片与byte转换
  5. android调用系统摄像头拍照图片和视频
  6. android数据库中存取图片
  7. 【TabHost】Android设置TabHost文字及图片

随机推荐

  1. 如何在苹果Mac“语音备忘录”中播放录音?
  2. AI手机产品化实践与思考
  3. 网易严选全链路市场投放的数据产品策略
  4. ansible
  5. Docker基本组件、概念介绍
  6. 金融资管数据中台体系探索实践
  7. 数据中心100G时代来临!Virtex UltraScale+
  8. 网站CDN加速是什么? 看完这篇你就明白了!
  9. 高性能图表控件LightningChart中有关用分
  10. 调试硬件BUG的神器——新型逻辑分析仪