payment_waffo.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package setting
  2. import (
  3. "github.com/QuantumNous/new-api/common"
  4. "github.com/QuantumNous/new-api/constant"
  5. )
  6. var (
  7. WaffoEnabled bool
  8. WaffoApiKey string
  9. WaffoPrivateKey string
  10. WaffoPublicCert string
  11. WaffoSandboxPublicCert string
  12. WaffoSandboxApiKey string
  13. WaffoSandboxPrivateKey string
  14. WaffoSandbox bool
  15. WaffoMerchantId string
  16. WaffoNotifyUrl string
  17. WaffoReturnUrl string
  18. WaffoSubscriptionReturnUrl string
  19. WaffoCurrency string
  20. WaffoUnitPrice float64 = 1.0
  21. WaffoMinTopUp int = 1
  22. )
  23. // GetWaffoPayMethods 从 options 读取 Waffo 支付方式配置
  24. func GetWaffoPayMethods() []constant.WaffoPayMethod {
  25. common.OptionMapRWMutex.RLock()
  26. jsonStr := common.OptionMap["WaffoPayMethods"]
  27. common.OptionMapRWMutex.RUnlock()
  28. if jsonStr == "" {
  29. return copyDefaultWaffoPayMethods()
  30. }
  31. var methods []constant.WaffoPayMethod
  32. if err := common.UnmarshalJsonStr(jsonStr, &methods); err != nil {
  33. return copyDefaultWaffoPayMethods()
  34. }
  35. return methods
  36. }
  37. // SetWaffoPayMethods 序列化 Waffo 支付方式配置并更新 OptionMap
  38. func SetWaffoPayMethods(methods []constant.WaffoPayMethod) error {
  39. jsonBytes, err := common.Marshal(methods)
  40. if err != nil {
  41. return err
  42. }
  43. common.OptionMapRWMutex.Lock()
  44. common.OptionMap["WaffoPayMethods"] = string(jsonBytes)
  45. common.OptionMapRWMutex.Unlock()
  46. return nil
  47. }
  48. func copyDefaultWaffoPayMethods() []constant.WaffoPayMethod {
  49. cp := make([]constant.WaffoPayMethod, len(constant.DefaultWaffoPayMethods))
  50. copy(cp, constant.DefaultWaffoPayMethods)
  51. return cp
  52. }
  53. // WaffoPayMethods2JsonString 将默认 WaffoPayMethods 序列化为 JSON 字符串(供 InitOptionMap 使用)
  54. func WaffoPayMethods2JsonString() string {
  55. jsonBytes, err := common.Marshal(constant.DefaultWaffoPayMethods)
  56. if err != nil {
  57. return "[]"
  58. }
  59. return string(jsonBytes)
  60. }