From 6abecdfd435cf77bc4a4002309878d852ae05335 Mon Sep 17 00:00:00 2001 From: Tanmay Sharma Date: Mon, 17 Nov 2025 11:15:21 +0530 Subject: [PATCH] update the logic to get the number of credits for a monthly plan --- .../billing/concerns/plan_provisioning_helper.rb | 10 ---------- .../enterprise/billing/handle_stripe_event_service.rb | 11 ++++++++++- .../services/enterprise/billing/v2/plan_catalog.rb | 5 ----- .../billing/v2/subscription_provisioning_service.rb | 4 +++- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/enterprise/app/services/enterprise/billing/concerns/plan_provisioning_helper.rb b/enterprise/app/services/enterprise/billing/concerns/plan_provisioning_helper.rb index cb5447c48..d2cb238ad 100644 --- a/enterprise/app/services/enterprise/billing/concerns/plan_provisioning_helper.rb +++ b/enterprise/app/services/enterprise/billing/concerns/plan_provisioning_helper.rb @@ -4,8 +4,6 @@ module Enterprise::Billing::Concerns::PlanProvisioningHelper private def provision_new_plan(new_pricing_plan_id) - sync_plan_credits(new_pricing_plan_id) - plan_definition = Enterprise::Billing::V2::PlanCatalog.definition_for(new_pricing_plan_id) return unless plan_definition @@ -13,14 +11,6 @@ module Enterprise::Billing::Concerns::PlanProvisioningHelper enable_plan_specific_features(plan_name) if plan_name.present? end - def sync_plan_credits(pricing_plan_id) - plan_credits = Enterprise::Billing::V2::PlanCatalog.monthly_credits_for(pricing_plan_id) - - Enterprise::Billing::V2::CreditManagementService - .new(account: account) - .sync_monthly_response_credits(plan_credits.to_i) - end - def extract_plan_name(plan_definition) plan_definition[:display_name].split.find { |word| %w[Startup Startups Business Enterprise].include?(word) } end 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 a1baa7b0f..fac927eef 100644 --- a/enterprise/app/services/enterprise/billing/handle_stripe_event_service.rb +++ b/enterprise/app/services/enterprise/billing/handle_stripe_event_service.rb @@ -105,7 +105,16 @@ class Enterprise::Billing::HandleStripeEventService return credits.to_i if credits.present? && credits.to_i.positive? end - 0 + amount = extract_attribute(grant, :amount) + return 0 if amount.blank? + + custom_pricing_unit = extract_attribute(amount, :custom_pricing_unit) + return 0 if custom_pricing_unit.blank? + + value = extract_attribute(custom_pricing_unit, :value) + return 0 if value.blank? + + value.to_i end def extract_attribute(object, attribute) diff --git a/enterprise/app/services/enterprise/billing/v2/plan_catalog.rb b/enterprise/app/services/enterprise/billing/v2/plan_catalog.rb index c0d9583df..12655104e 100644 --- a/enterprise/app/services/enterprise/billing/v2/plan_catalog.rb +++ b/enterprise/app/services/enterprise/billing/v2/plan_catalog.rb @@ -53,11 +53,6 @@ module Enterprise::Billing::V2::PlanCatalog nil end - def monthly_credits_for(plan_id) - definition = definition_for(plan_id) - definition ? definition[:monthly_credits] : nil - end - def plan_id_for(definition) InstallationConfig.find_by(name: definition[:config_key])&.value end diff --git a/enterprise/app/services/enterprise/billing/v2/subscription_provisioning_service.rb b/enterprise/app/services/enterprise/billing/v2/subscription_provisioning_service.rb index 6b0018e0d..9bdc106ec 100644 --- a/enterprise/app/services/enterprise/billing/v2/subscription_provisioning_service.rb +++ b/enterprise/app/services/enterprise/billing/v2/subscription_provisioning_service.rb @@ -63,7 +63,9 @@ class Enterprise::Billing::V2::SubscriptionProvisioningService < Enterprise::Bil update_custom_attributes(attributes) # Sync credits for Hacker plan (0 credits) - sync_plan_credits(pricing_plan_id) + Enterprise::Billing::V2::CreditManagementService + .new(account: account) + .sync_monthly_response_credits(0) # Disable all premium features and save disable_all_premium_features