From 459018fa52d0f3affb7a70c53de52aeb12d638c9 Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Fri, 2 Jun 2023 09:28:13 -0500 Subject: [PATCH] Refactor on_grace_period? implementation --- CHANGELOG.md | 2 ++ app/models/pay/subscription.rb | 17 ----------------- lib/pay/braintree/subscription.rb | 6 +++++- lib/pay/paddle/subscription.rb | 5 ++++- lib/pay/stripe/subscription.rb | 16 +++++++++++++++- 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b473a2d3..d6fcca6e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/app/models/pay/subscription.rb b/app/models/pay/subscription.rb index de00fe574..975da5792 100644 --- a/app/models/pay/subscription.rb +++ b/app/models/pay/subscription.rb @@ -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 diff --git a/lib/pay/braintree/subscription.rb b/lib/pay/braintree/subscription.rb index d919c578a..afba8f477 100644 --- a/lib/pay/braintree/subscription.rb +++ b/lib/pay/braintree/subscription.rb @@ -5,7 +5,7 @@ class Subscription delegate :active?, :canceled?, - :on_grace_period?, + :ends_at?, :customer, :ends_at, :name, @@ -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 diff --git a/lib/pay/paddle/subscription.rb b/lib/pay/paddle/subscription.rb index c4e91081d..b52ca6526 100644 --- a/lib/pay/paddle/subscription.rb +++ b/lib/pay/paddle/subscription.rb @@ -11,6 +11,7 @@ class Subscription :name, :owner, :pause_starts_at, + :pause_starts_at?, :processor_id, :processor_plan, :processor_subscription, @@ -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? diff --git a/lib/pay/stripe/subscription.rb b/lib/pay/stripe/subscription.rb index b287d7133..e1c7570ad 100644 --- a/lib/pay/stripe/subscription.rb +++ b/lib/pay/stripe/subscription.rb @@ -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, @@ -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