Skip to content

feat(payment): add optional daily FX sync for recharge multiplier - #8648

Closed
baowl2022 wants to merge 5 commits into
Wei-Shaw:mainfrom
baowl2022:feat/payment-fx-sync
Closed

baowl2022 wants to merge 5 commits into
Wei-Shaw:mainfrom
baowl2022:feat/payment-fx-sync

Conversation

@baowl2022

@baowl2022 baowl2022 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add an opt-in periodic FX sync for the balance recharge multiplier:

  • Fetch USD/CNY from one or more admin-configured public JSON APIs, with ordered fallback (open.er-api.com / frankfurter.app compatible).
  • Update BALANCE_RECHARGE_MULTIPLIER via the existing 6-decimal formatter; optionally also update the subscription USD→CNY rate and apply a markup.
  • Default interval 24h (1–168h), minute-level due check, one-hour retry after failure.
  • Redis/Postgres leader lock so one replica syncs; admin API/UI with last-run status and a “Sync now” action.
  • Disabled by default; no migration; ledger and order semantics unchanged.

Motivation

Operators collecting CNY need a live FX rate. #8642 (merged) raised the recharge multiplier precision to 6 decimals; this PR automates the refresh instead of requiring manual updates or an external CronJob.

This automates the existing recharge-multiplier flow. Full settlement/recharge currency separation (#5219) is out of scope.

Behavior

  • Config and last-run status live in the existing settings KV; the status keys are read-only through the admin API.
  • Rate parsing supports rates / conversion_rates and base / base_code, with USD/CNY cross-rate calculation and a 1–20 CNY/USD sanity check.
  • Multiplier = 1 / (market rate × (1 + markup/100)), rounded to 6 decimals. Credited balance still rounds to currency precision in the existing payment flow.
  • The FX client reuses the existing SSRF-safe HTTP client; it only connects to public hosts, and the admin UI documents that restriction.
  • Failures are persisted and shown in the UI; the scheduler retries after one hour. Manual sync returns 409 while another sync holds the lock.

API / UI

  • GET/PUT /api/v1/admin/settings/payment-fx
  • POST /api/v1/admin/settings/payment-fx/sync
  • New card under Settings → Payment for configuration, status, and manual sync.

Tests

  • Backend: go build ./..., go vet ..., targeted TestPaymentFXSync* tests (rate formats/fallbacks, invalid rates, 6-decimal update, markup, optional subscription rate, failure status, due logic, validation, lock contention).
  • Frontend: 48 targeted tests, vue-tsc, ESLint, i18n completeness, and production build.
  • Local checks passed; PR CI covers unit + integration, lint, security scans, and frontend.

Related

Add an opt-in background service that fetches USD/CNY rates from
configurable public APIs and updates the balance recharge multiplier
(optionally the subscription rate). Includes admin API/UI, manual sync,
leader-locked scheduling, last-run status, SSRF-safe client and tests.
No migration; disabled by default.
Address the errcheck finding from CI. The request URL remains
admin-configured and is validated at dial time by the SSRF-safe client.
Disable save/sync until the admin configuration has loaded and add a
reload action, so a failed load cannot overwrite the stored settings
with defaults. Add a focused component test.
@baowl2022

Copy link
Copy Markdown
Contributor Author

The test job failed in Unit tests after ~8 minutes without a failing-test annotation, while the backend is unchanged from commit 716611d (which passed the same suite). This looks like a flaky/hung test or runner issue rather than a code failure. Could a maintainer re-run the failed job? Thanks!

The failing job hit an unrelated flaky assertion in TestFilterThinkingBlocksSignedHistorySkipsDecode (allocs 16 vs < 16); the backend is unchanged from 716611d, which passed the same suite.
@baowl2022

Copy link
Copy Markdown
Contributor Author

CI update on 5ede39d: the test job is now green along with golangci-lint, frontend, shell and release-helpers. The earlier failure was the unrelated flaky testing.AllocsPerRun assertion in TestFilterThinkingBlocksSignedHistorySkipsDecode (allocs 16 vs < 16).

backend-security now fails on GO-2026-6859 (net/http@go1.27.3, fixed in go1.27.4), which also affects main. PR #8670 tracks the toolchain bump; I will sync main into this branch once it lands. Otherwise the PR is ready for review.

@baowl2022 baowl2022 closed this Nov 17, 2026
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