discourse-subscriptions/app/controllers/discourse_subscriptions/plans_controller.rb

28 lines
732 B
Ruby
Raw Normal View History

2019-10-09 22:52:55 -04:00
# frozen_string_literal: true
2019-12-03 19:23:45 -05:00
module DiscourseSubscriptions
2019-10-09 22:52:55 -04:00
class PlansController < ::ApplicationController
2019-12-03 19:23:45 -05:00
include DiscourseSubscriptions::Stripe
2019-10-09 22:52:55 -04:00
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::Price.list(active: true, product: params[:product_id])
2019-11-04 00:37:21 -05:00
else
plans = ::Stripe::Price.list(active: true)
2019-11-04 00:37:21 -05:00
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, :unit_amount, :currency, :type, :recurring)
2019-10-31 19:18:57 -04:00
end.sort_by { |plan| plan[:amount] }
render_json_dump serialized
2019-10-16 21:07:06 -04:00
rescue ::Stripe::InvalidRequestError => e
2019-12-11 17:59:38 -05:00
render_json_error e.message
2019-10-16 21:07:06 -04:00
end
2019-10-09 22:52:55 -04:00
end
end
end