This commit is contained in:
2025-12-02 22:55:38 +08:00
parent 00cd6a1e6e
commit 4d5013486c
2 changed files with 15 additions and 7 deletions

View File

@@ -63,6 +63,7 @@ type WxpayConfig struct {
MchPrivateKeyPath string
MchPublicKeyID string
MchPublicKeyPath string
MchPlatformRAS string
NotifyUrl string
RefundNotifyUrl string
}

View File

@@ -119,10 +119,10 @@ func newWechatPayServiceWithWxPayPubKey(c config.Config, userAuthModel model.Use
panic(fmt.Sprintf("初始化失败,服务停止: %v", err))
}
// 从文件中加载微信支付平台证书
// 从文件中加载微信支付公钥
mchPublicKey, err := utils.LoadPublicKeyWithPath(mchPublicKeyPath)
if err != nil {
logx.Errorf("加载微信支付平台证书失败: %v", err)
logx.Errorf("加载微信支付公钥失败: %v", err)
panic(fmt.Sprintf("初始化失败,服务停止: %v", err))
}
@@ -136,14 +136,21 @@ func newWechatPayServiceWithWxPayPubKey(c config.Config, userAuthModel model.Use
panic(fmt.Sprintf("初始化失败,服务停止: %v", err))
}
// 注册证书下载器,用于下载平台证书(回调验签需要)
// 注意:使用公钥方式时,需要手动注册证书下载器才能下载平台证书
err = downloader.MgrInstance().RegisterDownloaderWithClient(context.Background(), client, mchID, mchAPIv3Key)
if err != nil {
logx.Errorf("注册证书下载器失败: %v", err)
panic(fmt.Sprintf("初始化失败,服务停止: %v", err))
}
// 初始化 notify.Handler
// certificateVisitor := downloader.MgrInstance().GetCertificateVisitor(mchID)
// notifyHandler := notify.NewNotifyHandler(
// mchAPIv3Key,
// verifiers.NewSHA256WithRSACombinedVerifier(certificateVisitor, mchPublicKeyID, *mchPublicKey))
// 使用 SHA256WithRSACombinedVerifier 同时支持平台证书和公钥验签
// 原因:微信回调目前仍使用平台证书签名,需要兼容处理;同时支持未来切换到公钥签名
certificateVisitor := downloader.MgrInstance().GetCertificateVisitor(mchID)
notifyHandler := notify.NewNotifyHandler(
mchAPIv3Key,
verifiers.NewSHA256WithRSAPubkeyVerifier(mchPublicKeyID, *mchPublicKey))
verifiers.NewSHA256WithRSACombinedVerifier(certificateVisitor, mchPublicKeyID, *mchPublicKey))
logx.Infof("微信支付客户端初始化成功(微信支付公钥方式)")
return &WechatPayService{
config: c,