Skip to content

Commit bcd74d9

Browse files
committed
Handle subscription cancellations and persist cancellation date
1 parent 7d7f583 commit bcd74d9

File tree

7 files changed

+35
-5
lines changed

7 files changed

+35
-5
lines changed

app/controllers/stripe/webhooks_controller.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,13 @@ def handle_checkout_completed(session)
4848
end
4949

5050
def handle_subscription_updated(stripe_subscription)
51-
subscription = find_subscription_by_customer(stripe_subscription.customer)
52-
return unless subscription
53-
54-
subscription.update!(status: stripe_subscription.status, current_period_end: extract_current_period_end(stripe_subscription))
51+
if subscription = find_subscription_by_customer(stripe_subscription.customer)
52+
subscription.update!(
53+
status: stripe_subscription.status,
54+
current_period_end: extract_current_period_end(stripe_subscription),
55+
cancel_at: stripe_subscription.cancel_at ? Time.at(stripe_subscription.cancel_at) : nil
56+
)
57+
end
5558
end
5659

5760
def handle_subscription_deleted(stripe_subscription)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module SubscriptionsHelper
2+
def subscription_period_end_action(subscription)
3+
if subscription.canceled?
4+
subscription.cancel_at.past? ? "Ended" : "Ends"
5+
else
6+
"Renews"
7+
end
8+
end
9+
end

app/models/account/subscription.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ def plan
1212
def paid?
1313
!plan.free? && active?
1414
end
15+
16+
def canceled?
17+
cancel_at.present?
18+
end
1519
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<p>Status: <strong><%= subscription.canceled? ? "Canceled" : subscription.status.titleize %></strong></p>
2+
<% if subscription.current_period_end %>
3+
<p><%= subscription_period_end_action(subscription) %>: <strong><%= subscription.current_period_end.to_date.to_fs(:long) %></strong></p>
4+
<% end %>

app/views/account/settings/show.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
<% if Current.account.plan.free? %>
1717
<%= button_to "Upgrade to Monthly", account_subscription_path(plan: :monthly_v1), class: "btn btn--primary", data: { turbo: false } %>
1818
<% else %>
19+
<% if Current.account.subscription %>
20+
<%= render "account/settings/subscription", subscription: Current.account.subscription %>
21+
<% end %>
1922
<%= link_to "Manage Billing", account_billing_portal_path, class: "btn" %>
2023
<% end %>
2124
</section>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddCancelAtToAccountSubscriptions < ActiveRecord::Migration[8.2]
2+
def change
3+
add_column :account_subscriptions, :cancel_at, :datetime
4+
end
5+
end

db/schema.rb

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)