Skip to content

feat(topup): match highest qualifying amount discount threshold - #8438

Open
leoravyn wants to merge 1 commit into
QuantumNous:mainfrom
leoravyn:feat/tiered-topup-discount
Open

leoravyn wants to merge 1 commit into
QuantumNous:mainfrom
leoravyn:feat/tiered-topup-discount

Conversation

@leoravyn

@leoravyn leoravyn commented Nov 24, 2026 •

Copy link
Copy Markdown

🔗 关联任务 / Related Issue

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix)
  • ✨ 新功能 (New feature)
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

📝 变更描述 / Description

将充值金额折扣 AmountDiscount 从精确键值匹配调整为“匹配不超过充值金额的最大正阈值”,并同步前后端与不同支付网关的优惠展示与计价口径:

  • 后端阶梯匹配:在 setting/operation_setting/payment_setting.go 新增 PaymentSetting.MatchAmountDiscount(amount int64) float64,在配置映射中选取 0 < threshold <= amount 且 rate > 0 的最大阈值对应折扣(未命中返回 1.0),并在 controller/topup.go(getPayMoney)、controller/topup_waffo.go(getWaffoPayMoney)与 controller/topup_waffo_pancake.go(getWaffoPancakePayMoney)中统一调用。
  • Stripe 保持精确匹配:有意保留 controller/topup_stripe.go 的精确键值逻辑不变(Stripe Checkout 当前使用固定 StripePriceId + Quantity 与 AllowPromotionCodes,未更改结账定价方式),避免报价与 Stripe Checkout 实际扣款脱节。
  • 前端钱包同步:在 web/src/features/wallet/lib/format.ts 新增 getMatchedAmountDiscount 与 getDiscountRateForPaymentType(Stripe 走精确匹配、其他网关走阶梯匹配),同步更新 mergePresetAmounts、Wallet 的 getDiscountRate 与 RechargeFormCard(根据当前激活支付方式 activePaymentType 门控阶梯折扣展示,确保预设卡片价格、应付金额与确认弹窗一致)。

📸 运行证明 / Proof of Work

  • 代码格式检查:gofmt -l setting/operation_setting/payment_setting.go setting/operation_setting/payment_setting_test.go controller/topup.go controller/topup_waffo.go controller/topup_waffo_pancake.go controller/topup_waffo_pancake_test.go 输出为空。
  • 后端单测(基于 main 6627f0e 在 golang:1.26.10-alpine 容器执行):
    • go test -p 1 -v ./setting/operation_setting -count=1 全包 13 项测试全部 PASS(含 TestPaymentSettingMatchAmountDiscount 11 个边界用例:精确命中、区间命中、低于最低档、零金额、空/nil map、非正键/倍率过滤)。
    • go test -p 1 -v ./controller -run "TestGetWaffoPancakePayMoney" -count=1 全部 PASS:在 AmountDiscount={5: 0, 10: 0.8, 100: 0.95, 200: 0.90, 1500000: 0.5}、Price=1.0 时,getPayMoney / getWaffoPayMoney 返回 9 -> 9.0、99 -> 79.2(命中 10 档)、105 -> 99.75(命中 100 档)、250 -> 225.0(命中 200 档);getWaffoPancakePayMoney(UnitPrice=2.5)返回 105 -> 249.375、250 -> 562.5。
  • 前端类型检查与组件单测(web/):
    • bun check -b:No type errors in 1,501 files across 2 projects(8.80s)。
    • bun --bun x vitest run src/features/wallet/__tests__/copy.test.tsx:9/9 用例全部 PASS(1.76s):验证了 mergePresetAmounts([50, 105, 200], {100: 0.95})、RechargeFormCard 在普通网关对 200 档显示 优惠 5%支付 190 • 节省 10,以及在仅启用 Stripe 或多网关选中 Stripe 时对 200 档仅显示 支付 200 而对精确命中档 100 保持 优惠 5%支付 95 • 节省 5。
  • 未验证说明:未在真实外部支付网关发起实际支付订单;当前主机无浏览器环境,前端 UI 行为通过 React Testing Library 组件测试与 DOM 文本断言验证,未附浏览器截图。

