shanshanzhong c431393266
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 7m44s
fix: 折扣匹配改为精确匹配 quantity,不再取全局最小值
将 getDiscount 和 isNewUserOnlyForQuantity 的匹配逻辑从
inputMonths >= discount.Quantity 改为 inputMonths == discount.Quantity,
买多少天就用多少天对应的折扣,避免错误配置导致折扣泄漏。
同时增加 discount 值合法性校验(>0 且 <100)。

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-03-26 04:18:45 -07:00

29 lines
750 B
Go

package order
import "github.com/perfect-panel/server/internal/types"
func getDiscount(discounts []types.SubscribeDiscount, inputMonths int64, isNewUser bool) float64 {
for _, discount := range discounts {
if discount.NewUserOnly && !isNewUser {
continue
}
if inputMonths == discount.Quantity && discount.Discount > 0 && discount.Discount < 100 {
return discount.Discount / float64(100)
}
}
return 1
}
// isNewUserOnlyForQuantity checks whether the matched discount tier has new_user_only enabled.
func isNewUserOnlyForQuantity(discounts []types.SubscribeDiscount, inputQuantity int64) bool {
for _, discount := range discounts {
if inputQuantity == discount.Quantity {
return discount.NewUserOnly
}
}
return false
}