feat: Enable features on successful subscription (#6953)
* feat: Enable features on successful subscription * fix: Add specs * disable features for default plan * Remove mark as cloud customers --------- Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
@@ -27,6 +27,7 @@ class Enterprise::Billing::HandleStripeEventService
|
||||
subscribed_quantity: subscription['quantity']
|
||||
}
|
||||
)
|
||||
change_plan_features
|
||||
end
|
||||
|
||||
def process_subscription_deleted
|
||||
@@ -36,10 +37,23 @@ class Enterprise::Billing::HandleStripeEventService
|
||||
Enterprise::Billing::CreateStripeCustomerService.new(account: account).perform
|
||||
end
|
||||
|
||||
def change_plan_features
|
||||
if default_plan?
|
||||
account.disable_features(*features_to_update)
|
||||
else
|
||||
account.enable_features(*features_to_update)
|
||||
end
|
||||
account.save!
|
||||
end
|
||||
|
||||
def ensure_event_context(event)
|
||||
@event = event
|
||||
end
|
||||
|
||||
def features_to_update
|
||||
%w[help_center campaigns team_management channel_twitter channel_facebook channel_email]
|
||||
end
|
||||
|
||||
def subscription
|
||||
@subscription ||= @event.data.object
|
||||
end
|
||||
@@ -52,4 +66,10 @@ class Enterprise::Billing::HandleStripeEventService
|
||||
installation_config = InstallationConfig.find_by(name: 'CHATWOOT_CLOUD_PLANS')
|
||||
installation_config.value.find { |config| config['product_id'].include?(plan_id) }
|
||||
end
|
||||
|
||||
def default_plan?
|
||||
installation_config = InstallationConfig.find_by(name: 'CHATWOOT_CLOUD_PLANS')
|
||||
default_plan = installation_config.value.first
|
||||
@account.custom_attributes['plan_name'] == default_plan['name']
|
||||
end
|
||||
end
|
||||
|
||||
@@ -48,6 +48,21 @@ describe Enterprise::Billing::HandleStripeEventService do
|
||||
})
|
||||
end
|
||||
|
||||
it 'disable features on customer.subscription.updated for default plan' do
|
||||
allow(event).to receive(:type).and_return('customer.subscription.updated')
|
||||
allow(subscription).to receive(:customer).and_return('cus_123')
|
||||
stripe_event_service.new.perform(event: event)
|
||||
expect(account.reload.custom_attributes).to eq({
|
||||
'stripe_customer_id' => 'cus_123',
|
||||
'stripe_price_id' => 'test',
|
||||
'stripe_product_id' => 'plan_id',
|
||||
'plan_name' => 'Hacker',
|
||||
'subscribed_quantity' => '10'
|
||||
})
|
||||
expect(account).not_to be_feature_enabled('channel_email')
|
||||
expect(account).not_to be_feature_enabled('help_center')
|
||||
end
|
||||
|
||||
it 'handles customer.subscription.deleted' do
|
||||
stripe_customer_service = double
|
||||
allow(event).to receive(:type).and_return('customer.subscription.deleted')
|
||||
@@ -57,4 +72,32 @@ describe Enterprise::Billing::HandleStripeEventService do
|
||||
expect(Enterprise::Billing::CreateStripeCustomerService).to have_received(:new).with(account: account)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#perform for Startups plan' do
|
||||
before do
|
||||
allow(event).to receive(:data).and_return(data)
|
||||
allow(data).to receive(:object).and_return(subscription)
|
||||
allow(subscription).to receive(:[]).with('plan')
|
||||
.and_return({
|
||||
'id' => 'test', 'product' => 'plan_id_2', 'name' => 'plan_name'
|
||||
})
|
||||
allow(subscription).to receive(:[]).with('quantity').and_return('10')
|
||||
allow(subscription).to receive(:customer).and_return('cus_123')
|
||||
end
|
||||
|
||||
it 'enable features on customer.subscription.updated' do
|
||||
allow(event).to receive(:type).and_return('customer.subscription.updated')
|
||||
allow(subscription).to receive(:customer).and_return('cus_123')
|
||||
stripe_event_service.new.perform(event: event)
|
||||
expect(account.reload.custom_attributes).to eq({
|
||||
'stripe_customer_id' => 'cus_123',
|
||||
'stripe_price_id' => 'test',
|
||||
'stripe_product_id' => 'plan_id_2',
|
||||
'plan_name' => 'Startups',
|
||||
'subscribed_quantity' => '10'
|
||||
})
|
||||
expect(account).to be_feature_enabled('channel_email')
|
||||
expect(account).to be_feature_enabled('help_center')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user