2020-01-10 10:24:09 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseSubscriptions
|
|
|
|
class HooksController < ::ApplicationController
|
2020-01-14 20:58:34 +11:00
|
|
|
include DiscourseSubscriptions::Group
|
2020-08-19 14:37:47 -05:00
|
|
|
include DiscourseSubscriptions::Stripe
|
2022-10-03 13:33:50 +03:00
|
|
|
|
2020-08-19 14:37:47 -05:00
|
|
|
layout false
|
2022-10-03 13:33:50 +03:00
|
|
|
|
2020-08-19 14:37:47 -05:00
|
|
|
skip_before_action :check_xhr
|
|
|
|
skip_before_action :redirect_to_login_if_required
|
2020-01-14 15:37:53 +11:00
|
|
|
skip_before_action :verify_authenticity_token, only: [:create]
|
|
|
|
|
2020-01-10 10:24:09 +11:00
|
|
|
def create
|
2020-01-13 11:10:06 +11:00
|
|
|
begin
|
2020-01-14 15:37:53 +11:00
|
|
|
payload = request.body.read
|
|
|
|
sig_header = request.env['HTTP_STRIPE_SIGNATURE']
|
|
|
|
webhook_secret = SiteSetting.discourse_subscriptions_webhook_secret
|
2020-01-13 11:10:06 +11:00
|
|
|
|
2020-01-14 15:37:53 +11:00
|
|
|
event = ::Stripe::Webhook.construct_event(payload, sig_header, webhook_secret)
|
2020-01-13 11:10:06 +11:00
|
|
|
rescue JSON::ParserError => e
|
2022-10-03 13:33:50 +03:00
|
|
|
return render_json_error e.message
|
2020-08-19 14:37:47 -05:00
|
|
|
rescue ::Stripe::SignatureVerificationError => e
|
2022-10-03 13:33:50 +03:00
|
|
|
return render_json_error e.message
|
2020-01-13 11:10:06 +11:00
|
|
|
end
|
|
|
|
|
2020-01-14 20:58:34 +11:00
|
|
|
case event[:type]
|
2022-10-03 13:33:50 +03:00
|
|
|
when 'customer.subscription.created'
|
2020-01-25 15:06:05 +11:00
|
|
|
when 'customer.subscription.updated'
|
2020-01-26 10:49:51 +11:00
|
|
|
customer = Customer.find_by(
|
|
|
|
customer_id: event[:data][:object][:customer],
|
2022-10-03 13:33:50 +03:00
|
|
|
product_id: event[:data][:object][:plan][:product],
|
2020-01-26 10:49:51 +11:00
|
|
|
)
|
|
|
|
|
2022-10-03 13:33:50 +03:00
|
|
|
return render_json_error 'customer not found' if !customer
|
|
|
|
return head 200 if event[:data][:object][:status] != 'complete'
|
2020-01-26 10:49:51 +11:00
|
|
|
|
2022-10-03 13:33:50 +03:00
|
|
|
user = ::User.find_by(id: customer.user_id)
|
|
|
|
return render_json_error 'user not found' if !user
|
2020-01-26 10:49:51 +11:00
|
|
|
|
2022-10-03 13:33:50 +03:00
|
|
|
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],
|
|
|
|
)
|
2020-08-19 14:37:47 -05:00
|
|
|
|
2022-10-03 13:33:50 +03:00
|
|
|
return render_json_error 'customer not found' if !customer
|
2020-08-19 14:37:47 -05:00
|
|
|
|
2022-10-03 13:33:50 +03:00
|
|
|
Subscription.find_by(
|
2020-08-19 14:37:47 -05:00
|
|
|
customer_id: customer.id,
|
2022-10-03 13:33:50 +03:00
|
|
|
external_id: event[:data][:object][:id]
|
|
|
|
)&.destroy!
|
2020-08-19 14:37:47 -05:00
|
|
|
|
|
|
|
user = ::User.find(customer.user_id)
|
2022-10-03 13:33:50 +03:00
|
|
|
return render_json_error 'user not found' if !user
|
|
|
|
|
|
|
|
if group = plan_group(event[:data][:object][:plan])
|
|
|
|
group.remove(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
customer.destroy!
|
2020-08-19 14:37:47 -05:00
|
|
|
end
|
2022-10-03 13:33:50 +03:00
|
|
|
|
|
|
|
head 200
|
2020-01-26 10:49:51 +11:00
|
|
|
end
|
2020-01-10 10:24:09 +11:00
|
|
|
end
|
|
|
|
end
|