Skip to content

Commit

Permalink
Merge pull request #189 from rocboss/jc/alimy
Browse files Browse the repository at this point in the history
optimize alipay initial client logic
  • Loading branch information
alimy committed Nov 3, 2022
2 parents a491849 + 8a836ae commit 26f8cea
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 71 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
data
.vscode
release
custom
*Dockerfile*
web/node_modules
web/src-tauri/target
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
config.yaml
*.log
paopao-ce*
release
data
/release
/data
/custom
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions config.yaml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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: # 缓存泡泡广场消息流
Expand Down
Empty file removed configs/alipayAppCertPublicKey.crt
Empty file.
Empty file.
Empty file removed configs/alipayRootCert.crt
Empty file.
10 changes: 7 additions & 3 deletions internal/conf/settting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions internal/routers/api/api.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
65 changes: 3 additions & 62 deletions internal/routers/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{}
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 26f8cea

Please sign in to comment.