@Value("${merchant_id}")
private String merchantId;
@Value("${apiclient_key_path}")
private String privateKeyPath;
@Value("${merchant_serial_number}")
private String merchantSerialNumber;
@Value("${wechat_pay_cert_serial_number}")
private String wechatPayCertSerialNumber;
@Value("${api_v3_key}")
private String apiV3Key;
private Verifier verifier;
@PostConstruct
public void init() throws GeneralSecurityException, IOException, HttpCodeException, NotFoundException {
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey("-----BEGIN PRIVATE KEY-----\n" +
这里是我自己配置的私钥,商户的私钥(省略)
"-----END PRIVATE KEY-----\n");
CertificatesManager certificatesManager = CertificatesManager.getInstance();
certificatesManager.putMerchant(merchantId, new WechatPay2Credentials(merchantId,
new PrivateKeySigner(merchantSerialNumber, merchantPrivateKey)),
apiV3Key.getBytes(StandardCharsets.UTF_8));
verifier = certificatesManager.getVerifier(merchantId);
}
public void notificationHandlerTest(String nonce, String timestamp, String signature, String body) throws Exception {
NotificationRequest request = new NotificationRequest.Builder()
.withSerialNumber(wechatPayCertSerialNumber)
.withNonce(nonce)
.withTimestamp(timestamp)
.withSignature(signature)
.withBody(body)
.build();
NotificationHandler handler = new NotificationHandler(verifier, apiV3Key.getBytes(StandardCharsets.UTF_8));
Notification notification = handler.parse(request);
System.out.println(notification.toString());
}
在调用以后就提示以下错误
com.wechat.pay.contrib.apache.httpclient.exception.ValidationException: 验签失败:serial=[4FDB32320031C557EBDE89F20E0F25050846EDBE] message=[1735464766
4FDB32320031C557EBDE89F20E0F25050846EDBE
{"id":"6a817831-4fbd-5805-a6ed-23404f8f612e","create_time":"2024-12-29T17:32:45+08:00","resource_type":"encrypt-resource","event_type":"TRANSACTION.SUCCESS","summary":"支付成功","resource":{"original_type":"transaction","algorithm":"AEAD_AES_256_GCM","ciphertext":"ykhdq6bAoC8pAdYqZD18ZrWXIIGtw0epq314mxCKscNym6CWwzpzNTd5nql2bl2O8PKfyn4Esbp2Gd8Fw8GreJ88GApvVgUWorbP14Th+FCM2MWkIXOmexLcWoqoBYVdpXK7xDQn6VbMMiKWEbnqNAnOxtreaIpuFZD6ZTPV9pnsQcbXGjjTwCYXLQ1sCcmtF+uilU75uTQHrtJfTvJ/sNGObRrKp/yqpDbUU9CimIUG6yPW8MHMmVPP3c9t8ONcbGlqCNAw/zdQJQI6boLyqtMgEO0Yp9CKWogXgXeO5JTAs/TIeusDluMt/JaLBoo8bleMw/YaxibHMwIF2ISCRRYxzMXmJJ6452dubd2q91Q0cV+wHGlnYNt7rfOxGHe/UBcTmxqPuiFSR4mVGzzAuK0HWCJfu2d8rQ3y+puR966Jv2mcjNK4dzYgs0v6+QnBYFUWkrDqxJhcFXx2MH9EtPyAr+CS+pavP5ALBzMMV0mdm5g5HvhuGHUEGdUtcw/wixmIuWe7VoDd5CAWWjmufN6CiMs1DGYob9JjsPOlVtOk13YcK3w6U1XhlKRrmoIY1fvRH6lRq1BKHGM=","associated_data":"transaction","nonce":"SdZ6UszfuvZD"}}
] sign=[SleTeLLBwM86W9B8boODwkOTvy8cX4VugpmxkFrthiTMj08I6Fb4e2c/UB9zklUUEvDnhCrk/MWirEDnu7MKBWCEui5r7oY4uOVoPrEYnOaGaSOnrmlYk+ItSdafseClvFMHnb3avzDoZNG6n1zU2OElxr6NQzWD45PvlReKHwKf+8oVempe4NUxn+OABlAuhfReAInxVOy8P9pXbs74PpFN+KOeE2SCBjKbIzN1oW2nlawGYhtgo5xxuH3hFX6Q+ao4jrU/byzMQ3R5iJqPMgHCywwQGA/Y+f4x8nFy/OSGfQdLKUUoM2xyTzDXUKrneKom6b+mN7iSSsHYfSEOPw==]
我的controller是这样写的
public HashMap<String, Object> receivePayData(HttpServletRequest httpServletRequest, @RequestBody String requestData) throws Exception {
System.out.println("----------verify-----------");
weChatPayNotificationHandler.notificationHandlerTest(
httpServletRequest.getHeader("Wechatpay-Serial"),
httpServletRequest.getHeader("Wechatpay-Timestamp"),
httpServletRequest.getHeader("Wechatpay-Signature"),
requestData);
}
建议用这个sdk https://github.com/wechatpay-apiv3/wechatpay-java