Merge pull request #189 from rocboss/jc/alimy

optimize alipay initial client logic
pull/191/head
Michael Li 2 years ago committed by GitHub
commit 26f8cea4e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,7 @@
data data
.vscode .vscode
release release
custom
*Dockerfile* *Dockerfile*
web/node_modules web/node_modules
web/src-tauri/target web/src-tauri/target

5
.gitignore vendored

@ -4,5 +4,6 @@
config.yaml config.yaml
*.log *.log
paopao-ce* paopao-ce*
release /release
data /data
/custom

@ -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 ```sh
cp config.yaml.sample config.yaml cp config.yaml.sample config.yaml

@ -15,7 +15,7 @@ Features:
Develop: ["Base", "MySQL", "BigCacheIndex", "Meili", "Sms", "AliOSS", "LoggerMeili", "OSS:Retention"] Develop: ["Base", "MySQL", "BigCacheIndex", "Meili", "Sms", "AliOSS", "LoggerMeili", "OSS:Retention"]
Demo: ["Base", "MySQL", "Option", "Zinc", "Sms", "MinIO", "LoggerZinc", "Migration"] Demo: ["Base", "MySQL", "Option", "Zinc", "Sms", "MinIO", "LoggerZinc", "Migration"]
Slim: ["Base", "Sqlite3", "LocalOSS", "LoggerFile", "OSS:TempDir"] Slim: ["Base", "Sqlite3", "LocalOSS", "LoggerFile", "OSS:TempDir"]
Base: ["Redis", "Alipay", "PhoneBind"] Base: ["Redis", "PhoneBind"]
Option: ["SimpleCacheIndex"] Option: ["SimpleCacheIndex"]
Sms: "SmsJuhe" Sms: "SmsJuhe"
SmsJuhe: SmsJuhe:
@ -24,8 +24,11 @@ SmsJuhe:
TplID: TplID:
TplVal: "#code#=%d&#m#=%d" TplVal: "#code#=%d&#m#=%d"
Alipay: Alipay:
AppID: AppID:
PrivateKey: InProduction: True
RootCertFile: "custom/alipay/RootCert.crt"
PublicCertFile: "custom/alipay/CertPublicKey_RSA2.crt"
AppPublicCertFile: "custom/alipay/AppCertPublicKey.crt"
CacheIndex: CacheIndex:
MaxUpdateQPS: 100 # 最大添加/删除/更新Post的QPS, 设置范围[10, 10000], 默认100 MaxUpdateQPS: 100 # 最大添加/删除/更新Post的QPS, 设置范围[10, 10000], 默认100
SimpleCacheIndex: # 缓存泡泡广场消息流 SimpleCacheIndex: # 缓存泡泡广场消息流

@ -75,8 +75,12 @@ type BigCacheIndexSettingS struct {
} }
type AlipaySettingS struct { type AlipaySettingS struct {
AppID string AppID string
PrivateKey string PrivateKey string
RootCertFile string
PublicCertFile string
AppPublicCertFile string
InProduction bool
} }
type SmsJuheSettings struct { type SmsJuheSettings struct {
@ -204,7 +208,7 @@ func NewSetting() (*Setting, error) {
vp := viper.New() vp := viper.New()
vp.SetConfigName("config") vp.SetConfigName("config")
vp.AddConfigPath(".") vp.AddConfigPath(".")
vp.AddConfigPath("configs/") vp.AddConfigPath("custom/")
vp.SetConfigType("yaml") vp.SetConfigType("yaml")
err := vp.ReadInConfig() err := vp.ReadInConfig()
if err != nil { if err != nil {

@ -1,14 +1,46 @@
package api package api
import ( import (
"github.com/rocboss/paopao-ce/internal/conf"
"github.com/rocboss/paopao-ce/internal/core" "github.com/rocboss/paopao-ce/internal/core"
"github.com/rocboss/paopao-ce/internal/dao" "github.com/rocboss/paopao-ce/internal/dao"
"github.com/sirupsen/logrus"
"github.com/smartwalle/alipay/v3"
) )
var ( var (
alipayClient *alipay.Client
objectStorage core.ObjectStorageService objectStorage core.ObjectStorageService
) )
func Initialize() { func Initialize() {
objectStorage = dao.ObjectStorageService() 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)
}
} }

@ -6,7 +6,6 @@ import (
"unicode/utf8" "unicode/utf8"
"github.com/gin-gonic/gin" "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/model"
"github.com/rocboss/paopao-ce/internal/service" "github.com/rocboss/paopao-ce/internal/service"
"github.com/rocboss/paopao-ce/pkg/app" "github.com/rocboss/paopao-ce/pkg/app"
@ -413,36 +412,6 @@ func GetUserRechargeLink(c *gin.Context) {
logrus.Errorf("service.CreateRecharge err: %v\n", err) logrus.Errorf("service.CreateRecharge err: %v\n", err)
response.ToErrorResponse(errcode.RechargeReqFail) response.ToErrorResponse(errcode.RechargeReqFail)
return 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{} p := alipay.TradePreCreate{}
@ -451,7 +420,7 @@ func GetUserRechargeLink(c *gin.Context) {
p.TotalAmount = fmt.Sprintf("%.2f", float64(recharge.Amount)/100.0) p.TotalAmount = fmt.Sprintf("%.2f", float64(recharge.Amount)/100.0)
p.NotifyURL = "https://" + c.Request.Host + "/v1/alipay/notify" p.NotifyURL = "https://" + c.Request.Host + "/v1/alipay/notify"
rsp, err := client.TradePreCreate(p) rsp, err := alipayClient.TradePreCreate(p)
if err != nil { if err != nil {
logrus.Errorf("client.TradePreCreate err: %v\n", err) logrus.Errorf("client.TradePreCreate err: %v\n", err)
response.ToErrorResponse(errcode.RechargeReqFail) response.ToErrorResponse(errcode.RechargeReqFail)
@ -495,37 +464,9 @@ func AlipayNotify(c *gin.Context) {
response := app.NewResponse(c) response := app.NewResponse(c)
c.Request.ParseForm() c.Request.ParseForm()
aliClient, err := alipay.New(conf.AlipaySetting.AppID, conf.AlipaySetting.PrivateKey, true) _, err := alipayClient.GetTradeNotification(c.Request)
// 将 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)
if err != nil { if err != nil {
logrus.Errorf("aliClient.GetTradeNotification err: %v\n", err) logrus.Errorf("alipayClient.GetTradeNotification err: %v\n", err)
logrus.Infoln(c.Request.Form) logrus.Infoln(c.Request.Form)
response.ToErrorResponse(errcode.RechargeNotifyError) response.ToErrorResponse(errcode.RechargeNotifyError)
return return

Loading…
Cancel
Save