微信支付相信大家都接过,但是做过中间件的人来讲微信就比较坑了,因为微信支付的时候会去判断包名是否和平台上面的一致,现在SDK的解决方案只有两种,一种是用H5来唤起微信支付,一种是插件形式,但今天写的只是简单的唤起微信支付,至于SDK包名问题就有时间再写了。
首先我们要接入微信支付先要去微信的开放平台去申请对应的参数,包名和签名要和微信的一致,不然会唤不起微信支付的,至于这步就跳过了(一般都是专门负责去申请的),直接上教程。
1、把开发工具包中libs目录下的libammsdk.jar复制到该目录中
2、需要在AndroidManifest.xml 配置权限
3、在你的包名相应目录下新建一个wxapi目录,并在该wxapi目录下新增一个WXEntryActivity(没有什么特别需要这个直接在文档复制过去就好了)假如你的包名是:com.Android.test,新建后com.android.text.wxapi,在wxapi新建一个WXEntryActivity(我是直接copy过去的),包名必须是wxapi
4、在你的AndroidManifest.xml 配置
android:exported="true"
android:launchMode="singleTop"
/>
5、微信需要的参数(后台返回)
ps: 我是使用okHttp3框架访问后台的
客户端代码:
// PAY_NET_URL 服务端地址
// 将参数发送到服务端 (要什么参数需要和后台沟通下,这里不是一定的)
RequestCall requestCall = OkHttpUtils.get().url(Constants.PAY_NET_URL)
.addParams("userID", uid)
.addParams("appID", appid)
.addParams("productID", productID + "")
.addParams("productName", productName)
.addParams("productDesc", productDesc)
.addParams("money", price)
.addParams("roleID", roleID)
.addParams("roleName", roleName)
.addParams("serverID", serverId)
.addParams("serverName", serverName)
.addParams("payID", "" + 2)
.addParams("orderId", orderId)
.addParams("notifyUrl", payNotifyURL)
.build();
requestCall.execute(new StringCallback() {
@Override
public void onError(Call call, Exception e, int i) {
Log.e("requestCall is onError", e.getMessage());
Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
}
// 服务端返回过来的数据用来唤起微信支付
@Override
public void onResponse(String jsonObject, int i) {
if (jsonObject != null) {
try {
// 这里使用Gson解析
WeChatJson json = new Gson().fromJson(jsonObject, WeChatJson.class);
if (json.getData() != null && json.getData().getExtension() != null) {
WeChatJson.DataBean.ExtensionBean extensionBean = json.getData().getExtension();
// 我把注册到微信放在这里
api.registerApp(extensionBean.getAppid());
PayReq request = new PayReq();
request.appId = extensionBean.getAppid();
request.partnerId = extensionBean.getPartnerid();
request.prepayId = extensionBean.getPrepayid();
request.packageValue = extensionBean.getPackageX();
request.nonceStr = extensionBean.getNoncestr();
request.timeStamp = extensionBean.getTimestamp();
request.sign = extensionBean.getSign();
// 唤起微信支付
api.sendReq(request);
}
} catch (Exception e) {
Toast.makeText(mContext,"订单获取失败",Toast.LENGTH_SHORT).show();
processTip.hide();
e.printStackTrace();
}finally {
}
}else {
}
}
});
WeChatJson.java
public class WeChatJson {
private DataBean data;
private int state;
public DataBean getData() {
return data;
}
public void setData(DataBean data) {
this.data = data;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public static class DataBean {
private ExtensionBean extension;
private long orderID;
public String getAppID() {
return appID;
}
public void setAppID(String appID) {
this.appID = appID;
}
private String appID;
public ExtensionBean getExtension() {
return extension;
}
public void setExtension(ExtensionBean extension) {
this.extension = extension;
}
public long getOrderID() {
return orderID;
}
public void setOrderID(long orderID) {
this.orderID = orderID;
}
public static class ExtensionBean {
@SerializedName("package")
private String packageX;
private String appid;
private String sign;
private String partnerid;
private String prepayid;
private String noncestr;
private String timestamp;
public String getPackageX() {
return packageX;
}
public void setPackageX(String packageX) {
this.packageX = packageX;
}
public String getAppid() {
return appid;
}
public void setAppid(String appid) {
this.appid = appid;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
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 getNoncestr() {
return noncestr;
}
public void setNoncestr(String noncestr) {
this.noncestr = noncestr;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}
}
}
到这里就结束了,如果唤不起微信支付,请检查请求参数和包名是否有误。