Money.Subscription.change_plan
change_plan, go back to Money.Subscription module for more information.
Specs
change_plan( subscription_or_plan :: t() | Money.Subscription.Plan.t(), new_plan :: Money.Subscription.Plan.t(), options :: Keyword.t() ) :: {:ok, Money.Subscription.Change.t() | t()} | {:error, {module(), String.t()}}
Change plan from the current plan to a new plan.
Arguments
subscription_or_planis either aMoney.Subscription.torMoney.Subscription.Plan.tor a map with the same fieldsnew_planis aMoney.Subscription.Plan.tor a map with at least the fieldsinterval,interval_countandpricecurrent_interval_startedis aDate.tor other map with the fieldsyear,month,dayandcalendaroptionsis a keyword list of options the define how the change is to be made
Options
:effectivedefines when the new plan comes into effect. The values are:immediately, aDate.tor:next_period. The default is:next_period. Note that the date applied in the case of:immediatelyis the date returned byDate.utc_today.:proratewhich determines how to prorate the current plan into the new plan. The options are:pricewhich will reduce the price of the first period of the new plan by the credit amount left on the old plan (this is the default). Or:periodin which case the first period of the new plan is extended by theintervalamount of the new plan that the credit on the old plan will fund.:rounddetermines whether when prorating the:periodit is truncated or rounded up to the next nearest fullinterval_count. Valid values are:down,:half_up,:half_even,:ceiling,:floor,:half_down,:up. The default is:up.:first_interval_starteddetermines the anchor day for monthly billing. For example if a monthly plan starts on January 31st then the next period will start on February 28th (or 29th). The period following that should, however, be March 31st. Ifsubscription_or_planis aMoney.Subscription.tthen the:first_interval_startedis automatically populated from the subscription. If:first_interval_startedisnilthen the date defined by:effectiveis used.
Returns
A Money.Subscription.Change.t with the following elements:
:first_interval_startswhich is the start date of the first interval for the new plan:first_billing_amountis the amount to be billed, net of any credit, at the:first_interval_starts:next_interval_startsis the start date of the next interval after thefirst intervalincluding anycredit_days_applied:credit_amountis the amount of unconsumed credit of the current plan:credit_amount_appliedis the amount of credit applied to the new plan. If the:prorateoption is:price(the default) then:first_billing_amountis the plan:pricereduced by the:credit_amount_applied. If the:prorateoption is:periodthen the:first_billing_amountis the planpriceand the:next_interval_dateis extended by the:credit_days_appliedinstead.:credit_days_appliedis the number of days credit applied to the first interval by adding days to the:first_interval_startsdate.:credit_period_endsis the date on which any applied credit is consumed ornil:carry_forwardis any amount of credit carried forward to a subsequent period. If non-zero, this amount is a negativeMoney.t. It is non-zero when the credit amount for the current plan is greater than the:priceof the new plan. In this case the:first_billing_amountis zero.
Returns
{:ok, updated_subscription}or{:error, {exception, message}}
Examples
# Change at end of the current period so no proration
iex> current = Money.Subscription.Plan.new!(Money.new(:USD, 10), :month, 1)
iex> new = Money.Subscription.Plan.new!(Money.new(:USD, 10), :month, 3)
iex> Money.Subscription.change_plan current, new, current_interval_started: ~D[2018-01-01]
{:ok, %Money.Subscription.Change{
carry_forward: Money.zero(:USD),
credit_amount: Money.zero(:USD),
credit_amount_applied: Money.zero(:USD),
credit_days_applied: 0,
credit_period_ends: nil,
next_interval_starts: ~D[2018-05-01],
first_billing_amount: Money.new(:USD, 10),
first_interval_starts: ~D[2018-02-01]
}}
# Change during the current plan generates a credit amount
iex> current = Money.Subscription.Plan.new!(Money.new(:USD, 10), :month, 1)
iex> new = Money.Subscription.Plan.new!(Money.new(:USD, 10), :month, 3)
iex> Money.Subscription.change_plan current, new, current_interval_started: ~D[2018-01-01], effective: ~D[2018-01-15]
{:ok, %Money.Subscription.Change{
carry_forward: Money.zero(:USD),
credit_amount: Money.new(:USD, "5.49"),
credit_amount_applied: Money.new(:USD, "5.49"),
credit_days_applied: 0,
credit_period_ends: nil,
next_interval_starts: ~D[2018-04-15],
first_billing_amount: Money.new(:USD, "4.51"),
first_interval_starts: ~D[2018-01-15]
}}
# Change during the current plan generates a credit period
iex> current = Money.Subscription.Plan.new!(Money.new(:USD, 10), :month, 1)
iex> new = Money.Subscription.Plan.new!(Money.new(:USD, 10), :month, 3)
iex> Money.Subscription.change_plan current, new, current_interval_started: ~D[2018-01-01], effective: ~D[2018-01-15], prorate: :period
{:ok, %Money.Subscription.Change{
carry_forward: Money.zero(:USD),
credit_amount: Money.new(:USD, "5.49"),
credit_amount_applied: Money.zero(:USD),
credit_days_applied: 50,
credit_period_ends: ~D[2018-03-05],
next_interval_starts: ~D[2018-06-04],
first_billing_amount: Money.new(:USD, 10),
first_interval_starts: ~D[2018-01-15]
}}