Files
discourse-subscriptions/app/controllers/discourse_subscriptions/plans_controller.rb
T
Justin DiRose c9ff55b46a REFACTOR: Use the Prices API in place of Plans (#17)
Stripe has a newer API called Prices where you can create a price for any product and it can either be recurring or one-time. The easy part is existing Plans work with the Prices API by passing a Plan ID, but objects are returned in the slightly-different Prices API object format.

This commit is a refactor to the new API to handle the data in its new form, and lays the foundation for a one time payment plan to be added to any subscriptions product.
2020-07-15 08:44:40 -05:00

29 lines
726 B
Ruby

# frozen_string_literal: true
module DiscourseSubscriptions
class PlansController < ::ApplicationController
include DiscourseSubscriptions::Stripe
before_action :set_api_key
def index
begin
if params[:product_id].present?
plans = ::Stripe::Price.list(active: true, product: params[:product_id])
else
plans = ::Stripe::Price.list(active: true)
end
serialized = plans[:data].map do |plan|
plan.to_h.slice(:id, :unit_amount, :currency, :recurring)
end.sort_by { |plan| plan[:amount] }
render_json_dump serialized
rescue ::Stripe::InvalidRequestError => e
render_json_error e.message
end
end
end
end