From 62203da528fdf667bb1e6619c7336ceaeccec28b Mon Sep 17 00:00:00 2001 From: Tanmay Sharma Date: Mon, 13 Oct 2025 13:29:00 +0530 Subject: [PATCH] fix up extra env --- config/initializers/stripe.rb | 10 ------- .../enterprise/billing/v2/base_service.rb | 4 --- .../billing/v2/subscription_service.rb | 20 ++++---------- .../billing/v2/subscription_service_spec.rb | 26 +++++++------------ 4 files changed, 14 insertions(+), 46 deletions(-) diff --git a/config/initializers/stripe.rb b/config/initializers/stripe.rb index 66af4fc28..585d9dad4 100644 --- a/config/initializers/stripe.rb +++ b/config/initializers/stripe.rb @@ -1,13 +1,3 @@ require 'stripe' Stripe.api_key = ENV.fetch('STRIPE_SECRET_KEY', nil) - -# Set API version - using V2 preview version for credit management -Stripe.api_version = ENV['STRIPE_API_VERSION'] || '2025-08-27.preview' - -# V2 Billing Configuration -Rails.application.config.stripe_v2 = { - enabled: ENV['STRIPE_V2_ENABLED'] == 'true', - meter_id: ENV.fetch('STRIPE_V2_METER_ID', nil), - meter_event_name: ENV.fetch('STRIPE_V2_METER_EVENT_NAME', nil) -} diff --git a/enterprise/app/services/enterprise/billing/v2/base_service.rb b/enterprise/app/services/enterprise/billing/v2/base_service.rb index a01d065d2..685b19e9a 100644 --- a/enterprise/app/services/enterprise/billing/v2/base_service.rb +++ b/enterprise/app/services/enterprise/billing/v2/base_service.rb @@ -17,10 +17,6 @@ class Enterprise::Billing::V2::BaseService custom_attribute('stripe_billing_version').to_i == 2 end - def v2_config - Rails.application.config.stripe_v2 || {} - end - def monthly_credits custom_attribute('monthly_credits').to_i end diff --git a/enterprise/app/services/enterprise/billing/v2/subscription_service.rb b/enterprise/app/services/enterprise/billing/v2/subscription_service.rb index 8c2838a72..6118296df 100644 --- a/enterprise/app/services/enterprise/billing/v2/subscription_service.rb +++ b/enterprise/app/services/enterprise/billing/v2/subscription_service.rb @@ -1,10 +1,10 @@ class Enterprise::Billing::V2::SubscriptionService < Enterprise::Billing::V2::BaseService - def migrate_to_v2(plan_type: 'startup') + def migrate_to_v2(plan_type: 'startup', monthly_credits: 100) return { success: false, message: 'Already on V2' } if v2_enabled? with_locked_account do - apply_migration_attributes(plan_type) - log_migration_grant(plan_type) + apply_migration_attributes(plan_type, monthly_credits) + log_migration_grant(plan_type, monthly_credits) end { success: true, message: 'Successfully migrated to V2 billing' } @@ -22,8 +22,7 @@ class Enterprise::Billing::V2::SubscriptionService < Enterprise::Billing::V2::Ba private - def apply_migration_attributes(plan_type) - credits = plan_credits(plan_type) + def apply_migration_attributes(plan_type, credits) update_custom_attributes( 'stripe_billing_version' => 2, 'monthly_credits' => credits, @@ -33,8 +32,7 @@ class Enterprise::Billing::V2::SubscriptionService < Enterprise::Billing::V2::Ba ) end - def log_migration_grant(plan_type) - credits = plan_credits(plan_type) + def log_migration_grant(plan_type, credits) log_credit_transaction( type: 'grant', amount: credits, @@ -43,12 +41,4 @@ class Enterprise::Billing::V2::SubscriptionService < Enterprise::Billing::V2::Ba metadata: { 'source' => 'migration', 'plan_type' => plan_type } ) end - - def plan_credits(plan_type) - config = Rails.application.config.stripe_v2 - return 100 unless config && config[:plans] - - plan_config = config[:plans][plan_type.to_s.downcase.to_sym] - plan_config ? plan_config[:monthly_credits] : 100 - end end diff --git a/spec/enterprise/services/enterprise/billing/v2/subscription_service_spec.rb b/spec/enterprise/services/enterprise/billing/v2/subscription_service_spec.rb index 589a2df76..b9bb36a27 100644 --- a/spec/enterprise/services/enterprise/billing/v2/subscription_service_spec.rb +++ b/spec/enterprise/services/enterprise/billing/v2/subscription_service_spec.rb @@ -3,28 +3,13 @@ require 'rails_helper' describe Enterprise::Billing::V2::SubscriptionService do let(:account) { create(:account) } let(:service) { described_class.new(account: account) } - let(:config) { Rails.application.config.stripe_v2 } - - around do |example| - original_config = config.deep_dup - example.run - config.replace(original_config) - end - - before do - config[:plans] ||= {} - config[:plans][:startup] = { - monthly_credits: 800, - pricing_plan_id: 'plan_startup' - } - end describe '#migrate_to_v2' do it 'updates account attributes and logs initial credit grant' do # rubocop:disable RSpec/MultipleExpectations result = nil expect do - result = service.migrate_to_v2(plan_type: 'startup') + result = service.migrate_to_v2(plan_type: 'startup', monthly_credits: 800) end.to change { account.reload.credit_transactions.count }.by(1) account.reload @@ -39,10 +24,17 @@ describe Enterprise::Billing::V2::SubscriptionService do expect(transaction.metadata['plan_type']).to eq('startup') end + it 'uses default credits when not specified' do + result = service.migrate_to_v2 + + expect(result).to include(success: true) + expect(account.reload.custom_attributes['monthly_credits']).to eq(100) + end + it 'returns error when already on v2' do account.update!(custom_attributes: (account.custom_attributes || {}).merge('stripe_billing_version' => 2)) - result = service.migrate_to_v2 + result = service.migrate_to_v2(monthly_credits: 500) expect(result[:success]).to be(false) expect(result[:message]).to eq('Already on V2')