mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-12-05 08:03:01 +00:00
* feat(payment): 易支付支持 RSA 签名方式 支付设置新增「签名方式」,可在 MD5 与 RSA 之间二选一。默认 MD5,已配置 MD5 的站点升级后行为不变。 - RSA 模式用商户私钥签名下单请求,跳转 /api/pay/submit;充值和订阅回调用平台公钥验签,MD5 签名的回调会被拒绝 - 保存时校验密钥:RSA 生效期间商户私钥和平台公钥必须能解析,切换到 RSA 前要先保存两把密钥;读取设置时不返回密钥 - 设置页选择 MD5 时只显示商户密钥,选择 RSA 时显示商户私钥和平台公钥;七种语言补齐文案 - go-epay 升级到 v0.0.6 * refactor(payment): 易支付签名设置改用 payment_setting 注册配置 签名方式、商户私钥、平台公钥改为 PaymentSetting 的字段,键名为 payment_setting.epay_sign_type、payment_setting.epay_merchant_private_key、payment_setting.epay_platform_public_key,由配置管理器统一加载和保存,不再在 option.go 里逐项初始化和赋值。 - 保存校验移到 operation_setting.ValidateEpaySignOption,规则不变 - 读取设置时不返回以 private_key 结尾的配置项;平台公钥可以正常显示和修改
97 lines
3.4 KiB
Go
97 lines
3.4 KiB
Go
package operation_setting
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/Calcium-Ion/go-epay/epay"
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/setting/config"
|
|
)
|
|
|
|
type PaymentSetting struct {
|
|
AmountOptions []int `json:"amount_options"`
|
|
AmountDiscount map[int]float64 `json:"amount_discount"` // 充值金额对应的折扣,例如 100 元 0.9 表示 100 元充值享受 9 折优惠
|
|
|
|
ComplianceConfirmed bool `json:"compliance_confirmed"`
|
|
ComplianceTermsVersion string `json:"compliance_terms_version"`
|
|
ComplianceConfirmedAt int64 `json:"compliance_confirmed_at"`
|
|
ComplianceConfirmedBy int `json:"compliance_confirmed_by"`
|
|
ComplianceConfirmedIP string `json:"compliance_confirmed_ip"`
|
|
|
|
// 易支付签名方式:MD5 使用 EpayKey;RSA 使用商户私钥签名请求、平台公钥验证回调。
|
|
EpaySignType string `json:"epay_sign_type"`
|
|
EpayMerchantPrivateKey string `json:"epay_merchant_private_key"`
|
|
EpayPlatformPublicKey string `json:"epay_platform_public_key"`
|
|
}
|
|
|
|
const CurrentComplianceTermsVersion = "v1"
|
|
|
|
const (
|
|
EpaySignTypeMD5 = "MD5"
|
|
EpaySignTypeRSA = "RSA"
|
|
|
|
EpaySignTypeOptionKey = "payment_setting.epay_sign_type"
|
|
EpayMerchantPrivateKeyOptionKey = "payment_setting.epay_merchant_private_key"
|
|
EpayPlatformPublicKeyOptionKey = "payment_setting.epay_platform_public_key"
|
|
)
|
|
|
|
// 默认配置
|
|
var paymentSetting = PaymentSetting{
|
|
AmountOptions: []int{10, 20, 50, 100, 200, 500},
|
|
AmountDiscount: map[int]float64{},
|
|
// 没有保存过签名方式的站点按 MD5 运行,升级后行为不变。
|
|
EpaySignType: EpaySignTypeMD5,
|
|
}
|
|
|
|
func init() {
|
|
// 注册到全局配置管理器
|
|
config.GlobalConfig.Register("payment_setting", &paymentSetting)
|
|
}
|
|
|
|
func GetPaymentSetting() *PaymentSetting {
|
|
return &paymentSetting
|
|
}
|
|
|
|
func IsPaymentComplianceConfirmed() bool {
|
|
return paymentSetting.ComplianceConfirmed &&
|
|
paymentSetting.ComplianceTermsVersion == CurrentComplianceTermsVersion
|
|
}
|
|
|
|
// ValidateEpaySignOption checks a change to the Epay signing settings against
|
|
// the saved ones. While RSA is active, both keys must parse; under MD5 only a
|
|
// newly entered key is checked.
|
|
func ValidateEpaySignOption(key, value string) error {
|
|
if key != EpaySignTypeOptionKey && key != EpayMerchantPrivateKeyOptionKey && key != EpayPlatformPublicKeyOptionKey {
|
|
return nil
|
|
}
|
|
// Saved values are written under OptionMapRWMutex by the option update path.
|
|
common.OptionMapRWMutex.RLock()
|
|
signType := paymentSetting.EpaySignType
|
|
privateKey := paymentSetting.EpayMerchantPrivateKey
|
|
publicKey := paymentSetting.EpayPlatformPublicKey
|
|
common.OptionMapRWMutex.RUnlock()
|
|
switch key {
|
|
case EpaySignTypeOptionKey:
|
|
if value != EpaySignTypeMD5 && value != EpaySignTypeRSA {
|
|
return common.NewMessage("Epay signature type must be MD5 or RSA")
|
|
}
|
|
signType = value
|
|
case EpayMerchantPrivateKeyOptionKey:
|
|
privateKey = value
|
|
case EpayPlatformPublicKeyOptionKey:
|
|
publicKey = value
|
|
}
|
|
rsaActive := signType == EpaySignTypeRSA
|
|
if rsaActive || (key == EpayMerchantPrivateKeyOptionKey && strings.TrimSpace(value) != "") {
|
|
if _, err := epay.ParseRSAPrivateKey(privateKey); err != nil {
|
|
return common.NewMessage("The Epay merchant private key is missing or invalid")
|
|
}
|
|
}
|
|
if rsaActive || (key == EpayPlatformPublicKeyOptionKey && strings.TrimSpace(value) != "") {
|
|
if _, err := epay.ParseRSAPublicKey(publicKey); err != nil {
|
|
return common.NewMessage("The Epay platform public key is missing or invalid")
|
|
}
|
|
}
|
|
return nil
|
|
}
|