1、接入前准备,需要在微信开放平台创建应用
https://open.weixin.qq.com
a.创建应用;
b.提交审核;
c.开通支付能力;
支付流程
2、sdk接入
文档:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5
安全码策略:包名 + 应用名称 + keystore,任何一个参数变化,支付失败
具体的依赖在下图中的位置
依赖根据自己的需求添加两者中的一个就可以
但是文档中的依赖后面是一个 + 号,意思就是每次都会下载最新的依赖,这样是不行的,如果最新的依赖有bug怎么办,所以点击上图中的 Android开发工具包,进去后会看到最新的SDK的版本,然后直接把依赖改成最新的版本就行,不要使用文档中的 + 号,版本号在下图中:
把依赖改成如下就行:
implementation 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:5.3.1'
3、代码操作
1、添加权限
具体的权限和支付宝一样,上篇简书有
2、包名目录下新建一个 wxapi 目录,将微信官方 demo 中的 WXPayEntryActivity 拷贝出来放到 wxapi 目录下,此类用来处理支付结果,主要在 onResp(BaseResp resp) 方法中处理
WXPayEntryActivity
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
private IWXAPI api;
private TextView mTv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pay_result);
//调用API前,需要先向微信注册您的APPID
api = WXAPIFactory.createWXAPI(this, null);
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) {
System.out.println(Thread.currentThread().getName());
if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
int errCode = resp.errCode;
if (errCode == 0){
}else if (errCode == -1){
//ToastUtils.showLong("支付失败");
}else if (errCode == -2){
//ToastUtils.showLong("支付已被取消");
}
}
Log.d("Result:", "errCode: "+resp.errCode+",type"+resp.getType());
//finish();
}
}
里面需要的 pay_result 直接 Alt + Enter 生成就行,里面啥也没有
然后在清单文件中配置 WXPayEntryActivity
<activity
android:name=".wxapi.WXPayEntryActivity"
android:exported="true"
android:launchMode="singleTop"/>
3、请求网络数据,和支付宝一样,只不过接口换成了后台提供的微信的接口
private void wxPay() {
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
StringRequest request = new StringRequest(Request.Method.POST, mUrl, this, this) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
ArrayMap<String, String> map = new ArrayMap<>();
map.put("amount", "1.0");
map.put("token", "mKVoQRrXlMvmfWbgWzyDYHF1KOUN5e4bwWrOiab9tZ8sWIQAZmg48lSa2NOORqpko6V0l4cYCs1Uip23M6MglE6Xp6LvHKO9RUNP4m8pHjp1zHb8nmK1bU1pvRp2LFkwA");
map.put("version", "3.3.3");
map.put("os", "android");
return map;
}
};
requestQueue.add(request);
}
@Override
public void onErrorResponse(VolleyError volleyError) {
}
@Override
public void onResponse(String string) {
//2.解析自己家服务器返回的的数据,拿到支付串码
Gson gson = new Gson();
WxPayBean wxPayBean = new Gson().fromJson(string, WxPayBean.class);
//3.调用支付的sdk,拉起支付界面
callWxSdk(wxPayBean);
}
private void callWxSdk(WxPayBean wxPayBean) {
final IWXAPI msgApi = WXAPIFactory.createWXAPI(this, null);
// 将该appid注册到微信
WxPayBean.ResultBean.WxBean wx = wxPayBean.getResult().getWx();
msgApi.registerApp(wx.getAppid());
//调起支付
PayReq request = new PayReq();
request.appId = wx.getAppid();
request.partnerId = wx.getPartnerid();
request.prepayId= wx.getPrepayid();
request.packageValue = wx.getPackageX();
request.nonceStr= wx.getNoncestr();
request.timeStamp= wx.getTimestamp();
request.sign= wx.getSign();
msgApi.sendReq(request);
//4.处理支付宝app返回的支付结果,在WXPayEntryActivity中处理支付结果
}
4、WxPayBean(bean类)
public class WxPayBean {
/**
* code : 0
* desc :
* result : {"id":10577,"wx":{"appid":"wx2281f6dda3069029","noncestr":"eby1bE4coKXyd7fKHMZlEu423yY8bkTl","package":"Sign=WXPay","partnerid":"1462681502","prepayid":"wx04145641830624444bbcb84c1596838800","timestamp":"1559631401","sign":"55F0087367596D6D76B6CFDB2A8B463D"}}
*/
private int code;
private String desc;
private ResultBean result;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public ResultBean getResult() {
return result;
}
public void setResult(ResultBean result) {
this.result = result;
}
public static class ResultBean {
/**
* id : 10577
* wx : {"appid":"wx2281f6dda3069029","noncestr":"eby1bE4coKXyd7fKHMZlEu423yY8bkTl","package":"Sign=WXPay","partnerid":"1462681502","prepayid":"wx04145641830624444bbcb84c1596838800","timestamp":"1559631401","sign":"55F0087367596D6D76B6CFDB2A8B463D"}
*/
private int id;
private WxBean wx;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public WxBean getWx() {
return wx;
}
public void setWx(WxBean wx) {
this.wx = wx;
}
public static class WxBean {
/**
* appid : wx2281f6dda3069029
* noncestr : eby1bE4coKXyd7fKHMZlEu423yY8bkTl
* package : Sign=WXPay
* partnerid : 1462681502
* prepayid : wx04145641830624444bbcb84c1596838800
* timestamp : 1559631401
* sign : 55F0087367596D6D76B6CFDB2A8B463D
*/
private String appid;
private String noncestr;
@SerializedName("package")
private String packageX;
private String partnerid;
private String prepayid;
private String timestamp;
private String sign;
public String getAppid() {
return appid;
}
public void setAppid(String appid) {
this.appid = appid;
}
public String getNoncestr() {
return noncestr;
}
public void setNoncestr(String noncestr) {
this.noncestr = noncestr;
}
public String getPackageX() {
return packageX;
}
public void setPackageX(String packageX) {
this.packageX = packageX;
}
public String getPartnerid() {
return partnerid;
}
public void setPartnerid(String partnerid) {
this.partnerid = partnerid;
}
public String getPrepayid() {
return prepayid;
}
public void setPrepayid(String prepayid) {
this.prepayid = prepayid;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
}
}
}
完成之后开启的是一个空白页面,但这已经意味着成功了,因为我们的应用没有提交审核,这篇简书只是个demo