billing.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package service
  2. import (
  3. "fmt"
  4. "github.com/QuantumNous/new-api/logger"
  5. relaycommon "github.com/QuantumNous/new-api/relay/common"
  6. "github.com/QuantumNous/new-api/types"
  7. "github.com/gin-gonic/gin"
  8. )
  9. const (
  10. BillingSourceWallet = "wallet"
  11. BillingSourceSubscription = "subscription"
  12. )
  13. // PreConsumeBilling 根据用户计费偏好创建 BillingSession 并执行预扣费。
  14. // 会话存储在 relayInfo.Billing 上,供后续 Settle / Refund 使用。
  15. func PreConsumeBilling(c *gin.Context, preConsumedQuota int, relayInfo *relaycommon.RelayInfo) *types.NewAPIError {
  16. session, apiErr := NewBillingSession(c, relayInfo, preConsumedQuota)
  17. if apiErr != nil {
  18. return apiErr
  19. }
  20. relayInfo.Billing = session
  21. return nil
  22. }
  23. // ---------------------------------------------------------------------------
  24. // SettleBilling — 后结算辅助函数
  25. // ---------------------------------------------------------------------------
  26. // SettleBilling 执行计费结算。如果 RelayInfo 上有 BillingSession 则通过 session 结算,
  27. // 否则回退到旧的 PostConsumeQuota 路径(兼容按次计费等场景)。
  28. func SettleBilling(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, actualQuota int) error {
  29. if relayInfo.Billing != nil {
  30. preConsumed := relayInfo.Billing.GetPreConsumedQuota()
  31. delta := actualQuota - preConsumed
  32. if delta > 0 {
  33. logger.LogInfo(ctx, fmt.Sprintf("预扣费后补扣费:%s(实际消耗:%s,预扣费:%s)",
  34. logger.FormatQuota(delta),
  35. logger.FormatQuota(actualQuota),
  36. logger.FormatQuota(preConsumed),
  37. ))
  38. } else if delta < 0 {
  39. logger.LogInfo(ctx, fmt.Sprintf("预扣费后返还扣费:%s(实际消耗:%s,预扣费:%s)",
  40. logger.FormatQuota(-delta),
  41. logger.FormatQuota(actualQuota),
  42. logger.FormatQuota(preConsumed),
  43. ))
  44. } else {
  45. logger.LogInfo(ctx, fmt.Sprintf("预扣费与实际消耗一致,无需调整:%s(按次计费)",
  46. logger.FormatQuota(actualQuota),
  47. ))
  48. }
  49. if err := relayInfo.Billing.Settle(actualQuota); err != nil {
  50. return err
  51. }
  52. // 发送额度通知(订阅计费使用订阅剩余额度)
  53. if actualQuota != 0 {
  54. if relayInfo.BillingSource == BillingSourceSubscription {
  55. checkAndSendSubscriptionQuotaNotify(relayInfo)
  56. } else {
  57. checkAndSendQuotaNotify(relayInfo, actualQuota-preConsumed, preConsumed)
  58. }
  59. }
  60. return nil
  61. }
  62. // 回退:无 BillingSession 时使用旧路径
  63. quotaDelta := actualQuota - relayInfo.FinalPreConsumedQuota
  64. if quotaDelta != 0 {
  65. return PostConsumeQuota(relayInfo, quotaDelta, relayInfo.FinalPreConsumedQuota, true)
  66. }
  67. return nil
  68. }