discourse-subscriptions/app/controllers/products_controller.rb

54 lines
1.1 KiB
Ruby
Raw Normal View History

2019-10-24 17:18:16 -04:00
# frozen_string_literal: true
module DiscoursePatrons
class ProductsController < ::ApplicationController
include DiscoursePatrons::Stripe
before_action :set_api_key
def index
begin
2019-11-04 00:37:21 -05:00
response = ::Stripe::Product.list(active: true)
2019-10-24 17:18:16 -04:00
2019-11-04 00:37:21 -05:00
products = response[:data].map do |p|
serialize(p)
end
render_json_dump products
rescue ::Stripe::InvalidRequestError => e
return render_json_error e.message
end
end
def show
begin
product = ::Stripe::Product.retrieve(params[:id])
render_json_dump serialize(product)
2019-10-24 17:18:16 -04:00
rescue ::Stripe::InvalidRequestError => e
return render_json_error e.message
end
end
2019-11-04 00:37:21 -05:00
private
def serialize(product)
{
id: product[:id],
name: product[:name],
description: product[:metadata][:description],
subscribed: current_user_products.include?(product[:id])
2019-11-04 00:37:21 -05:00
}
end
def current_user_products
::DiscoursePatrons::Customer
.select(:product_id)
.where(user_id: current_user.id)
.map { |c| c.product_id }.compact
end
2019-10-24 17:18:16 -04:00
end
end