style(billing): make comments concise

This commit is contained in:
Tanmay Deep Sharma
2026-06-03 10:35:54 +05:30
parent 8995203473
commit 4bf6a15c06
8 changed files with 13 additions and 37 deletions
+1 -3
View File
@@ -23,9 +23,7 @@ module BillingHelper
account.users.count
end
# current_period_end moved from the subscription top-level to the subscription
# item in recent Stripe API versions — read both so the paid-through date is
# never lost.
# current_period_end moved to the subscription item in newer Stripe API versions; read both.
def subscription_period_end(subscription)
subscription['current_period_end'] || subscription['items']['data'].first&.[]('current_period_end')
end
@@ -28,8 +28,7 @@ class EnterpriseAccountAPI extends ApiClient {
return axios.post(`${this.url}topup_checkout`, { credits });
}
// Returns { currency, options: [{ credits, amount, currency }] } for the
// account's billing currency, sourced from CHATWOOT_CLOUD_TOPUP_OPTIONS.
// Topup packages for the account's billing currency.
getTopupOptions() {
return axios.get(`${this.url}topup_options`);
}
@@ -25,8 +25,7 @@ const selectedCredits = ref(null);
const isLoading = ref(false);
const currentStep = ref(STEP_SELECT);
// Topup packages come from the backend (CHATWOOT_CLOUD_TOPUP_OPTIONS) for the
// account's billing currency — only the relevant currency's options are shown.
// Topup packages come from the backend for the account's billing currency.
const topupOptions = ref([]);
const optionsCurrency = ref(DEFAULT_BILLING_CURRENCY);
const isFetchingOptions = ref(false);
+2 -7
View File
@@ -68,17 +68,12 @@ module Enterprise::Account
saml_settings&.saml_enabled? || false
end
# Effective billing currency, kept in sync with Stripe by the billing services
# and the subscription webhook.
def billing_currency
stored = custom_attributes&.dig('billing_currency')
return Enterprise::Billing::Currencies.normalize(stored) if Enterprise::Billing::Currencies.supported?(stored)
# An account that already has a Stripe customer is billed in real money
# (legacy USD) and gets its currency backfilled from the subscription by the
# webhook — never infer it from locale, or existing pt_BR customers on USD
# would be shown/charged BRL. Only brand-new accounts (no Stripe customer
# yet) default by locale.
# Existing Stripe customers stay on USD (webhook backfills the real currency);
# only brand-new accounts infer from locale, so existing pt_BR users aren't charged BRL.
return Enterprise::Billing::Currencies::DEFAULT if custom_attributes&.dig('stripe_customer_id').present?
Enterprise::Billing::Currencies.for_locale(locale)
@@ -1,19 +1,14 @@
# Single source of truth for the billing currencies Chatwoot Cloud supports.
# Adding a new currency (e.g. EUR) is a one-line edit here plus the matching
# price_ids in CHATWOOT_CLOUD_PLANS and rates in CHATWOOT_CLOUD_TOPUP_OPTIONS.
# Supported billing currencies and their Stripe/locale mappings.
module Enterprise::Billing::Currencies
DEFAULT = 'usd'.freeze
SUPPORTED = %w[usd brl].freeze
# Account locale label (the enum label, e.g. 'pt_BR') => default currency.
# Anything not listed falls back to DEFAULT.
# Account locale label (e.g. 'pt_BR') => default currency; unlisted falls back to DEFAULT.
LOCALE_DEFAULTS = {
'pt_BR' => 'brl'
}.freeze
# Used to keep the Stripe customer's location/currency in sync with the
# account's billing currency.
COUNTRY_BY_CURRENCY = {
'usd' => 'US',
'brl' => 'BR'
@@ -69,9 +69,7 @@ class Enterprise::Billing::HandleStripeEventService
)
end
# Paid subscriptions are billed in real money, so their currency is
# authoritative and overrides the stored value. The default/free plan is $0
# and not currency-defining, so we preserve the account's chosen preference.
# Paid subscriptions define the currency; the free/default plan keeps the stored preference.
def billing_currency_for(subscription, plan)
return account.billing_currency if plan['name'] == Enterprise::Billing::PlanConfiguration.default_plan&.dig('name')
@@ -1,11 +1,5 @@
# Reads CHATWOOT_CLOUD_PLANS and resolves Stripe price ids in a currency-aware,
# backward-compatible way. Owns all plan-shape parsing so the currency logic
# isn't scattered across the billing services.
#
# A plan's `price_ids` may be:
# - a currency-keyed Hash: { 'usd' => ['price_x'], 'brl' => ['price_y'] }
# - a flat Array (legacy): ['price_x'] -> treated as usd
# - a bare String (legacy): 'price_x' -> treated as usd
# Resolves Stripe price ids from CHATWOOT_CLOUD_PLANS per currency.
# A plan's `price_ids` may be a currency-keyed Hash, or a legacy Array/String (treated as usd).
module Enterprise::Billing::PlanConfiguration
CLOUD_PLANS_CONFIG = 'CHATWOOT_CLOUD_PLANS'.freeze
@@ -29,8 +23,7 @@ module Enterprise::Billing::PlanConfiguration
end
end
# Price id to subscribe `plan` in `currency`. Falls back to usd, then to any
# configured price, so a free plan with only a usd price still resolves.
# Price id for `plan` in `currency`, falling back to usd then any configured price.
def price_id_for(plan, currency)
by_currency = price_ids_by_currency(plan)
code = Enterprise::Billing::Currencies.coerce(currency)
@@ -44,7 +37,7 @@ module Enterprise::Billing::PlanConfiguration
price_ids_by_currency(plan).values.flatten.compact.include?(price_id)
end
# Webhook currency inference: [plan, currency] for a given price id, or [nil, nil].
# [plan, currency] for a price id, else [nil, nil].
def find_plan_by_price_id(price_id)
plans.each do |plan|
price_ids_by_currency(plan).each do |currency, ids|
@@ -7,8 +7,7 @@ class Enterprise::Billing::TopupCheckoutService
pattr_initialize [:account!]
# Topup packages for the account's billing currency, used by the controller
# to render the same options the frontend offers.
# Topup packages for the account's billing currency (used by the controller).
def available_options
topup_options
end