2019-10-14 09:52:43 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2019-12-04 11:23:45 +11:00
|
|
|
module DiscourseSubscriptions
|
2020-10-21 13:36:31 -05:00
|
|
|
class SubscribeController < ::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
|
2020-10-21 13:36:31 -05:00
|
|
|
product_ids = Product.all.pluck(:external_id)
|
|
|
|
|
products = []
|
|
|
|
|
|
|
|
|
|
if product_ids.present? && is_stripe_configured?
|
|
|
|
|
response = ::Stripe::Product.list({
|
|
|
|
|
ids: product_ids,
|
|
|
|
|
active: true
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
products = response[:data].map do |p|
|
|
|
|
|
serialize_product(p)
|
|
|
|
|
end
|
2019-10-29 11:43:32 +11:00
|
|
|
|
2019-11-04 16:37:21 +11:00
|
|
|
end
|
2019-10-29 11:43:32 +11:00
|
|
|
|
2020-10-21 13:36:31 -05:00
|
|
|
render_json_dump products
|
|
|
|
|
|
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
|
|
|
render_json_error e.message
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def show
|
|
|
|
|
params.require(:id)
|
|
|
|
|
begin
|
|
|
|
|
product = ::Stripe::Product.retrieve(params[:id])
|
|
|
|
|
plans = ::Stripe::Price.list(active: true, product: params[:id])
|
|
|
|
|
|
|
|
|
|
response = {
|
|
|
|
|
product: serialize_product(product),
|
|
|
|
|
plans: serialize_plans(plans)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render_json_dump response
|
2019-10-29 11:43:32 +11:00
|
|
|
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
|
2020-10-21 13:36:31 -05:00
|
|
|
params.require([:source, :plan])
|
2019-10-17 12:07:06 +11:00
|
|
|
begin
|
2020-10-21 13:36:31 -05:00
|
|
|
customer = create_customer(params[:source])
|
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(
|
2020-10-21 13:36:31 -05:00
|
|
|
customer: customer[:id],
|
2020-07-22 11:06:34 -05:00
|
|
|
items: [{ price: params[:plan] }],
|
|
|
|
|
metadata: metadata_user,
|
|
|
|
|
trial_period_days: trial_days
|
|
|
|
|
)
|
2020-07-24 15:07:18 -05:00
|
|
|
|
|
|
|
|
payment_intent = retrieve_payment_intent(transaction[:latest_invoice]) if transaction[:status] == 'incomplete'
|
2020-07-22 11:06:34 -05:00
|
|
|
else
|
|
|
|
|
invoice_item = ::Stripe::InvoiceItem.create(
|
2020-10-21 13:36:31 -05:00
|
|
|
customer: customer[:id],
|
2020-07-22 11:06:34 -05:00
|
|
|
price: params[:plan]
|
|
|
|
|
)
|
|
|
|
|
invoice = ::Stripe::Invoice.create(
|
2020-10-21 13:36:31 -05:00
|
|
|
customer: customer[:id]
|
2020-07-22 11:06:34 -05:00
|
|
|
)
|
2020-07-24 15:07:18 -05:00
|
|
|
transaction = ::Stripe::Invoice.finalize_invoice(invoice[:id])
|
|
|
|
|
payment_intent = retrieve_payment_intent(transaction[:id]) if transaction[:status] == 'open'
|
|
|
|
|
transaction = ::Stripe::Invoice.pay(invoice[:id]) if payment_intent[:status] == 'successful'
|
2020-07-15 08:44:40 -05:00
|
|
|
end
|
2019-10-24 11:37:20 +11:00
|
|
|
|
2020-07-24 15:07:18 -05:00
|
|
|
finalize_transaction(transaction, plan) if transaction_ok(transaction)
|
2019-10-14 09:52:43 +11:00
|
|
|
|
2020-07-24 15:07:18 -05:00
|
|
|
transaction = transaction.to_h.merge(transaction, payment_intent: payment_intent)
|
2019-10-17 12:07:06 +11:00
|
|
|
|
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
|
|
|
|
|
|
2020-07-24 15:07:18 -05:00
|
|
|
def finalize
|
2020-10-21 13:36:31 -05:00
|
|
|
params.require([:plan, :transaction])
|
2020-07-24 15:07:18 -05:00
|
|
|
begin
|
|
|
|
|
price = ::Stripe::Price.retrieve(params[:plan])
|
|
|
|
|
transaction = retrieve_transaction(params[:transaction])
|
|
|
|
|
finalize_transaction(transaction, price) if transaction_ok(transaction)
|
|
|
|
|
|
|
|
|
|
render_json_dump params[:transaction]
|
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
|
|
|
render_json_error e.message
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def finalize_transaction(transaction, plan)
|
|
|
|
|
group = plan_group(plan)
|
|
|
|
|
|
|
|
|
|
group.add(current_user) if group
|
|
|
|
|
|
|
|
|
|
customer = Customer.create(
|
|
|
|
|
user_id: current_user.id,
|
|
|
|
|
customer_id: transaction[:customer],
|
|
|
|
|
product_id: plan[:product]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if transaction[:object] == 'subscription'
|
|
|
|
|
Subscription.create(
|
|
|
|
|
customer_id: customer.id,
|
|
|
|
|
external_id: transaction[:id]
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-17 12:07:06 +11:00
|
|
|
private
|
|
|
|
|
|
2020-10-21 13:36:31 -05:00
|
|
|
def serialize_product(product)
|
|
|
|
|
{
|
|
|
|
|
id: product[:id],
|
|
|
|
|
name: product[:name],
|
|
|
|
|
description: PrettyText.cook(product[:metadata][:description]),
|
|
|
|
|
subscribed: current_user_products.include?(product[:id])
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def current_user_products
|
|
|
|
|
return [] if current_user.nil?
|
|
|
|
|
|
|
|
|
|
Customer
|
|
|
|
|
.select(:product_id)
|
|
|
|
|
.where(user_id: current_user.id)
|
|
|
|
|
.map { |c| c.product_id }.compact
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def serialize_plans(plans)
|
|
|
|
|
plans[:data].map do |plan|
|
|
|
|
|
plan.to_h.slice(:id, :unit_amount, :currency, :type, :recurring)
|
|
|
|
|
end.sort_by { |plan| plan[:amount] }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create_customer(source)
|
|
|
|
|
::Stripe::Customer.create(
|
|
|
|
|
email: current_user.email,
|
|
|
|
|
source: source
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def retrieve_payment_intent(invoice_id)
|
|
|
|
|
invoice = ::Stripe::Invoice.retrieve(invoice_id)
|
|
|
|
|
::Stripe::PaymentIntent.retrieve(invoice[:payment_intent])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def retrieve_transaction(transaction)
|
|
|
|
|
begin
|
|
|
|
|
case transaction
|
|
|
|
|
when /^sub_/
|
|
|
|
|
::Stripe::Subscription.retrieve(transaction)
|
|
|
|
|
when /^in_/
|
|
|
|
|
::Stripe::Invoice.retrieve(transaction)
|
|
|
|
|
end
|
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
|
|
|
e.message
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
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
|