fix up extra env

This commit is contained in:
Tanmay Sharma
2025-10-13 13:29:00 +05:30
parent 029294e16b
commit 62203da528
4 changed files with 14 additions and 46 deletions
-10
View File
@@ -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)
}
@@ -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
@@ -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
@@ -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')