From 190ca2808940a8f7682a602b62b50e456e4896f5 Mon Sep 17 00:00:00 2001 From: Rimian Perkins Date: Tue, 14 Jan 2020 20:58:34 +1100 Subject: [PATCH] rm subscription event --- app/controllers/hooks_controller.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/app/controllers/hooks_controller.rb b/app/controllers/hooks_controller.rb index fcc16e0..d536a35 100644 --- a/app/controllers/hooks_controller.rb +++ b/app/controllers/hooks_controller.rb @@ -2,6 +2,7 @@ module DiscourseSubscriptions class HooksController < ::ApplicationController + include DiscourseSubscriptions::Group skip_before_action :verify_authenticity_token, only: [:create] def create @@ -20,6 +21,29 @@ module DiscourseSubscriptions return end + # Handle the event + case event[:type] + when 'customer.subscription.deleted' + + customer = Customer.find_by( + customer_id: event[:customer], + product_id: event[:plan][:product] + ) + + if customer + customer.delete + + user = ::User.find(customer.user_id) + group = plan_group(event[:plan]) + group.remove(user) if group + end + + else + # Unexpected event type + status 400 + return + end + head 200 end end