discourse-subscriptions/app/controllers/invoices_controller.rb

36 lines
798 B
Ruby
Raw Normal View History

2019-10-27 23:05:58 -04:00
# frozen_string_literal: true
2019-12-03 19:23:45 -05:00
module DiscourseSubscriptions
2019-10-27 23:05:58 -04:00
class InvoicesController < ::ApplicationController
2019-12-03 19:23:45 -05:00
include DiscourseSubscriptions::Stripe
2019-10-27 23:05:58 -04:00
before_action :set_api_key
2019-10-28 20:43:32 -04:00
requires_login
2019-10-27 23:05:58 -04:00
def index
begin
customer = find_customer
if viewing_own_invoices && customer.present?
invoices = ::Stripe::Invoice.list(customer: customer.customer_id)
render_json_dump invoices.data
else
render_json_dump []
end
rescue ::Stripe::InvalidRequestError => e
2019-12-11 17:59:38 -05:00
render_json_error e.message
2019-10-27 23:05:58 -04:00
end
end
private
def viewing_own_invoices
current_user.id == params[:user_id].to_i
end
def find_customer
2019-12-03 19:23:45 -05:00
DiscourseSubscriptions::Customer.find_user(current_user)
2019-10-27 23:05:58 -04:00
end
end
end