最近在帮客户做一个网站,里面新增微信jsapi方式支付,尝试用jsapi前端发起支付时,总是弹出“支付签名验证失败”,多方查找后发现是由于统一下单时的加密方式与前端再次签名时的签名方式不同导致。
找了很多资料,尝试多次后完美解决了,关于微信jsapi签名如下:,
/**
* 构建支付参数,以便前端Js可以jsAPi的唤起微信支付
* @param appId
* @param prepayId
* @param keyPath
* @return
* @throws Exception
*/
public static Map<String, String> buildPayMap(String appId, String prepayId, String keyPath) throws Exception{
String timeStamp = String.valueOf(System.currentTimeMillis() / 1000L);
String nonceStr = String.valueOf(System.currentTimeMillis());
String packageStr = "prepay_id=" + prepayId;
Map<String, String> packageParams = new HashMap(6);
packageParams.put("appId", appId);
packageParams.put("timeStamp", timeStamp);
packageParams.put("nonceStr", nonceStr);
packageParams.put("package", packageStr);
packageParams.put("signType", "RSA");
ArrayList<String> list = new ArrayList();
list.add(appId);
list.add(timeStamp);
list.add(nonceStr);
list.add(packageStr);
String packageSign = CommonUtil.createSign(CommonUtil.buildSignMessage(list), keyPath);
packageParams.put("paySign", packageSign);
return packageParams;
}
详见demo:微信jsapi支付签名demo