2019-09-24 16:04:42 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-12-04 11:23:45 +11:00
|
|
|
module DiscourseSubscriptions
|
2019-10-10 12:08:52 +11:00
|
|
|
module Admin
|
|
|
|
class PlansController < ::Admin::AdminController
|
2019-12-04 11:23:45 +11:00
|
|
|
include DiscourseSubscriptions::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
|
2019-10-16 11:22:58 +11:00
|
|
|
begin
|
2020-07-15 08:44:40 -05:00
|
|
|
plans = ::Stripe::Price.list(product_params)
|
2019-10-16 11:22:58 +11:00
|
|
|
|
|
|
|
render_json_dump plans.data
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
2019-12-12 09:59:38 +11:00
|
|
|
render_json_error e.message
|
2019-10-16 11:22:58 +11:00
|
|
|
end
|
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
|
2020-07-22 11:06:34 -05:00
|
|
|
price_object = {
|
2019-10-17 20:34:26 +11:00
|
|
|
nickname: params[:nickname],
|
2020-07-15 08:44:40 -05:00
|
|
|
unit_amount: params[:amount],
|
2019-10-23 10:16:17 +11:00
|
|
|
product: params[:product],
|
2019-11-30 17:24:49 +11:00
|
|
|
currency: params[:currency],
|
2019-10-24 10:02:31 +11:00
|
|
|
active: params[:active],
|
2020-07-15 08:44:40 -05:00
|
|
|
metadata: {
|
|
|
|
group_name: params[:metadata][:group_name],
|
2022-12-29 12:35:06 +00:00
|
|
|
trial_period_days: params[:trial_period_days],
|
|
|
|
},
|
2020-07-22 11:06:34 -05:00
|
|
|
}
|
2019-10-08 15:55:38 +11:00
|
|
|
|
2022-12-29 12:35:06 +00:00
|
|
|
price_object[:recurring] = { interval: params[:interval] } if params[:type] == "recurring"
|
2020-07-22 11:06:34 -05:00
|
|
|
|
|
|
|
plan = ::Stripe::Price.create(price_object)
|
2019-10-08 15:55:38 +11:00
|
|
|
|
2020-07-22 11:06:34 -05:00
|
|
|
render_json_dump plan
|
2019-10-10 12:08:52 +11:00
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
2019-12-12 09:59:38 +11:00
|
|
|
render_json_error e.message
|
2019-10-10 12:08:52 +11:00
|
|
|
end
|
2019-10-08 15:55:38 +11:00
|
|
|
end
|
2019-09-25 13:20:28 +10:00
|
|
|
|
2019-10-17 20:34:26 +11:00
|
|
|
def show
|
2019-10-16 11:22:58 +11:00
|
|
|
begin
|
2020-07-15 08:44:40 -05:00
|
|
|
plan = ::Stripe::Price.retrieve(params[:id])
|
2019-10-16 11:22:58 +11:00
|
|
|
|
2020-07-15 08:44:40 -05:00
|
|
|
if plan[:metadata] && plan[:metadata][:trial_period_days]
|
|
|
|
trial_days = plan[:metadata][:trial_period_days]
|
|
|
|
elsif plan[:recurring] && plan[:recurring][:trial_period_days]
|
|
|
|
trial_days = plan[:recurring][:trial_period_days]
|
|
|
|
end
|
|
|
|
|
2021-11-12 16:20:16 -07:00
|
|
|
interval = nil
|
2022-12-29 12:35:06 +00:00
|
|
|
interval = plan[:recurring][:interval] if plan[:recurring] && plan[:recurring][:interval]
|
2021-09-21 18:12:17 +05:30
|
|
|
|
2022-12-29 12:35:06 +00:00
|
|
|
serialized =
|
|
|
|
plan.to_h.merge(
|
|
|
|
trial_period_days: trial_days,
|
|
|
|
currency: plan[:currency].upcase,
|
|
|
|
interval: interval,
|
|
|
|
)
|
2019-11-30 16:39:28 +11:00
|
|
|
|
|
|
|
render_json_dump serialized
|
2019-10-16 11:22:58 +11:00
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
2019-12-12 09:59:38 +11:00
|
|
|
render_json_error e.message
|
2019-10-16 11:22:58 +11:00
|
|
|
end
|
2019-10-10 12:08:52 +11:00
|
|
|
end
|
2019-10-08 15:55:38 +11:00
|
|
|
|
2019-10-23 15:55:06 +11:00
|
|
|
def update
|
|
|
|
begin
|
2022-12-29 12:35:06 +00:00
|
|
|
plan =
|
|
|
|
::Stripe::Price.update(
|
|
|
|
params[:id],
|
|
|
|
nickname: params[:nickname],
|
|
|
|
active: params[:active],
|
|
|
|
metadata: {
|
|
|
|
group_name: params[:metadata][:group_name],
|
|
|
|
trial_period_days: params[:trial_period_days],
|
|
|
|
},
|
|
|
|
)
|
2019-10-23 15:55:06 +11:00
|
|
|
|
|
|
|
render_json_dump plan
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
2019-12-12 09:59:38 +11:00
|
|
|
render_json_error e.message
|
2019-10-23 15:55:06 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-21 15:28:45 +11:00
|
|
|
private
|
|
|
|
|
|
|
|
def product_params
|
|
|
|
{ product: params[:product_id] } if params[:product_id]
|
|
|
|
end
|
2019-10-08 15:55:38 +11:00
|
|
|
end
|
2019-09-24 16:04:42 +10:00
|
|
|
end
|
|
|
|
end
|