* 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
100 lines
2.2 KiB
Ruby
100 lines
2.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module DiscourseSubscriptions
|
|
module Admin
|
|
class ProductsController < ::Admin::AdminController
|
|
include DiscourseSubscriptions::Stripe
|
|
|
|
before_action :set_api_key
|
|
|
|
def index
|
|
begin
|
|
product_ids = Product.all.pluck(:external_id)
|
|
products = []
|
|
|
|
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
|
|
end
|
|
|
|
def create
|
|
begin
|
|
create_params = product_params.merge!(type: 'service')
|
|
|
|
if params[:statement_descriptor].blank?
|
|
create_params.except!(:statement_descriptor)
|
|
end
|
|
|
|
product = ::Stripe::Product.create(create_params)
|
|
|
|
Product.create(
|
|
external_id: product[:id]
|
|
)
|
|
|
|
render_json_dump product
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
render_json_error e.message
|
|
end
|
|
end
|
|
|
|
def show
|
|
begin
|
|
product = ::Stripe::Product.retrieve(params[:id])
|
|
|
|
render_json_dump product
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
render_json_error e.message
|
|
end
|
|
end
|
|
|
|
def update
|
|
begin
|
|
product = ::Stripe::Product.update(
|
|
params[:id],
|
|
product_params
|
|
)
|
|
|
|
render_json_dump product
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
render_json_error e.message
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
begin
|
|
product = ::Stripe::Product.delete(params[:id])
|
|
|
|
Product.delete_by(external_id: params[:id])
|
|
|
|
render_json_dump product
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
render_json_error e.message
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def product_params
|
|
params.permit!
|
|
|
|
{
|
|
name: params[:name],
|
|
active: params[:active],
|
|
statement_descriptor: params[:statement_descriptor],
|
|
metadata: { description: params.dig(:metadata, :description) }
|
|
}
|
|
end
|
|
end
|
|
end
|
|
end
|