feat(payment-gated-subs): Cover activation of subscriptions coming from upgrades and downgrades#5586
Open
osmarluz wants to merge 5 commits into
Conversation
…s coming from upgrades
…e-subscriptions-from-upgrade
…ng from downgrades
osmarluz
commented
May 27, 2026
| # events emitted during gate_subscription — only the previous needs billing. | ||
| unless from_incomplete | ||
| emit_fixed_charge_events | ||
| billable_subscriptions << subscription if subscription.fixed_charges.pay_in_advance.any? || subscription.plan.pay_in_advance? |
Contributor
Author
There was a problem hiding this comment.
terminate_and_start_next on app/services/subscriptions/terminate_service.rb previously did:
# NOTE: Create an invoice for the terminated subscription
# if it has not been billed yet
# or only for the charges if subscription was billed in advance
# Also, add new pay in advance plan inside if applicable
billable_subscriptions = if next_subscription.plan.pay_in_advance? || next_subscription.fixed_charges.pay_in_advance.any?
[subscription, next_subscription]
else
[subscription]
endIt's omitting the !subscription.in_trial_period? check and this is probably a bug, but the current behavior was kept unchanged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Continues the payment-gated subscriptions work. #5533 gated upgrades only on the from-pending path, but a gated upgrade actually completes from the incomplete state once payment resolves, and downgrades reach activation through a different route entirely — a pending downgrade is rotated in at the next billing day via
TerminateService#terminate_and_start_next. This PR brings upgrades and downgrades under the same activation path so either can be payment-gated.Description
TerminateService#terminate_and_start_nextnow delegates the whole rotation toSubscriptions::ActivateService.call!instead of terminating, activating and billing inline, so a rotated-in downgrade runs through rule evaluation and can be payment-gated (failures raise so the job retries).ActivateServicegains adowngrade?path: the from-pending and from-incomplete flows route downgrades to a newactivate_for_downgrade, which terminates the previous subscription and activates the new one, mirroringactivate_for_upgrade— including the from-incomplete handling that skips re-emitting fixed-charge events and re-billing the already-gated new subscription.bill_rotation_subscriptions(billable_subscriptions, billing_at:)(non-invoiceable fees always target the previous subscription), andnotify_startednow creates the Hubspot record for downgraded subscriptions too.