2019-10-14 22:18:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscoursePatrons
|
|
|
|
module Admin
|
|
|
|
class ProductsController < ::Admin::AdminController
|
|
|
|
include DiscoursePatrons::Stripe
|
|
|
|
|
|
|
|
before_action :set_api_key
|
|
|
|
|
|
|
|
def index
|
2019-10-16 06:06:19 -04:00
|
|
|
begin
|
|
|
|
products = ::Stripe::Product.list
|
|
|
|
|
|
|
|
render_json_dump products.data
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
|
|
return render_json_error e.message
|
|
|
|
end
|
2019-10-14 22:18:25 -04:00
|
|
|
end
|
2019-10-15 06:50:30 -04:00
|
|
|
|
|
|
|
def create
|
|
|
|
begin
|
2019-10-23 00:55:06 -04:00
|
|
|
create_params = product_params.merge!(type: 'service')
|
|
|
|
|
|
|
|
if params[:statement_descriptor].blank?
|
|
|
|
create_params.except!(:statement_descriptor)
|
|
|
|
end
|
|
|
|
|
|
|
|
product = ::Stripe::Product.create(create_params)
|
2019-10-15 06:50:30 -04:00
|
|
|
|
|
|
|
render_json_dump product
|
|
|
|
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
|
|
return render_json_error e.message
|
|
|
|
end
|
|
|
|
end
|
2019-10-15 08:14:04 -04:00
|
|
|
|
2019-10-15 23:15:01 -04:00
|
|
|
def show
|
|
|
|
begin
|
|
|
|
product = ::Stripe::Product.retrieve(params[:id])
|
|
|
|
|
|
|
|
render_json_dump product
|
|
|
|
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
|
|
return render_json_error e.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
begin
|
|
|
|
product = ::Stripe::Product.update(
|
2019-10-16 21:07:06 -04:00
|
|
|
params[:id],
|
2019-10-21 18:24:54 -04:00
|
|
|
product_params
|
2019-10-15 23:15:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
render_json_dump product
|
|
|
|
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
|
|
return render_json_error e.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-15 08:14:04 -04:00
|
|
|
def destroy
|
|
|
|
begin
|
|
|
|
product = ::Stripe::Product.delete(params[:id])
|
|
|
|
|
|
|
|
render_json_dump product
|
|
|
|
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
|
|
return render_json_error e.message
|
|
|
|
end
|
|
|
|
end
|
2019-10-16 06:06:19 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-10-21 18:24:54 -04:00
|
|
|
def product_params
|
2019-10-31 21:30:19 -04:00
|
|
|
params.permit!
|
|
|
|
|
2019-10-21 18:24:54 -04:00
|
|
|
{
|
|
|
|
name: params[:name],
|
|
|
|
active: params[:active],
|
2019-10-31 21:30:19 -04:00
|
|
|
statement_descriptor: params[:statement_descriptor],
|
|
|
|
metadata: { description: params.dig(:metadata, :description) }
|
2019-10-21 18:24:54 -04:00
|
|
|
}
|
2019-10-16 06:06:19 -04:00
|
|
|
end
|
2019-10-14 22:18:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|