📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
real currency conversion
Blue Mouse0001f4e3d2db
diff --git a/backend/apps/cloud/src/revenue/adapters/paddle.adapter.ts b/backend/apps/cloud/src/revenue/adapters/paddle.adapter.tsindex 632af2cc6..caa75fab6 100644--- a/backend/apps/cloud/src/revenue/adapters/paddle.adapter.ts+++ b/backend/apps/cloud/src/revenue/adapters/paddle.adapter.ts@@ -1,6 +1,7 @@import { Injectable } from '@nestjs/common'import { AppLoggerService } from '../../logger/logger.service'import { RevenueService } from '../revenue.service'+import { CurrencyService } from '../currency.service'import {RevenueProvider,RevenueType,@@ -82,6 +83,7 @@ export class PaddleAdapter {constructor(private readonly logger: AppLoggerService,private readonly revenueService: RevenueService,+ private readonly currencyService: CurrencyService,) {}async validateApiKey(apiKey: string): Promise<boolean> {@@ -283,8 +285,8 @@ export class PaddleAdapter {type = RevenueType.SUBSCRIPTION}- // Convert currency if needed (simplified - in production, use real exchange rates)- const amount = this.convertCurrency(+ // Convert currency+ const amount = await this.currencyService.convert(originalAmount,originalCurrency,targetCurrency,@@ -328,7 +330,7 @@ export class PaddleAdapter {const originalAmount = parseFloat(adjustment.totals.total) / 100const originalCurrency = adjustment.totals.currency_code- const amount = this.convertCurrency(+ const amount = await this.currencyService.convert(originalAmount,originalCurrency,targetCurrency,@@ -356,32 +358,4 @@ export class PaddleAdapter {await this.revenueService.insertTransaction(revenueTransaction)}-- private convertCurrency(- amount: number,- fromCurrency: string,- toCurrency: string,- ): number {- if (fromCurrency === toCurrency) {- return amount- }-- // Simplified conversion rates - in production, use a real exchange rate API- // These are approximate rates as of late 2024- const rates: Record<string, number> = {- USD: 1,- EUR: 1.08,- GBP: 1.27,- CAD: 0.74,- AUD: 0.66,- JPY: 0.0067,- }-- const fromRate = rates[fromCurrency] || 1- const toRate = rates[toCurrency] || 1-- // Convert to USD first, then to target currency- const usdAmount = amount * fromRate- return usdAmount / toRate- }}diff --git a/backend/apps/cloud/src/revenue/adapters/stripe.adapter.ts b/backend/apps/cloud/src/revenue/adapters/stripe.adapter.tsindex 5f07c3038..d791fc94a 100644--- a/backend/apps/cloud/src/revenue/adapters/stripe.adapter.ts+++ b/backend/apps/cloud/src/revenue/adapters/stripe.adapter.ts@@ -1,6 +1,7 @@import { Injectable } from '@nestjs/common'import { AppLoggerService } from '../../logger/logger.service'import { RevenueService } from '../revenue.service'+import { CurrencyService } from '../currency.service'import {RevenueProvider,RevenueStatus,@@ -61,6 +62,7 @@ export class StripeAdapter {constructor(private readonly logger: AppLoggerService,private readonly revenueService: RevenueService,+ private readonly currencyService: CurrencyService,) {}async validateApiKey(apiKey: string): Promise<boolean> {@@ -209,7 +211,7 @@ export class StripeAdapter {: intent.amount) / 100const originalCurrency = (intent.currency || '').toUpperCase()- const amount = this.convertCurrency(+ const amount = await this.currencyService.convert(originalAmount,originalCurrency,targetCurrency,@@ -274,7 +276,7 @@ export class StripeAdapter {const originalAmount = refund.amount / 100const originalCurrency = (refund.currency || '').toUpperCase()- const amount = this.convertCurrency(+ const amount = await this.currencyService.convert(originalAmount,originalCurrency,targetCurrency,@@ -318,29 +320,4 @@ export class StripeAdapter {await this.revenueService.insertTransaction(revenueTransaction)}-- private convertCurrency(- amount: number,- fromCurrency: string,- toCurrency: string,- ): number {- if (fromCurrency === toCurrency) {- return amount- }-- const rates: Record<string, number> = {- USD: 1,- EUR: 1.08,- GBP: 1.27,- CAD: 0.74,- AUD: 0.66,- JPY: 0.0067,- }-- const fromRate = rates[fromCurrency] || 1- const toRate = rates[toCurrency] || 1-- const usdAmount = amount * fromRate- return usdAmount / toRate- }}diff --git a/backend/apps/cloud/src/revenue/currency.service.ts b/backend/apps/cloud/src/revenue/currency.service.tsnew file mode 100644index 000000000..77edc734d--- /dev/null+++ b/backend/apps/cloud/src/revenue/currency.service.ts@@ -0,0 +1,89 @@+import { Injectable } from '@nestjs/common'+import axios from 'axios'+import { redis } from '../common/constants'+import { AppLoggerService } from '../logger/logger.service'++@Injectable()+export class CurrencyService {+ private readonly CACHE_KEY = 'revenue:currency_rates'+ private readonly CACHE_TTL = 86400 // 1 day in seconds+ private readonly API_URL =+ 'https://cdn.jsdelivr.net/gh/prebid/currency-file@1/latest.json'++ constructor(private readonly logger: AppLoggerService) {}++ async convert(+ amount: number,+ fromCurrency: string,+ toCurrency: string,+ ): Promise<number> {+ fromCurrency = fromCurrency.toUpperCase()+ toCurrency = toCurrency.toUpperCase()++ if (fromCurrency === toCurrency) {+ return amount+ }++ const rates = await this.getRates()+ if (!rates) {+ this.logger.warn(+ { fromCurrency, toCurrency },+ 'Currency rates unavailable, falling back to 1:1 conversion',+ )+ return amount+ }++ // Try to find direct conversion from 'fromCurrency' if it's a base currency in the API+ if (rates[fromCurrency]?.[toCurrency]) {+ return amount * rates[fromCurrency][toCurrency]+ }++ // Otherwise, convert via USD (which is most likely present as a base currency)+ const usdRates = rates['USD']+ if (!usdRates) {+ this.logger.warn(+ { fromCurrency, toCurrency },+ 'USD rates unavailable in currency API, falling back to 1:1 conversion',+ )+ return amount+ }++ // amount in fromCurrency -> amount in USD+ // 1 USD = usdRates[fromCurrency] units of fromCurrency+ // So 1 unit of fromCurrency = 1 / usdRates[fromCurrency] USD+ const fromRateInUsd = usdRates[fromCurrency]+ ? 1 / usdRates[fromCurrency]+ : 1+ const toRateInUsd = usdRates[toCurrency] ? 1 / usdRates[toCurrency] : 1++ const amountInUsd = amount * fromRateInUsd+ return amountInUsd / toRateInUsd+ }++ private async getRates(): Promise<Record<string, Record<string, number>> | null> {+ try {+ const cached = await redis.get(this.CACHE_KEY)+ if (cached) {+ return JSON.parse(cached)+ }++ const response = await axios.get(this.API_URL)+ const rates = response.data?.conversions++ if (rates) {+ await redis.set(+ this.CACHE_KEY,+ JSON.stringify(rates),+ 'EX',+ this.CACHE_TTL,+ )+ return rates+ }++ return null+ } catch (error) {+ this.logger.error({ error }, 'Failed to fetch currency rates')+ return null+ }+ }+}diff --git a/backend/apps/cloud/src/revenue/revenue.module.ts b/backend/apps/cloud/src/revenue/revenue.module.tsindex 9c6dad00f..1f05e714c 100644--- a/backend/apps/cloud/src/revenue/revenue.module.ts+++ b/backend/apps/cloud/src/revenue/revenue.module.ts@@ -6,6 +6,7 @@ import { AppLoggerModule } from '../logger/logger.module'import { AnalyticsModule } from '../analytics/analytics.module'import { Project } from '../project/entity/project.entity'import { RevenueService } from './revenue.service'+import { CurrencyService } from './currency.service'import {RevenueController,RevenueAnalyticsController,@@ -20,8 +21,8 @@ import { StripeAdapter } from './adapters/stripe.adapter'AppLoggerModule,forwardRef(() => AnalyticsModule),],- providers: [RevenueService, PaddleAdapter, StripeAdapter],- exports: [RevenueService, PaddleAdapter, StripeAdapter],+ providers: [RevenueService, CurrencyService, PaddleAdapter, StripeAdapter],+ exports: [RevenueService, CurrencyService, PaddleAdapter, StripeAdapter],controllers: [RevenueController, RevenueAnalyticsController],})export class RevenueModule {}