Skip to content

Commit

Permalink
Merge pull request #8018 from fjordllc/feature/remove-card-at-resign
Browse files Browse the repository at this point in the history
退会時にカード情報を削除するようにした
  • Loading branch information
komagata authored Aug 18, 2024
2 parents 686e4cf + 3fa708a commit bd49ba8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
37 changes: 22 additions & 15 deletions app/controllers/retirement_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ def create
user = current_user
current_user.delete_and_assign_new_organizer
Newspaper.publish(:retirement_create, { user: })
begin
UserMailer.retire(user).deliver_now
rescue Postmark::InactiveRecipientError => e
logger.warn "[Postmark] 受信者由来のエラーのためメールを送信できませんでした。:#{e.message}"
end

destroy_subscription
notify_to_admins
notify_to_mentors

destroy_subscription(user)
destroy_card(user)
notify_to_user(user)
notify_to_admins(user)
notify_to_mentors(user)
logout
redirect_to retirement_url
else
Expand All @@ -37,19 +34,29 @@ def retire_reason_params
params.require(:user).permit(:retire_reason, :satisfaction, :opinion, retire_reasons: [])
end

def destroy_subscription
Subscription.new.destroy(current_user.subscription_id) if current_user.subscription_id
def destroy_subscription(user)
Subscription.new.destroy(user.subscription_id) if user.subscription_id?
end

def destroy_card(user)
Card.destroy_all(user.customer_id) if user.customer_id?
end

def notify_to_user(user)
UserMailer.retire(user).deliver_now
rescue Postmark::InactiveRecipientError => e
logger.warn "[Postmark] 受信者由来のエラーのためメールを送信できませんでした。:#{e.message}"
end

def notify_to_admins
def notify_to_admins(user)
User.admins.each do |admin_user|
ActivityDelivery.with(sender: current_user, receiver: admin_user).notify(:retired)
ActivityDelivery.with(sender: user, receiver: admin_user).notify(:retired)
end
end

def notify_to_mentors
def notify_to_mentors(user)
User.mentor.each do |mentor_user|
ActivityDelivery.with(sender: current_user, receiver: mentor_user).notify(:retired)
ActivityDelivery.with(sender: user, receiver: mentor_user).notify(:retired)
end
end
end
17 changes: 17 additions & 0 deletions app/models/card.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
# frozen_string_literal: true

class Card
def self.all(customer_id)
Stripe::Customer.list_sources(customer_id)&.data || []
end

def self.destroy_all(customer_id)
all(customer_id).each do |card|
destroy(customer_id, card['id'])
end
end

def self.destroy(customer_id, card_id)
Stripe::Customer.delete_source(
customer_id,
card_id
)
end

def create(user, card_token, idempotency_key = SecureRandom.uuid)
Stripe::Customer.create({
email: user.email,
Expand Down

0 comments on commit bd49ba8

Please sign in to comment.