Skip to content

fix(subscription): charge Epay subscriptions at the price per USD (HQ-Task T261105-08) - #4

Merged
AdoreJc merged 1 commit into
fork-mainfrom
hq/T261105-08-subscription-epay-price
Nov 6, 2026
Merged

AdoreJc merged 1 commit into
fork-mainfrom
hq/T261105-08-subscription-epay-price

Conversation

@AdoreJc

@AdoreJc AdoreJc commented Nov 6, 2026

Copy link
Copy Markdown
Owner

HQ-Task: T261105-08

Agent

  • Tool: Claude Code (desktop app, Code tab; session started by the HQ supervisor session)
  • Tool version: Claude Code desktop app (version not exposed to the session)
  • Model (full id): claude-fable-5-1
  • Host (CLI / IDE / GitHub coding agent / other): other (Claude desktop app, Windows 10)
  • Date (UTC): 2026-11-05

Note: the git user (AdoreJC) is not a historical core developer of this repository. The code in this PR was AI-generated under the user's instructions and reviewed by the user's HQ supervisor process.

Links

User request

  • Verbatim:

HQ-Task T261105-08:new-api(D:/WSL/workspace/repos/new-api,origin = AdoreJc/new-api,fork)订阅用 Epay 支付时,金额要按「每美元余额的收费金额」换算后再传给支付商,而不是把套餐的美元价直接当成支付金额。

要做的事:

  1. Epay 订阅支付:实付金额 = plan.PriceAmount × operation_setting.Price,用 decimal 计算、两位小数,与 getPayMoney 的换算口径一致(订阅不套充值分组倍率和充值预设折扣;若你认为应当套用,在 PR 里说明理由并做成可选)。把换算抽成可复用的函数,getPayMoney 与订阅共用汇率来源,避免两处漂移。下限检查改为按换算后的实付金额。
  2. SubscriptionOrder.Money 与回调校验:查清 /api/subscription/epay/notify 回调里怎样核对金额(对比 controller/topup.go 的 Epay notify 处理),保证订单记录的金额、Epay 回调的金额、日志里的「payment amount」三者一致,不能让换算后的订单被回调校验拒掉或被篡改金额通过。若需要同时保留美元价和实付金额,优先加字段而不是改语义,并说明三库兼容。
  3. 前端:订阅购买对话框(subscription-purchase-dialog.tsx、subscription-plans-card.tsx)在选择 Epay 支付时显示换算后的实付金额和货币,参考充值页展示「实付」的做法;需要后端提供汇率时复用已有的支付设置接口,不新增泄露配置的接口。新增文案进 7 个 locale 文件,不改既有键的转义。
  4. 其他支付方式(Stripe 用 price id、Creem、Waffo)只排查是否有同样问题,不改;在 PR 的 Not verified / Follow-ups 里写明结论。
  5. 测试:为换算函数和 Epay 订阅下单、notify 金额核对写 testify 测试(汇率 7.3 时 $10 → 73.00;汇率 1;下限;回调金额不符被拒)。
  6. Docker 验证:tailnet 测试机 … 在 UI 上:设置每美元价格为 7.3,建一个 $10 套餐,用 Epay 下单,确认请求支付商的 money 是 73.00。
  • Later constraints or corrections from the user (quote, or none): none

Out of scope — refuse

  • Matched: no
  • If yes, what was told to the user (stop here; do not open a PR): n/a

