FIX: Simplify Stripe webhook handler (#135)

The 'customer.subscription.created' webhooks were not handled at all and
the logic was overcomplicated.
This commit is contained in:
Bianca Nenciu 2022-10-03 13:33:50 +03:00 committed by GitHub
parent aba86e5e25
commit a58743e376
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 46 deletions

View File

@ -4,7 +4,9 @@ module DiscourseSubscriptions
class HooksController < ::ApplicationController class HooksController < ::ApplicationController
include DiscourseSubscriptions::Group include DiscourseSubscriptions::Group
include DiscourseSubscriptions::Stripe include DiscourseSubscriptions::Stripe
layout false layout false
skip_before_action :check_xhr skip_before_action :check_xhr
skip_before_action :redirect_to_login_if_required skip_before_action :redirect_to_login_if_required
skip_before_action :verify_authenticity_token, only: [:create] skip_before_action :verify_authenticity_token, only: [:create]
@ -17,66 +19,52 @@ module DiscourseSubscriptions
event = ::Stripe::Webhook.construct_event(payload, sig_header, webhook_secret) event = ::Stripe::Webhook.construct_event(payload, sig_header, webhook_secret)
rescue JSON::ParserError => e rescue JSON::ParserError => e
render_json_error e.message return render_json_error e.message
return
rescue ::Stripe::SignatureVerificationError => e rescue ::Stripe::SignatureVerificationError => e
render_json_error e.message return render_json_error e.message
return
end end
case event[:type] case event[:type]
when 'customer.subscription.created'
when 'customer.subscription.updated' when 'customer.subscription.updated'
customer = Customer.find_by( customer = Customer.find_by(
customer_id: event[:data][:object][:customer], 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) return render_json_error 'customer not found' if !customer
user = ::User.find(customer.user_id) return head 200 if event[:data][:object][:status] != 'complete'
group = plan_group(event[:data][:object][:plan])
group.add(user) if group 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 end
when 'customer.subscription.deleted' customer.destroy!
delete_subscription(event)
end end
head 200 head 200
end 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
end end