密钥
String PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----\n" +
"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC22GDMFODt5jzI\n" +
"qeZ1Lk5rBIySzBmAbL2PH5k=\n" +
"-----END PRIVATE KEY-----";
签名方法
public static String sign(byte[] message) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException, UnsupportedEncodingException {
PrivateKey privateKey = PemUtil.loadPrivateKey(new ByteArrayInputStream(WxPayConstants.PRIVATE_KEY.getBytes("utf-8")));
Signature sign = Signature.getInstance("SHA256withRSA");
sign.initSign(privateKey);
sign.update(message);
return Base64.getEncoder().encodeToString(sign.sign());
}
调用方式
String timestamp = System.currentTimeMillis() / 1000 + "";
String nonce = RandomUtil.randomString(32);
StringBuilder builder = new StringBuilder();
builder.append(WxPayConstants.APP_ID).append("\n");
builder.append(timestamp).append("\n");
builder.append(nonce).append("\n");
JsonNode jsonNode = objectMapper.readTree(bodyAsString);
String prepayId = jsonNode.get("prepay_id").asText();
builder.append(prepayId).append("\n");
String sign = sign(builder.toString().getBytes(StandardCharsets.UTF_8));
这样子生成的签名在前端无法调起微信支付,这个签名哪里出现了问题
检查一下实际调用参数类型、大小写是否正确,计算签名参数与调起支付时的是否一致