Files
discourse-subscriptions/app/controllers/discourse_subscriptions/subscriptions_controller.rb
T
Justin DiRose c9ff55b46a REFACTOR: Use the Prices API in place of Plans (#17)
Stripe has a newer API called Prices where you can create a price for any product and it can either be recurring or one-time. The easy part is existing Plans work with the Prices API by passing a Plan ID, but objects are returned in the slightly-different Prices API object format.

This commit is a refactor to the new API to handle the data in its new form, and lays the foundation for a one time payment plan to be added to any subscriptions product.
2020-07-15 08:44:40 -05:00

78 lines
1.8 KiB
Ruby

# frozen_string_literal: true
module DiscourseSubscriptions
class SubscriptionsController < ::ApplicationController
include DiscourseSubscriptions::Stripe
include DiscourseSubscriptions::Group
before_action :set_api_key
requires_login
def index
begin
products = ::Stripe::Product.list(active: true)
subscriptions = products[:data].map do |p|
{
id: p[:id],
description: p.dig(:metadata, :description)
}
end
render_json_dump subscriptions
rescue ::Stripe::InvalidRequestError => e
render_json_error e.message
end
end
def create
begin
plan = ::Stripe::Price.retrieve(params[:plan])
if plan[:metadata] && plan[:metadata][:trial_period_days]
trial_days = plan[:metadata][:trial_period_days]
end
@subscription = ::Stripe::Subscription.create(
customer: params[:customer],
items: [ { price: params[:plan] } ],
metadata: metadata_user,
trial_period_days: trial_days
)
group = plan_group(plan)
if subscription_ok && group
group.add(current_user)
end
customer = Customer.create(
user_id: current_user.id,
customer_id: params[:customer],
product_id: plan[:product]
)
Subscription.create(
customer_id: customer.id,
external_id: @subscription[:id]
)
render_json_dump @subscription
rescue ::Stripe::InvalidRequestError => e
render_json_error e.message
end
end
private
def metadata_user
{ user_id: current_user.id, username: current_user.username_lower }
end
def subscription_ok
['active', 'trialing'].include?(@subscription[:status])
end
end
end