From 425c0eddc9cc00408bdf89b4ec783c3a881e372b Mon Sep 17 00:00:00 2001 From: lwl0608 Date: Thu, 4 Jul 2024 11:39:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20parse=20notify?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/mix/conf/cfg.go | 3 ++- app/mix/controller/alipay_callback.go | 4 +--- app/mix/service/service.go | 2 +- library/payclients/alipaycli/client.go | 31 +++++++++++++++++++++++++- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/app/mix/conf/cfg.go b/app/mix/conf/cfg.go index 171138f4..14046ecd 100644 --- a/app/mix/conf/cfg.go +++ b/app/mix/conf/cfg.go @@ -11,7 +11,8 @@ type ConfigSt struct { WxpayTFZ *configcenter.WxpayClientConfig `json:"wxpay_tfz" yaml:"wxpay_tfz"` // 微信支付 Alipay *configcenter.AlipayClientConfig `json:"alipay" yaml:"alipay"` // 支付宝 心意到了 AlipayMYTS *configcenter.AlipayClientConfig `json:"alipay_myts" yaml:"alipay_myts"` // 支付宝 觅缘天使 - AlipayLX01 *configcenter.AlipayClientConfig `json:"alipay_lx01" yaml:"alipay_lx01"` // 支付宝 星揽01 + AlipayLX01 *configcenter.AlipayClientConfig `json:"alipay_lx01" yaml:"alipay_lx01"` // 支付宝 揽星02 + AlipayLX02 *configcenter.AlipayClientConfig `json:"alipay_lx02" yaml:"alipay_lx02"` // 支付宝 揽星02 Apollo *configcenter.ApolloConfig `json:"apollo" yaml:"apollo"` // Apollo Media *configcenter.MediaConfig `json:"media" yaml:"media"` // 媒体配置 Dysmsapi *configcenter.DysmsapiConfig `json:"dysmsapi" yaml:"dysmsapi"` // 阿里云短信服务 diff --git a/app/mix/controller/alipay_callback.go b/app/mix/controller/alipay_callback.go index 00a63c91..ab8626c5 100644 --- a/app/mix/controller/alipay_callback.go +++ b/app/mix/controller/alipay_callback.go @@ -10,9 +10,7 @@ import ( ) func AlipayCallback(ctx *gin.Context) { - //req, _ := ctx.GetRawData() - //logger.Info("_AlipayCallback req: %v", string(req)) - bm, err := alipaycli.GetDefaultAlipayClient().ParseNotify(ctx.Request) + bm, err := alipaycli.ParseNotify(ctx.Request) if err != nil { logger.Error("ParseNotify fail, req: %v, err: %v", util.ToJson(bm), err) return diff --git a/app/mix/service/service.go b/app/mix/service/service.go index e580f6b5..3bc8601e 100644 --- a/app/mix/service/service.go +++ b/app/mix/service/service.go @@ -170,7 +170,7 @@ func (s *Service) Init(c any) (err error) { s.defaultMelody = melody.New() - err = alipaycli.InitMulti(cfg.Alipay, cfg.AlipayMYTS, cfg.AlipayLX01) + err = alipaycli.InitMulti(cfg.Alipay, cfg.AlipayMYTS, cfg.AlipayLX01, cfg.AlipayLX02) if err != nil { logger.Error("alipaycli.Init fail, cfg: %v, err: %v", util.ToJson(cfg.Alipay), err) return diff --git a/library/payclients/alipaycli/client.go b/library/payclients/alipaycli/client.go index a3dabf79..d02d5bc5 100644 --- a/library/payclients/alipaycli/client.go +++ b/library/payclients/alipaycli/client.go @@ -79,8 +79,28 @@ func NewAlipayClient(cfg *configcenter.AlipayClientConfig) (ret *AlipayClient, e return } +//// 解析回调参数 +//func (c *AlipayClient) ParseNotify(req *http.Request) (notify gopay.BodyMap, err error) { +// // 解析参数 +// notifyTmp, err := alipay.ParseNotifyToBodyMap(req) +// if err != nil { +// logger.Error("ParseNotifyToBodyMap fail, req: %v, err: %v", util.ToJson(req), err) +// return +// } +// logger.Info("Alipay ParseNotify, %v", util.ToJson(notifyTmp)) +// +// // 验签 +// ok, err := alipay.VerifySignWithCert(c.alipayPublicCertPath, notifyTmp) +// if !ok { +// logger.Error("VerifySign fail, not ok, bm: %v", util.ToJson(notifyTmp)) +// return +// } +// notify = notifyTmp +// return +//} + // 解析回调参数 -func (c *AlipayClient) ParseNotify(req *http.Request) (notify gopay.BodyMap, err error) { +func ParseNotify(req *http.Request) (notify gopay.BodyMap, err error) { // 解析参数 notifyTmp, err := alipay.ParseNotifyToBodyMap(req) if err != nil { @@ -89,6 +109,15 @@ func (c *AlipayClient) ParseNotify(req *http.Request) (notify gopay.BodyMap, err } logger.Info("Alipay ParseNotify, %v", util.ToJson(notifyTmp)) + // 获取appId + appId := notifyTmp.Get("app_id") + c := GetAlipayClientByAppId(appId) + if c == nil { + err = fmt.Errorf("GetAlipayClientByAppId fail, appId: %v", appId) + logger.Error("GetAlipayClientByAppId fail, appId: %v", appId) + return + } + // 验签 ok, err := alipay.VerifySignWithCert(c.alipayPublicCertPath, notifyTmp) if !ok {