转自:http://www.2cto.com/kf/201409/330136.html
准备:
1,导入微信的libs包libammsdk.jar;
2,测试时使用weixinDemo中的debug_keystore;
3,需要注意应用要通过审核,并且几个Key值正确,一下为微信支付Demo中的值:
1
2
3
4
5
6
7
8
9
10
|
//微信公众平台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值第二步要用;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
private
class
GetAccessTokenTask
extends
AsyncTask<
void
,
void
,=
""
wxgetaccesstokenresult=
""
> {
@Override
protected
WxGetAccessTokenResult doInBackground(Void... params) {
WxGetAccessTokenResult result = getAccessToken();
return
result;
}
@Override
protected
void
onPostExecute(WxGetAccessTokenResult result) {
if
(result.localRetCode == WxLocalRetCode.ERR_OK) {
GetPrepayIdTask getPrepayId =
new
GetPrepayIdTask();
getPrepayId.execute(result);
}
}
}</
void
,>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
private
class
GetPrepayIdTask
extends
AsyncTask<wxgetaccesstokenresult,
void
,=
""
wxgetprepayidresult=
""
> {
@Override
protected
WxGetPrepayIdResult doInBackground(WxGetAccessTokenResult... params) {
WxGetPrepayIdResult result = getPrepayId(params[
0
]);
return
result;
}
@Override
protected
void
onPostExecute(WxGetPrepayIdResult result) {
if
(result.localRetCode == WxLocalRetCode.ERR_OK) {
sendPayReq(result);
}
}
}</wxgetaccesstokenresult,>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<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);
}</namevaluepair></namevaluepair>
|
1
|
|
1
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package
net.sourceforge.simcpux.wxapi;
public
class
WXPayEntryActivity
extends
Activity
implements
IWXAPIEventHandler{<pre name=
"code"
class
=
"java"
><span style=
"white-space:pre"
> </span>
@Override
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(
"支付结果"
);
builder.setMessage(
"支付结果"
+String.valueOf(resp.errCode));
builder.show();
}
}</pre>}
<p></p>
<pre
class
=
"brush:java;"
></pre>
==========================================
<p></p>
<p>暂时没想到其他想说的,先看个效果</p>
<p>
1
,包结构,需要注意的就是接收微信返回结果的那个类名;</p>
<p><img src=
"http://www.2cto.com/uploadfile/Collfiles/20140901/201409010901522.png"
alt=
"\" style="
display: inline; width: 239px; height: 238px;"></p>
<p>
2
,组装数据,规则在文档中有说明<喎�
"http://www.2cto.com/kf/ware/vc/"
target=
"_blank"
class
=
"keylink"
>vcD4KPHByZSBjbGFzcz0=
"brush:java;"
>//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</p>
|
![\](http://www.2cto.com/uploadfile/Collfiles/20140901/201409010901523.png)
3,对应的支付界面
![](http://www.2cto.com/uploadfile/Collfiles/20140901/201409010901525.png)