✅ 提交前检查项 / Checklist

  • 人工确认: 无论描述是否由 AI 生成,我已审阅并精炼全文,仅保留可复核的要点,并声明对其准确性与完整性负责。PR 正文及后续评论均未直接粘贴未经人工过滤的大段 AI 文本。
  • 非重复提交: 我已搜索现有的 Issues 与 PRs,确认不是重复提交。
  • 新功能关联 Issue: 若此 PR 标记为 New feature,我已关联对应 Issue;若尚无 Issue,我已先自行创建。
  • 事前沟通: 若改动较大或涉及方向性变更,已在关联 Issue 中与维护者沟通并达成一致。
  • 功能范围: 本 PR 不是 Coding Plan、逆向渠道、第三方封装接口,也不是对 Codex 渠道类型的改动。
  • 非透传: 本 PR 不是针对开启透传后的转发行为;透传会直接发送内容,不会经过 new-api 的逻辑处理。
  • 范围聚焦: 本 PR 为一项聚焦改动,未包含无关代码。
  • 本地验证: 已按变更路径实际验证,并写明命令与观察结果。仅声明 go build 通过或测试通过,不视为有效证明。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

Summary by CodeRabbit

  • New Features
    • Recharge discounts now apply by amount tier: eligible payments receive the rate for the highest qualifying threshold, including between configured tiers.
    • Discount handling reflects the selected payment method, with Stripe continuing to use exact-amount matches.
    • Recharge presets display savings based on the applicable discount rate.
  • Bug Fixes
    • Savings labels are no longer shown when the discount does not result in a positive displayed percentage.
    • Discounted payment amounts now use consistent tier matching across supported recharge methods.

Copilot AI balanced review requested due to automatic review settings November 24, 2026 12:13
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Backend top-up calculations and wallet discount handling now use the highest qualifying positive discount threshold. Stripe retains exact-amount matching in the wallet. Wallet preset displays and discount labels reflect the selected discount rate.

Changes

Amount Discount Matching

Layer / File(s) Summary
Backend tier-matching rule
setting/operation_setting/payment_setting.go, setting/operation_setting/payment_setting_test.go
MatchAmountDiscount selects the rate for the greatest positive threshold at or below the amount. It returns 1.0 when no threshold matches. Tests cover tier selection and fallback cases.
Backend gateway calculations
controller/topup.go, controller/topup_waffo.go, controller/topup_waffo_pancake.go, controller/topup_waffo_pancake_test.go
The Epay, Waffo, and Waffo Pancake calculations use MatchAmountDiscount. Tests cover tiered amounts and a non-positive discount case.
Wallet discount selection
web/src/features/wallet/lib/format.ts, web/src/features/wallet/lib/payment.ts, web/src/features/wallet/__tests__/copy.test.tsx
Wallet helpers use threshold matching for non-Stripe payment types and exact-key matching for Stripe. Preset merging uses the matched rate, and discount labels suppress non-positive discounts. Tests cover matching and preset discounts.
Wallet payment selection and display
web/src/features/wallet/components/recharge-form-card.tsx, web/src/features/wallet/index.tsx, web/src/features/wallet/__tests__/copy.test.tsx
The wallet passes the active payment type to preset cards. Cards select the corresponding discount behavior. Rendering tests cover Stripe-only and multi-gateway configurations.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Feature

Possibly related PRs

  • QuantumNous/new-api#8400: This PR implements the same tiered amount-discount matching behavior in backend calculations and wallet discount handling.

Suggested reviewers: seefs001, calcium-ion

Merge Risk: Minimal · up to 3ca48

