module Billing module Stripe module PlanChangeMessages module_function def flash_notice(result, current_plan: nil) plan = Plan[result.plan_slug] interval_label = PriceCatalog.label(plan_slug: result.plan_slug, interval: result.interval) if result.upgrade? amount = format_amount(result.amount_cents, result.currency) if amount.present? I18n.t("billing.messages.upgrade_completed_with_amount", plan: plan.name, interval: interval_label, amount: amount) else I18n.t("billing.messages.upgrade_completed_pending_charge", plan: plan.name, interval: interval_label) end else when_label = result.effective_at.present? ? I18n.l(result.effective_at, format: :long) : I18n.t("billing.messages.next_renewal") current_name = current_plan&.name || I18n.t("billing.messages.current_plan_generic") I18n.t("billing.messages.downgrade_scheduled", plan: plan.name, interval: interval_label, current_name: current_name, when: when_label) end end def button_label(plan:, interval:, kind:) price = PriceCatalog.label(plan_slug: plan.slug, interval: interval) name = I18n.t("pages.plans.cta_name.#{plan.slug}", default: plan.name) if kind == :checkout I18n.t("billing.messages.activate_button", plan: name, price: price) else I18n.t("billing.messages.switch_button", plan: name, price: price) end end def billing_info_lines(subscription: nil) lines = [ I18n.t("billing.messages.upgrade_info_line"), I18n.t("billing.messages.downgrade_info_line") ] if subscription&.plan_change_pending? && subscription.pending_plan.present? when_at = subscription.current_period_end when_label = when_at.present? ? I18n.l(when_at, format: :long) : I18n.t("billing.messages.next_renewal") lines.unshift( I18n.t("billing.messages.already_requested_line", plan: subscription.pending_plan.name, when: when_label, current_plan: subscription.plan.name) ) end lines end def format_amount(cents, currency) return nil if cents.blank? || cents.to_i <= 0 unit = currency.to_s.upcase.presence || "EUR" amount = cents.to_i / 100.0 format("%.2f %s", amount, unit == "EUR" ? "€" : unit) end end end end