From ef6538528d4573d5423b8f6c41283d030709f15a Mon Sep 17 00:00:00 2001 From: lwl0608 Date: Sun, 18 Feb 2024 23:24:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E4=BB=98=E5=AE=9D=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/mix/conf/cfg.go | 4 +-- app/mix/controller/alipay_callback.go | 2 +- app/mix/dao/mysql.go | 6 ++-- app/mix/service/logic/vas.go | 8 ++--- app/mix/service/service.go | 6 +++- library/payclients/alipaycli/client.go | 38 +++++++++++++++++---- library/payclients/alipaycli/client_test.go | 4 +-- 7 files changed, 48 insertions(+), 20 deletions(-) diff --git a/app/mix/conf/cfg.go b/app/mix/conf/cfg.go index cefb9211..839670a9 100644 --- a/app/mix/conf/cfg.go +++ b/app/mix/conf/cfg.go @@ -8,8 +8,8 @@ type ConfigSt struct { MixMysql *configcenter.MysqlConfig `json:"mix_mysql" yaml:"mix_mysql"` // mix mysql Crypto *configcenter.CryptoConfig `json:"crypto" yaml:"crypto"` // 账号相关加密 Wxpay *configcenter.WxpayClientConfig `json:"wxpay" yaml:"wxpay"` // 微信支付 - Alipay *configcenter.AlipayClientConfig `json:"alipay" yaml:"alipay"` // 支付宝 - AlipayMYTS *configcenter.AlipayClientConfig `json:"alipay_myts" yaml:"alipay_myts"` // 支付宝 + Alipay *configcenter.AlipayClientConfig `json:"alipay" yaml:"alipay"` // 支付宝 心意到了 + AlipayMYTS *configcenter.AlipayClientConfig `json:"alipay_myts" yaml:"alipay_myts"` // 支付宝 觅缘天使 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 08afbe5d..5af4feb3 100644 --- a/app/mix/controller/alipay_callback.go +++ b/app/mix/controller/alipay_callback.go @@ -12,7 +12,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.GetAlipayClient().ParseNotify(ctx.Request) if err != nil { logger.Error("ParseNotify fail, req: %v, err: %v", util.ToJson(bm), err) return diff --git a/app/mix/dao/mysql.go b/app/mix/dao/mysql.go index b3cd6528..c8093359 100644 --- a/app/mix/dao/mysql.go +++ b/app/mix/dao/mysql.go @@ -113,20 +113,20 @@ func NewMysql(cfg *conf.ConfigSt) (mysql *Mysql, err error) { func (m *Mysql) CreateOrder(ctx *gin.Context, tx *sqlx.Tx, order *dbstruct.Order) error { var err error sqlStr := "insert into " + TableOrder + - " (id, mid, uid, product_id, pay_type, pay_amount, " + + " (id, mid, uid, product_id, pay_type, pay_amount, oid1, oid2, oid3, " + " out_order_id, receipt_data, coins, order_status, order_from, " + " ct, ut, ext, b_did, b_ver, b_osver, b_dt, b_ch, b_model, b_nt, ip) " + " values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) " if tx != nil { _, err = tx.ExecContext(ctx, sqlStr, - order.GetID(), order.GetMid(), order.GetUid(), order.GetProductId(), order.GetPayType(), order.GetPayAmount(), + order.GetID(), order.GetMid(), order.GetUid(), order.GetProductId(), order.GetPayType(), order.GetPayAmount(), order.GetOid1(), order.GetOid2(), order.GetOid3(), order.GetOutOrderID(), order.GetReceiptData(), order.GetCoins(), order.GetOrderStatus(), order.GetOrderFrom(), order.GetCt(), order.GetUt(), order.GetExt(), order.GetDid(), order.GetVersion(), order.GetOsVersion(), order.GetDevType(), order.GetChannel(), order.GetModel(), order.GetNetType(), order.GetIp(), ) } else { db := m.getDBVas() _, err = db.ExecContext(ctx, sqlStr, - order.GetID(), order.GetMid(), order.GetUid(), order.GetProductId(), order.GetPayType(), order.GetPayAmount(), + order.GetID(), order.GetMid(), order.GetUid(), order.GetProductId(), order.GetPayType(), order.GetPayAmount(), order.GetOid1(), order.GetOid2(), order.GetOid3(), order.GetOutOrderID(), order.GetReceiptData(), order.GetCoins(), order.GetOrderStatus(), order.GetOrderFrom(), order.GetCt(), order.GetUt(), order.GetExt(), order.GetDid(), order.GetVersion(), order.GetOsVersion(), order.GetDevType(), order.GetChannel(), order.GetModel(), order.GetNetType(), order.GetIp(), ) diff --git a/app/mix/service/logic/vas.go b/app/mix/service/logic/vas.go index 754c79ac..ba901ad0 100644 --- a/app/mix/service/logic/vas.go +++ b/app/mix/service/logic/vas.go @@ -168,7 +168,7 @@ func (v *Vas) CreateOrder(ctx *gin.Context, req *vasproto.CreateOrderReq) (data case vasproto.PayTypeOp: case vasproto.PayTypeAlipay: - alipayCli := alipaycli.GetDefaultAlipayClient() + alipayCli := alipaycli.GetAlipayClient() appPayParam := &alipaycli.AppPayParam{ OutTradeNo: orderId, Subject: product.Subject, @@ -182,7 +182,7 @@ func (v *Vas) CreateOrder(ctx *gin.Context, req *vasproto.CreateOrderReq) (data } req.Oid3 = alipayCli.AppId case vasproto.PayTypeAlipayH5: - alipayCli := alipaycli.GetDefaultAlipayClient() + alipayCli := alipaycli.GetAlipayClient() wapPayParam := &alipaycli.WapPayParam{ OutTradeNo: orderId, Subject: product.Subject, @@ -1945,7 +1945,7 @@ func (v *Vas) WithdrawApply(ctx *gin.Context, req *vasproto.WithdrawApplyReq) (t AlipayLoginId: alipayId, AlipayName: alipayName, } - transferResp, err = alipaycli.GetDefaultAlipayClient().UniTransfer(ctx, transferParam) + transferResp, err = alipaycli.GetAlipayClient().UniTransfer(ctx, transferParam) if err != nil { logger.Error("UniTransfer fail, param: %v, err: %v", util.ToJson(transferParam), err) return @@ -2083,7 +2083,7 @@ func (v *Vas) DealOneOrder(ctx *gin.Context, orderId string) (err error) { // 支付宝查询订单 switch order.GetPayType() { case vasproto.PayTypeAlipayH5: - alipayCli := alipaycli.GetDefaultAlipayClient() + alipayCli := alipaycli.GetAlipayClient() var alipayResp *alipay.TradeQueryResponse alipayResp, err = alipayCli.QueryOrder(ctx, &alipaycli.QueryOrderParam{ OutTradeNo: orderId, diff --git a/app/mix/service/service.go b/app/mix/service/service.go index 295f810c..99facaed 100644 --- a/app/mix/service/service.go +++ b/app/mix/service/service.go @@ -43,6 +43,7 @@ import ( "service/bizcommon/util" "service/dbstruct" "service/library/apollo" + "service/library/configcenter" "service/library/contentaudit/imageaudit" "service/library/contentaudit/textaudit" "service/library/logger" @@ -128,7 +129,10 @@ func (s *Service) Init(c any) (err error) { s.defaultMelody = melody.New() - err = alipaycli.Init(cfg.AlipayMYTS) + err = alipaycli.InitMulti([]*configcenter.AlipayClientConfig{ + cfg.Alipay, + cfg.AlipayMYTS, + }) 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 188d3fd9..663e3466 100644 --- a/library/payclients/alipaycli/client.go +++ b/library/payclients/alipaycli/client.go @@ -2,6 +2,7 @@ package alipaycli import ( "context" + "errors" "fmt" "github.com/go-pay/gopay" "github.com/go-pay/gopay/alipay" @@ -16,17 +17,39 @@ const ( DefaultOrderTimeoutSeconds = 900 // 默认订单超时时间,单位: s ) -var defaultAlipayClient *AlipayClient +const ( + AppIdXinYiDaoLe = "2021004115647165" // 心意到了 + AppIdMiYuanTianShi = "2021004135664261" // 觅缘天使 +) -func GetDefaultAlipayClient() *AlipayClient { - return defaultAlipayClient +var allAlipayClients = map[string]*AlipayClient{} + +func GetAlipayClient() *AlipayClient { + return allAlipayClients[AppIdXinYiDaoLe] } type AlipayClient struct { *alipay.Client + alipayPublicCertPath string } -func Init(cfg *configcenter.AlipayClientConfig) (err error) { +func InitMulti(cfgList []*configcenter.AlipayClientConfig) (err error) { + for _, cfg := range cfgList { + var cli *AlipayClient + cli, err = NewAlipayClient(cfg) + if err != nil { + return + } + if cli == nil { + err = errors.New("NewAlipayClient fail") + return + } + allAlipayClients[cli.AppId] = cli + } + return +} + +func NewAlipayClient(cfg *configcenter.AlipayClientConfig) (ret *AlipayClient, err error) { alipayCli, err := alipay.NewClient(cfg.Appid, cfg.PrivateKey, true) if err != nil { logger.Error("NewClient fail, cfg: %v, err: %v", util.ToJson(cfg), err) @@ -44,8 +67,9 @@ func Init(cfg *configcenter.AlipayClientConfig) (err error) { return } - defaultAlipayClient = &AlipayClient{ - Client: alipayCli, + ret = &AlipayClient{ + Client: alipayCli, + alipayPublicCertPath: cfg.AlipayPublicCertPath, } return } @@ -61,7 +85,7 @@ func (c *AlipayClient) ParseNotify(req *http.Request) (notify gopay.BodyMap, err logger.Info("Alipay ParseNotify, %v", util.ToJson(notifyTmp)) // 验签 - ok, err := alipay.VerifySignWithCert("/app/wishpal-ironfan/etc/mix/alipaycert_myts/alipayCertPublicKey_RSA2.crt", notifyTmp) + ok, err := alipay.VerifySignWithCert(c.alipayPublicCertPath, notifyTmp) if !ok { logger.Error("VerifySign fail, not ok, bm: %v", util.ToJson(notifyTmp)) return diff --git a/library/payclients/alipaycli/client_test.go b/library/payclients/alipaycli/client_test.go index 26ed492e..8b69a3d9 100644 --- a/library/payclients/alipaycli/client_test.go +++ b/library/payclients/alipaycli/client_test.go @@ -27,7 +27,7 @@ func TestMain(m *testing.M) { } func TestAlipayClient_WapPay(t *testing.T) { - cli := GetDefaultAlipayClient() + cli := GetAlipayClient() paramStr, err := cli.WapPay(context.Background(), &WapPayParam{ OutTradeNo: idgenerator.GenOrderId(), Subject: "哈哈", @@ -42,7 +42,7 @@ func TestAlipayClient_WapPay(t *testing.T) { } func TestAlipayClient_QueryOrder(t *testing.T) { - cli := GetDefaultAlipayClient() + cli := GetAlipayClient() resp, err := cli.QueryOrder(context.Background(), &QueryOrderParam{ OutTradeNo: "1750539072771731456", })