diff --git a/app/controllers/discourse_subscriptions/hooks_controller.rb b/app/controllers/discourse_subscriptions/hooks_controller.rb index 487e807..e0ae0e3 100644 --- a/app/controllers/discourse_subscriptions/hooks_controller.rb +++ b/app/controllers/discourse_subscriptions/hooks_controller.rb @@ -4,7 +4,9 @@ module DiscourseSubscriptions class HooksController < ::ApplicationController include DiscourseSubscriptions::Group include DiscourseSubscriptions::Stripe + layout false + skip_before_action :check_xhr skip_before_action :redirect_to_login_if_required skip_before_action :verify_authenticity_token, only: [:create] @@ -17,66 +19,52 @@ module DiscourseSubscriptions event = ::Stripe::Webhook.construct_event(payload, sig_header, webhook_secret) rescue JSON::ParserError => e - render_json_error e.message - return + return render_json_error e.message rescue ::Stripe::SignatureVerificationError => e - render_json_error e.message - return + return render_json_error e.message end case event[:type] + when 'customer.subscription.created' when 'customer.subscription.updated' customer = Customer.find_by( customer_id: event[:data][:object][:customer], - product_id: event[:data][:object][:plan][:product] + product_id: event[:data][:object][:plan][:product], ) - if customer && subscription_completion?(event) - user = ::User.find(customer.user_id) - group = plan_group(event[:data][:object][:plan]) - group.add(user) if group + return render_json_error 'customer not found' if !customer + return head 200 if event[:data][:object][:status] != 'complete' + + user = ::User.find_by(id: customer.user_id) + return render_json_error 'user not found' if !user + + if group = plan_group(event[:data][:object][:plan]) + group.add(user) + end + when 'customer.subscription.deleted' + customer = Customer.find_by( + customer_id: event[:data][:object][:customer], + product_id: event[:data][:object][:plan][:product], + ) + + return render_json_error 'customer not found' if !customer + + Subscription.find_by( + customer_id: customer.id, + external_id: event[:data][:object][:id] + )&.destroy! + + user = ::User.find(customer.user_id) + return render_json_error 'user not found' if !user + + if group = plan_group(event[:data][:object][:plan]) + group.remove(user) end - when 'customer.subscription.deleted' - delete_subscription(event) + customer.destroy! end head 200 end - - private - - def subscription_completion?(event) - subscription_complete?(event) && previously_incomplete?(event) - end - - def subscription_complete?(event) - event && event[:data] && event[:data][:object] && event[:data][:object][:status] && event[:data][:object][:status] == 'complete' - end - - def previously_incomplete?(event) - event && event[:data] && event[:data][:previous_attributes] && event[:data][:previous_attributes][:status] && event[:data][:previous_attributes][:status] == 'incomplete' - end - - def delete_subscription(event) - customer = Customer.find_by( - customer_id: event[:data][:object][:customer], - product_id: event[:data][:object][:plan][:product] - ) - - if customer - sub_model = Subscription.find_by( - customer_id: customer.id, - external_id: [:id] - ) - - sub_model.delete if sub_model - - user = ::User.find(customer.user_id) - customer.delete - group = plan_group(event[:data][:object][:plan]) - group.remove(user) if group - end - end end end