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

90 lines
2.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module DiscoursePatrons
2019-10-09 21:08:52 -04:00
module Admin
class PlansController < ::Admin::AdminController
include DiscoursePatrons::Stripe
2019-09-24 06:44:51 -04:00
2019-10-09 21:08:52 -04:00
before_action :set_api_key
2019-09-24 06:44:51 -04:00
2019-10-09 21:08:52 -04:00
def index
2019-10-15 20:22:58 -04:00
begin
2019-10-21 00:28:45 -04:00
plans = ::Stripe::Plan.list(product_params)
2019-10-15 20:22:58 -04:00
render_json_dump plans.data
rescue ::Stripe::InvalidRequestError => e
return render_json_error e.message
end
2019-10-09 21:08:52 -04:00
end
2019-09-24 21:18:11 -04:00
2019-10-09 21:08:52 -04:00
def create
begin
plan = ::Stripe::Plan.create(
2019-10-17 05:34:26 -04:00
nickname: params[:nickname],
2019-10-09 21:08:52 -04:00
amount: params[:amount],
interval: params[:interval],
2019-10-22 19:16:17 -04:00
product: params[:product],
trial_period_days: params[:trial_period_days],
2019-11-30 01:24:49 -05:00
currency: params[:currency],
2019-10-23 19:02:31 -04:00
active: params[:active],
2019-10-22 20:50:54 -04:00
metadata: { group_name: params[:metadata][:group_name] }
2019-10-09 21:08:52 -04:00
)
2019-10-09 21:08:52 -04:00
render_json_dump plan
2019-10-09 21:08:52 -04:00
rescue ::Stripe::InvalidRequestError => e
return render_json_error e.message
end
end
2019-09-24 23:20:28 -04:00
2019-10-17 05:34:26 -04:00
def show
2019-10-15 20:22:58 -04:00
begin
2019-10-17 05:34:26 -04:00
plan = ::Stripe::Plan.retrieve(params[:id])
2019-10-15 20:22:58 -04:00
2019-11-30 00:39:28 -05:00
serialized = plan.to_h.merge(currency: plan[:currency].upcase)
render_json_dump serialized
2019-10-15 20:22:58 -04:00
rescue ::Stripe::InvalidRequestError => e
return render_json_error e.message
end
2019-10-09 21:08:52 -04:00
end
2019-10-23 00:55:06 -04:00
def update
begin
plan = ::Stripe::Plan.update(
params[:id],
nickname: params[:nickname],
trial_period_days: params[:trial_period_days],
2019-10-23 19:02:31 -04:00
active: params[:active],
2019-10-23 00:55:06 -04:00
metadata: { group_name: params[:metadata][:group_name] }
)
render_json_dump plan
rescue ::Stripe::InvalidRequestError => e
return render_json_error e.message
end
end
2019-10-17 05:34:26 -04:00
def destroy
begin
plan = ::Stripe::Plan.delete(params[:id])
render_json_dump plan
2019-10-17 05:34:26 -04:00
rescue ::Stripe::InvalidRequestError => e
return render_json_error e.message
end
2019-10-09 21:08:52 -04:00
end
2019-10-21 00:28:45 -04:00
private
def product_params
{ product: params[:product_id] } if params[:product_id]
end
end
end
end