Skip to content

fix: 小程序虚拟支付用户态签名 calcSig 未转小写导致 SIGNATURE_INVALID#3938

Merged
binarywang merged 2 commits intodevelopfrom
copilot/fix-signature-case-issue
Mar 21, 2026
Merged

fix: 小程序虚拟支付用户态签名 calcSig 未转小写导致 SIGNATURE_INVALID#3938
binarywang merged 2 commits intodevelopfrom
copilot/fix-signature-case-issue

Conversation

Copy link
Contributor

Copilot AI commented Mar 20, 2026

WxMaXPaySigParams.calcSig() 返回的 HMAC-SHA256 签名未转小写,而微信服务端要求签名为全小写十六进制字符串,导致报错 SIGNATURE_INVALID -15005

变更

  • 修复 calcSig() 返回值添加 .toLowerCase(),与同类 calcPaySig() 行为对齐:
// Before
public String calcSig(String postBody) {
    String sk = StringUtils.trimToEmpty(this.sessionKey);
    return calcSignature(postBody, sk);
}

// After
public String calcSig(String postBody) {
    String sk = StringUtils.trimToEmpty(this.sessionKey);
    return calcSignature(postBody, sk).toLowerCase();
}
  • 新增 WxMaXPaySigParamsTest,覆盖 calcSig / calcPaySig 均返回全小写 64 字符 HMAC-SHA256 签名的断言
Original prompt

This section details on the original issue you should resolve

<issue_title>小程序虚拟支付,用户态签名没有转小写?导致签名无效</issue_title>
<issue_description>简要描述
小程序虚拟支付,用户态签名没有转小写?导致签名无效

模块版本情况
WxJava 模块名: weixin-java-miniapp
WxJava 版本号:4.8.0
详细描述
虚拟支付参数类:WxMaXPaySigParams
方法:
public String calcSig(String postBody) {
String sk = StringUtils.trimToEmpty(this.sessionKey);
return calcSignature(postBody, sk);
}
protected String calcSignature(String postBody, String sessionKey) {
return SignUtils.createHmacSha256Sign(postBody, sessionKey);
}
签名最后没有转小写?

日志
小测序报错:requestVirtualPayment fail requestVirtualPayment:fail SIGNATURE_INVALID -15005</issue_description>

Comments on the Issue (you are @copilot in this section)

Custom agent used: 全部用中文
需要用中文,包括PR标题和分析总结过程


📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

在 WxMaXPaySigParams.calcSig() 方法返回值添加 .toLowerCase(),
与 calcPaySig() 保持一致,修复签名大小写问题导致的 SIGNATURE_INVALID -15005 错误。
同时新增 WxMaXPaySigParamsTest 单元测试验证签名均为小写。

Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix user signature case issue in virtual payment fix: 小程序虚拟支付用户态签名 calcSig 未转小写导致 SIGNATURE_INVALID Mar 20, 2026
Copilot AI requested a review from binarywang March 20, 2026 03:54
@binarywang binarywang marked this pull request as ready for review March 20, 2026 04:13
Copilot AI review requested due to automatic review settings March 20, 2026 04:13
@binarywang
Copy link
Owner

auggie review

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

该 PR 修复 weixin-java-miniapp 模块中小程序虚拟支付(xpay)用户态签名的大小写不一致问题:calcSig() 计算出的 HMAC-SHA256 签名此前未转换为小写,可能导致微信侧验签失败并返回 SIGNATURE_INVALID -15005

Changes:

  • 修复 WxMaXPaySigParams#calcSig(String):在返回签名结果前补充小写转换,与 calcPaySig() 行为对齐。
  • 新增 WxMaXPaySigParamsTest:覆盖 calcSig / calcPaySig 返回值为全小写且符合 64 位十六进制 HMAC-SHA256 的断言。

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/xpay/WxMaXPaySigParams.java 将用户态签名 calcSig() 的返回值统一转换为小写,避免与微信服务端验签要求不一致
weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/bean/xpay/WxMaXPaySigParamsTest.java 新增单元测试,验证 calcSig/calcPaySig 的签名输出为小写且为 64 位十六进制字符串

@augmentcode
Copy link

augmentcode bot commented Mar 20, 2026

🤖 Augment PR Summary

概述: 修复小程序虚拟支付用户态签名 calcSig 输出未转小写导致微信侧校验失败(SIGNATURE_INVALID -15005)。

变更:

  • WxMaXPaySigParams#calcSig 的 HMAC-SHA256 结果统一转为小写十六进制,与 calcPaySig 行为对齐
  • 新增 WxMaXPaySigParamsTest,覆盖 calcSig/calcPaySig 输出为 64 位小写十六进制签名的断言

技术说明: 底层 SignUtils.createHmacSha256Sign 当前返回大写十六进制字符串,本次修复保证调用方输出满足微信服务端对签名小写格式的要求。

🤖 Was this summary useful? React with 👍 or 👎

Copy link

@augmentcode augmentcode bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. 1 suggestion posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.

String sig = sigParams.calcSig(POST_BODY);
String paySig = sigParams.calcPaySig(URL, POST_BODY);

assertEquals(sig.length(), 64, "HMAC-SHA256 签名应为 64 个十六进制字符");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的测试目前只覆盖了“小写/长度/hex 格式”,如果实现意外返回固定的 64 位小写 hex 常量也会通过。可以考虑针对固定输入断言具体签名值(例如与 SignUtils.createHmacSha256Sign(...) 的小写结果一致),从而同时覆盖算法/拼接逻辑的正确性。

Severity: low

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

Copy link

@augmentcode augmentcode bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. 1 suggestion posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.

public String calcSig(String postBody) {
String sk = StringUtils.trimToEmpty(this.sessionKey);
return calcSignature(postBody, sk);
return calcSignature(postBody, sk).toLowerCase();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SignUtils.createHmacSha256Sign 在捕获 NoSuchAlgorithmException/InvalidKeyException 时会返回 null,这里直接调用 .toLowerCase() 可能触发 NullPointerException 并改变之前 calcSig 返回 null 的行为。是否需要明确该异常场景下的返回/抛出策略(例如保证非空或显式处理)?

Severity: medium

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

@binarywang binarywang merged commit 724e80a into develop Mar 21, 2026
5 checks passed
@binarywang binarywang deleted the copilot/fix-signature-case-issue branch March 21, 2026 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

小程序虚拟支付,用户态签名没有转小写?导致签名无效

3 participants