mirror of
https://github.com/discourse/discourse-subscriptions.git
synced 2025-02-18 09:24:47 +00:00
Building off the foundation of using the Prices API, this PR adds the ability to create a one-time purchase plan for any product, which then can add a user to the specified plan group. Some things to be aware of: One-time purchases cannot have trials. One-time purchases use the Invoice API instead of Subscriptions. Invoices are created then charged immediately. Users should receive emails for these invoices directly from Stripe just like subscriptions.
93 lines
2.4 KiB
Ruby
93 lines
2.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module DiscourseSubscriptions
|
|
module Admin
|
|
class PlansController < ::Admin::AdminController
|
|
include DiscourseSubscriptions::Stripe
|
|
|
|
before_action :set_api_key
|
|
|
|
def index
|
|
begin
|
|
plans = ::Stripe::Price.list(product_params)
|
|
|
|
render_json_dump plans.data
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
render_json_error e.message
|
|
end
|
|
end
|
|
|
|
def create
|
|
begin
|
|
price_object = {
|
|
nickname: params[:nickname],
|
|
unit_amount: params[:amount],
|
|
product: params[:product],
|
|
currency: params[:currency],
|
|
active: params[:active],
|
|
metadata: {
|
|
group_name: params[:metadata][:group_name],
|
|
trial_period_days: params[:trial_period_days]
|
|
}
|
|
}
|
|
|
|
if params[:type] == 'recurring'
|
|
price_object[:recurring] = {
|
|
interval: params[:interval]
|
|
}
|
|
end
|
|
|
|
plan = ::Stripe::Price.create(price_object)
|
|
|
|
render_json_dump plan
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
render_json_error e.message
|
|
end
|
|
end
|
|
|
|
def show
|
|
begin
|
|
plan = ::Stripe::Price.retrieve(params[:id])
|
|
|
|
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
|
|
|
|
serialized = plan.to_h.merge(trial_period_days: trial_days, currency: plan[:currency].upcase)
|
|
|
|
render_json_dump serialized
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
render_json_error e.message
|
|
end
|
|
end
|
|
|
|
def update
|
|
begin
|
|
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]
|
|
}
|
|
)
|
|
|
|
render_json_dump plan
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
render_json_error e.message
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def product_params
|
|
{ product: params[:product_id] } if params[:product_id]
|
|
end
|
|
end
|
|
end
|
|
end
|