Money-native · real, tested code

Prorate a Mid-Cycle Subscription Upgrade in Larzscript

A customer upgrades plans partway through a billing period - credit the unused days on the old plan, charge only the difference for the new one.

subscription.prorate(period_cents, period_days, unused_days) does the exact-cent proration math for each side; the actual money movement is a single real pay from the customer's wallet, guarded by require so an underfunded upgrade fails closed instead of leaving a partial charge.

larzscript pkg install subscription

Code

import "subscription" as subscription

wallet customer = $50.00
wallet business

let sub = subscription.start_trial("sub_1", 0)
subscription.activate(sub)

let old_plan_cents = 1000
let new_plan_cents = 2500
let period_days = 30
let days_into_cycle = 12
let days_remaining = period_days - days_into_cycle

let unused_credit_cents = subscription.prorate(old_plan_cents, period_days, days_remaining)
let new_plan_cost_cents = subscription.prorate(new_plan_cents, period_days, days_remaining)
let due_cents = new_plan_cost_cents - unused_credit_cents

print("credit for", days_remaining, "unused days of the old plan:", unused_credit_cents, "cents")
print("cost of the new plan for those same days:", new_plan_cost_cents, "cents")
print("net charge due today:", due_cents, "cents")

price upgrade_charge = money(float(due_cents) / 100.0)
require customer.balance >= upgrade_charge, "insufficient funds for upgrade"
pay upgrade_charge from customer to business

print("upgraded mid-cycle. customer balance:", customer.balance)
print("subscription state (unaffected by the plan swap):", sub["state"])
Output
credit for 18 unused days of the old plan: 600 cents
cost of the new plan for those same days: 1500 cents
net charge due today: 900 cents
upgraded mid-cycle. customer balance: $41.00
subscription state (unaffected by the plan swap): active

Why this matters in Larzscript

Proration math (prorate()) is provider-agnostic and portable to any language, but the actual charge is where this stays money-native: require guards the payment before it happens, so a failed upgrade can never leave the customer half-charged.