使用环境:
需要微信支付的地方。
步骤:
1 copy支付的依赖到项目
implementation 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
2 按照微信的文档指示,在项目包下写一个包名为wxapi,类名为WXPayEntryActivity
类照搬文档的就可以:
书写位置:
copy类
至于xml文件,随便写,不重要,这个类不会显示的。
package com.higo.wxapi;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.higo.R;
import com.higo.entity.Constants;
import com.higo.ui.activity.BaseActivity;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
/**
* Created by SensYang on 2017/09/05 17:18
*/
public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {
private IWXAPI api;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wx_pay_result);
api = WXAPIFactory.createWXAPI(this, Constants.APP_ID_WX);
api.handleIntent(getIntent(), this);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
api.handleIntent(intent, this);
}
@Override
public void onReq(BaseReq req) {
}
@Override
public void onResp(BaseResp resp) {
if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
if (resp.errCode == -1) {
Toast.makeText(WXPayEntryActivity.this, "支付失败,请确认您的手机微信登录正常", Toast.LENGTH_SHORT).show();
finishPayAct();
} else if (resp.errCode == -2) {
Toast.makeText(WXPayEntryActivity.this, "取消支付", Toast.LENGTH_SHORT).show();
finishPayAct();
} else if (resp.errCode == 0) {
Toast.makeText(WXPayEntryActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
finishPayAct();
}
}
}
/**
* 做一个方法返回开始支付的界面,并关闭它
*/
public void finishPayAct() {
finish();
}
}
3 copy支付工具类,位置随便写
public class WXPayUtils {
private IWXAPI iwxapi;
private WXPayBuilder builder;
private WXPayUtils(WXPayBuilder builder) {
this.builder = builder;
}
/**
* 调起微信支付的方法,不需要在客户端签名
**/
public void toWXPayNotSign(Context context) {
//初始化微信api
iwxapi = WXAPIFactory.createWXAPI(context, null);
//注册appid appid可以在开发平台获取
iwxapi.registerApp(builder.getAppId());
//这里注意要放在子线程
Runnable payRunnable = new Runnable() {
@Override
public void run() {
//调起微信APP的对象
PayReq request = new PayReq();
//下面是设置必要的参数,也就是前面说的参数,这几个参数从何而来请看上面说明
request.appId = builder.getAppId();
request.partnerId = builder.getPartnerId();
request.prepayId = builder.getPrepayId();
request.packageValue = builder.getPackageValue();
request.nonceStr = builder.getNonceStr();
request.timeStamp =builder.getTimeStamp();
request.sign = builder.getSign();
//发送调起微信的请求
iwxapi.sendReq(request);
}
};
Thread payThread = new Thread(payRunnable);
payThread.start();
}
/**
* 调起微信支付的方法,需要在客户端签名
**/
public void toWXPayAndSign(Context context, String appid, final String key) {
//初始化微信api
iwxapi = WXAPIFactory.createWXAPI(context, null);
//注册appid appid可以在开发平台获取
iwxapi.registerApp(appid);
//这里注意要放在子线程
Runnable payRunnable = new Runnable() {
@Override
public void run() {
//调起微信APP的对象
PayReq request = new PayReq();
//下面是设置必要的参数,也就是前面说的参数,这几个参数从何而来请看上面说明
request.appId = builder.getAppId();
request.partnerId = builder.getPartnerId();
request.prepayId = builder.getPrepayId();
request.packageValue = "Sign=WXPay";
request.nonceStr = builder.getNonceStr();
request.timeStamp = builder.getTimeStamp();
request.sign = builder.getSign();
//签名
LinkedHashMap signParams = new LinkedHashMap<>();
signParams.put("appid", request.appId);
signParams.put("noncestr", request.nonceStr);
signParams.put("package", request.packageValue);
signParams.put("partnerid", request.partnerId);
signParams.put("prepayid", request.prepayId);
signParams.put("timestamp", request.timeStamp);
request.sign = genPackageSign(signParams, key);
//发送调起微信的请求
iwxapi.sendReq(request);
}
};
Thread payThread = new Thread(payRunnable);
payThread.start();
}
/**
* 调起微信APP支付,签名
* 生成签名
*/
private String genPackageSign(LinkedHashMap params, String key) {
StringBuilder sb = new StringBuilder();
for (Map.Entry entry : params.entrySet()) {
sb.append(entry.getKey());
sb.append('=');
sb.append(entry.getValue());
sb.append('&');
}
sb.append("key=");
sb.append(key);
String packageSign = getMessageDigest(sb.toString().getBytes()).toUpperCase();
return packageSign;
}
/**
* md5加密
*
* @param buffer
* @return
*/
private String getMessageDigest(byte[] buffer) {
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
try {
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(buffer);
byte[] md = mdTemp.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
return null;
}
}
public static class WXPayBuilder {
String appId;
String partnerId;
String prepayId;
String packageValue;
String nonceStr;
String timeStamp;
String sign;
public WXPayUtils build() {
return new WXPayUtils(this);
}
public String getAppId() {
return appId;
}
public WXPayBuilder setAppId(String appId) {
this.appId = appId;
return this;
}
public String getPartnerId() {
return partnerId;
}
public WXPayBuilder setPartnerId(String partnerId) {
this.partnerId = partnerId;
return this;
}
public String getPrepayId() {
return prepayId;
}
public WXPayBuilder setPrepayId(String prepayId) {
this.prepayId = prepayId;
return this;
}
public String getPackageValue() {
return packageValue;
}
public WXPayBuilder setPackageValue(String packageValue) {
this.packageValue = packageValue;
return this;
}
public String getNonceStr() {
return nonceStr;
}
public WXPayBuilder setNonceStr(String nonceStr) {
this.nonceStr = nonceStr;
return this;
}
public String getTimeStamp() {
return timeStamp;
}
public WXPayBuilder setTimeStamp(String timeStamp) {
this.timeStamp = timeStamp;
return this;
}
public String getSign() {
return sign;
}
public WXPayBuilder setSign(String sign) {
this.sign = sign;
return this;
}
}
}
4 联网,接收返回值,并处理微信支付问题:
微信支付的APPID 和APPKEY 大致样式如下
吊起支付代码:
//在服务端签名
findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//假装请求了服务器 获取到了所有的数据,注意参数不能少
WXPayUtils.WXPayBuilder builder = new WXPayUtils.WXPayBuilder();
builder.setAppId(APPID)
.setPartnerId("1900006771")
.setPrepayId("wx3014491958518907422718666969545432")
.setPackageValue("Sign=WXPay")
.setNonceStr("51c1e88abe5a280d2b59174f10a73caa")
.setTimeStamp("1538290159")
.setSign("811FA6D2811765399147B376B6950BF4")
.build().toWXPayNotSign(MainActivity.this);
}
});
//在客户端签名
findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//假装请求了服务端信息,并获取了appid、partnerId、prepayId,注意参数不能少
WXPayUtils.WXPayBuilder builder = new WXPayUtils.WXPayBuilder();
builder.setAppId(APPID)
.setPartnerId("1900006771")
.setPrepayId("wx3014491958518907422718666969545432")
.setPackageValue("Sign=WXPay")
.build()
.toWXPayAndSign(MainActivity.this,"微信支付的APPID","微信支付的Appkey");
}
});
先声明下,在服务器端签名的方法可以直接使用,但是在客户端签名需要使用APPkey我们没有可以测试的。
demo地址:https://gitee.com/longlyblack/WXPayUtils-master
demo是借鉴人家的,做了一点点修改而已,在此谢过。
有任何支付的问题请联系
QQ群: 88627109