@churchapps/apphelper
Version:
Library of helper functions for React and NextJS ChurchApps
76 lines • 3.05 kB
JavaScript
var _a;
import { ApiHelper } from "@churchapps/helpers";
export class CurrencyHelper {
static formatCurrency(amount) {
const formatter = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
minimumFractionDigits: 2
});
return formatter.format(amount);
}
static formatCurrencyWithLocale(amount, currency = "usd", fractionDigits = 2) {
const symbol = this.getCurrencySymbol(currency);
const normalizedCurrency = currency.toUpperCase();
const locale = this.getLocaleForCurrency(normalizedCurrency);
const formatter = new Intl.NumberFormat(locale, {
style: "currency",
currency: normalizedCurrency,
currencyDisplay: "code",
minimumFractionDigits: fractionDigits,
maximumFractionDigits: fractionDigits,
});
// replace the currency code with the symbol
const formattedAmount = formatter.format(amount);
return formattedAmount.replace(normalizedCurrency, symbol);
}
static getCurrencySymbol(currency) {
const normalizedCurrency = currency?.toLowerCase() || "usd";
const stripeCurrencyFees = {
usd: { percent: 2.9, fixed: 0.3, symbol: "$" },
eur: { percent: 2.9, fixed: 0.25, symbol: "€" },
gbp: { percent: 2.9, fixed: 0.2, symbol: "£" },
cad: { percent: 2.9, fixed: 0.3, symbol: "C$" },
aud: { percent: 2.9, fixed: 0.3, symbol: "A$" },
inr: { percent: 2.9, fixed: 3.0, symbol: "₹" },
jpy: { percent: 2.9, fixed: 30.0, symbol: "¥" },
sgd: { percent: 2.9, fixed: 0.5, symbol: "S$" },
hkd: { percent: 2.9, fixed: 2.35, symbol: "元" },
sek: { percent: 2.9, fixed: 2.5, symbol: "SEK" },
nok: { percent: 2.9, fixed: 2.0, symbol: "NOK" },
dkk: { percent: 2.9, fixed: 1.8, symbol: "DKK" },
chf: { percent: 2.9, fixed: 0.3, symbol: "CHF" },
mxn: { percent: 2.9, fixed: 3.0, symbol: "MXN" },
brl: { percent: 3.9, fixed: 0.5, symbol: "R$" }
};
return stripeCurrencyFees[normalizedCurrency]?.symbol || "$";
}
static getLocaleForCurrency(currency) {
const currencyLocaleMap = {
USD: "en-US",
EUR: "en-GB",
GBP: "en-GB",
CAD: "en-CA",
AUD: "en-AU",
INR: "en-IN",
JPY: "ja-JP",
SGD: "en-SG",
HKD: "en-HK",
SEK: "sv-SE",
NOK: "nb-NO",
DKK: "da-DK",
CHF: "de-CH",
MXN: "es-MX",
BRL: "pt-BR"
};
return currencyLocaleMap[currency] || "en-US";
}
}
_a = CurrencyHelper;
CurrencyHelper.loadCurrency = async () => {
const gateways = await ApiHelper.get("/gateways", "GivingApi");
if (gateways.length === 0)
return "usd";
return gateways[0].currency || "usd";
};
//# sourceMappingURL=CurrencyHelper.js.map