diff --git a/internal/logic/public/order/getDiscount.go b/internal/logic/public/order/getDiscount.go index d5d59bf..565c14b 100644 --- a/internal/logic/public/order/getDiscount.go +++ b/internal/logic/public/order/getDiscount.go @@ -4,32 +4,34 @@ import "github.com/perfect-panel/server/internal/types" // getDiscount returns the discount factor for the given quantity. // -// Design: when a quantity has both a new_user_only=true entry and a new_user_only=false entry, -// the new_user_only=true entry is a "gating marker" (its discount value is irrelevant). -// The actual discounted price is always carried by the new_user_only=false entry. -// Whether the buyer is eligible to use this quantity is enforced by isNewUserOnlyForQuantity -// before calling this function. Here we simply return the best applicable discount. -// -// Priority: new_user_only=false entry beats new_user_only=true entry for the same quantity. +// - New user: pick the tier with the lowest discount value (best deal) among all matching tiers. +// - Non-new user: pick the tier with the highest discount value (most expensive / least discount) +// among tiers where new_user_only=false only. func getDiscount(discounts []types.SubscribeDiscount, inputMonths int64, isNewUser bool) float64 { - var best float64 = 1 - foundFallback := false + best := float64(-1) for _, d := range discounts { if d.Quantity != inputMonths || d.Discount <= 0 || d.Discount >= 100 { continue } - factor := d.Discount / float64(100) - if !d.NewUserOnly { - // Prefer the non-new-user-only tier as the actual price tier. - best = factor - foundFallback = true - } else if !foundFallback && isNewUser { - // Fall back to new-user-only tier only when no general tier exists - // and the buyer qualifies. - best = factor + if !isNewUser && d.NewUserOnly { + continue + } + if isNewUser { + // lowest discount value = biggest saving + if best < 0 || d.Discount < best { + best = d.Discount + } + } else { + // highest discount value = least discount (closest to original price) + if best < 0 || d.Discount > best { + best = d.Discount + } } } - return best + if best < 0 { + return 1 + } + return best / float64(100) } // isNewUserOnlyForQuantity checks whether the matched discount tier has new_user_only enabled.