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
|
|
|
|
2024-03-06 14:51:25 +01:00
|
|
|
requires_plugin DiscourseSubscriptions::PLUGIN_NAME
|
|
|
|
|
2020-08-19 14:37:47 -05:00
|
|
|
layout false
|
2022-10-03 13:33:50 +03:00
|
|
|
|
2024-04-29 12:47:58 -06:00
|
|
|
before_action :set_api_key
|
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
|
2022-12-29 12:35:06 +00:00
|
|
|
sig_header = request.env["HTTP_STRIPE_SIGNATURE"]
|
2020-01-14 15:37:53 +11:00
|
|
|
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]
|
2024-04-29 12:47:58 -06:00
|
|
|
when "checkout.session.completed"
|
|
|
|
checkout_session = event[:data][:object]
|
2024-08-27 18:40:12 -06:00
|
|
|
|
|
|
|
if SiteSetting.discourse_subscriptions_enable_verbose_logging
|
2024-08-28 09:17:08 -06:00
|
|
|
Rails.logger.warn("checkout.session.completed data: #{checkout_session}")
|
2024-08-27 18:40:12 -06:00
|
|
|
end
|
2024-04-29 12:47:58 -06:00
|
|
|
email = checkout_session[:customer_email]
|
|
|
|
|
|
|
|
return head 200 if checkout_session[:status] != "complete"
|
2024-05-03 17:01:51 -06:00
|
|
|
return render_json_error "email not found" if !email
|
2024-04-29 12:47:58 -06:00
|
|
|
|
2024-08-28 11:27:55 -06:00
|
|
|
if checkout_session[:customer].nil?
|
|
|
|
customer = ::Stripe::Customer.create({ email: email })
|
|
|
|
customer_id = customer[:id]
|
|
|
|
else
|
|
|
|
customer_id = checkout_session[:customer]
|
|
|
|
end
|
2024-04-29 12:47:58 -06:00
|
|
|
|
2024-08-27 18:40:12 -06:00
|
|
|
if SiteSetting.discourse_subscriptions_enable_verbose_logging
|
2024-08-28 09:17:08 -06:00
|
|
|
Rails.logger.warn("Looking up user with email: #{email}")
|
2024-08-27 18:40:12 -06:00
|
|
|
end
|
|
|
|
|
2024-04-29 12:47:58 -06:00
|
|
|
user = ::User.find_by_username_or_email(email)
|
|
|
|
|
2024-08-28 11:27:55 -06:00
|
|
|
return render_json_error "user not found" if !user
|
2024-05-03 17:01:51 -06:00
|
|
|
|
2024-05-02 13:38:30 -06:00
|
|
|
discourse_customer = Customer.create(user_id: user.id, customer_id: customer_id)
|
2024-04-29 12:47:58 -06:00
|
|
|
|
2024-08-20 13:32:32 -06:00
|
|
|
subscription = checkout_session[:subscription]
|
|
|
|
|
2024-08-28 15:03:22 -06:00
|
|
|
if subscription.present?
|
2024-08-20 13:32:32 -06:00
|
|
|
Subscription.create(customer_id: discourse_customer.id, external_id: subscription)
|
|
|
|
end
|
2024-04-29 12:47:58 -06:00
|
|
|
|
|
|
|
line_items =
|
|
|
|
::Stripe::Checkout::Session.list_line_items(checkout_session[:id], { limit: 1 })
|
|
|
|
item = line_items[:data].first
|
2024-08-20 13:32:32 -06:00
|
|
|
|
2024-04-29 12:47:58 -06:00
|
|
|
group = plan_group(item[:price])
|
|
|
|
group.add(user) unless group.nil?
|
2024-10-18 14:29:42 -06:00
|
|
|
|
|
|
|
if SiteSetting.discourse_subscriptions_enable_verbose_logging
|
|
|
|
Rails.logger.warn("Line item with group name meta data: #{item[:price]}")
|
|
|
|
if group.nil?
|
|
|
|
Rails.logger.warn("Group not found or not listed in metadata!")
|
|
|
|
else
|
|
|
|
Rails.logger.warn("Group: #{group.name}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-04-29 12:47:58 -06:00
|
|
|
discourse_customer.product_id = item[:price][:product]
|
|
|
|
discourse_customer.save!
|
|
|
|
|
2024-08-20 13:32:32 -06:00
|
|
|
if !subscription.nil?
|
|
|
|
::Stripe::Subscription.update(
|
|
|
|
subscription,
|
|
|
|
{ metadata: { user_id: user.id, username: user.username } },
|
|
|
|
)
|
|
|
|
end
|
2022-12-29 12:35:06 +00:00
|
|
|
when "customer.subscription.created"
|
|
|
|
when "customer.subscription.updated"
|
2024-05-02 13:38:30 -06:00
|
|
|
subscription = event[:data][:object]
|
|
|
|
status = subscription[:status]
|
2024-04-29 15:50:05 -06:00
|
|
|
return head 200 if !%w[complete active].include?(status)
|
|
|
|
|
2024-05-02 13:38:30 -06:00
|
|
|
customer = find_active_customer(subscription[:customer], subscription[:plan][:product])
|
2020-01-26 10:49:51 +11:00
|
|
|
|
2022-12-29 12:35:06 +00:00
|
|
|
return render_json_error "customer not found" if !customer
|
2020-01-26 10:49:51 +11:00
|
|
|
|
2024-05-02 13:38:30 -06:00
|
|
|
update_status(customer.id, subscription[:id], status)
|
|
|
|
|
2022-10-03 13:33:50 +03:00
|
|
|
user = ::User.find_by(id: customer.user_id)
|
2022-12-29 12:35:06 +00:00
|
|
|
return render_json_error "user not found" if !user
|
2020-01-26 10:49:51 +11:00
|
|
|
|
2024-05-02 13:38:30 -06:00
|
|
|
if group = plan_group(subscription[:plan])
|
2022-10-03 13:33:50 +03:00
|
|
|
group.add(user)
|
|
|
|
end
|
2022-12-29 12:35:06 +00:00
|
|
|
when "customer.subscription.deleted"
|
2024-05-02 13:38:30 -06:00
|
|
|
subscription = event[:data][:object]
|
|
|
|
|
|
|
|
customer = find_active_customer(subscription[:customer], subscription[:plan][:product])
|
2020-08-19 14:37:47 -05:00
|
|
|
|
2022-12-29 12:35:06 +00:00
|
|
|
return render_json_error "customer not found" if !customer
|
2020-08-19 14:37:47 -05:00
|
|
|
|
2024-05-02 13:38:30 -06:00
|
|
|
update_status(customer.id, subscription[:id], subscription[:status])
|
2020-08-19 14:37:47 -05:00
|
|
|
|
|
|
|
user = ::User.find(customer.user_id)
|
2022-12-29 12:35:06 +00:00
|
|
|
return render_json_error "user not found" if !user
|
2022-10-03 13:33:50 +03:00
|
|
|
|
2024-05-02 13:38:30 -06:00
|
|
|
if group = plan_group(subscription[:plan])
|
2022-10-03 13:33:50 +03:00
|
|
|
group.remove(user)
|
|
|
|
end
|
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
|
2024-05-02 13:38:30 -06:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def update_status(customer_id, subscription_id, status)
|
|
|
|
discourse_subscription =
|
|
|
|
Subscription.find_by(customer_id: customer_id, external_id: subscription_id)
|
|
|
|
discourse_subscription.update(status: status) if discourse_subscription
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_active_customer(customer_id, product_id)
|
|
|
|
Customer
|
|
|
|
.joins(:subscriptions)
|
|
|
|
.where(customer_id: customer_id, product_id: product_id)
|
|
|
|
.where(
|
|
|
|
Subscription.arel_table[:status].eq(nil).or(
|
|
|
|
Subscription.arel_table[:status].not_eq("canceled"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.first
|
|
|
|
end
|
2020-01-10 10:24:09 +11:00
|
|
|
end
|
|
|
|
end
|