优化支付宝和微信支付服务参数:

1. 删减支付宝参数,只需要传入应用私钥和支付宝公钥。
2. 所有的秘钥直接写入配置文档传入,而不是传入秘钥文件路径,简化参数配置
This commit is contained in:
GeekMaster
2025-07-25 19:15:36 +08:00
parent 6cfc7175e8
commit c19b7db8c0
3 changed files with 11 additions and 29 deletions

View File

@@ -53,11 +53,8 @@ type AlipayConfig struct {
Enabled bool // 是否启用该支付通道
SandBox bool // 是否沙盒环境
AppId string // 应用 ID
UserId string // 支付宝用户 ID
PrivateKey string // 用户私钥文件路径
PublicKey string // 用户公钥文件路径
AlipayPublicKey string // 支付宝公钥文件路径
RootCert string // Root 秘钥路径
PrivateKey string // 商户私钥
AlipayPublicKey string // 支付宝公钥
NotifyURL string // 异步通知地址
ReturnURL string // 同步通知地址
}
@@ -67,7 +64,7 @@ type WechatPayConfig struct {
AppId string // 公众号的APPID,如wxd678efh567hg6787
MchId string // 直连商户的商户号,由微信支付生成并下发
SerialNo string // 商户证书的证书序列号
PrivateKey string // 户私钥文件路径
PrivateKey string // 户私钥
ApiV3Key string // API V3 秘钥
NotifyURL string // 异步通知地址
}

View File

@@ -12,10 +12,11 @@ import (
"fmt"
"geekai/core/types"
logger2 "geekai/logger"
"github.com/go-pay/gopay"
"github.com/go-pay/gopay/alipay"
"net/http"
"os"
"github.com/go-pay/gopay"
"github.com/go-pay/gopay/alipay"
)
type AlipayService struct {
@@ -31,25 +32,12 @@ func NewAlipayService(appConfig *types.AppConfig) (*AlipayService, error) {
logger.Info("Disabled Alipay service")
return nil, nil
}
priKey, err := readKey(config.PrivateKey)
if err != nil {
return nil, fmt.Errorf("error with read App Private key: %v", err)
}
client, err := alipay.NewClient(config.AppId, priKey, !config.SandBox)
client, err := alipay.NewClient(config.AppId, config.PrivateKey, !config.SandBox)
if err != nil {
return nil, fmt.Errorf("error with initialize alipay service: %v", err)
}
//client.DebugSwitch = gopay.DebugOn // 开启调试模式
client.SetLocation(alipay.LocationShanghai). // 设置时区,不设置或出错均为默认服务器时间
SetCharset(alipay.UTF8). // 设置字符编码,不设置默认 utf-8
SetSignType(alipay.RSA2) // 设置签名类型,不设置默认 RSA2
if err = client.SetCertSnByPath(config.PublicKey, config.RootCert, config.AlipayPublicKey); err != nil {
return nil, fmt.Errorf("error with load payment public key: %v", err)
}
return &AlipayService{config: &config, client: client}, nil
}

View File

@@ -11,10 +11,11 @@ import (
"context"
"fmt"
"geekai/core/types"
"github.com/go-pay/gopay"
"github.com/go-pay/gopay/wechat/v3"
"net/http"
"time"
"github.com/go-pay/gopay"
"github.com/go-pay/gopay/wechat/v3"
)
type WechatPayService struct {
@@ -28,12 +29,8 @@ func NewWechatService(appConfig *types.AppConfig) (*WechatPayService, error) {
logger.Info("Disabled WechatPay service")
return nil, nil
}
priKey, err := readKey(config.PrivateKey)
if err != nil {
return nil, fmt.Errorf("error with read App Private key: %v", err)
}
client, err := wechat.NewClientV3(config.MchId, config.SerialNo, config.ApiV3Key, priKey)
client, err := wechat.NewClientV3(config.MchId, config.SerialNo, config.ApiV3Key, config.PrivateKey)
if err != nil {
return nil, fmt.Errorf("error with initialize WechatPay service: %v", err)
}