2019-10-10 13:52:55 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-12-04 11:23:45 +11:00
|
|
|
module DiscourseSubscriptions
|
2019-10-10 13:52:55 +11:00
|
|
|
class PlansController < ::ApplicationController
|
2019-12-04 11:23:45 +11:00
|
|
|
include DiscourseSubscriptions::Stripe
|
2019-10-10 13:52:55 +11:00
|
|
|
|
|
|
|
before_action :set_api_key
|
|
|
|
|
|
|
|
def index
|
2019-10-17 12:07:06 +11:00
|
|
|
begin
|
2019-11-04 16:37:21 +11:00
|
|
|
if params[:product_id].present?
|
2020-07-15 08:44:40 -05:00
|
|
|
plans = ::Stripe::Price.list(active: true, product: params[:product_id])
|
2019-11-04 16:37:21 +11:00
|
|
|
else
|
2020-07-15 08:44:40 -05:00
|
|
|
plans = ::Stripe::Price.list(active: true)
|
2019-11-04 16:37:21 +11:00
|
|
|
end
|
2019-10-17 12:07:06 +11:00
|
|
|
|
2019-11-01 10:18:57 +11:00
|
|
|
serialized = plans[:data].map do |plan|
|
2020-07-22 11:06:34 -05:00
|
|
|
plan.to_h.slice(:id, :unit_amount, :currency, :type, :recurring)
|
2019-11-01 10:18:57 +11:00
|
|
|
end.sort_by { |plan| plan[:amount] }
|
|
|
|
|
|
|
|
render_json_dump serialized
|
2019-10-17 12:07:06 +11:00
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
2019-12-12 09:59:38 +11:00
|
|
|
render_json_error e.message
|
2019-10-17 12:07:06 +11:00
|
|
|
end
|
2019-10-10 13:52:55 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|