use stripe gem beta version

This commit is contained in:
Tanmay Deep Sharma
2025-11-06 13:38:47 +05:30
parent 79bb3d0ef3
commit d51148951f
10 changed files with 46 additions and 223 deletions
@@ -5,7 +5,6 @@ RSpec.describe Enterprise::Billing::CreditSyncJob, type: :job do
before do
allow(ENV).to receive(:fetch).and_call_original
allow(ENV).to receive(:fetch).with('STRIPE_BILLING_V2_ENABLED', 'false').and_return('true')
allow(ENV).to receive(:fetch).with('STRIPE_SECRET_KEY', nil).and_return('sk_test_123')
allow(InstallationConfig).to receive(:find_by).and_call_original
allow(InstallationConfig).to receive(:find_by).with(name: 'STRIPE_METER_ID')
@@ -7,42 +7,15 @@ describe Enterprise::Billing::CreateStripeCustomerService do
let!(:admin1) { create(:user, account: account, role: :administrator) }
describe '#perform' do
context 'when customer does not exist' do
it 'creates a stripe customer and saves the customer_id' do
customer = double
allow(Stripe::Customer).to receive(:create).and_return(customer)
allow(customer).to receive(:id).and_return('cus_random_number')
create_stripe_customer_service.new(account: account).perform
expect(Stripe::Customer).to have_received(:create).with({ name: account.name, email: admin1.email })
expect(account.reload.custom_attributes).to eq(
{
stripe_customer_id: 'cus_random_number'
}.with_indifferent_access
)
context 'when V2 configs are missing' do
it 'raises a configuration error' do
expect do
create_stripe_customer_service.new(account: account).perform
end.to raise_error(StandardError, /V2 billing configuration is required/)
end
end
context 'when customer already exists' do
before do
account.update!(custom_attributes: { stripe_customer_id: 'cus_existing_customer' })
end
it 'does not create a new customer' do
allow(Stripe::Customer).to receive(:create)
# Stub the subscription check to return no subscriptions
subscriptions_response = OpenStruct.new(data: [])
allow(Stripe::Subscription).to receive(:list).and_return(subscriptions_response)
create_stripe_customer_service.new(account: account).perform
expect(Stripe::Customer).not_to have_received(:create)
expect(account.reload.custom_attributes['stripe_customer_id']).to eq('cus_existing_customer')
end
end
context 'with V2 billing enabled' do
context 'with V2 billing' do
let(:cloud_plans_config) do
create(:installation_config,
name: 'CHATWOOT_CLOUD_PLANS',
@@ -65,8 +38,6 @@ describe Enterprise::Billing::CreateStripeCustomerService do
# Setup configs
cloud_plans_config
hacker_plan_config
# Enable V2 billing
allow(ENV).to receive(:fetch).with('STRIPE_BILLING_V2_ENABLED', 'false').and_return('true')
end
it 'creates a stripe customer and sets up V2 billing' do
@@ -83,6 +54,7 @@ describe Enterprise::Billing::CreateStripeCustomerService do
expect(Stripe::Customer).to have_received(:create).with({ name: account.name, email: admin1.email })
expect(account.reload.custom_attributes).to include(
'stripe_customer_id' => 'cus_random_number',
'stripe_billing_version' => 'v2',
'stripe_pricing_plan_id' => 'bpp_hacker_123',
'plan_name' => 'Hacker',
'subscribed_quantity' => 2
@@ -107,6 +79,7 @@ describe Enterprise::Billing::CreateStripeCustomerService do
expect(Stripe::Customer).not_to have_received(:create)
expect(account.reload.custom_attributes).to include(
'stripe_customer_id' => 'cus_existing_v2',
'stripe_billing_version' => 'v2',
'stripe_pricing_plan_id' => 'bpp_hacker_123',
'plan_name' => 'Hacker',
'subscribed_quantity' => 2
@@ -128,48 +101,5 @@ describe Enterprise::Billing::CreateStripeCustomerService do
expect(account.reload.custom_attributes['stripe_customer_id']).to eq('cus_existing_v2')
end
end
context 'with V1 billing (traditional subscription)' do
let(:cloud_plans_config) do
create(:installation_config,
name: 'CHATWOOT_CLOUD_PLANS',
value: [
{
'name' => 'Startup',
'price_ids' => ['price_startup_123'],
'default_quantity' => 2
}
])
end
before do
cloud_plans_config
allow(ENV).to receive(:fetch).with('STRIPE_BILLING_V2_ENABLED', 'false').and_return('false')
end
it 'creates a stripe customer and traditional subscription' do
customer = double
allow(Stripe::Customer).to receive(:create).and_return(customer)
allow(customer).to receive(:id).and_return('cus_random_number')
subscription = {
'plan' => { 'id' => 'price_startup_123', 'product' => 'prod_startup' },
'quantity' => 2
}
allow(Stripe::Subscription).to receive(:create).and_return(subscription)
create_stripe_customer_service.new(account: account).perform
expect(Stripe::Customer).to have_received(:create).with({ name: account.name, email: admin1.email })
expect(Stripe::Subscription).to have_received(:create)
expect(account.reload.custom_attributes).to include(
'stripe_customer_id' => 'cus_random_number',
'stripe_price_id' => 'price_startup_123',
'stripe_product_id' => 'prod_startup',
'plan_name' => 'Startup',
'subscribed_quantity' => 2
)
end
end
end
end