From 35094559a0e4254b0441b2708e0402f04f728b5d Mon Sep 17 00:00:00 2001 From: liangzai <2440983361@qq.com> Date: Wed, 19 Nov 2025 19:45:00 +0800 Subject: [PATCH] fix --- .../api/internal/service/wechatpayService.go | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/app/main/api/internal/service/wechatpayService.go b/app/main/api/internal/service/wechatpayService.go index 6a2dec5..cfd0ac1 100644 --- a/app/main/api/internal/service/wechatpayService.go +++ b/app/main/api/internal/service/wechatpayService.go @@ -2,14 +2,14 @@ package service import ( "context" - "tydata-server/app/main/api/internal/config" - "tydata-server/app/main/model" - "tydata-server/common/ctxdata" - "tydata-server/pkg/lzkit/lzUtils" "fmt" "net/http" "strconv" "time" + "tydata-server/app/main/api/internal/config" + "tydata-server/app/main/model" + "tydata-server/common/ctxdata" + "tydata-server/pkg/lzkit/lzUtils" "github.com/wechatpay-apiv3/wechatpay-go/core" "github.com/wechatpay-apiv3/wechatpay-go/core/auth/verifiers" @@ -137,10 +137,15 @@ func newWechatPayServiceWithWxPayPubKey(c config.Config, userAuthModel model.Use } // 初始化 notify.Handler - certificateVisitor := downloader.MgrInstance().GetCertificateVisitor(mchID) + // 使用公钥方式时,certificateVisitor 可能为 nil,直接使用公钥验证器 notifyHandler := notify.NewNotifyHandler( mchAPIv3Key, - verifiers.NewSHA256WithRSACombinedVerifier(certificateVisitor, mchPublicKeyID, *mchPublicKey)) + verifiers.NewSHA256WithRSACombinedVerifier(nil, mchPublicKeyID, *mchPublicKey)) + + if notifyHandler == nil { + logx.Errorf("创建 notifyHandler 失败") + panic("初始化失败,服务停止: notifyHandler 为 nil") + } logx.Infof("微信支付客户端初始化成功(微信支付公钥方式)") return &WechatPayService{ @@ -294,6 +299,9 @@ func (w *WechatPayService) CreateWechatOrder(ctx context.Context, amount float64 // HandleWechatPayNotification 处理微信支付回调 func (w *WechatPayService) HandleWechatPayNotification(ctx context.Context, req *http.Request) (*payments.Transaction, error) { + if w.notifyHandler == nil { + return nil, fmt.Errorf("微信支付通知处理器未初始化") + } transaction := new(payments.Transaction) _, err := w.notifyHandler.ParseNotifyRequest(ctx, req, transaction) if err != nil { @@ -305,6 +313,9 @@ func (w *WechatPayService) HandleWechatPayNotification(ctx context.Context, req // HandleRefundNotification 处理微信退款回调 func (w *WechatPayService) HandleRefundNotification(ctx context.Context, req *http.Request) (*refunddomestic.Refund, error) { + if w.notifyHandler == nil { + return nil, fmt.Errorf("微信支付通知处理器未初始化") + } refund := new(refunddomestic.Refund) _, err := w.notifyHandler.ParseNotifyRequest(ctx, req, refund) if err != nil {