收藏
回答

wx.requestCommonPayment调用报错说是签名错误?这是我的签名代码

public static Map getSignature(WxPayConfig wxPayConfig, WeChatBasePayDTO weChatBasePayDTO) {
       Map payParams = getB2bParams(wxPayConfig, weChatBasePayDTO);
       String postBody = JSONUtil.toJsonStr(payParams);


       Map params = new HashMap<>();
       params.put("singData", postBody);
       String s = calcSignature(postBody, weChatBasePayDTO.getSessionKey());


       params.put("signature", s);
       return params;
    }


    //封装参数
    public static Map getB2bParams(WxPayConfig wxPayConfig, WeChatBasePayDTO basePayData) {


       Map paramsMap = new HashMap<>();


       // 基础信息
       paramsMap.put("mchid", wxPayConfig.getMchId());
       paramsMap.put("env", basePayData.getEnv());
       paramsMap.put("attach","test_attach");
       paramsMap.put("description", basePayData.getDescription());
       paramsMap.put("out_trade_no", basePayData.getOrderNum());


       // 金额信息
       paramsMap.put("amount", buildB2bAmountMap(basePayData));


       return paramsMap;
    }


    /**
     * 构建b2b金额信息Map
     */
    private static Map buildB2bAmountMap(WeChatBasePayDTO basePayData) {
       Map amountMap = new HashMap<>();
       amountMap.put("order_amount", basePayData.getPrice().multiply(new BigDecimal("100")).intValue());
/*     amountMap.put("currency", StringUtils.isNotBlank(basePayData.getCurrency()) ?
             basePayData.getCurrency() : "CNY");*/
       return amountMap;
    }




    /**
     * 计算 signature(用户登录态签名)
     * @param postBody 参与签名的post body(JSON字符串,必须和实际请求完全一致)
     * @param sessionKey 当前用户有效的session_key
     * @return signature 签名字符串(小写16进制)
     */
    public static String calcSignature(String postBody, String sessionKey) {
       try {
          Mac hmacSha256 = Mac.getInstance("HmacSHA256");
          SecretKeySpec keySpec = new SecretKeySpec(sessionKey.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
          hmacSha256.init(keySpec);
          byte[] hash = hmacSha256.doFinal(postBody.getBytes(StandardCharsets.UTF_8));
          return bytesToHex(hash);
       } catch (Exception e) {
          throw new RuntimeException("签名计算失败", e);
       }
    }


/**
 * 字节数组转16进制字符串(小写)
 */
private static String bytesToHex(byte[] bytes) {
    StringBuilder hexString = new StringBuilder();
    for (byte b : bytes) {
       String hex = Integer.toHexString(0xFF & b);
       if (hex.length() == 1) {
          hexString.append('0');
       }
       hexString.append(hex);
    }
    return hexString.toString();
}


{"paySig":"521db8172f08fc1dab65d834863f8b7ff0f9113c966662da9e08d10e9ffc266a","signature":"172032dfac37ee2f0aa6973743acdd871527bb7bc57a6fc7bc4fe020d3100898","singData":"{"amount":{"orderAmount":200,"currency":"CNY"},"mchid":"1722637185","out_trade_no":"202508060077","description":"优香岛味精90g"}"}


现在哪里有问题,哪里有错误呢?有大佬知道的话可以帮我看看吗

最后一次编辑于  2025-08-06
回答关注问题邀请回答
收藏

3 个回答

  • 社区运营-💯
    社区运营-💯
    2025-08-06

    参考QA自助排查:https://docs.qq.com/doc/DQ1dXWWdNYXhJYnpG

    2025-08-06
    有用
    回复 3
    • 愿一切都是梦
      愿一切都是梦
      2025-08-06
      这个地方参数大小写我完全一致,参数也是对的:{\"paySig\":\"222190a1ac5f181e0b950726a426858888caeee89f7850335d9e015e33b166f6\",\"signature\":\"b953d337a6c123030cc004673ac7feae66ac2cb769a1de8c3b7135cbd05f947c\",\"singData\":\"{\\\"amount\\\":{\\\"orderAmount\\\":2,\\\"currency\\\":\\\"CNY\\\"},\\\"mchid\\\":\\\"1722637185\\\",\\\"out_trade_no\\\":\\\"202508060081\\\",\\\"description\\\":\\\"优香岛味精90g\\\"}\"}
      2025-08-06
      回复
    • 愿一切都是梦
      愿一切都是梦
      2025-08-06
      这个文档我看了我参数返回大小写正确,没有空格,uri也是requestCommonPayment,singdata也是这个下面的参数,appkey也是调用api获取的,并且我的代码编写我也通过示例验证了一遍,结果和示例的结果一样,但是实际使用就出错了
      2025-08-06
      回复
    • 愿一切都是梦
      愿一切都是梦
      2025-08-07
      requestCommonPayment&{"amount":{"order_amount":280,"currency":"CNY"},"mchid":"1722637185","out_trade_no":"202508070000","description":"优香岛味精90g","env":0}这个是uri拼接的body参数;appKey我也是通过接口调用获取到的,sessionkey也是没有过期的,到底哪里有问题呢?
      2025-08-07
      回复
  • 刘先生
    刘先生
    01-08

    最后怎么解决

    01-08
    有用
    回复
  • 智能回答 智能回答 本次回答由AI生成
    2025-08-06
    有用
    回复 2
    • 愿一切都是梦
      愿一切都是梦
      2025-08-06
      {"paySig":"521db8172f08fc1dab65d834863f8b7ff0f9113c966662da9e08d10e9ffc266a","signature":"172032dfac37ee2f0aa6973743acdd871527bb7bc57a6fc7bc4fe020d3100898","singData":"{"amount":{"orderAmount":200,"currency":"CNY"},"mchid":"1722637185","out_trade_no":"202508060077","description":"优香岛味精90g"}"}
      2025-08-06
      回复
    • 愿一切都是梦
      愿一切都是梦
      2025-08-06
      这是我的请求参数
      2025-08-06
      回复
登录 后发表内容