diff --git a/apps/edr-passenger-api/src/modules/currency/currency.service.ts b/apps/edr-passenger-api/src/modules/currency/currency.service.ts index 806ab423e..57276b116 100644 --- a/apps/edr-passenger-api/src/modules/currency/currency.service.ts +++ b/apps/edr-passenger-api/src/modules/currency/currency.service.ts @@ -51,26 +51,31 @@ export class CurrencyService { } /** - * Converts an ETB minor-unit amount to the charge major-unit amount sent to the - * payment provider. Applies the exchange rate for foreign currencies then divides - * by 100 to yield major units (e.g. 300000 ETB minor → 3000.00 ETB major). + * Converts a booking's stored minor-unit amount (in its own `fromCurrency`) to the charge + * major-unit amount sent to the payment provider in `targetCurrency`. When the two currencies + * match, no exchange rate is applied — the stored amount is charged as-is. Otherwise the + * fromCurrency→targetCurrency rate is applied. In both cases the result is divided by 100 to + * yield major units and rounded to the target currency's precision + * (e.g. 300000 ETB minor → 3000.00 ETB major; DJF rounds to whole francs). */ - async convertEtbMinorToChargeMajor( - amountMinorEtb: number, + async convertMinorToChargeMajor( + amountMinor: number, + fromCurrency: string, targetCurrency: string, ): Promise { + const from = fromCurrency.toUpperCase(); const target = targetCurrency.toUpperCase(); if (CHARGE_CURRENCY_DECIMALS[target] === undefined) { throw new BadRequestException(`Unsupported charge currency: ${targetCurrency}`); } const decimals = CHARGE_CURRENCY_DECIMALS[target]; - if (target === Currency.ETB) { - return this.roundTo(amountMinorEtb / 100, decimals); + if (from === target) { + return this.roundTo(amountMinor / 100, decimals); } - const rate = await this.getRateOrThrow(Currency.ETB, target as Currency); - return this.roundTo((amountMinorEtb * rate) / 100, decimals); + const rate = await this.getRateOrThrow(from as Currency, target as Currency); + return this.roundTo((amountMinor * rate) / 100, decimals); } async getRateOrThrow( diff --git a/apps/edr-passenger-api/src/modules/payments/payments.service.ts b/apps/edr-passenger-api/src/modules/payments/payments.service.ts index 5ef160135..fb70b2d76 100644 --- a/apps/edr-passenger-api/src/modules/payments/payments.service.ts +++ b/apps/edr-passenger-api/src/modules/payments/payments.service.ts @@ -242,8 +242,9 @@ export class PaymentsService { const chargeCurrency = ( paymentMethod?.currency ?? booking.currency ).toUpperCase(); - const chargeAmount = await this.currencyService.convertEtbMinorToChargeMajor( + const chargeAmount = await this.currencyService.convertMinorToChargeMajor( booking.totalMinor, + booking.currency, chargeCurrency, );