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
|
|
|
|
2021-02-05 11:57:53 -06:00
|
|
|
PAGE_LIMIT = 10
|
|
|
|
|
2019-10-10 13:09:24 +11:00
|
|
|
def index
|
2019-10-16 21:06:19 +11:00
|
|
|
begin
|
2020-05-22 11:20:05 -05:00
|
|
|
subscription_ids = Subscription.all.pluck(:external_id)
|
2021-02-05 11:57:53 -06:00
|
|
|
subscriptions = {
|
|
|
|
has_more: false,
|
|
|
|
data: [],
|
|
|
|
length: 0,
|
2022-12-29 12:35:06 +00:00
|
|
|
last_record: params[:last_record],
|
2021-02-05 11:57:53 -06:00
|
|
|
}
|
2020-05-22 11:20:05 -05:00
|
|
|
|
2020-05-28 10:32:57 -05:00
|
|
|
if subscription_ids.present? && is_stripe_configured?
|
2021-02-05 11:57:53 -06:00
|
|
|
while subscriptions[:length] < PAGE_LIMIT
|
|
|
|
current_set = get_subscriptions(subscriptions[:last_record])
|
|
|
|
|
2022-12-29 12:35:06 +00:00
|
|
|
until valid_subscriptions =
|
|
|
|
find_valid_subscriptions(current_set[:data], subscription_ids)
|
2021-02-05 11:57:53 -06:00
|
|
|
current_set = get_subscriptions(current_set[:data].last)
|
|
|
|
break if current_set[:has_more] == false
|
|
|
|
end
|
|
|
|
|
|
|
|
subscriptions[:data] = subscriptions[:data].concat(valid_subscriptions.to_a)
|
2022-12-29 12:35:06 +00:00
|
|
|
subscriptions[:last_record] = current_set[:data].last[:id] if current_set[
|
|
|
|
:data
|
|
|
|
].present?
|
2021-02-05 11:57:53 -06:00
|
|
|
subscriptions[:length] = subscriptions[:data].length
|
|
|
|
subscriptions[:has_more] = current_set[:has_more]
|
|
|
|
break if subscriptions[:has_more] == false
|
|
|
|
end
|
2020-05-28 10:32:57 -05:00
|
|
|
elsif !is_stripe_configured?
|
|
|
|
subscriptions = nil
|
2020-05-22 11:20:05 -05:00
|
|
|
end
|
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
|
2020-10-29 10:31:12 -05:00
|
|
|
params.require(:id)
|
2019-11-14 10:51:04 +11:00
|
|
|
begin
|
2020-10-29 10:31:12 -05:00
|
|
|
refund_subscription(params[:id]) if params[:refund]
|
2019-11-14 10:51:04 +11:00
|
|
|
subscription = ::Stripe::Subscription.delete(params[:id])
|
|
|
|
|
2022-12-29 12:35:06 +00:00
|
|
|
customer =
|
|
|
|
Customer.find_by(
|
|
|
|
product_id: subscription[:plan][:product],
|
|
|
|
customer_id: subscription[:customer],
|
|
|
|
)
|
2019-11-28 20:44:38 +11:00
|
|
|
|
2020-05-22 11:20:05 -05:00
|
|
|
Subscription.delete_by(external_id: params[:id])
|
2019-11-29 10:37:32 +11:00
|
|
|
|
2020-05-22 11:20:05 -05:00
|
|
|
if customer
|
2019-11-29 10:37:32 +11:00
|
|
|
user = ::User.find(customer.user_id)
|
2020-05-22 11:20:05 -05:00
|
|
|
customer.delete
|
2019-11-29 10:37:32 +11:00
|
|
|
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
|
2020-10-29 10:31:12 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-02-05 11:57:53 -06:00
|
|
|
def get_subscriptions(start)
|
2022-12-29 12:35:06 +00:00
|
|
|
::Stripe::Subscription.list(
|
|
|
|
expand: ["data.plan.product"],
|
|
|
|
limit: PAGE_LIMIT,
|
|
|
|
starting_after: start,
|
|
|
|
)
|
2021-02-05 11:57:53 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_valid_subscriptions(data, ids)
|
|
|
|
valid = data.select { |sub| ids.include?(sub[:id]) }
|
|
|
|
valid.empty? ? nil : valid
|
|
|
|
end
|
|
|
|
|
2020-10-29 10:31:12 -05:00
|
|
|
# this will only refund the most recent subscription payment
|
|
|
|
def refund_subscription(subscription_id)
|
|
|
|
subscription = ::Stripe::Subscription.retrieve(subscription_id)
|
2022-12-29 12:35:06 +00:00
|
|
|
invoice = ::Stripe::Invoice.retrieve(subscription[:latest_invoice]) if subscription[
|
|
|
|
:latest_invoice
|
|
|
|
]
|
2020-10-29 10:31:12 -05:00
|
|
|
payment_intent = invoice[:payment_intent] if invoice[:payment_intent]
|
2022-12-29 12:35:06 +00:00
|
|
|
refund = ::Stripe::Refund.create({ payment_intent: payment_intent })
|
2020-10-29 10:31:12 -05:00
|
|
|
end
|
2019-09-23 17:53:05 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|