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

46 lines
1.1 KiB
Ruby
Raw Normal View History

2019-09-23 17:53:05 +10:00
# frozen_string_literal: true
module DiscoursePatrons
2019-10-10 13:09:24 +11:00
module Admin
class SubscriptionsController < ::Admin::AdminController
include DiscoursePatrons::Stripe
2019-11-29 10:37:32 +11:00
include DiscoursePatrons::Group
2019-10-10 13:09:24 +11:00
before_action :set_api_key
2019-10-10 13:09:24 +11:00
def index
2019-10-16 21:06:19 +11:00
begin
subscriptions = ::Stripe::Subscription.list(expand: ['data.plan.product'])
2019-10-16 21:06:19 +11:00
render_json_dump subscriptions
rescue ::Stripe::InvalidRequestError => e
return render_json_error e.message
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-11-28 20:44:38 +11:00
customer = DiscoursePatrons::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
return render_json_error e.message
end
end
2019-09-23 17:53:05 +10:00
end
end
end