46 lines
975 B
Ruby
Raw Normal View History

# frozen_string_literal: true
module DiscoursePatrons
2019-10-10 12:08:52 +11:00
module Admin
class PlansController < ::Admin::AdminController
include DiscoursePatrons::Stripe
2019-09-24 20:44:51 +10:00
2019-10-10 12:08:52 +11:00
before_action :set_api_key
2019-09-24 20:44:51 +10:00
2019-10-10 12:08:52 +11:00
def index
plans = ::Stripe::Plan.list
2019-10-14 12:36:46 +11:00
render_json_dump plans.data
2019-10-10 12:08:52 +11:00
end
2019-09-25 11:18:11 +10:00
2019-10-10 12:08:52 +11:00
def create
begin
2019-10-10 12:08:52 +11:00
plan = ::Stripe::Plan.create(
amount: params[:amount],
interval: params[:interval],
product: { name: params[:name] },
currency: SiteSetting.discourse_patrons_currency,
id: plan_id,
)
2019-10-10 12:08:52 +11:00
render_json_dump plan
2019-10-10 12:08:52 +11:00
rescue ::Stripe::InvalidRequestError => e
return render_json_error e.message
end
end
2019-09-25 13:20:28 +10:00
2019-10-10 12:08:52 +11:00
def destroy
plan = ::Stripe::Plan.delete(params[:id])
2019-10-14 12:36:46 +11:00
render_json_dump plan
2019-10-10 12:08:52 +11:00
end
2019-10-10 12:08:52 +11:00
private
2019-10-10 12:08:52 +11:00
def plan_id
params[:name].parameterize.dasherize if params[:name]
end
end
end
end