Exchange Rates, Sync & History
Currency separates live spot rates (stored on currency-currencies) from locked historical snapshots (stored on currency-rate-history) so historical accounting lookups remain deterministic even after future rate updates.
Built-In Exchange Rate Providers
Section titled “Built-In Exchange Rate Providers”| Provider Key | Class | Notes |
|---|---|---|
ecb |
EcbProvider |
Fetches European Central Bank daily XML (eurofxref-daily.xml) and 90-day historical XML (eurofxref-hist-90d.xml). |
tcmb |
TcmbProvider |
Fetches Central Bank of Turkey daily XML (today.xml) supporting forex_selling (and buying/banknote types), automatically skipping weekends and looking back up to 5 business days on public holidays (HTTP 404). |
manual |
ManualProvider |
Snapshots manually entered rates from currency-currencies into currency-rate-history without external network calls. |
Converting & Historical Cross-Rates
Section titled “Converting & Historical Cross-Rates”<?php namespace ProcessWire;
use Totoglu\Currency\Value\Money;
$currency = Currency::requireInstance($wire);
// Spot conversion with currency spread markup applied$usd = $currency->convert(Money::fromMajor('14700.00', 'EUR'), 'USD', date: null, applySpread: true);// $usd->converted, $usd->fxRate, $usd->provider, $usd->effectiveDate, $usd->spreadApplied
// Exact 8-decimal rate lookup on or before a historical date (cross-rate aware)$rate = $currency->exchangeRate('USD', 'TRY', date: '2026-06-01', applySpread: false);
// Direct RateHistoryRepository queries$hist = $currency->rateHistory();$direct = $hist->findRateOnOrBefore('EUR', 'USD', '2026-06-01');$cross = $hist->findCrossRateOnOrBefore('GBP', 'TRY', '2026-06-01');Synchronizing Rates & Atomic Base Currency Switching
Section titled “Synchronizing Rates & Atomic Base Currency Switching”- Live Sync (
syncRates()): Performs the HTTPS fetch outside database locks, then acquiresCurrencyWriteLock::run()to persistcurrency-rate-historysnapshots and update currencies whoserate_sourceisautoor matches the synced provider key (leavingmanualand other provider-pinned currencies untouched). - Historical Backfill (
syncRates($provider, '2026-06-01')): Writes locked snapshots tocurrency-rate-historywithout overwriting live spot rates orlast_sync_*timestamps. Future dates (> today) are rejected. - Atomic Base Currency Switch (
$currency->converter()->setBaseCurrency('USD', rebaseExistingRates: true)): Runs insideCurrencyWriteLock::run()viaEntries::saveMany(), promoting the target currency to1.00000000and rebasing all sibling rates atomically.
Registering a Custom Provider
Section titled “Registering a Custom Provider”Implement ExchangeRateProviderInterface (key(), label(), fetchRates()) using WireHttpTransport and register it via $currency->registerExchangeRateProvider($provider) (provider keys must match [a-z0-9_-]{1,32}):
<?php namespace ProcessWire;
use Totoglu\Currency\Http\WireHttpTransport;
$currency = Currency::requireInstance($wire);$currency->registerExchangeRateProvider(new AegeanTreasuryRateProvider(new WireHttpTransport()));See site/modules/Currency/examples/real-world/custom-rate-provider.php for a complete implementation.
Step-by-Step: Adding a New Currency & Syncing Rates (Admin UI)
Section titled “Step-by-Step: Adding a New Currency & Syncing Rates (Admin UI)”- Open Setup -> Currencies -> Currencies and click + New Currency.
- Enter the ISO 4217 code (for example
GBP), decimal places (2), optional spread markup percentage (1.5), and leave Rate Source set toauto. - Save the currency, then return to Setup -> Currencies -> Overview and click Sync Rates Now (or run
php vendor/bin/wire currency:sync --json).
Expected currency:sync --json Output
Section titled “Expected currency:sync --json Output”{ "ok": true, "provider": "ecb", "baseCurrency": "EUR", "effectiveDate": "2026-12-05", "updated": ["USD", "GBP", "TRY"], "skippedManual": []}