Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancel past due subscriptions immediately instead of next period #848

Merged
merged 4 commits into from
Oct 17, 2023
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

Rename webhooks for `paddle.*` to `paddle_classic.*`

* [Breaking] Subscriptions with `status: :canceled` and `ends_at: future` are now considered canceled. Previously, these were considered active to accomodate canceling a Braintree subscription during trial (and allowing the user to continue using until the end of the trial).
* [Breaking] `stripe_account` has been moved from the `data:json` column to a dedicated column

To migrate, create a migration to add the column.
Expand All @@ -25,6 +24,9 @@
Pay::Customer.find_each{ |c| c.update(stripe_account: c.data["stripe_account"]) }
```

* [Breaking] Subscriptions with `status: :canceled` and `ends_at: future` are now considered canceled. Previously, these were considered active to accomodate canceling a Braintree subscription during trial (and allowing the user to continue using until the end of the trial).
* [Breaking] Subscriptions with `status: :past_due` will be canceled immediately by default.

### 6.8.1

* [Stripe] Skip sync if object is not attached to a customer. Fixes #842
Expand Down
13 changes: 11 additions & 2 deletions lib/pay/stripe/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Subscription
:ends_at,
:name,
:on_trial?,
:past_due?,
:pause_starts_at,
:pause_starts_at?,
:processor_id,
Expand Down Expand Up @@ -158,11 +159,19 @@ def client_secret
stripe_sub&.pending_setup_intent&.client_secret || stripe_sub&.latest_invoice&.payment_intent&.client_secret
end

# Marks a subscription to cancel at period end
#
# If subscription is already past_due, the subscription will be cancelled immediately
# To disable this, pass past_due_cancel_now: false
def cancel(**options)
return if canceled?

@stripe_subscription = ::Stripe::Subscription.update(processor_id, {cancel_at_period_end: true}.merge(expand_options), stripe_options)
pay_subscription.update(ends_at: (on_trial? ? trial_ends_at : Time.at(@stripe_subscription.current_period_end)))
if past_due? && options.fetch(:past_due_cancel_now, true)
cancel_now!
else
@stripe_subscription = ::Stripe::Subscription.update(processor_id, {cancel_at_period_end: true}.merge(expand_options), stripe_options)
pay_subscription.update(ends_at: (on_trial? ? trial_ends_at : Time.at(@stripe_subscription.current_period_end)))
end
rescue ::Stripe::StripeError => e
raise Pay::Stripe::Error, e
end
Expand Down