2019-12-17 16:31:58 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseSubscriptions
|
|
|
|
module User
|
|
|
|
class PaymentsController < ::ApplicationController
|
|
|
|
include DiscourseSubscriptions::Stripe
|
|
|
|
before_action :set_api_key
|
|
|
|
requires_login
|
|
|
|
|
|
|
|
def index
|
|
|
|
begin
|
2020-06-09 12:07:46 -05:00
|
|
|
customer = Customer.where(user_id: current_user.id)
|
|
|
|
customer_ids = customer.map { |c| c.customer_id } if customer
|
2020-05-22 11:20:05 -05:00
|
|
|
product_ids = Product.all.pluck(:external_id)
|
2019-12-17 16:31:58 +11:00
|
|
|
|
|
|
|
data = []
|
|
|
|
|
2020-06-09 12:07:46 -05:00
|
|
|
if customer_ids.present? && product_ids.present?
|
|
|
|
customer_ids.each do |customer_id|
|
|
|
|
# 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_id)
|
2020-06-09 12:37:46 -05:00
|
|
|
invoices_with_products = all_invoices[:data].select do |invoice|
|
2020-06-09 12:07:46 -05:00
|
|
|
# i cannot dig it so we must get iffy with it
|
|
|
|
if invoice[:lines] && invoice[:lines][:data] && invoice[:lines][:data][0] && invoice[:lines][:data][0][:plan] && invoice[:lines][:data][0][:plan][:product]
|
|
|
|
product_ids.include?(invoice[:lines][:data][0][:plan][:product])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
invoice_ids = invoices_with_products.map { |invoice| invoice[:id] }
|
|
|
|
payments = ::Stripe::PaymentIntent.list(customer: customer_id)
|
|
|
|
payments_from_invoices = payments[:data].select { |payment| invoice_ids.include?(payment[:invoice]) }
|
|
|
|
data.concat(payments_from_invoices)
|
|
|
|
end
|
2019-12-17 16:31:58 +11:00
|
|
|
end
|
|
|
|
|
2020-06-09 12:07:46 -05:00
|
|
|
data = data.sort_by { |pmt| pmt[:created] }.reverse
|
|
|
|
|
2019-12-17 16:31:58 +11:00
|
|
|
render_json_dump data
|
|
|
|
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
|
|
render_json_error e.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|