FIX: Couldn't dig the payments
This commit is contained in:
parent
029232e881
commit
c30daa1b86
|
@ -9,21 +9,31 @@ module DiscourseSubscriptions
|
||||||
|
|
||||||
def index
|
def index
|
||||||
begin
|
begin
|
||||||
customer = Customer.find_by(user_id: current_user.id)
|
customer = Customer.where(user_id: current_user.id)
|
||||||
|
customer_ids = customer.map { |c| c.customer_id } if customer
|
||||||
product_ids = Product.all.pluck(:external_id)
|
product_ids = Product.all.pluck(:external_id)
|
||||||
|
|
||||||
data = []
|
data = []
|
||||||
|
|
||||||
if customer.present? && product_ids.present?
|
if customer_ids.present? && product_ids.present?
|
||||||
# lots of matching because the Stripe API doesn't make it easy to match products => payments except from invoices
|
customer_ids.each do |customer_id|
|
||||||
all_invoices = ::Stripe::Invoice.list(customer: customer[:customer_id])
|
# lots of matching because the Stripe API doesn't make it easy to match products => payments except from invoices
|
||||||
invoices_with_products = all_invoices[:data].select { |invoice| product_ids.include?(invoice.dig(:lines, :data, 0, :plan, :product)) }
|
all_invoices = ::Stripe::Invoice.list(customer: customer_id)
|
||||||
invoice_ids = invoices_with_products.map { |invoice| invoice[:id] }
|
invoices_with_products = all_invoices[:data].select do |invoice|
|
||||||
payments = ::Stripe::PaymentIntent.list(customer: customer[:customer_id])
|
# i cannot dig it so we must get iffy with it
|
||||||
payments_from_invoices = payments[:data].select { |payment| invoice_ids.include?(payment[:invoice]) }
|
if invoice[:lines] && invoice[:lines][:data] && invoice[:lines][:data][0] && invoice[:lines][:data][0][:plan] && invoice[:lines][:data][0][:plan][:product]
|
||||||
data = payments_from_invoices
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
data = data.sort_by { |pmt| pmt[:created] }.reverse
|
||||||
|
|
||||||
render_json_dump data
|
render_json_dump data
|
||||||
|
|
||||||
rescue ::Stripe::InvalidRequestError => e
|
rescue ::Stripe::InvalidRequestError => e
|
||||||
|
|
|
@ -45,8 +45,14 @@ module DiscourseSubscriptions
|
||||||
customer: 'c_345678',
|
customer: 'c_345678',
|
||||||
).returns(
|
).returns(
|
||||||
data: [
|
data: [
|
||||||
{ invoice: "inv_900007" },
|
{
|
||||||
{ invoice: "inv_007" }
|
invoice: "inv_900007",
|
||||||
|
created: Time.now
|
||||||
|
},
|
||||||
|
{
|
||||||
|
invoice: "inv_007",
|
||||||
|
created: Time.now
|
||||||
|
}
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -61,7 +61,8 @@ module DiscourseSubscriptions
|
||||||
|
|
||||||
it "gets subscriptions" do
|
it "gets subscriptions" do
|
||||||
::Stripe::Plan.expects(:list).with(
|
::Stripe::Plan.expects(:list).with(
|
||||||
expand: ['data.product']
|
expand: ['data.product'],
|
||||||
|
limit: 100
|
||||||
).returns(plans)
|
).returns(plans)
|
||||||
|
|
||||||
::Stripe::Customer.expects(:list).with(
|
::Stripe::Customer.expects(:list).with(
|
||||||
|
|
Loading…
Reference in New Issue