The change makes top-up discounts follow the highest qualifying tier for Epay, Waffo, and Waffo Pancake, while Stripe keeps exact matching. The tests reported for the matcher, gateway calculations, and wallet display cover the changed behavior, so merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "feat(topup): match highest qualifying amount discount threshold" describes the main change. The PR replaces exact-key AmountDiscount lookup with largest qualifying threshold matching (Match…
Linked Issues check Passed Issue #8436 requires selecting the largest threshold k with 0 < k <= amount and rate > 0, otherwise returning 1.0. At head 3ca4865, PaymentSetting.MatchAmountDiscount in setting/operation_setting/paym…
Out of Scope Changes check Passed The changed files relate to the tiered AmountDiscount feature. The backend matcher and the three gateway pricing functions implement the issue. The cross-gateway and matcher tests verify that feature.…
Full details: Title check

Explanation

The title "feat(topup): match highest qualifying amount discount threshold" describes the main change. The PR replaces exact-key AmountDiscount lookup with largest qualifying threshold matching (MatchAmountDiscount) for Epay, Waffo, and Waffo Pancake, and keeps exact matching for Stripe. The wallet UI discount display also follows the active payment type. The title is specific and concise, and it follows a conventional commit style.

Full details: Linked Issues check

Explanation

Issue #8436 requires selecting the largest threshold k with 0 < k <= amount and rate > 0, otherwise returning 1.0. At head 3ca4865, PaymentSetting.MatchAmountDiscount in setting/operation_setting/payment_setting.go implements this rule. It returns 1.0 for a nil receiver, amount <= 0, an empty map, or no qualifying threshold. The head source of controller/topup.go shows getPayMoney calling MatchAmountDiscount(amount), which covers Epay. The PR summary states the same call in getWaffoPayMoney (controller/topup_waffo.go) and getWaffoPancakePayMoney (controller/topup_waffo_pancake.go). Stripe (controller/topup_stripe.go) is not in the changed files and keeps exact-key matching, as the issue requires. Frontend helpers in web/src/features/wallet/lib/format.ts (getMatchedAmountDiscount, getDiscountRateForPaymentType) apply threshold matching to non-Stripe types and exact lookup to Stripe. mergePresetAmounts, the Wallet callback, and RechargeFormCard use these helpers for preset cards and payable amounts. Automated tests cover the matcher (TestPaymentSettingMatchAmountDiscount), cross-gateway pricing, and frontend display (copy.test.tsx). Limits: the incremental diff from previous head 73fafcd to head could not be read, so changes in that range were checked only through head source and the change summary. The custom-amount hint path was checked only through the summary, not a full UI code read. Real payment gateways were not exercised, as the PR states.

Full details: Out of Scope Changes check

Explanation

The changed files relate to the tiered AmountDiscount feature. The backend matcher and the three gateway pricing functions implement the issue. The cross-gateway and matcher tests verify that feature. The frontend format, payment, wallet, and recharge-form changes implement the wallet display and payable-amount consistency that the issue requests. The change to controller/topup_waffo_pancake_test.go (non-positive-discount case moves from amount 20 to amount 5) is a required fixture update, because amount 20 now matches the threshold-10 discount. Head source of setting/operation_setting/payment_setting.go contains no unrelated code. The incremental diff from previous head 73fafcd could not be read, so unrelated changes in that range cannot be ruled out from diff evidence. The change summary lists no such change.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks the threshold row,
And finds the highest tier to show.
Stripe keeps its exact-key way,
While wallet cards display the pay.
Soft paws hop as savings glow.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f33d36ae-72ce-4fab-a176-64c7a096965d
Commits

Reviewing files that changed from the base of the PR and between 73fafcd and 3ca4865.

Files selected for processing: 1

  • controller/topup_waffo_pancake_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The tests add a second new backend test file and duplicate an existing settings fixture, violating the project’s requirement to extend suitable existing tests and add at most one new file for a small feature.

1 open issue
What changed in this PR

Implements #8436 by applying the highest qualifying recharge discount threshold across supported non-Stripe gateways and synchronizing wallet displays. Stripe retains its existing exact-match behavior.

Changes:

  • Adds shared threshold matching for Epay, Waffo, and Waffo Pancake pricing.
  • Makes wallet discount displays depend on the active payment method.
  • Adds regression coverage for threshold boundaries, gateway pricing, and Stripe-specific displays.
File Description
web/​src/​features/​wallet/​lib/​payment.ts Applies threshold discounts to preset amounts.
web/​src/​features/​wallet/​lib/​format.ts Adds discount matchers and suppresses empty discount labels.
web/​src/​features/​wallet/​index.tsx Uses the active payment method when resolving discounts.
web/​src/​features/​wallet/​components/​recharge-form-card.tsx Aligns preset discounts with the selected gateway.
web/​src/​features/​wallet/​__tests__/​copy.test.tsx Tests threshold matching and gateway-specific displays.
setting/​operation_setting/​payment_setting.go Adds the shared backend threshold matcher.
setting/​operation_setting/​payment_setting_test.go Covers matcher boundaries and invalid entries.
controller/​topup.go Uses threshold matching for Epay pricing.
controller/​topup_waffo.go Uses threshold matching for Waffo pricing.
controller/​topup_waffo_pancake.go Uses threshold matching for Waffo Pancake pricing.
controller/​topup_waffo_pancake_test.go Adjusts the non-positive discount regression case.
controller/​topup_discount_test.go Adds cross-gateway threshold pricing assertions.

Review effort: Balanced

Comment thread controller/topup_discount_test.go Resolved
@leoravyn
leoravyn force-pushed the feat/tiered-topup-discount branch from 73fafcd to 3ca4865 Compare November 24, 2026 12:43
leoravyn pushed a commit to leoravyn/new-api that referenced this pull request Nov 24, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: 充值金额折扣 AmountDiscount 支持按最大合格阈值阶梯匹配

2 participants