1
0
mirror of synced 2026-07-28 02:45:28 +00:00
Files
discourse-subscriptions/app/controllers/discourse_subscriptions/user/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

86 lines
2.5 KiB
Ruby

# frozen_string_literal: true
module DiscourseSubscriptions
module User
class SubscriptionsController < ::ApplicationController
include DiscourseSubscriptions::Stripe
include DiscourseSubscriptions::Group
before_action :set_api_key
requires_login
def index
begin
customer = Customer.where(user_id: current_user.id)
customer_ids = customer.map { |c| c.id } if customer
subscription_ids = Subscription.where("customer_id in (?)", customer_ids).pluck(:external_id) if customer_ids
subscriptions = []
if subscription_ids
plans = ::Stripe::Price.list(
expand: ['data.product'],
limit: 100
)
customers = ::Stripe::Customer.list(
email: current_user.email,
expand: ['data.subscriptions']
)
subscriptions = customers[:data].map do |sub_customer|
sub_customer[:subscriptions][:data]
end.flatten(1)
subscriptions = subscriptions.select { |sub| subscription_ids.include?(sub[:id]) }
subscriptions.map! do |subscription|
plan = plans[:data].find { |p| p[:id] == subscription[:items][:data][0][:price][:id] }
subscription.to_h.except!(:plan)
subscription.to_h.merge(plan: plan, product: plan[:product].to_h.slice(:id, :name))
end
end
render_json_dump subscriptions
rescue ::Stripe::InvalidRequestError => e
render_json_error e.message
end
end
def destroy
begin
subscription = ::Stripe::Subscription.retrieve(params[:id])
customer = Customer.find_by(
user_id: current_user.id,
customer_id: subscription[:customer],
product_id: subscription[:plan][:product]
)
if customer.present?
sub_model = Subscription.find_by(
customer_id: customer.id,
external_id: params[:id]
)
deleted = ::Stripe::Subscription.delete(params[:id])
customer.delete
sub_model.delete if sub_model
group = plan_group(subscription[:plan])
group.remove(current_user) if group
render_json_dump deleted
else
render_json_error I18n.t('discourse_subscriptions.customer_not_found')
end
rescue ::Stripe::InvalidRequestError => e
render_json_error e.message
end
end
end
end
end