From 775db4aa3c28f1d617cad9dbfce24347ab14f91c Mon Sep 17 00:00:00 2001 From: Wilfred Asomani Date: Thu, 31 Aug 2023 11:52:24 +0000 Subject: [PATCH] satellite/payments: filter out draft invoices This change filters the list of invoices sent to the frontend to only contain open or paid invoices. Change-Id: I9ac2640a7587a76b0baf46d941f799e742aa2b3b --- satellite/payments/stripe/invoices.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/satellite/payments/stripe/invoices.go b/satellite/payments/stripe/invoices.go index a1c0b1ef3..22004cdc3 100644 --- a/satellite/payments/stripe/invoices.go +++ b/satellite/payments/stripe/invoices.go @@ -317,12 +317,15 @@ func (invoices *invoices) ListPaged(ctx context.Context, userID uuid.UUID, curso Customer: &customerID, } + // stripe will initially fetch this number of invoices. + // Calling iter.Next() at the end will fetch another batch + // if there's more. params.Limit = stripe.Int64(int64(cursor.Limit)) - params.Single = true if cursor.StartingAfter != "" { page.Previous = true params.StartingAfter = stripe.String(cursor.StartingAfter) } else if cursor.EndingBefore != "" { + page.Next = true params.EndingBefore = stripe.String(cursor.EndingBefore) } @@ -330,6 +333,18 @@ func (invoices *invoices) ListPaged(ctx context.Context, userID uuid.UUID, curso for invoicesIterator.Next() { stripeInvoice := invoicesIterator.Invoice() + if stripeInvoice.Status != stripe.InvoiceStatusOpen && stripeInvoice.Status != stripe.InvoiceStatusPaid { + continue + } + + if len(page.Invoices) == cursor.Limit { + // in this case, cursor.EndingBefore was + // not provided, so we have to check if + // there's a (cursor.Limit+1)th invoice. + page.Next = true + break + } + total := stripeInvoice.Total for _, line := range stripeInvoice.Lines.Data { // If amount is negative, this is a coupon or a credit line item. @@ -348,13 +363,16 @@ func (invoices *invoices) ListPaged(ctx context.Context, userID uuid.UUID, curso Link: stripeInvoice.InvoicePDF, Start: time.Unix(stripeInvoice.PeriodStart, 0), }) + + if page.Next && len(page.Invoices) >= cursor.Limit { + break + } } if err = invoicesIterator.Err(); err != nil { return nil, Error.Wrap(err) } - page.Next = invoicesIterator.Meta().HasMore || cursor.EndingBefore != "" return page, nil }