f
This commit is contained in:
@@ -15,12 +15,17 @@ import (
|
||||
)
|
||||
|
||||
type AlipayConfig struct {
|
||||
AppID string
|
||||
PrivateKey string
|
||||
AlipayPublicKey string
|
||||
IsProduction bool
|
||||
NotifyUrl string
|
||||
ReturnURL string // 同步回调地址
|
||||
AppID string
|
||||
Mode string
|
||||
PrivateKey string
|
||||
AlipayPublicKey string
|
||||
AppCertPath string
|
||||
AlipayCertPath string
|
||||
AlipayRootCertPath string
|
||||
EncryptKey string
|
||||
IsProduction bool
|
||||
NotifyUrl string
|
||||
ReturnURL string // 同步回调地址
|
||||
}
|
||||
type AliPayService struct {
|
||||
config AlipayConfig
|
||||
@@ -34,11 +39,30 @@ func NewAliPayService(config AlipayConfig) *AliPayService {
|
||||
panic(fmt.Sprintf("创建支付宝客户端失败: %v", err))
|
||||
}
|
||||
|
||||
// 加载支付宝公钥
|
||||
err = client.LoadAliPayPublicKey(config.AlipayPublicKey)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("加载支付宝公钥失败: %v", err))
|
||||
if config.Mode == "cert" {
|
||||
// 证书模式:加载应用公钥证书、支付宝公钥证书、支付宝根证书
|
||||
if err = client.LoadAppCertPublicKeyFromFile(config.AppCertPath); err != nil {
|
||||
panic(fmt.Sprintf("加载应用公钥证书失败: %v", err))
|
||||
}
|
||||
if err = client.LoadAlipayCertPublicKeyFromFile(config.AlipayCertPath); err != nil {
|
||||
panic(fmt.Sprintf("加载支付宝公钥证书失败: %v", err))
|
||||
}
|
||||
if err = client.LoadAliPayRootCertFromFile(config.AlipayRootCertPath); err != nil {
|
||||
panic(fmt.Sprintf("加载根证书失败: %v", err))
|
||||
}
|
||||
} else {
|
||||
// 默认密钥模式:加载支付宝公钥
|
||||
err = client.LoadAliPayPublicKey(config.AlipayPublicKey)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("加载支付宝公钥失败: %v", err))
|
||||
}
|
||||
}
|
||||
|
||||
// 可选:开启支付宝接口内容加密(AES)
|
||||
if config.EncryptKey != "" {
|
||||
client.SetEncryptKey(config.EncryptKey)
|
||||
}
|
||||
|
||||
return &AliPayService{
|
||||
config: config,
|
||||
AlipayClient: client,
|
||||
|
||||
Reference in New Issue
Block a user