Money.Subscription.Plan (Money v5.23.0)
View SourceDefines a standard subscription plan data structure.
Summary
Functions
Defines the structure of a subscription plan.
Returns {:ok, Money.Subscription.Plan.t} or an {:error, reason}
tuple.
Returns {:ok, Money.Subscription.Plan.t} or raises an
exception.
Return a localised string representation of a subscription plan.
Types
@type interval() :: :day | :week | :month | :year
A plan interval type.
@type interval_count() :: non_neg_integer()
A integer interval count for a plan.
@type t() :: %Money.Subscription.Plan{ interval: interval(), interval_count: interval_count(), price: Money.t() | nil }
A Subscription Plan
Functions
Defines the structure of a subscription plan.
Returns {:ok, Money.Subscription.Plan.t} or an {:error, reason}
tuple.
Arguments
:priceis anyMoney.t:intervalis the period of the plan. The valid intervals are:day,:week,:monthor ':year.:interval_countis an integer count of the number of:intervals of the plan. The default is1
Returns
A Money.Subscription.Plan.t
Examples
iex> Money.Subscription.Plan.new Money.new(:USD, 100), :month, 1
{:ok,
%Money.Subscription.Plan{
interval: :month,
interval_count: 1,
price: Money.new(:USD, 100)
}}
iex> Money.Subscription.Plan.new Money.new(:USD, 100), :month
{:ok,
%Money.Subscription.Plan{
interval: :month,
interval_count: 1,
price: Money.new(:USD, 100)
}}
iex> Money.Subscription.Plan.new Money.new(:USD, 100), :day, 30
{:ok,
%Money.Subscription.Plan{
interval: :day,
interval_count: 30,
price: Money.new(:USD, 100)
}}
iex> Money.Subscription.Plan.new 23, :day, 30
{:error, {Money.Invalid, "Invalid subscription plan definition"}}
@spec new!(Money.t(), interval(), interval_count()) :: t() | no_return()
Returns {:ok, Money.Subscription.Plan.t} or raises an
exception.
Takes the same arguments as Money.Subscription.Plan.new/3.
##@ Example
iex> Money.Subscription.Plan.new! Money.new(:USD, 100), :day, 30
%Money.Subscription.Plan{
interval: :day,
interval_count: 30,
price: Money.new(:USD, 100)
}
Return a localised string representation of a subscription plan.
Arguments
Any
Money.Subscription.Plan.t/0as returned fromMoney.Subscription.Plan.new/3.optionsis a keyword list of options.
Options
- See
Cldr.Unit.to_string/1for available options.
Returns
{:ok, localized_string}or{:error, reason}
Examples
iex> {:ok, plan} = Money.Subscription.Plan.new(Money.new(:USD, 10), :year)
iex> Money.Subscription.Plan.to_string(plan)
{:ok, "$10.00 per year"}
iex> Money.Subscription.Plan.to_string(plan, locale: :ja)
{:ok, "$10.00毎年"}
iex> Money.Subscription.Plan.to_string(plan, locale: :de, style: :narrow)
{:ok, "10,00 $/J"}
iex> {:ok, plan} = Money.Subscription.Plan.new(Money.new(:USD, 10), :day, 30)
iex> Money.Subscription.Plan.to_string(plan)
{:ok, "$10.00 per 30 days"}
iex> Money.Subscription.Plan.to_string(plan, locale: :de)
{:ok, "10,00 $ pro 30 Tage"}
iex> Money.Subscription.Plan.to_string(plan, locale: :de, style: :short)
{:ok, "10,00 $/30 Tg."}