From 8a836ae56ce9357a22bf592534576fa6d5ef1691 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Thu, 3 Nov 2022 19:17:44 +0800 Subject: [PATCH] optimize alipay initial client logic --- .dockerignore | 1 + .gitignore | 5 ++- README.md | 2 +- config.yaml.sample | 9 ++-- configs/alipayAppCertPublicKey.crt | 0 configs/alipayCertPublicKey_RSA2.crt | 0 configs/alipayRootCert.crt | 0 internal/conf/settting.go | 10 +++-- internal/routers/api/api.go | 32 ++++++++++++++ internal/routers/api/user.go | 65 ++-------------------------- 10 files changed, 53 insertions(+), 71 deletions(-) delete mode 100644 configs/alipayAppCertPublicKey.crt delete mode 100644 configs/alipayCertPublicKey_RSA2.crt delete mode 100644 configs/alipayRootCert.crt diff --git a/.dockerignore b/.dockerignore index 6431dcd7..175d4553 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,6 +1,7 @@ data .vscode release +custom *Dockerfile* web/node_modules web/src-tauri/target diff --git a/.gitignore b/.gitignore index 61bdf2ca..ea7a71a2 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,6 @@ config.yaml *.log paopao-ce* -release -data +/release +/data +/custom diff --git a/README.md b/README.md index 56946ab3..15497298 100644 --- a/README.md +++ b/README.md @@ -248,7 +248,7 @@ make run TAGS='docs' ### 配置说明 -`config.yaml.sample` 是一份完整的配置文件模版,paopao-ce启动时会读取`./configs/config.yaml`、`./config.yaml`任意一份配置文件(优先读取最先找到的文件)。 +`config.yaml.sample` 是一份完整的配置文件模版,paopao-ce启动时会读取`./custom/config.yaml`、`./config.yaml`任意一份配置文件(优先读取最先找到的文件)。 ```sh cp config.yaml.sample config.yaml diff --git a/config.yaml.sample b/config.yaml.sample index 1fee92cb..abf8f707 100644 --- a/config.yaml.sample +++ b/config.yaml.sample @@ -15,7 +15,7 @@ Features: Develop: ["Base", "MySQL", "BigCacheIndex", "Meili", "Sms", "AliOSS", "LoggerMeili", "OSS:Retention"] Demo: ["Base", "MySQL", "Option", "Zinc", "Sms", "MinIO", "LoggerZinc", "Migration"] Slim: ["Base", "Sqlite3", "LocalOSS", "LoggerFile", "OSS:TempDir"] - Base: ["Redis", "Alipay", "PhoneBind"] + Base: ["Redis", "PhoneBind"] Option: ["SimpleCacheIndex"] Sms: "SmsJuhe" SmsJuhe: @@ -24,8 +24,11 @@ SmsJuhe: TplID: TplVal: "#code#=%d&#m#=%d" Alipay: - AppID: - PrivateKey: + AppID: + InProduction: True + RootCertFile: "custom/alipay/RootCert.crt" + PublicCertFile: "custom/alipay/CertPublicKey_RSA2.crt" + AppPublicCertFile: "custom/alipay/AppCertPublicKey.crt" CacheIndex: MaxUpdateQPS: 100 # 最大添加/删除/更新Post的QPS, 设置范围[10, 10000], 默认100 SimpleCacheIndex: # 缓存泡泡广场消息流 diff --git a/configs/alipayAppCertPublicKey.crt b/configs/alipayAppCertPublicKey.crt deleted file mode 100644 index e69de29b..00000000 diff --git a/configs/alipayCertPublicKey_RSA2.crt b/configs/alipayCertPublicKey_RSA2.crt deleted file mode 100644 index e69de29b..00000000 diff --git a/configs/alipayRootCert.crt b/configs/alipayRootCert.crt deleted file mode 100644 index e69de29b..00000000 diff --git a/internal/conf/settting.go b/internal/conf/settting.go index b9256039..2e2ae9ec 100644 --- a/internal/conf/settting.go +++ b/internal/conf/settting.go @@ -75,8 +75,12 @@ type BigCacheIndexSettingS struct { } type AlipaySettingS struct { - AppID string - PrivateKey string + AppID string + PrivateKey string + RootCertFile string + PublicCertFile string + AppPublicCertFile string + InProduction bool } type SmsJuheSettings struct { @@ -204,7 +208,7 @@ func NewSetting() (*Setting, error) { vp := viper.New() vp.SetConfigName("config") vp.AddConfigPath(".") - vp.AddConfigPath("configs/") + vp.AddConfigPath("custom/") vp.SetConfigType("yaml") err := vp.ReadInConfig() if err != nil { diff --git a/internal/routers/api/api.go b/internal/routers/api/api.go index 3ee6c52d..afc24578 100644 --- a/internal/routers/api/api.go +++ b/internal/routers/api/api.go @@ -1,14 +1,46 @@ package api import ( + "github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/internal/core" "github.com/rocboss/paopao-ce/internal/dao" + "github.com/sirupsen/logrus" + "github.com/smartwalle/alipay/v3" ) var ( + alipayClient *alipay.Client objectStorage core.ObjectStorageService ) func Initialize() { objectStorage = dao.ObjectStorageService() + + if conf.CfgIf("Alipay") { + initAlipay() + } +} + +func initAlipay() { + s := conf.AlipaySetting + // 将 key 的验证调整到初始化阶段 + client, err := alipay.New(s.AppID, s.PrivateKey, s.InProduction) + if err != nil { + logrus.Fatalf("alipay.New err: %s", err) + } + + // 加载应用公钥证书 + if err = client.LoadAppPublicCertFromFile(s.AppPublicCertFile); err != nil { + logrus.Fatalf("client.LoadAppPublicCertFromFile err: %s\n", err) + } + + // 加载支付宝根证书 + if err = client.LoadAliPayRootCertFromFile(s.RootCertFile); err != nil { + logrus.Fatalf("client.LoadAliPayRootCertFromFile err: %s\n", err) + } + + // 加载支付宝公钥证书 + if err = client.LoadAliPayPublicCertFromFile(s.PublicCertFile); err != nil { + logrus.Fatalf("client.LoadAliPayPublicCertFromFile err: %s\n", err) + } } diff --git a/internal/routers/api/user.go b/internal/routers/api/user.go index ec91c815..18267d81 100644 --- a/internal/routers/api/user.go +++ b/internal/routers/api/user.go @@ -6,7 +6,6 @@ import ( "unicode/utf8" "github.com/gin-gonic/gin" - "github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/internal/model" "github.com/rocboss/paopao-ce/internal/service" "github.com/rocboss/paopao-ce/pkg/app" @@ -413,36 +412,6 @@ func GetUserRechargeLink(c *gin.Context) { logrus.Errorf("service.CreateRecharge err: %v\n", err) response.ToErrorResponse(errcode.RechargeReqFail) return - - } - - client, err := alipay.New(conf.AlipaySetting.AppID, conf.AlipaySetting.PrivateKey, true) - // 将 key 的验证调整到初始化阶段 - if err != nil { - logrus.Errorf("alipay.New err: %v\n", err) - response.ToErrorResponse(errcode.RechargeReqFail) - return - } - - err = client.LoadAppPublicCertFromFile("configs/alipayAppCertPublicKey.crt") // 加载应用公钥证书 - if err != nil { - logrus.Errorf("client.LoadAppPublicCertFromFile err: %v\n", err) - response.ToErrorResponse(errcode.RechargeReqFail) - return - } - - err = client.LoadAliPayRootCertFromFile("configs/alipayRootCert.crt") // 加载支付宝根证书 - if err != nil { - logrus.Errorf("client.LoadAliPayRootCertFromFile err: %v\n", err) - response.ToErrorResponse(errcode.RechargeReqFail) - return - } - - err = client.LoadAliPayPublicCertFromFile("configs/alipayCertPublicKey_RSA2.crt") // 加载支付宝公钥证书 - if err != nil { - logrus.Errorf("client.LoadAliPayPublicCertFromFile err: %v\n", err) - response.ToErrorResponse(errcode.RechargeReqFail) - return } p := alipay.TradePreCreate{} @@ -451,7 +420,7 @@ func GetUserRechargeLink(c *gin.Context) { p.TotalAmount = fmt.Sprintf("%.2f", float64(recharge.Amount)/100.0) p.NotifyURL = "https://" + c.Request.Host + "/v1/alipay/notify" - rsp, err := client.TradePreCreate(p) + rsp, err := alipayClient.TradePreCreate(p) if err != nil { logrus.Errorf("client.TradePreCreate err: %v\n", err) response.ToErrorResponse(errcode.RechargeReqFail) @@ -495,37 +464,9 @@ func AlipayNotify(c *gin.Context) { response := app.NewResponse(c) c.Request.ParseForm() - aliClient, err := alipay.New(conf.AlipaySetting.AppID, conf.AlipaySetting.PrivateKey, true) - // 将 key 的验证调整到初始化阶段 - if err != nil { - logrus.Errorf("alipay.New err: %v\n", err) - response.ToErrorResponse(errcode.RechargeReqFail) - return - } - err = aliClient.LoadAppPublicCertFromFile("configs/alipayAppCertPublicKey.crt") // 加载应用公钥证书 - if err != nil { - logrus.Errorf("client.LoadAppPublicCertFromFile err: %v\n", err) - response.ToErrorResponse(errcode.RechargeReqFail) - return - } - - err = aliClient.LoadAliPayRootCertFromFile("configs/alipayRootCert.crt") // 加载支付宝根证书 - if err != nil { - logrus.Errorf("client.LoadAliPayRootCertFromFile err: %v\n", err) - response.ToErrorResponse(errcode.RechargeReqFail) - return - } - - err = aliClient.LoadAliPayPublicCertFromFile("configs/alipayCertPublicKey_RSA2.crt") // 加载支付宝公钥证书 - if err != nil { - logrus.Errorf("client.LoadAliPayPublicCertFromFile err: %v\n", err) - response.ToErrorResponse(errcode.RechargeReqFail) - return - } - - _, err = aliClient.GetTradeNotification(c.Request) + _, err := alipayClient.GetTradeNotification(c.Request) if err != nil { - logrus.Errorf("aliClient.GetTradeNotification err: %v\n", err) + logrus.Errorf("alipayClient.GetTradeNotification err: %v\n", err) logrus.Infoln(c.Request.Form) response.ToErrorResponse(errcode.RechargeNotifyError) return