storj/satellite/payments/stripecoinpayments/invoices.go
Kaloyan Raev 12fa505692 satellite/payments: fix invoice amount in billing history
The billing history currently shows the Total amount from the Stripe
invoice. In fact, this value is just the amount deducted from the Stripe
balance. It does not reflect any deduction from promotional coupon or
bonus credits.

This patch adds the deducted amount form the promotional coupons and
bonus credits to the displayed amount in billing history. This way
customers have better understading of the total amount deducted from
their account balance on the satellite.

Change-Id: Ibd7f611a6cea0143a3059f39dd1d9ef21c264d8c
2020-05-08 11:43:41 +00:00

65 lines
1.5 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package stripecoinpayments
import (
"context"
"time"
"github.com/stripe/stripe-go"
"storj.io/common/uuid"
"storj.io/storj/satellite/payments"
)
// invoices is an implementation of payments.Invoices.
//
// architecture: Service
type invoices struct {
service *Service
}
// List returns a list of invoices for a given payment account.
func (invoices *invoices) List(ctx context.Context, userID uuid.UUID) (invoicesList []payments.Invoice, err error) {
defer mon.Task()(&ctx, userID)(&err)
customerID, err := invoices.service.db.Customers().GetCustomerID(ctx, userID)
if err != nil {
return nil, Error.Wrap(err)
}
params := &stripe.InvoiceListParams{
Customer: &customerID,
}
invoicesIterator := invoices.service.stripeClient.Invoices.List(params)
for invoicesIterator.Next() {
stripeInvoice := invoicesIterator.Invoice()
total := stripeInvoice.Total
for _, line := range stripeInvoice.Lines.Data {
// If amount is negative, this is a coupon or a credit line item.
// Add them to the total.
if line.Amount < 0 {
total -= line.Amount
}
}
invoicesList = append(invoicesList, payments.Invoice{
ID: stripeInvoice.ID,
Description: stripeInvoice.Description,
Amount: total,
Status: string(stripeInvoice.Status),
Link: stripeInvoice.InvoicePDF,
Start: time.Unix(stripeInvoice.PeriodStart, 0),
})
}
if err = invoicesIterator.Err(); err != nil {
return nil, Error.Wrap(err)
}
return invoicesList, nil
}