Skip to content

fix(payment): support up to 6 decimals for balance recharge multiplier - #8642

Merged
Wei-Shaw merged 1 commit into
Wei-Shaw:mainfrom
baowl2022:fix/balance-recharge-multiplier-6-decimals
Nov 10, 2026
Merged

Wei-Shaw merged 1 commit into
Wei-Shaw:mainfrom
baowl2022:fix/balance-recharge-multiplier-6-decimals

Conversation

@baowl2022

Copy link
Copy Markdown
Contributor

Summary

  • Persist BALANCE_RECHARGE_MULTIPLIER with up to 6 decimal places instead of silently truncating to 2.
  • Reject values with more than 6 decimal places at the API boundary instead of rounding them.
  • Admin settings: accept 6-decimal input and show the exact configured rate in the preview.
  • Checkout page: show the exact configured rate (minimum 2 decimals, up to 6).
  • Credited balance still rounds to settlement-currency precision (USD cents). No ledger or order-schema change.

Motivation

Operators that collect CNY through the balance recharge flow need to track a live FX rate. With the current 2-decimal limit, a rate of 1 USD = 7.100000 CNY can only be stored as 0.14 or 0.15, producing a 0.7%–7% error for every recharge.

With 6 decimals an operator can store the live rate (e.g. 0.140845) and refresh it from a daily CronJob:

  • CNY 100 × 0.140845 = USD 14.0845 → credits USD 14.08 (at 0.15 it credited USD 15.00)
  • CNY 10000 × 0.140845 = USD 1408.45 (no additional drift at larger amounts)

This is a minimal, backward-compatible step toward CNY billing. It does not change the USD ledger currency or the payment order schema; full settlement/recharge currency separation is tracked in #5219 and the CNY request in #7087.

Changes

Backend

  • New formatBalanceRechargeMultiplier formatter that stores up to 6 decimals without trailing zeros.
  • New balanceRechargeMultiplierHasExcessPrecision validation; values with > 6 decimals return INVALID_BALANCE_RECHARGE_MULTIPLIER instead of being silently rounded.
  • calculateCreditedBalance keeps Round(2), so balances/orders remain at currency precision.
  • Unit tests for 6-decimal persistence, float artifacts (0.1 + 0.2), legacy 2-decimal values, and rejection of 7-decimal values.

Frontend

  • Admin input step/min changed from 0.01 to 0.000001.
  • Admin preview and checkout rate preview use the shared formatter (min 2, max 6 decimals).
  • i18n hints mention the 6-decimal support.
  • Unit test for the formatter.

Compatibility

  • Existing 2-decimal values are parsed and displayed exactly as before (0.14 → 0.14, 0.5 → 0.50).
  • No database migration: users.balance is already numeric(20,8); payment_orders.amount remains numeric(20,2) and credited amounts continue to round to cents.
  • Requests that previously sent more than 2 decimals were silently truncated; they are now rejected with a clear validation error. The admin UI never produced such values.

Test plan

  • go vet ./internal/service
  • go test ./internal/service -run 'TestParsePaymentConfig|TestUpdatePaymentConfig|TestCalculateCreditedBalance' -count=1
  • pnpm vitest run src/utils/__tests__/rechargeBonus.spec.ts src/views/user/__tests__/PaymentView.spec.ts src/views/admin/__tests__/SettingsView.spec.ts
  • pnpm exec vue-tsc --noEmit
  • pnpm exec eslint src/views/admin/SettingsView.vue src/views/user/PaymentView.vue src/utils/rechargeBonus.ts src/utils/__tests__/rechargeBonus.spec.ts

All passed locally on commit ee58c0013c76ccd3f36439881bc848d342ec1c9d.

Related

Persist BALANCE_RECHARGE_MULTIPLIER with up to 6 decimal places so operators
can track a live FX rate (e.g. 1 USD = 7.100000 CNY -> 0.140845). Values with
more than 6 decimals are rejected instead of being silently truncated.
Credited balance still rounds to settlement-currency precision, so order and
ledger semantics are unchanged.

Adds backend unit tests and a frontend formatter test; updates the admin input
step/preview and the checkout rate display.
@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA. ✅
Posted by the CLA Assistant Lite bot.

@baowl2022

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Nov 10, 2026
@Wei-Shaw
Wei-Shaw merged commit 48b11bf into Wei-Shaw:main Nov 10, 2026
10 of 11 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 10, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants