discourse-subscriptions/app/controllers/admin/subscriptions_controller.rb

46 lines
1.2 KiB
Ruby
Raw Normal View History

2019-09-23 03:53:05 -04:00
# frozen_string_literal: true
2019-12-03 19:23:45 -05:00
module DiscourseSubscriptions
2019-10-09 22:09:24 -04:00
module Admin
class SubscriptionsController < ::Admin::AdminController
2019-12-03 19:23:45 -05:00
include DiscourseSubscriptions::Stripe
include DiscourseSubscriptions::Group
2019-10-09 22:09:24 -04:00
before_action :set_api_key
2019-10-09 22:09:24 -04:00
def index
2019-10-16 06:06:19 -04:00
begin
subscriptions = ::Stripe::Subscription.list(expand: ['data.plan.product'])
2019-10-16 06:06:19 -04:00
render_json_dump subscriptions
rescue ::Stripe::InvalidRequestError => e
2019-12-11 17:59:38 -05:00
render_json_error e.message
2019-10-16 06:06:19 -04:00
end
2019-10-09 22:09:24 -04:00
end
2019-11-13 18:51:04 -05:00
def destroy
begin
subscription = ::Stripe::Subscription.delete(params[:id])
2019-12-03 19:23:45 -05:00
customer = DiscourseSubscriptions::Customer.find_by(
2019-11-28 18:37:32 -05:00
product_id: subscription[:plan][:product],
2019-11-28 04:44:38 -05:00
customer_id: subscription[:customer]
)
2019-11-28 18:37:32 -05: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 04:44:38 -05:00
2019-11-13 18:51:04 -05:00
render_json_dump subscription
rescue ::Stripe::InvalidRequestError => e
2019-12-11 17:59:38 -05:00
render_json_error e.message
2019-11-13 18:51:04 -05:00
end
end
2019-09-23 03:53:05 -04:00
end
end
end