diff --git a/internal/logic/public/order/getDiscount.go b/internal/logic/public/order/getDiscount.go index db107f3..d5d59bf 100644 --- a/internal/logic/public/order/getDiscount.go +++ b/internal/logic/public/order/getDiscount.go @@ -2,17 +2,34 @@ package order 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. func getDiscount(discounts []types.SubscribeDiscount, inputMonths int64, isNewUser bool) float64 { - for _, discount := range discounts { - if discount.NewUserOnly && !isNewUser { + var best float64 = 1 + foundFallback := false + for _, d := range discounts { + if d.Quantity != inputMonths || d.Discount <= 0 || d.Discount >= 100 { continue } - if inputMonths == discount.Quantity && discount.Discount > 0 && discount.Discount < 100 { - return discount.Discount / float64(100) + 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 } } - - return 1 + return best } // isNewUserOnlyForQuantity checks whether the matched discount tier has new_user_only enabled.