mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-26 18:42:49 +00:00
@@ -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<number> {
|
||||
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(
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user