2019-10-09 22:52:55 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscoursePatrons
|
|
|
|
class PlansController < ::ApplicationController
|
|
|
|
include DiscoursePatrons::Stripe
|
|
|
|
|
|
|
|
before_action :set_api_key
|
|
|
|
|
|
|
|
def index
|
2019-10-16 21:07:06 -04:00
|
|
|
begin
|
2019-11-04 00:37:21 -05:00
|
|
|
if params[:product_id].present?
|
|
|
|
plans = ::Stripe::Plan.list(active: true, product: params[:product_id])
|
|
|
|
else
|
|
|
|
plans = ::Stripe::Plan.list(active: true)
|
|
|
|
end
|
2019-10-16 21:07:06 -04:00
|
|
|
|
2019-10-31 19:18:57 -04:00
|
|
|
serialized = plans[:data].map do |plan|
|
|
|
|
plan.to_h.slice(:id, :amount, :currency, :interval)
|
|
|
|
end.sort_by { |plan| plan[:amount] }
|
|
|
|
|
|
|
|
render_json_dump serialized
|
2019-10-16 21:07:06 -04:00
|
|
|
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
|
|
return render_json_error e.message
|
|
|
|
end
|
2019-10-09 22:52:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|