Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 1 addition & 57 deletions app/services/subscriptions/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,53 +191,7 @@ def upgrade_subscription
end

def downgrade_subscription
if current_subscription.starting_in_the_future?
update_pending_subscription

return current_subscription
end

cancel_pending_subscription if pending_subscription?

# NOTE: When downgrading a subscription, we keep the current one active
# until the next billing day. The new subscription will become active at this date
new_sub = current_subscription.next_subscriptions.create!(
organization_id: customer.organization_id,
customer:,
plan: params.key?(:plan_overrides) ? override_plan(plan) : plan,
name:,
external_id: current_subscription.external_id,
subscription_at: current_subscription.subscription_at,
status: :pending,
billing_time: current_subscription.billing_time,
ending_at: params.key?(:ending_at) ? params[:ending_at] : current_subscription.ending_at,
progressive_billing_disabled: params[:progressive_billing_disabled] || false
)

if params.key?(:payment_method)
new_sub.payment_method_type = params[:payment_method][:payment_method_type] if params[:payment_method].key?(:payment_method_type)
new_sub.payment_method_id = params[:payment_method][:payment_method_id] if params[:payment_method].key?(:payment_method_id)
new_sub.save!
end

InvoiceCustomSections::AttachToResourceService.call(resource: new_sub, params:)

after_commit do
SendWebhookJob.perform_later("subscription.updated", current_subscription)
Utils::ActivityLog.produce(current_subscription, "subscription.updated")
end

current_subscription
end

def pending_subscription?
return false unless current_subscription&.next_subscription

current_subscription.next_subscription.pending?
end

def cancel_pending_subscription
current_subscription.next_subscription.mark_as_canceled!
PlanDowngradeService.call!(customer:, current_subscription:, plan:, params:).subscription
end

def subscription_type
Expand All @@ -253,16 +207,6 @@ def currency_missmatch?(old_plan, new_plan)
old_plan.amount_currency != new_plan.amount_currency
end

def update_pending_subscription
current_subscription.plan = plan
current_subscription.name = name if name.present?
current_subscription.save!

if current_subscription.should_sync_hubspot_subscription?
Integrations::Aggregator::Subscriptions::Hubspot::UpdateJob.perform_later(subscription: current_subscription)
end
end

def apply_activation_rules(subscription)
return unless params[:activation_rules]&.present?

Expand Down
91 changes: 91 additions & 0 deletions app/services/subscriptions/plan_downgrade_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# frozen_string_literal: true

module Subscriptions
class PlanDowngradeService < BaseService
Result = BaseResult[:subscription]

def initialize(customer:, current_subscription:, plan:, params:)
@customer = customer
@current_subscription = current_subscription
@plan = plan

@params = params
@name = params[:name].to_s.strip
super
end

def call
if current_subscription.starting_in_the_future?
update_pending_subscription

result.subscription = current_subscription
return result
end

ActiveRecord::Base.transaction do
cancel_pending_subscription if pending_subscription?

# NOTE: When downgrading a subscription, we keep the current one active
# until the next billing day. The new subscription will become active at this date
new_sub = current_subscription.next_subscriptions.create!(
organization_id: customer.organization_id,
customer:,
plan: params.key?(:plan_overrides) ? override_plan : plan,
name:,
external_id: current_subscription.external_id,
subscription_at: current_subscription.subscription_at,
status: :pending,
billing_time: current_subscription.billing_time,
ending_at: params.key?(:ending_at) ? params[:ending_at] : current_subscription.ending_at,
progressive_billing_disabled: params[:progressive_billing_disabled] || false
)

if params.key?(:payment_method)
new_sub.payment_method_type = params[:payment_method][:payment_method_type] if params[:payment_method].key?(:payment_method_type)
new_sub.payment_method_id = params[:payment_method][:payment_method_id] if params[:payment_method].key?(:payment_method_id)
new_sub.save!
end

InvoiceCustomSections::AttachToResourceService.call(resource: new_sub, params:)

after_commit do
SendWebhookJob.perform_later("subscription.updated", current_subscription)
Utils::ActivityLog.produce(current_subscription, "subscription.updated")
end
end

result.subscription = current_subscription
result
rescue ActiveRecord::RecordInvalid => e
result.record_validation_failure!(record: e.record)
end

private

attr_reader :customer, :current_subscription, :plan, :params, :name

def update_pending_subscription
current_subscription.plan = plan
current_subscription.name = name if name.present?
current_subscription.save!

if current_subscription.should_sync_hubspot_subscription?
Integrations::Aggregator::Subscriptions::Hubspot::UpdateJob.perform_later(subscription: current_subscription)
end
end

def override_plan
Plans::OverrideService.call(plan:, params: params[:plan_overrides].to_h.with_indifferent_access).plan
end

def cancel_pending_subscription
current_subscription.next_subscription.mark_as_canceled!
end

def pending_subscription?
return false unless current_subscription.next_subscription

current_subscription.next_subscription.pending?
end
end
end
22 changes: 22 additions & 0 deletions spec/services/subscriptions/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,28 @@
expect(result.error.messages[:subscription]).to eq(["subscription_incomplete"])
end
end

context "when subscription downgrade fails" do
let(:result_failure) do
BaseService::Result.new.validation_failure!(
errors: {billing_time: ["value_is_invalid"]}
)
end

before do
allow(Subscriptions::PlanDowngradeService)
.to receive(:call)
.and_return(result_failure)
end

it "returns an error" do
result = create_service.call

expect(result).not_to be_success
expect(result.error).to be_a(BaseService::ValidationFailure)
expect(result.error.messages).to eq({billing_time: ["value_is_invalid"]})
end
end
end
end
end
Expand Down
Loading
Loading