PhoenixKit.Modules.Billing.Currency (phoenix_kit v1.7.71)

Copy Markdown View Source

Currency schema for PhoenixKit Billing system.

Manages supported currencies with exchange rates for multi-currency billing.

Schema Fields

  • code: ISO 4217 currency code (e.g., "EUR", "USD", "GBP")
  • name: Full currency name (e.g., "Euro", "US Dollar")
  • symbol: Currency symbol (e.g., "€", "$", "£")
  • decimal_places: Number of decimal places (usually 2)
  • is_default: Whether this is the default currency
  • enabled: Whether currency is available for use
  • exchange_rate: Rate relative to base currency
  • sort_order: Display order in currency lists

Usage Examples

# List all enabled currencies
currencies = PhoenixKit.Modules.Billing.list_currencies()

# Get default currency
currency = PhoenixKit.Modules.Billing.get_default_currency()

# Format amount in currency
PhoenixKit.Modules.Billing.Currency.format_amount(99.99, currency)
# => "€99.99"

Summary

Functions

Creates a changeset for currency creation and updates.

Converts amount from one currency to another.

Formats an amount with currency symbol.

Formats an amount without currency symbol.

Functions

changeset(currency, attrs)

Creates a changeset for currency creation and updates.

convert(amount, currency1, currency2)

Converts amount from one currency to another.

Examples

iex> from = %Currency{exchange_rate: Decimal.new("1.0")}  # EUR (base)
iex> to = %Currency{exchange_rate: Decimal.new("1.1")}    # USD
iex> Currency.convert(100, from, to)
Decimal.new("110.00")

format_amount(amount, currency)

Formats an amount with currency symbol.

Examples

iex> currency = %Currency{symbol: "€", decimal_places: 2}
iex> Currency.format_amount(Decimal.new("99.99"), currency)
"€99.99"

iex> Currency.format_amount(1234.5, currency)
"€1,234.50"

format_amount_plain(amount, currency)

Formats an amount without currency symbol.