Open gate — do not open unless all are satisfied

  • Out of scope (including pass-through-only forwarding): no
  • Usage / configuration / integration (answered instead of opening): no
  • Required issue facts present without invention: yes (from the user's production observation, see below)
  • Verification is actual commands or steps and observed results, not only go build or tests passed: yes
  • Body is short and factual; no unfiltered AI-generated text: yes
  • Open: yes
  • If no, what was told to the user (stop here): n/a

Kind

  • Bug fix
  • New feature
  • Performance / refactor
  • Docs
  • Other:

Issue facts

  • Actual behavior: a subscription plan priced $10 paid through Epay creates a gateway order with money=10.00; the top-up page charges 10 × 每美元充值价格 (7.3) = 73.00 for the same $10 of credit.
  • Impact: every Epay subscription purchase is under-charged by the configured price-per-USD factor (7.3× by default); the user saw this on their production deployment.
  • Frequency: deterministic, every Epay subscription order.
  • Evidence that the problem is in new-api rather than the client or upstream: controller/subscription_payment_epay.go (fork-main 1f7d6cb) passes plan.PriceAmount unchanged as epay.PurchaseArgs.Money, while controller/topup.go getPayMoney multiplies by operation_setting.Price for top-ups.
  • Applicable types and their fields (relay / billing / frontend / deployment; write "not applicable" otherwise):
    • billing: Epay subscription order amount (SubscriptionOrder.Money, epay.PurchaseArgs.Money), price-per-USD setting operation_setting.Price.
    • frontend: subscription purchase dialog Epay section (web/src/features/subscriptions/components/dialogs/subscription-purchase-dialog.tsx).
    • deployment: SQLite on the tailnet test host (Docker image built from this branch); no schema change.

Change

  • controller/topup.go: new payMoneyFromUSD(usd decimal.Decimal) = usd × operation_setting.Price. getPayMoney now calls it (same multiplication order as before, so top-up amounts are unchanged); it is the single exchange-rate source for Epay top-ups and Epay subscriptions.
  • controller/subscription_payment_epay.go:
    • subscriptionEpayMoney(plan) = payMoneyFromUSD(plan.PriceAmount); the handler rejects it below 0.01 before rounding (same bound as getPayMoney), then rounds half-away-from-zero to 2 decimals. That rounded value goes to both SubscriptionOrder.Money and epay.PurchaseArgs.Money (StringFixed(2)). Top-up group ratio and top-up preset discounts are not applied: plans carry a fixed list price and the discounts are keyed by top-up amount, so they have no meaning for a plan; nothing optional was added.
    • completeEpaySubscriptionOrder (shared by /api/subscription/epay/notify and /api/subscription/epay/return): after the signature check it loads the order by trade number, checks PaymentProvider == epay, and compares the callback money with order.Money at 2 decimals (epayCallbackMoneyMatches); a mismatch or unparsable amount is logged and answered with fail / ?pay=fail, and the order stays pending. Only then does it call the unchanged model.CompleteSubscriptionOrder.
    • Order creation and callback outcomes are now logged like the top-up path (Epay subscription order created … price_usd=10.00 price_per_usd=7.3000 money=73.00), with zh-CN lines in common/log_text.zh-CN.json.
  • SubscriptionOrder.Money semantics: for Epay orders it is now the amount charged by the gateway in the gateway currency, which is what TopUp.Money already means for Epay top-ups and what upsertSubscriptionTopUpTx copies into the mirrored topups row (billing history "Payment" column) and what the "Subscription purchased … payment amount: {{amount}}" log prints. Order record, Epay callback money and the log therefore all read 73.00. No column was added: the USD list price is still reachable through plan_id, no consumer needs a USD snapshot on the order, and avoiding a schema change keeps this PR free of the 3-database migration matrix (MySQL cannot be pulled on the test host, see "Not verified"). Balance / Stripe / Creem / Waffo orders keep writing plan.PriceAmount as before.
  • Frontend: the purchase dialog takes priceRatio (the price field /api/status already exposes, the same source the top-up page uses — no new endpoint) and, when Epay is available, shows "Online payment amount" = formatLocalCurrencyAmount(round2(price_amount × priceRatio)) and renders the Epay button as Pay {{amount}}. subscription-plans-card.tsx and wallet/index.tsx only pass the prop through. New key Online payment amount in all 7 locale files, inserted in sorted position without touching existing lines.

Research

Duplicate / prior art

Docs and code

  • https://docs.newapi.ai/ : not consulted; the meaning of Price was taken from the settings UI label (How much to charge for each US dollar of balance (Epay) / 「每美元余额(Epay)的收费金额」 in the payment settings) and from getPayMoney, which is the only other consumer.
  • https://deepwiki.com/QuantumNous/new-api : not consulted (fork-internal task on code already read in full).
  • README / repo docs: .agents/rules/billing.md (read in full; this change does not touch quota conversion, it only converts money for the gateway), AGENTS.md backend/frontend rules.
  • Code paths and what they imply for this change: controller/topup.go getPayMoney / RequestEpay / EpayNotify + model.RechargeEpay (top-up: converts money, but the notify path does not compare the callback amount either); controller/subscription_payment_epay.go + model.CompleteSubscriptionOrder / upsertSubscriptionTopUpTx (order money is copied into topups.money and the log); web/src/features/wallet/index.tsx passes status.price as priceRatio to the recharge card, so the same value is passed to the subscription card.

Alternatives considered

  • Option A: keep SubscriptionOrder.Money as the USD price and add a new pay_money column with the converted amount. Rejected: needs an AutoMigrate column on three databases, and every existing consumer of Money (mirrored top-up row, log) wants the gateway amount anyway.
  • Option B: put the money comparison inside model.CompleteSubscriptionOrder. Rejected: that function is shared by Stripe, Creem and Waffo callbacks whose amounts live in different payloads; the Epay-specific check sits next to the Epay signature check instead, and the completing transaction still re-reads the order under lock.
  • Why this approach: smallest change that makes order money, callback money and log agree, with one exchange-rate function shared with top-ups.

Files

Path Why
controller/topup.go payMoneyFromUSD shared conversion; getPayMoney uses it
controller/subscription_payment_epay.go convert plan price for Epay, lower bound on converted amount, callback money verification, logging
controller/subscription_payment_epay_test.go testify tests: conversion table, order creation (7.3 / 1 / below one cent), notify and return money checks
common/log_text.zh-CN.json zh-CN text for the new log lines
web/src/features/subscriptions/components/dialogs/subscription-purchase-dialog.tsx show converted Epay amount; Pay {{amount}} button
web/src/features/wallet/components/subscription-plans-card.tsx, web/src/features/wallet/index.tsx pass priceRatio (status.price) through
web/src/features/subscriptions/__tests__/purchase-dialog-epay-amount.test.tsx dialog regression test
web/src/i18n/locales/*.json (7) key Online payment amount

Behavior

  • Before: $10 plan, price per USD 7.3 → Epay form money=10.00, order money=10, log "payment amount: 10.00"; callback amount never compared; dialog shows only $10.00.
  • After: Epay form money=73.00, order money=73, mirrored top-up money=73, log "payment amount: 73.00"; a signed callback whose money differs from the order is rejected (fail / redirect pay=fail, order stays pending); dialog shows "Online payment amount ¥73.00" (currency per display settings) and the button reads "Pay ¥73.00". Price per USD = 1 keeps $10 → 10.00.
  • Explicit non-goals / leftover work: Stripe / Creem / Waffo subscription payments and the Epay top-up callback are unchanged (see Follow-ups); no schema change.

Verification

  • Commands and results:
    • GOWORK=off go build ./... (after bun run build, the embed needs web/dist): ok. GOWORK=off go vet ./controller/ ./model/: clean. gofmt -l controller/: empty. cd relaykit && GOWORK=off go build ./... / cd tokenkit && GOWORK=off go build ./...: ok.
    • GOWORK=off go test ./controller/ -run 'TestPayMoneyFromUSD|TestSubscriptionRequestEpay|TestSubscriptionEpay' -count=1 -v: 6/6 PASS (ok github.com/QuantumNous/new-api/controller 5.591s); the log lines show price_usd=10.00 price_per_usd=7.3000 money=73.00, price_per_usd=1.0000 money=10.00, and callback money mismatch … order_money=73.00 callback_money="10.00".
    • GOWORK=off go test ./controller/ -count=1: 40 --- FAIL, every one of them the known Windows testing.go:1464: TempDir RemoveAll cleanup: unlinkat …�udit.db: The process cannot access the file because it is being used by another process (all subtests PASS, no assertion failures). The same test names fail the same way on a clean origin/fork-main (1f7d6cb) worktree. GOWORK=off go test ./model/ -run 'Subscription|TopUp|Topup|Payment|Epay': ok.
    • cd web && bun install --frozen-lockfile (no changes) && bun run test: 209 files / 2503 tests passed; bun run build: ok. tsgo -b (Bun 1.4.0 here cannot run bun run typecheck): clean. oxlint + oxfmt --check on the four touched TS/TSX files: clean. bun run i18n:sync: no-op.
  • Manual steps and observed result:
    • Test host (tailnet, Docker Desktop): shallow clone of this branch (d2da8df) → docker build -t new-api-fork:t261105-08 (log C:\Docker ew-api-t261105-08�uild.log) → docker run -d --name new-api-t06 -p 39684:3000 -v C:\Docker ew-api-t261105-08\data:/data new-api-fork:t261105-08: container up, New API ready in 1132 ms, GET /api/status → 200, no panic in docker logs. Setup wizard completed with a test admin; price per USD set to 7.3 and Epay placeholders entered on the payment settings page.
    • UI purchase check (set price 7.3 → create $10 plan → Epay order → gateway form money=73.00): pending. Subscription purchase on this build is behind the payment-compliance attestation dialog, which the agent was not permitted to submit on the user's behalf; the HQ user will confirm it on the test instance, after which the purchase step is re-run and this section updated.
  • UI: screenshot or recording (or why none): none yet; the purchase dialog screenshot is added once the compliance gate on the test instance is confirmed by the user (see above).
  • Tests added or updated, or why none: controller/subscription_payment_epay_test.go (6 tests: payMoneyFromUSD table 7.3→73.00 / 1→10.00 / 7.333→7.33; order creation sends money=73.00 and stores 73; price 1 sends 10.00; $0.001 plan rejected with "Plan amount is too low" and no order; notify with money=10.00 → fail + order pending + no subscription, then money=73.00 → success + subscription + mirrored top-up 73 + log contains 73.00, duplicate stays idempotent; return URL with wrong money redirects to pay=fail). web/src/features/subscriptions/__tests__/purchase-dialog-epay-amount.test.tsx (3 tests: converted amount and button at ratio 7.3, ratio 1, no Epay → no row).
  • Databases / providers / platforms exercised: SQLite (unit tests in-memory; Docker container on the tailnet host with a fresh SQLite data dir). No real Epay gateway: the gateway request parameters were read from the /api/subscription/pay/epay response and the container log.
  • Not verified: the end-to-end Epay order in the running container (gated as described above; the same request path is covered by TestSubscriptionRequestEpay_ChargesConvertedMoney, which reads money=73.00 from the signed gateway form); a real Epay gateway callback; MySQL / PostgreSQL (no schema or query change, only an extra SELECT by trade_no on subscription_orders through GORM); Stripe / Creem / Waffo subscription flows (unchanged).

Risks

  • Failure modes: an Epay-compatible gateway that omits or reformats money in its callback would now be rejected (logged as "callback money mismatch"); amounts are compared after decimal parsing at 2 decimals, so 73, 73.0 and 73.00 all match. A price-per-USD change between order creation and callback does not matter: the callback is compared with the stored order amount.
  • Billing / quota / auth impact: only the money sent to / accepted from Epay for subscription orders changes; quota granted by the plan is untouched. Top-up amounts are byte-for-byte the same computation as before (getPayMoney only delegates the × Price step).
  • Follow-ups:
    • Epay top-up callback (EpayNotify → model.RechargeEpay) also never compares the callback money with topups.money; same hardening could be applied there.
    • Stripe subscriptions use stripe_price_id (amount defined in Stripe) and Creem uses creem_product_id (amount defined in Creem): not affected by this bug; their orders still record plan.PriceAmount (USD) in Money, which is a display-only inconsistency with Epay orders. Waffo Pancake sends plan.PriceAmount with StringFixed(2) as the amount in the Waffo currency and does not apply price-per-USD either; whether that is intended depends on how the Waffo account currency is configured, left as-is per the task.
    • Admin plan editor could show the converted Epay amount next to the USD price.

Scope check

  • Single focused change: yes
  • Secrets included: no
  • Out of scope (Coding Plan / reverse-engineered channel / third-party wrapper / Codex / pass-through-only forwarding): no

🤖 Generated with Claude Code

…-Task T261105-08)

Epay subscription orders sent the plan's USD list price to the gateway as
the payment amount, so a $10 plan was charged 10.00 instead of
10 × 每美元余额收费金额 (73.00 at the default 7.3), while Epay top-ups
already converted through getPayMoney.

- controller/topup.go: payMoneyFromUSD (usd × operation_setting.Price) is
  the single exchange-rate step shared by getPayMoney and subscriptions.
- controller/subscription_payment_epay.go: convert the plan price, reject
  sub-cent amounts before rounding, store the rounded gateway amount in
  SubscriptionOrder.Money (the unit TopUp.Money already uses for Epay),
  and verify the callback money against the order in notify/return before
  completing it. Order creation and callback outcomes are logged.
- web: the purchase dialog shows the converted "Online payment amount"
  (status.price, as the top-up page) and the Epay button reads
  "Pay {{amount}}"; wallet passes priceRatio through. New locale key in
  all seven languages.
- tests: controller/subscription_payment_epay_test.go and
  web/src/features/subscriptions/__tests__/purchase-dialog-epay-amount.test.tsx.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
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.

1 participant