* REFACTOR: Use api to add subscribe link * FIX: I18n subscribe link * REFACTOR: Use models to store some data This enables the plugin to show only subscription information which was generated on Discourse. Subscription data storage is limited to the external identifiers Stripe generates so we can interact with the API. * DEV: Test/linting fixes/rake task
53 lines
1.4 KiB
Ruby
53 lines
1.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module DiscourseSubscriptions
|
|
module Admin
|
|
class SubscriptionsController < ::Admin::AdminController
|
|
include DiscourseSubscriptions::Stripe
|
|
include DiscourseSubscriptions::Group
|
|
before_action :set_api_key
|
|
|
|
def index
|
|
begin
|
|
subscription_ids = Subscription.all.pluck(:external_id)
|
|
subscriptions = []
|
|
|
|
if subscription_ids.present?
|
|
subscriptions = ::Stripe::Subscription.list(expand: ['data.plan.product'])
|
|
subscriptions = subscriptions.select { |sub| subscription_ids.include?(sub[:id]) }
|
|
end
|
|
|
|
render_json_dump subscriptions
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
render_json_error e.message
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
begin
|
|
subscription = ::Stripe::Subscription.delete(params[:id])
|
|
|
|
customer = Customer.find_by(
|
|
product_id: subscription[:plan][:product],
|
|
customer_id: subscription[:customer]
|
|
)
|
|
|
|
Subscription.delete_by(external_id: params[:id])
|
|
|
|
if customer
|
|
user = ::User.find(customer.user_id)
|
|
customer.delete
|
|
group = plan_group(subscription[:plan])
|
|
group.remove(user) if group
|
|
end
|
|
|
|
render_json_dump subscription
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
render_json_error e.message
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|