本人在做项目涉及到微信支付,已经卡住一天了,本人确定使用微信提供的签名获取工具获取到的应用签名与微信开放平台上的app签名是一致的,测试前,是将微信卸载掉重装过的,包括换手机,但是实验结果依旧是支付失败,code= -1。项目中,appId,partnerId,prepayId,nonceStr,sign均由服务端提供,ios版本没有问题,安卓版本则一直返回code=-1 ,求各路高手帮忙出计献策。
代码如下:
public class WechatPay {
private IWXAPI msgApi = null;
private Context mContext;
private PayReq req;
public WechatPay(Context context) {
mContext = context;
req = new PayReq();
}
public void payByWechat(WeChatPayModel weChatPayModel) {
genPayReq(weChatPayModel);
msgApi = WXAPIFactory.createWXAPI(mContext.getApplicationContext(), weChatPayModel.getAppid(), false);
msgApi.registerApp(req.appId);
msgApi.sendReq(req);
}
//WeChatPayModel为本人定义的一个实体类,包括: Appid,MchId,PrepayId,NonceStr,Sign,由服务端提供数据。
private void genPayReq(WeChatPayModel weChatPayModel) {
req.appId = weChatPayModel.getAppid();
req.partnerId = weChatPayModel.getMchId();
req.prepayId = weChatPayModel.getPrepayId();
req.packageValue = “Sign=WXPay”;
req.nonceStr = weChatPayModel.getNonceStr();
req.timeStamp = String.valueOf(genTimeStamp());
req.sign = weChatPayModel.getSign();
}
private long genTimeStamp() {
return System.currentTimeMillis() / 1000;
}
}
代码如下:
public class WechatPay {
private IWXAPI msgApi = null;
private Context mContext;
private PayReq req;
public WechatPay(Context context) {
mContext = context;
req = new PayReq();
}
public void payByWechat(WeChatPayModel weChatPayModel) {
genPayReq(weChatPayModel);
msgApi = WXAPIFactory.createWXAPI(mContext.getApplicationContext(), weChatPayModel.getAppid(), false);
msgApi.registerApp(req.appId);
msgApi.sendReq(req);
}
//WeChatPayModel为本人定义的一个实体类,包括: Appid,MchId,PrepayId,NonceStr,Sign,由服务端提供数据。
private void genPayReq(WeChatPayModel weChatPayModel) {
req.appId = weChatPayModel.getAppid();
req.partnerId = weChatPayModel.getMchId();
req.prepayId = weChatPayModel.getPrepayId();
req.packageValue = “Sign=WXPay”;
req.nonceStr = weChatPayModel.getNonceStr();
req.timeStamp = String.valueOf(genTimeStamp());
req.sign = weChatPayModel.getSign();
}
private long genTimeStamp() {
return System.currentTimeMillis() / 1000;
}
}
解决方案