From 3fa708add1f3337defd533a9c9b1a3a46ae92c5d Mon Sep 17 00:00:00 2001 From: komagata Date: Mon, 19 Aug 2024 08:21:23 +0900 Subject: [PATCH] =?UTF-8?q?=E9=80=80=E4=BC=9A=E6=99=82=E3=81=AB=E3=82=AB?= =?UTF-8?q?=E3=83=BC=E3=83=89=E6=83=85=E5=A0=B1=E3=82=92=E5=89=8A=E9=99=A4?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/retirement_controller.rb | 37 ++++++++++++++---------- app/models/card.rb | 17 +++++++++++ 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/app/controllers/retirement_controller.rb b/app/controllers/retirement_controller.rb index 3d24ce74bd0..a1dc33d2d66 100644 --- a/app/controllers/retirement_controller.rb +++ b/app/controllers/retirement_controller.rb @@ -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 @@ -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 diff --git a/app/models/card.rb b/app/models/card.rb index 851fdc7c855..bca8fee1a9b 100644 --- a/app/models/card.rb +++ b/app/models/card.rb @@ -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,