mirror of
https://github.com/discourse/discourse-subscriptions.git
synced 2025-03-07 01:59:35 +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.
28 lines
732 B
Ruby
28 lines
732 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, :type, :recurring)
|
|
end.sort_by { |plan| plan[:amount] }
|
|
|
|
render_json_dump serialized
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
render_json_error e.message
|
|
end
|
|
end
|
|
end
|
|
end
|