2019-10-14 09:52:43 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2019-12-04 11:23:45 +11:00
|
|
|
module DiscourseSubscriptions
|
2019-10-14 09:52:43 +11:00
|
|
|
class SubscriptionsController < ::ApplicationController
|
2019-12-04 11:23:45 +11:00
|
|
|
include DiscourseSubscriptions::Stripe
|
|
|
|
|
include DiscourseSubscriptions::Group
|
2019-10-14 09:52:43 +11:00
|
|
|
before_action :set_api_key
|
2019-10-29 11:43:32 +11:00
|
|
|
requires_login
|
|
|
|
|
|
|
|
|
|
def index
|
|
|
|
|
begin
|
2019-11-04 16:37:21 +11:00
|
|
|
products = ::Stripe::Product.list(active: true)
|
2019-10-29 11:43:32 +11:00
|
|
|
|
2019-11-04 16:37:21 +11:00
|
|
|
subscriptions = products[:data].map do |p|
|
|
|
|
|
{
|
|
|
|
|
id: p[:id],
|
|
|
|
|
description: p.dig(:metadata, :description)
|
|
|
|
|
}
|
|
|
|
|
end
|
2019-10-29 11:43:32 +11:00
|
|
|
|
|
|
|
|
render_json_dump subscriptions
|
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
2019-12-12 09:59:38 +11:00
|
|
|
render_json_error e.message
|
2019-10-29 11:43:32 +11:00
|
|
|
end
|
|
|
|
|
end
|
2019-10-14 09:52:43 +11:00
|
|
|
|
|
|
|
|
def create
|
2019-10-17 12:07:06 +11:00
|
|
|
begin
|
2020-07-15 08:44:40 -05:00
|
|
|
plan = ::Stripe::Price.retrieve(params[:plan])
|
|
|
|
|
|
2020-07-22 11:06:34 -05:00
|
|
|
recurring_plan = plan[:type] == 'recurring'
|
|
|
|
|
|
|
|
|
|
if recurring_plan
|
|
|
|
|
trial_days = plan[:metadata][:trial_period_days] if plan[:metadata] && plan[:metadata][:trial_period_days]
|
|
|
|
|
|
|
|
|
|
transaction = ::Stripe::Subscription.create(
|
|
|
|
|
customer: params[:customer],
|
|
|
|
|
items: [{ price: params[:plan] }],
|
|
|
|
|
metadata: metadata_user,
|
|
|
|
|
trial_period_days: trial_days
|
|
|
|
|
)
|
|
|
|
|
else
|
|
|
|
|
invoice_item = ::Stripe::InvoiceItem.create(
|
|
|
|
|
customer: params[:customer],
|
|
|
|
|
price: params[:plan]
|
|
|
|
|
)
|
|
|
|
|
invoice = ::Stripe::Invoice.create(
|
|
|
|
|
customer: params[:customer]
|
|
|
|
|
)
|
|
|
|
|
transaction = ::Stripe::Invoice.pay(invoice[:id])
|
2020-07-15 08:44:40 -05:00
|
|
|
end
|
2019-10-24 11:37:20 +11:00
|
|
|
|
2020-07-22 11:06:34 -05:00
|
|
|
if transaction_ok(transaction)
|
|
|
|
|
group = plan_group(plan)
|
2019-10-14 09:52:43 +11:00
|
|
|
|
2020-07-22 11:06:34 -05:00
|
|
|
group.add(current_user) if group
|
2019-10-24 11:37:20 +11:00
|
|
|
|
2020-07-22 11:06:34 -05:00
|
|
|
customer = Customer.create(
|
|
|
|
|
user_id: current_user.id,
|
|
|
|
|
customer_id: params[:customer],
|
|
|
|
|
product_id: plan[:product]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if transaction[:object] == 'subscription'
|
|
|
|
|
Subscription.create(
|
|
|
|
|
customer_id: customer.id,
|
|
|
|
|
external_id: transaction[:id]
|
|
|
|
|
)
|
|
|
|
|
end
|
2019-10-17 12:07:06 +11:00
|
|
|
end
|
|
|
|
|
|
2020-07-22 11:06:34 -05:00
|
|
|
render_json_dump transaction
|
2019-10-17 12:07:06 +11:00
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
2019-12-12 09:59:38 +11:00
|
|
|
render_json_error e.message
|
2019-10-17 12:07:06 +11:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2019-11-13 14:21:21 +11:00
|
|
|
def metadata_user
|
|
|
|
|
{ user_id: current_user.id, username: current_user.username_lower }
|
|
|
|
|
end
|
|
|
|
|
|
2020-07-22 11:06:34 -05:00
|
|
|
def transaction_ok(transaction)
|
|
|
|
|
%w[active trialing paid].include?(transaction[:status])
|
2019-10-14 09:52:43 +11:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|