首先要去微信开放平台注册账号,开通支付功能,需要注意的是应用签名要是打包时的正式签名。
Paste_Image.png下面开始集成:
去微信的官网下载开发工具包,将工具包中libs目录下的libammsdk.jar复制到你的libs目录下,
在清单文件中添加相关权限,接下来修改wxapi包下的WXPayEntryActivity.class,在该类onCreate()方法中 WXAPIFactory.createWXAPI(this, Constant.APP_ID);替换你的APP_ID。
客户端得到sign的步骤
Paste_Image.png
具体代码如下:
private void wxpay(String appid,String partnerid,String prepay_id,String noncestr,String timestamp) {
PayReq req = new PayReq();
req.appId = appid;
req.partnerId = partnerid;
req.prepayId = prepay_id;
req.nonceStr = noncestr;
req.timeStamp = String.valueOf(genTimeStamp());
req.packageValue = "Sign=WXPay";
List<NameValuePair> signParams = new LinkedList<>();
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);
Log.e(TAG, "wxpay: sign="+sign);
req.extData = "app data"; // optional
api.registerApp(Constant.APP_ID);
api.sendReq(req);
}
// 获取时间戳
private long genTimeStamp() {
return System.currentTimeMillis() / 1000;
}
// 获取签名
private String genAppSign(List<NameValuePair> params) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < params.size(); i++) {
sb.append(params.get(i).getName());
sb.append('=');
sb.append(params.get(i).getValue());
sb.append('&');
}
sb.append("key=");
sb.append(Constant.API_KEY);//秘钥
String appSign = MD5.getMessageDigest(sb.toString().getBytes())
.toUpperCase(); //MD5加密并将小写字母转成大写
return appSign;
}
/**
* MD5加密工具类
*
*/
public class MD5Util {
private static final char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
public static String getMD5(String inStr){
byte[] inStrBytes = inStr.getBytes();
try {
MessageDigest MD = MessageDigest.getInstance("MD5");
MD.update(inStrBytes);
byte[] mdByte = MD.digest();
char[] str = new char[mdByte.length * 2];
int k = 0;
for(int i=0;i<mdByte.length;i++) {
byte temp = mdByte[i];
str[k++] = hexDigits[temp >>> 4 & 0xf];
str[k++] = hexDigits[temp & 0xf];
}
return new String(str);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
最后是支付结果回调,是在微信写好的WXPayEntryActivity.class的onResp()中进行处理的
如下图:
@Override
public void onResp(BaseResp resp) {
if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
String msg="";
if ("0".equals(resp.errCode + "")) {
msg=getString(R.string.pay_result_callback_msg,
"支付成功");
} else if ("-1".equals(resp.errCode + "")) {
msg=getString(R.string.pay_result_callback_msg,
"支付失败");
} else if ("-2".equals(resp.errCode + "")) {
msg=getString(R.string.pay_result_callback_msg,
"用户取消支付");
}
new AlertDialog(this).builder().setMsg(msg)
.setPositiveButton("确定", new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
}).show();
}
}
微信支付需要签名打包之后才能运行,但是可以在build.gradle中android{}里添加如下代码,就可以不打包也能测试支付功能。
signingConfigs {
release {
//.jks文件放在项目目录
storeFile file("***")
storePassword "***"
keyAlias "***"
keyPassword "***"
}
debug {
//.jks文件放在项目目录
storeFile file("***")
storePassword "***"
keyAlias "***"
keyPassword "***"
}
}
网友评论