Sumup.Money.MinorUnit (Sumup v1.0.0)

Copy Markdown View Source

Money as represented by SumUp's newer Readers API: an integer value in minor units, an explicit minor_unit (decimal exponent), and an ISO 4217 currency code, e.g. %{"value" => 5033, "minor_unit" => 2, "currency" => "EUR"} for €50.33.

This is the numerically-safe representation (no floating point rounding) and correctly handles zero-decimal currencies such as JPY, CLP, COP and HUF, where minor_unit is 0 and value is already the full amount.

Summary

Functions

Builds a t/0 directly from a major-unit decimal amount, e.g. Sumup.Money.MinorUnit.from_decimal(50.33, "EUR", 2) == %Sumup.Money.MinorUnit{value: 5033, minor_unit: 2, currency: "EUR"}.

Builds a t/0 from a decoded JSON map (value/minor_unit/currency).

Converts back to a major-unit decimal amount, e.g. 5033 cents at minor_unit: 2 becomes 50.33.

Encodes back into the wire shape expected by the Readers API.

Types

t()

@type t() :: %Sumup.Money.MinorUnit{
  currency: String.t(),
  minor_unit: non_neg_integer(),
  value: integer()
}

Functions

from_decimal(amount, currency, minor_unit)

@spec from_decimal(number(), String.t(), non_neg_integer()) :: t()

Builds a t/0 directly from a major-unit decimal amount, e.g. Sumup.Money.MinorUnit.from_decimal(50.33, "EUR", 2) == %Sumup.Money.MinorUnit{value: 5033, minor_unit: 2, currency: "EUR"}.

from_map(arg1)

@spec from_map(map() | nil) :: t() | nil

Builds a t/0 from a decoded JSON map (value/minor_unit/currency).

to_decimal(minor_unit)

@spec to_decimal(t()) :: float()

Converts back to a major-unit decimal amount, e.g. 5033 cents at minor_unit: 2 becomes 50.33.

to_params(minor_unit)

@spec to_params(t()) :: map()

Encodes back into the wire shape expected by the Readers API.