storj/satellite/payments/stripecoinpayments/invoices.go
Michal Niewrzal 2eb2c25e51 satellite/payments/stripecoinpayments: add StripeClient interface to be
able to cover more testing scenarios

Currently, its hard to implement test suite for payments because
mockpayments is on to high level and we cannot emulate many things e.g.
adding credit card. This change is first to be able to add mock for
Stripe client and do more granular tests.

Change-Id: Ied85d4bd0642debdffe1161657c1e475202e9d23
2020-05-15 10:52:44 +02: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
}