fix customer creation flow
This commit is contained in:
@@ -12,7 +12,7 @@ class Enterprise::Billing::CreateStripeCustomerService
|
||||
|
||||
customer_id = prepare_customer_id
|
||||
update_account_for_v2_billing(customer_id)
|
||||
enable_plan_specific_features(default_plan['name'])
|
||||
enable_plan_specific_features('Hacker')
|
||||
end
|
||||
|
||||
private
|
||||
@@ -26,26 +26,16 @@ class Enterprise::Billing::CreateStripeCustomerService
|
||||
customer_id
|
||||
end
|
||||
|
||||
def default_quantity
|
||||
default_plan['default_quantity'] || DEFAULT_QUANTITY
|
||||
end
|
||||
|
||||
def billing_email
|
||||
account.administrators.first.email
|
||||
end
|
||||
|
||||
def default_plan
|
||||
installation_config = InstallationConfig.find_by(name: 'CHATWOOT_CLOUD_PLANS')
|
||||
@default_plan ||= installation_config&.value&.first
|
||||
end
|
||||
|
||||
def v2_configs_present?
|
||||
InstallationConfig.find_by(name: 'STRIPE_HACKER_PLAN_ID').present? &&
|
||||
default_plan.present?
|
||||
InstallationConfig.find_by(name: 'STRIPE_HACKER_PLAN_ID').present?
|
||||
end
|
||||
|
||||
def raise_config_error
|
||||
raise StandardError, 'V2 billing configuration is required. Please configure STRIPE_HACKER_PLAN_ID and CHATWOOT_CLOUD_PLANS.'
|
||||
raise StandardError, 'V2 billing configuration is required. Please configure STRIPE_HACKER_PLAN_ID.'
|
||||
end
|
||||
|
||||
def existing_subscription?
|
||||
|
||||
@@ -78,12 +78,6 @@ class Enterprise::Billing::HandleStripeEventService
|
||||
cloud_plans.find { |config| config['product_id'].include?(plan_id) }
|
||||
end
|
||||
|
||||
def default_plan?
|
||||
cloud_plans = InstallationConfig.find_by(name: CLOUD_PLANS_CONFIG)&.value || []
|
||||
default_plan = cloud_plans.first || {}
|
||||
account.custom_attributes['plan_name'] == default_plan['name']
|
||||
end
|
||||
|
||||
def process_credit_grant_created
|
||||
grant_id = extract_credit_grant_id(@event.data.object)
|
||||
return if grant_id.blank?
|
||||
|
||||
@@ -205,8 +205,10 @@ RSpec.describe 'Enterprise Billing APIs', type: :request do
|
||||
},
|
||||
'conversation' => {},
|
||||
'captain' => {
|
||||
'documents' => { 'consumed' => 0, 'current_available' => ChatwootApp.max_limit, 'total_count' => ChatwootApp.max_limit },
|
||||
'responses' => { 'consumed' => 0, 'current_available' => ChatwootApp.max_limit, 'total_count' => ChatwootApp.max_limit }
|
||||
'documents' => { 'consumed' => 0, 'current_available' => ChatwootApp.max_limit, 'total_count' => ChatwootApp.max_limit,
|
||||
'monthly' => nil, 'topup' => nil },
|
||||
'responses' => { 'consumed' => 0, 'current_available' => ChatwootApp.max_limit, 'total_count' => ChatwootApp.max_limit,
|
||||
'monthly' => 0, 'topup' => 0 }
|
||||
},
|
||||
'non_web_inboxes' => {}
|
||||
}
|
||||
|
||||
@@ -1,31 +1,32 @@
|
||||
require 'rails_helper'
|
||||
# rubocop:disable RSpec/VerifiedDoubles
|
||||
|
||||
RSpec.describe 'Enterprise::Webhooks::StripeController', type: :request do
|
||||
describe 'POST /enterprise/webhooks/stripe' do
|
||||
let(:params) { { content: 'hello' } }
|
||||
let(:v1_event_params) { { type: 'invoice.created', data: { object: {} } }.to_json }
|
||||
|
||||
it 'delegates to the v1 handler for legacy events' do
|
||||
handle_stripe = double
|
||||
event = double('Stripe::Event', type: 'invoice.created')
|
||||
handle_stripe = instance_double(Enterprise::Billing::HandleStripeEventService)
|
||||
event = instance_double(Stripe::Event, type: 'invoice.created')
|
||||
|
||||
allow(Stripe::Webhook).to receive(:construct_event).and_return(event)
|
||||
allow(Enterprise::Billing::HandleStripeEventService).to receive(:new).and_return(handle_stripe)
|
||||
allow(handle_stripe).to receive(:perform)
|
||||
|
||||
post '/enterprise/webhooks/stripe', headers: { 'Stripe-Signature': 'test' }, params: params
|
||||
post '/enterprise/webhooks/stripe', headers: { 'Stripe-Signature': 'test' }, params: v1_event_params, as: :json
|
||||
|
||||
expect(handle_stripe).to have_received(:perform).with(event: event)
|
||||
end
|
||||
|
||||
it 'delegates v2 billing events to the v2 webhook handler' do
|
||||
event = double('Stripe::Event', type: 'v2.billing.pricing_plan_subscription.servicing_activated')
|
||||
v2_event_params = { type: 'v2.billing.pricing_plan_subscription.servicing_activated', data: { object: {} } }.to_json
|
||||
event = instance_double(Stripe::Event, type: 'v2.billing.pricing_plan_subscription.servicing_activated')
|
||||
handler_double = instance_double(Enterprise::Billing::V2::WebhookHandlerService, perform: { success: true })
|
||||
|
||||
allow(Stripe::Webhook).to receive(:construct_event).and_return(event)
|
||||
allow(Enterprise::Billing::V2::WebhookHandlerService).to receive(:new).and_return(handler_double)
|
||||
|
||||
post '/enterprise/webhooks/stripe', headers: { 'Stripe-Signature': 'test' }, params: params
|
||||
post '/enterprise/webhooks/stripe', headers: { 'Stripe-Signature': 'test' }, params: v2_event_params, as: :json
|
||||
|
||||
expect(Enterprise::Billing::V2::WebhookHandlerService).to have_received(:new)
|
||||
expect(handler_double).to have_received(:perform).with(event: event)
|
||||
@@ -43,4 +44,3 @@ RSpec.describe 'Enterprise::Webhooks::StripeController', type: :request do
|
||||
end
|
||||
end
|
||||
end
|
||||
# rubocop:enable RSpec/VerifiedDoubles
|
||||
|
||||
Reference in New Issue
Block a user