2019-09-23 17:53:05 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-12-04 11:23:45 +11:00
|
|
|
module DiscourseSubscriptions
|
2019-10-10 13:09:24 +11:00
|
|
|
module Admin
|
|
|
|
class SubscriptionsController < ::Admin::AdminController
|
2019-12-04 11:23:45 +11:00
|
|
|
include DiscourseSubscriptions::Stripe
|
|
|
|
include DiscourseSubscriptions::Group
|
2019-10-10 13:09:24 +11:00
|
|
|
before_action :set_api_key
|
2019-09-24 16:04:42 +10:00
|
|
|
|
2019-10-10 13:09:24 +11:00
|
|
|
def index
|
2019-10-16 21:06:19 +11:00
|
|
|
begin
|
2019-11-26 12:55:49 +11:00
|
|
|
subscriptions = ::Stripe::Subscription.list(expand: ['data.plan.product'])
|
2019-10-16 21:06:19 +11:00
|
|
|
|
|
|
|
render_json_dump subscriptions
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
2019-12-12 09:59:38 +11:00
|
|
|
render_json_error e.message
|
2019-10-16 21:06:19 +11:00
|
|
|
end
|
2019-10-10 13:09:24 +11:00
|
|
|
end
|
2019-11-14 10:51:04 +11:00
|
|
|
|
|
|
|
def destroy
|
|
|
|
begin
|
|
|
|
subscription = ::Stripe::Subscription.delete(params[:id])
|
|
|
|
|
2019-12-04 11:23:45 +11:00
|
|
|
customer = DiscourseSubscriptions::Customer.find_by(
|
2019-11-29 10:37:32 +11:00
|
|
|
product_id: subscription[:plan][:product],
|
2019-11-28 20:44:38 +11:00
|
|
|
customer_id: subscription[:customer]
|
|
|
|
)
|
|
|
|
|
2019-11-29 10:37:32 +11:00
|
|
|
if customer
|
|
|
|
customer.delete
|
|
|
|
|
|
|
|
user = ::User.find(customer.user_id)
|
|
|
|
group = plan_group(subscription[:plan])
|
|
|
|
group.remove(user) if group
|
|
|
|
end
|
2019-11-28 20:44:38 +11:00
|
|
|
|
2019-11-14 10:51:04 +11:00
|
|
|
render_json_dump subscription
|
|
|
|
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
2019-12-12 09:59:38 +11:00
|
|
|
render_json_error e.message
|
2019-11-14 10:51:04 +11:00
|
|
|
end
|
|
|
|
end
|
2019-09-23 17:53:05 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|