Skip to content

Commit

Permalink
Automatically cancel subscriptions on user deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
parterburn committed Feb 29, 2024
1 parent 4fa4a93 commit ff3c377
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
23 changes: 14 additions & 9 deletions app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ def create
def update
if params[:submit_method] == "delete account"
if current_user.valid_password?(params[:user][:current_password])
if current_user.is_pro?
UserMailer.pro_deleted(current_user.full_name, current_user.email, current_user.plan, current_user.entries.size, current_user.id, current_user.payhere_id, current_user.stripe_id).deliver_later
end
cancel_subscription
current_user.destroy
redirect_to root_path, notice: "Your account has been deleted."
else
Expand Down Expand Up @@ -68,12 +66,7 @@ def settings

def destroy
if current_user.valid_password?(params[:user][:current_password])
if current_user.is_pro? && current_user.plan_type_unlinked == "Stripe" && current_user.stripe_id.present?
customer = Stripe::Customer.retrieve(current_user.stripe_id)
customer.subscriptions.each do |sub|
sub.delete
end
end
cancel_subscription
super
else
flash[:alert] = "Incorrect current password."
Expand Down Expand Up @@ -111,6 +104,18 @@ def unsubscribe

private

def cancel_subscription
if current_user.is_pro?
Sentry.capture_message("Pro User Deleted", level: :info, extra: { email: current_user.email, plan: current_user.plan, entries: current_user.entries.size, user_id: current_user.id, payhere_id: current_user.payhere_id, stripe_id: current_user.stripe_id })
if current_user.stripe_id.present?
customer = Stripe::Customer.retrieve(current_user.stripe_id)
customer.subscriptions.each do |sub|
sub.delete
end
end
end
end

def check_captcha
if valid_captcha?(model: resource)
true
Expand Down
16 changes: 0 additions & 16 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,6 @@ def no_user_here(params)
mail(to: "hello@#{ENV['MAIN_DOMAIN']}", subject: '[REFUND REQUIRED] Payment Without a User', body: params.to_yaml)
end

def pro_deleted(name, email, plan, entries, user_id, payhere_id, stripe_id)
subscription_status = nil
if stripe_id.present?
subscriptions = Stripe::Subscription.list(customer: stripe_id)
subscription_status = subscriptions.map do |s|
if s.status == "active" && s.cancel_at_period_end
"#{s.id} will be canceled on #{Time.at(s.cancel_at).strftime("%B %-d, %Y")}"
else
"#{s.id} is #{s.status}"
end
end.to_sentence
end

mail(to: "hello@#{ENV['MAIN_DOMAIN']}", subject: '[ACTION REQUIRED] Pro User Deleted', body: "Name: #{name}\nEmail: #{email}\nUser ID: #{user_id}\nPlan: #{plan}\nEntries: #{entries}\nPayhere ID: #{payhere_id}\nStripe Subscription Status: #{subscription_status}\n\nStripe: https://dashboard.stripe.com/customers/#{stripe_id}")
end

def failed_entry(user, errors, date, body)
@user = user
@errors = errors
Expand Down

0 comments on commit ff3c377

Please sign in to comment.