From 4bf6a15c0611eebb2871c0762914a7468612589d Mon Sep 17 00:00:00 2001 From: Tanmay Deep Sharma Date: Wed, 3 Jun 2026 10:35:54 +0530 Subject: [PATCH] style(billing): make comments concise --- app/helpers/billing_helper.rb | 4 +--- .../dashboard/api/enterprise/account.js | 3 +-- .../billing/components/PurchaseCreditsModal.vue | 3 +-- enterprise/app/models/enterprise/account.rb | 9 ++------- .../app/services/enterprise/billing/currencies.rb | 9 ++------- .../billing/handle_stripe_event_service.rb | 4 +--- .../enterprise/billing/plan_configuration.rb | 15 ++++----------- .../enterprise/billing/topup_checkout_service.rb | 3 +-- 8 files changed, 13 insertions(+), 37 deletions(-) diff --git a/app/helpers/billing_helper.rb b/app/helpers/billing_helper.rb index f7538cc01..7545b6d8f 100644 --- a/app/helpers/billing_helper.rb +++ b/app/helpers/billing_helper.rb @@ -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 diff --git a/app/javascript/dashboard/api/enterprise/account.js b/app/javascript/dashboard/api/enterprise/account.js index a377468f4..2810410bd 100644 --- a/app/javascript/dashboard/api/enterprise/account.js +++ b/app/javascript/dashboard/api/enterprise/account.js @@ -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`); } diff --git a/app/javascript/dashboard/routes/dashboard/settings/billing/components/PurchaseCreditsModal.vue b/app/javascript/dashboard/routes/dashboard/settings/billing/components/PurchaseCreditsModal.vue index 8bc1f33d4..ce59a8738 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/billing/components/PurchaseCreditsModal.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/billing/components/PurchaseCreditsModal.vue @@ -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); diff --git a/enterprise/app/models/enterprise/account.rb b/enterprise/app/models/enterprise/account.rb index 10752371e..c604421c8 100644 --- a/enterprise/app/models/enterprise/account.rb +++ b/enterprise/app/models/enterprise/account.rb @@ -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) diff --git a/enterprise/app/services/enterprise/billing/currencies.rb b/enterprise/app/services/enterprise/billing/currencies.rb index cba6d71cb..783ba7547 100644 --- a/enterprise/app/services/enterprise/billing/currencies.rb +++ b/enterprise/app/services/enterprise/billing/currencies.rb @@ -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' diff --git a/enterprise/app/services/enterprise/billing/handle_stripe_event_service.rb b/enterprise/app/services/enterprise/billing/handle_stripe_event_service.rb index afcb10962..927374c37 100644 --- a/enterprise/app/services/enterprise/billing/handle_stripe_event_service.rb +++ b/enterprise/app/services/enterprise/billing/handle_stripe_event_service.rb @@ -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') diff --git a/enterprise/app/services/enterprise/billing/plan_configuration.rb b/enterprise/app/services/enterprise/billing/plan_configuration.rb index e7ee67aad..dffae7ff8 100644 --- a/enterprise/app/services/enterprise/billing/plan_configuration.rb +++ b/enterprise/app/services/enterprise/billing/plan_configuration.rb @@ -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| diff --git a/enterprise/app/services/enterprise/billing/topup_checkout_service.rb b/enterprise/app/services/enterprise/billing/topup_checkout_service.rb index e896a4d48..c845a15e6 100644 --- a/enterprise/app/services/enterprise/billing/topup_checkout_service.rb +++ b/enterprise/app/services/enterprise/billing/topup_checkout_service.rb @@ -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