REFACTOR: Use models to store data (#11)
* REFACTOR: Use api to add subscribe link * FIX: I18n subscribe link * REFACTOR: Use models to store some data This enables the plugin to show only subscription information which was generated on Discourse. Subscription data storage is limited to the external identifiers Stripe generates so we can interact with the API. * DEV: Test/linting fixes/rake task
This commit is contained in:
@@ -9,9 +9,15 @@ module DiscourseSubscriptions
|
||||
|
||||
def index
|
||||
begin
|
||||
products = ::Stripe::Product.list
|
||||
product_ids = Product.all.pluck(:external_id)
|
||||
products = []
|
||||
|
||||
render_json_dump products.data
|
||||
if product_ids.present?
|
||||
products = ::Stripe::Product.list({ ids: product_ids })
|
||||
products = products[:data]
|
||||
end
|
||||
|
||||
render_json_dump products
|
||||
rescue ::Stripe::InvalidRequestError => e
|
||||
render_json_error e.message
|
||||
end
|
||||
@@ -27,6 +33,10 @@ module DiscourseSubscriptions
|
||||
|
||||
product = ::Stripe::Product.create(create_params)
|
||||
|
||||
Product.create(
|
||||
external_id: product[:id]
|
||||
)
|
||||
|
||||
render_json_dump product
|
||||
|
||||
rescue ::Stripe::InvalidRequestError => e
|
||||
@@ -63,6 +73,8 @@ module DiscourseSubscriptions
|
||||
begin
|
||||
product = ::Stripe::Product.delete(params[:id])
|
||||
|
||||
Product.delete_by(external_id: params[:id])
|
||||
|
||||
render_json_dump product
|
||||
|
||||
rescue ::Stripe::InvalidRequestError => e
|
||||
|
||||
@@ -9,7 +9,13 @@ module DiscourseSubscriptions
|
||||
|
||||
def index
|
||||
begin
|
||||
subscriptions = ::Stripe::Subscription.list(expand: ['data.plan.product'])
|
||||
subscription_ids = Subscription.all.pluck(:external_id)
|
||||
subscriptions = []
|
||||
|
||||
if subscription_ids.present?
|
||||
subscriptions = ::Stripe::Subscription.list(expand: ['data.plan.product'])
|
||||
subscriptions = subscriptions.select { |sub| subscription_ids.include?(sub[:id]) }
|
||||
end
|
||||
|
||||
render_json_dump subscriptions
|
||||
rescue ::Stripe::InvalidRequestError => e
|
||||
@@ -21,15 +27,16 @@ module DiscourseSubscriptions
|
||||
begin
|
||||
subscription = ::Stripe::Subscription.delete(params[:id])
|
||||
|
||||
customer = DiscourseSubscriptions::Customer.find_by(
|
||||
customer = Customer.find_by(
|
||||
product_id: subscription[:plan][:product],
|
||||
customer_id: subscription[:customer]
|
||||
)
|
||||
|
||||
if customer
|
||||
customer.delete
|
||||
Subscription.delete_by(external_id: params[:id])
|
||||
|
||||
if customer
|
||||
user = ::User.find(customer.user_id)
|
||||
customer.delete
|
||||
group = plan_group(subscription[:plan])
|
||||
group.remove(user) if group
|
||||
end
|
||||
|
||||
@@ -29,7 +29,7 @@ module DiscourseSubscriptions
|
||||
end
|
||||
|
||||
def find_customer
|
||||
DiscourseSubscriptions::Customer.find_user(current_user)
|
||||
Customer.find_user(current_user)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -11,7 +11,7 @@ module DiscourseSubscriptions
|
||||
|
||||
def create
|
||||
begin
|
||||
customer = DiscourseSubscriptions::Customer.where(user_id: current_user.id, product_id: nil).first_or_create do |c|
|
||||
customer = Customer.where(user_id: current_user.id, product_id: nil).first_or_create do |c|
|
||||
new_customer = ::Stripe::Customer.create(
|
||||
email: current_user.email
|
||||
)
|
||||
|
||||
@@ -8,10 +8,18 @@ module DiscourseSubscriptions
|
||||
|
||||
def index
|
||||
begin
|
||||
response = ::Stripe::Product.list(active: true)
|
||||
product_ids = Product.all.pluck(:external_id)
|
||||
products = []
|
||||
|
||||
products = response[:data].map do |p|
|
||||
serialize(p)
|
||||
if product_ids.present?
|
||||
response = ::Stripe::Product.list({
|
||||
ids: product_ids,
|
||||
active: true
|
||||
})
|
||||
|
||||
products = response[:data].map do |p|
|
||||
serialize(p)
|
||||
end
|
||||
end
|
||||
|
||||
render_json_dump products
|
||||
@@ -46,7 +54,7 @@ module DiscourseSubscriptions
|
||||
def current_user_products
|
||||
return [] if current_user.nil?
|
||||
|
||||
::DiscourseSubscriptions::Customer
|
||||
Customer
|
||||
.select(:product_id)
|
||||
.where(user_id: current_user.id)
|
||||
.map { |c| c.product_id }.compact
|
||||
|
||||
@@ -41,12 +41,17 @@ module DiscourseSubscriptions
|
||||
group.add(current_user)
|
||||
end
|
||||
|
||||
DiscourseSubscriptions::Customer.create(
|
||||
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
|
||||
|
||||
@@ -9,13 +9,19 @@ module DiscourseSubscriptions
|
||||
|
||||
def index
|
||||
begin
|
||||
customer = DiscourseSubscriptions::Customer.find_by(user_id: current_user.id, product_id: nil)
|
||||
customer = Customer.find_by(user_id: current_user.id)
|
||||
product_ids = Product.all.pluck(:external_id)
|
||||
|
||||
data = []
|
||||
|
||||
if customer.present?
|
||||
if customer.present? && product_ids.present?
|
||||
# lots of matching because the Stripe API doesn't make it easy to match products => payments except from invoices
|
||||
all_invoices = ::Stripe::Invoice.list(customer: customer[:customer_id])
|
||||
invoices_with_products = all_invoices[:data].select { |invoice| product_ids.include?(invoice.dig(:lines, :data, 0, :plan, :product)) }
|
||||
invoice_ids = invoices_with_products.map { |invoice| invoice[:id] }
|
||||
payments = ::Stripe::PaymentIntent.list(customer: customer[:customer_id])
|
||||
data = payments[:data]
|
||||
payments_from_invoices = payments[:data].select { |payment| invoice_ids.include?(payment[:invoice]) }
|
||||
data = payments_from_invoices
|
||||
end
|
||||
|
||||
render_json_dump data
|
||||
|
||||
@@ -10,22 +10,31 @@ module DiscourseSubscriptions
|
||||
|
||||
def index
|
||||
begin
|
||||
plans = ::Stripe::Plan.list(
|
||||
expand: ['data.product']
|
||||
)
|
||||
customer = Customer.find_by(user_id: current_user.id)
|
||||
subscription_ids = Subscription.where(customer_id: customer.id).pluck(:external_id) if customer
|
||||
|
||||
customers = ::Stripe::Customer.list(
|
||||
email: current_user.email,
|
||||
expand: ['data.subscriptions']
|
||||
)
|
||||
subscriptions = []
|
||||
|
||||
subscriptions = customers[:data].map do |customer|
|
||||
customer[:subscriptions][:data]
|
||||
end.flatten(1)
|
||||
if subscription_ids
|
||||
plans = ::Stripe::Plan.list(
|
||||
expand: ['data.product']
|
||||
)
|
||||
|
||||
subscriptions.map! do |subscription|
|
||||
plan = plans[:data].find { |p| p[:id] == subscription[:plan][:id] }
|
||||
subscription.to_h.merge(product: plan[:product].to_h.slice(:id, :name))
|
||||
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[:plan][:id] }
|
||||
subscription.to_h.merge(product: plan[:product].to_h.slice(:id, :name))
|
||||
end
|
||||
end
|
||||
|
||||
render_json_dump subscriptions
|
||||
@@ -46,9 +55,16 @@ module DiscourseSubscriptions
|
||||
)
|
||||
|
||||
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
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ module DiscourseSubscriptions
|
||||
|
||||
scope :find_user, ->(user) { find_by_user_id(user.id) }
|
||||
|
||||
has_many :subscriptions
|
||||
|
||||
def self.create_customer(user, customer)
|
||||
create(customer_id: customer[:id], user_id: user.id)
|
||||
end
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module DiscourseSubscriptions
|
||||
class Product < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module DiscourseSubscriptions
|
||||
class Subscription < ActiveRecord::Base
|
||||
belongs_to :customer
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user