收藏
回答

Native支付通知验签失败,怎么解决?

错误描述

Native支付通知验签失败

运行环境

java:JDK8

示例代码

public String wechatChannelPayNotifyV3(HttpServletRequest request, HttpServletResponse response) throws Exception {
    //获取微信POST过来反馈信息
    BufferedReader bis = new BufferedReader(new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
    String line = null;
    String body = "";
    try {
        while ((line = bis.readLine()) != null) {
            result += line + "\r\n";
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        bis.close();
    }

    //随机串
    String nonceStr = request.getHeader("wechatpay-nonce");
    //微信传递过来的签名
    String signature = request.getHeader("wechatpay-signature");
    //证书序列号(微信平台)
    String serialNo = request.getHeader("wechatpay-serial");
    //时间戳
    String timestamp = request.getHeader("wechatpay-timestamp");

    try {
        // 构造 RequestParam
        RequestParam requestParam = new RequestParam.Builder()
                .serialNumber(serialNo)
                .nonce(nonceStr)
                .signature(signature)
                .timestamp(timestamp)
                .body(body)
                .build();
        // 如果已经初始化了 RSAAutoCertificateConfig,可以直接使用  config
        // 初始化 NotificationParser
        Config config = new RSAAutoCertificateConfig.Builder()
                .merchantId(config.getMchId())
                .privateKeyFromPath(config.getPrivateKeyStr())
                .merchantSerialNumber(config.getPrivateKeyPwd())
                .apiV3Key(config.getSecretKey())
                .build();
        NotificationParser parser = new NotificationParser((NotificationConfig) config);
        // 验签、解密并转换成 Transaction
        Transaction transaction = parser.parse(requestParam, Transaction.class);
        logger.info("transaction=" + JSON.toJSONString(transaction));
        //记录日志信息
        Transaction.TradeStateEnum state = transaction.getTradeState();
        if (!StringUtils.equals("SUCCESS", state.toString())) {
            //通知微信回调失败
            response.getWriter().write("<xml><return_code><![CDATA[FAIL]]></return_code></xml>");
        }
        //通知微信回调成功
        response.getWriter().write("<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>");
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        response.getWriter().write("<xml><return_code><![CDATA[FAIL]]></return_code></xml>");
    }
    return "";

异常信息

Processing WechatPay notification,signature verification failed,signType[WECHATPAY2-SHA256-RSA2048]
回答关注问题邀请回答
收藏

1 个回答

  • Memory
    Memory
    1天前

    验证签名你要用原参,不能修改

    1天前
    有用
    回复 2
    • 陈育秀
      陈育秀
      发表于移动端
      1天前
      你好,我这边没有进行修改,直接从request上取出来的
      1天前
      回复
    • Memory
      Memory
      1天前回复陈育秀
      你换行干啥?
      1天前
      回复
登录 后发表内容