Zoonk.Billing.Price (Zoonk v0.1.0-dev)

View Source

Represents a pricing option for a subscription plan.

Fields

FieldTypeDescription
planplan/0The subscription plan
intervalinterval/0Payment frequency
currencyStringThe currency code (e.g., "usd", "brl")
valueStringThe price value in the specified currency
stripe_price_idStringThe Stripe price ID for this plan

Summary

Functions

Extracts currency information from Stripe's currency_options.

Transforms a Stripe Price object into a Price struct for a specific currency.

Types

interval()

@type interval() :: :monthly | :yearly

plan()

@type plan() :: :plus

t()

@type t() :: %Zoonk.Billing.Price{
  currency: String.t(),
  interval: interval(),
  plan: plan(),
  stripe_price_id: String.t(),
  value: String.t()
}

Functions

extract_currencies(currencies)

Extracts currency information from Stripe's currency_options.

Converts the raw currency data from Stripe's format to a map of currency strings to decimal prices. The unit_amount from Stripe is in cents, so it's divided by 100 to get the actual price.

This is a helper function used internally by transform_from_stripe.

Examples

iex> extract_currencies(%{
...>   "usd" => %{"unit_amount" => 500},
...>   "brl" => %{"unit_amount" => 1999}
...> })
%{"usd" => "$5.00", "brl" => "R$19.99"}

transform_from_stripe(price, preferred \\ "usd")

Transforms a Stripe Price object into a Price struct for a specific currency.

Takes a raw Price object from the Stripe API and converts it to our internal Price struct, extracting the plan, interval, and price for the specified currency. If the currency is not available, falls back to USD, and if USD is not available, returns an error.

For more information about the Stripe Price object structure, see: https://docs.stripe.com/api/prices/object

Examples

iex> transform_from_stripe(%{
...>   "lookup_key" => "plus_monthly",
...>   "currency_options" => %{
...>     "usd" => %{"unit_amount" => 500},
...>     "brl" => %{"unit_amount" => 1999}
...>   }
...> }, "brl")
{:ok, %Price{
  plan: :plus,
  interval: :monthly,
  currency: "brl",
  value: "R$19.99"
}}