优化 parse notify

This commit is contained in:
lwl0608 2024-07-04 11:39:10 +08:00
parent 0dc67b4291
commit 425c0eddc9
4 changed files with 34 additions and 6 deletions

View File

@ -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"` // 阿里云短信服务

View File

@ -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

View File

@ -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

View File

@ -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 {