收藏
回答

微信支付回调使用官方平台下载pub_key验签失败?

AppID
wx3662e3aab9c96fe9

订单支付成功后使用官方的支付demo中相关验签的方法, 最总结果验签失败, 然后尝试自己写验签方式, 结果一样, 具体哪里出了问题,都不确定

public ResponseEntity<String> parseOrderNotifyResult2(@RequestBody String notifyData, HttpServletRequest request) throws IOException {
    SignatureHeader header = getRequestHeader(request);
    log.info("微信支付回调内容 - header: {} - body: {}", JSON.toJSONString(header), notifyData);

    // 构造 RequestParam
    RequestParam requestParam = new RequestParam.Builder()
            .serialNumber(header.getSerial())
            .nonce(header.getNonce())
            .signature(header.getSignature())
            .timestamp(header.getTimeStamp())
            .body(notifyData)
            .build();

    // 如果已经初始化了 NotificationConfig,可直接使用
    // 没有的话,则构造一个。以下多种 Config 根据情况选择一种即可:

    // 1. 如果你使用的是微信支付公私钥,则使用 RSAPublicKeyNotificationConfig
    String path = staticFileReader.getFilePath("cert/pub_key.pem");
    NotificationConfig config = new RSAPublicKeyNotificationConfig.Builder()
            .publicKeyFromPath(path)
            .publicKeyId(wxPayProperties.getPublicKeyId())
            .apiV3Key(wxPayProperties.getApiV3Key())
            .build();


    // 初始化 NotificationParser
    NotificationParser parser = new NotificationParser(config);

    try {
        // 以支付通知回调为例,验签、解密并转换成 Transaction
        Transaction transaction = parser.parse(requestParam, Transaction.class);
        System.out.println(JSON.toJSONString(transaction));
        log.info("支付回调通知处理2 - transaction: {}", JSON.toJSONString(transaction));
        return ResponseEntity.status(200).body("");
    } catch (ValidationException e) {
        e.printStackTrace();
        // 签名验证失败,返回 401 UNAUTHORIZED 状态码
        log.error("sign verification failed", e);
        return ResponseEntity.status(200).body("");
    }
}
回答关注问题邀请回答
收藏

1 个回答

  • 智能回答 智能回答 本次回答由AI生成
    2025-10-28
    有用
    回复 1
    • 哎哟喂🐱
      哎哟喂🐱
      2025-10-28
      4200002897202510287420248866
      2025-10-28
      回复
登录 后发表内容