这两天APP微信支付后端代码更换为微信官方的java版SDK(java_sdk_v3.0.9),更换之后APP端发起支付提示支付签名有问题,最后查看SDK源码发现,原来签名方式有问题,记得以前公司老代码加密方式都是MD5,一直是没问题的。官方的SDK只有在沙盒环境下才是MD5加密方式,最后不得不修改SDK源码,手动设置签名方式为MD5。异步回调验签方法默认是MD5的。
com.github.wxpay.sdk.WXPay类中加了两个重载构造方法:
// 原有的构造方法
public WXPay(final WXPayConfig config) throws Exception {
this(config, null, true, false);
}
// 新增的构造方法
public WXPay(final WXPayConfig config, SignType signType) throws Exception {
this(config, null, true, false, signType);
}
// 原有的构造方法
public WXPay(final WXPayConfig config, final String notifyUrl,
final boolean autoReport, final boolean useSandbox)
throws Exception {
this.config = config;
this.notifyUrl = notifyUrl;
this.autoReport = autoReport;
this.useSandbox = useSandbox;
if (useSandbox) {
this.signType = SignType.MD5; // 沙箱环境
} else {
this.signType = SignType.HMACSHA256;
}
this.wxPayRequest = new WXPayRequest(config);
}
// 新增的构造方法
public WXPay(final WXPayConfig config, final String notifyUrl,
final boolean autoReport, final boolean useSandbox,
SignType signType) throws Exception {
this.config = config;
this.notifyUrl = notifyUrl;
this.autoReport = autoReport;
this.useSandbox = useSandbox;
this.signType = signType;
this.wxPayRequest = new WXPayRequest(config);
}
/**
* APP,统一下单,获取预支付ID
*
* https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1
*
* @param outTradeNo
* @param totalFee
* 元
* @param notifyUrl
* @param ipaddress
* @return
*/
public String getPrepayId(WXPay wxpay, String outTradeNo, String totalFee,
String notifyUrl, String ipaddress) {
String totalFeeFen = AmountUtils.changeY2F(totalFee);
String prepayId = null;
Map data = new HashMap();
data.put("body", outTradeNo);
data.put("out_trade_no", outTradeNo);
data.put("total_fee", totalFeeFen);
data.put("spbill_create_ip", ipaddress);
data.put("notify_url", notifyUrl);
data.put("trade_type", TradeTypeEnum.APP.name());
try {
Map resp = wxpay.unifiedOrder(data);
if (ReturnCodeEnum.SUCCESS.name().equals(resp.get("return_code"))) {
prepayId = resp.get("prepay_id");
} else {
System.err.println("微信统一下单失败:");
System.err.println(resp);
}
} catch (Exception e) {
System.err.println("微信统一下单异常!");
e.printStackTrace();
}
return prepayId;
}
/**
* 调起支付接口
*
* https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_12&index=2
*
* @param outTradeNo
* @param totalFee
* 人民币 CNY 元
* @param notifyUrl
* @param ipaddress
* @return
* @throws Exception
*/
public Record requestWxpay(String outTradeNo, String totalFee,
String notifyUrl, String ipaddress) throws Exception {
Record payData = null;
AppConfig config = AppConfig.getInstance();
WXPay wxpay = new WXPay(config, SignType.MD5);//指定签名方式为MD5!!!!!!!!!!!!!!
String prepayId = getPrepayId(wxpay, outTradeNo, totalFee, notifyUrl,
ipaddress);
if (StringUtils.isNotBlank(prepayId)) {
Map params = new HashMap();
String appid = config.getAppID();
params.put("appid", appid);
params.put("noncestr", WXPayUtil.generateNonceStr());
params.put("package", "Sign=WXPay");
params.put("partnerid", config.getMchID());
params.put("prepayid", prepayId);
params.put("timestamp", config.getTimestamp());
String sign = WXPayUtil.generateSignature(params, config.getKey(),
SignType.MD5);//指定签名方式为MD5!!!!!!!!!!!!!!
params.put("sign", sign);
payData = new Record();
payData.set("data", params);
}
return payData;
}