wechatpay-apiv3 / wechatpay-go

微信支付 APIv3 的官方 Go Library
https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pages/index.shtml
Apache License 2.0
1.07k stars 145 forks source link

支付回调异常nvalid notification, err: validate verify fail serial=[***] request-id=[] err=certificate[***] not found in verifier #196

Closed jerrylvq closed 1 year ago

jerrylvq commented 1 year ago

代码段:

image

报错:

image
jerrylvq commented 1 year ago

方法一(大多数场景):先手动注册下载器,再获取微信平台证书访问器。 适用场景: 仅需要对回调通知验证签名并解密的场景。例如,基础支付的回调通知。

ctx := context.Background() // 1. 使用 RegisterDownloaderWithPrivateKey 注册下载器 err := downloader.MgrInstance().RegisterDownloaderWithPrivateKey(ctx, mchPrivateKey, mchCertificateSerialNumber, mchID, mchAPIV3Key) // 2. 获取商户号对应的微信支付平台证书访问器 certificateVisitor := downloader.MgrInstance().GetCertificateVisitor(mchID) // 3. 使用证书访问器初始化 notify.Handler handler := notify.NewNotifyHandler(mchAPIv3Key, verifiers.NewSHA256WithRSAVerifier(certificateVisitor))

以上方法解决问题

alexz2008 commented 1 year ago

怎么解决的呀,我也出现invalid notification, err: timestamp=[1694490266] expires, request-i。。。。。错误

jerrylvq commented 1 year ago

//PayAction 支付回调 func PayAction(ctx iris.Context, mr *mvc.Response) { logrus.Info("支付回调开始01") ctext := context.Background() mchAPIV3Key := viper.GetString("wechat.payments.app.mchAPIv3Key") mchPrivateKey, err := util.LoadPrivateKeyWithPath(viper.GetString("wechat.payments.app.privateKeyPath")) if err != nil { logrus.Error("Load Private Key Error: " + err.Error()) mr.Object = mvc.Response{ Object: map[string]interface{}{ "code": "FAIL", "message": "Load Private Key Error", }, } return }

logrus.Info("支付回调开中02") mchCertificateSerialNumber := viper.GetString("wechat.payments.app.mchCertificateSerialNumber") mchID := viper.GetString("wechat.payments.app.mchID") // 1. 使用 RegisterDownloaderWithPrivateKey 注册下载器 err = downloader.MgrInstance().RegisterDownloaderWithPrivateKey(ctext, mchPrivateKey, mchCertificateSerialNumber, mchID, mchAPIV3Key) if err != nil { logrus.Error("Register Downloader With Private Key Error: " + err.Error()) mr.Object = mvc.Response{ Object: map[string]interface{}{ "code": "FAIL", "message": "Register Downloader With Private Key Error", }, } return }

logrus.Info("支付回调开中03") // 2. 获取商户号对应的微信支付平台证书访问器 certificateVisitor := downloader.MgrInstance().GetCertificateVisitor(mchID) // 3. 使用证书访问器初始化 notify.Handler handler, _ := notify.NewRSANotifyHandler(mchAPIV3Key, verifiers.NewSHA256WithRSAVerifier(certificateVisitor))

//支付成功通知参数 transaction := new(payments.Transaction) notifyReq, err := handler.ParseNotifyRequest(context.Background(), ctx.Request(), transaction) logrus.Info("支付回调开中04") if err != nil { logrus.Error(err.Error()) utils.FailWithMsg(mr, err.Error()) return } // 支付结果处理 if notifyReq.EventType == "TRANSACTION.SUCCESS" { //支付成功 mr.Object = mvc.Response{ Object: map[string]interface{}{ "code": "SUCCESS", "message": "", }, } logrus.Info("准备更新数据库。") //更新数据库

logrus.Info("更新数据库成功。") //notifyReq.Resource.Plaintext 解密类容 } else { logrus.Info(notifyReq) } return }

Message ID: @.***>

JinkunSun commented 11 months ago

正常支付回调正常,但每天下午八点会有一个回调报错 validate verify fail serial=... 是为啥