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"}"}
现在哪里有问题,哪里有错误呢?有大佬知道的话可以帮我看看吗

参考QA自助排查:https://docs.qq.com/doc/DQ1dXWWdNYXhJYnpG
最后怎么解决