Skip to content

Commit

Permalink
Refactor on_grace_period? implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Jun 2, 2023
1 parent e4ae875 commit 459018f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Unreleased

* Refactor `on_grace_period?` to be implemented separately by each payment processor

### 6.5.0

* Add new configuration option named `send_emails` which can be used to disable the sending of all current and future emails.
Expand Down
17 changes: 0 additions & 17 deletions app/models/pay/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,6 @@ def ended?
ends_at? && ends_at <= Time.current
end

def on_grace_period?
(ends_at? && ends_at > Time.current) ||
((status == "paused" || pause_behavior == "void") && will_pause?)
end

def will_pause?
pause_starts_at? && Time.current < pause_starts_at
end

def pause_active?
if status == "paused" # Paddle
true
elsif pause_behavior == "void"
pause_starts_at.nil? || Time.current.after?(pause_starts_at)
end
end

# If you cancel during a trial, you should still retain access until the end of the trial
# Otherwise a subscription is active unless it has ended or is currently paused
# Check the subscription status so we don't accidentally consider "incomplete", "past_due", or other statuses as active
Expand Down
6 changes: 5 additions & 1 deletion lib/pay/braintree/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Subscription

delegate :active?,
:canceled?,
:on_grace_period?,
:ends_at?,
:customer,
:ends_at,
:name,
Expand Down Expand Up @@ -100,6 +100,10 @@ def change_quantity(quantity, **options)
raise NotImplementedError, "Braintree does not support setting quantity on subscriptions"
end

def on_grace_period?
ends_at? && ends_at > Time.current
end

def paused?
false
end
Expand Down
5 changes: 4 additions & 1 deletion lib/pay/paddle/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Subscription
:name,
:owner,
:pause_starts_at,
:pause_starts_at?,
:processor_id,
:processor_plan,
:processor_subscription,
Expand Down Expand Up @@ -107,8 +108,10 @@ def change_quantity(quantity, **options)
raise NotImplementedError, "Paddle does not support setting quantity on subscriptions"
end

# A subscription could be set to cancel or pause in the future
# It is considered on grace period until the cancel or pause time begins
def on_grace_period?
canceled? && Time.current < ends_at || paused? && Time.current < paddle_paused_from
(canceled? && Time.current < ends_at) || (paused? && pause_starts_at? && Time.current < pause_starts_at)
end

def paused?
Expand Down
16 changes: 15 additions & 1 deletion lib/pay/stripe/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ class Subscription

delegate :active?,
:canceled?,
:on_grace_period?,
:ends_at?,
:ends_at,
:name,
:on_trial?,
:pause_starts_at,
:pause_starts_at?,
:processor_id,
:processor_plan,
:processor_subscription,
Expand Down Expand Up @@ -184,6 +186,18 @@ def change_quantity(quantity, **options)
raise Pay::Stripe::Error, e
end

def on_grace_period?
(ends_at? && ends_at > Time.current) || (paused? && will_pause?)
end

def pause_active?
paused? && (pause_starts_at.nil? || Time.current.after?(pause_starts_at))
end

def will_pause?
pause_starts_at? && Time.current < pause_starts_at
end

def paused?
pause_behavior == "void"
end
Expand Down

0 comments on commit 459018f

Please sign in to comment.