63 lines
2.0 KiB
Go
63 lines
2.0 KiB
Go
package payment
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"github.com/qianfree/team-api/internal/logic/common"
|
|
)
|
|
|
|
// GetGlobalPaymentSettings loads global payment settings from ConfigService.
|
|
// Currency 固定返回 "CNY":订单/支付层币种由收款渠道决定(epay 收人民币),
|
|
// 系统计账与显示货币由本位币 billing_currency 决定(初始化选定,不在此配置)。
|
|
func GetGlobalPaymentSettings(ctx context.Context) (*GlobalPaymentSettings, error) {
|
|
cfg := common.Config()
|
|
settings := &GlobalPaymentSettings{
|
|
AmountOptions: []int{10, 20, 50, 100, 200, 500},
|
|
AmountDiscount: map[int]float64{},
|
|
MinTopUp: 1,
|
|
Currency: "CNY",
|
|
}
|
|
|
|
if val := cfg.GetString(ctx, "payment_amount_options"); val != "" {
|
|
json.Unmarshal([]byte(val), &settings.AmountOptions)
|
|
}
|
|
if val := cfg.GetString(ctx, "payment_amount_discount"); val != "" {
|
|
json.Unmarshal([]byte(val), &settings.AmountDiscount)
|
|
}
|
|
if n := cfg.GetFloat(ctx, "payment_min_topup"); n > 0 {
|
|
settings.MinTopUp = n
|
|
}
|
|
settings.CallbackBaseURL = cfg.GetString(ctx, "payment_callback_base_url")
|
|
|
|
return settings, nil
|
|
}
|
|
|
|
// SaveGlobalPaymentSettings persists global payment settings via ConfigService.
|
|
// Currency 字段不落库(订单层币种恒 CNY,不接受配置)。
|
|
func SaveGlobalPaymentSettings(ctx context.Context, settings *GlobalPaymentSettings) error {
|
|
cfg := common.Config()
|
|
items := map[string]string{
|
|
"payment_amount_options": mustJSON(settings.AmountOptions),
|
|
"payment_amount_discount": mustJSON(settings.AmountDiscount),
|
|
"payment_min_topup": mustJSON(settings.MinTopUp),
|
|
"payment_callback_base_url": settings.CallbackBaseURL,
|
|
}
|
|
for key, value := range items {
|
|
if err := cfg.SetOption(ctx, key, value); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetCallbackBaseURL returns the payment callback base URL.
|
|
func GetCallbackBaseURL(ctx context.Context) string {
|
|
return common.Config().GetString(ctx, "payment_callback_base_url")
|
|
}
|
|
|
|
func mustJSON(v interface{}) string {
|
|
b, _ := json.Marshal(v)
|
|
return string(b)
|
|
}
|