2019-10-31 16:56:54 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package stripecoinpayments
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2021-06-22 01:09:56 +01:00
|
|
|
"github.com/stripe/stripe-go/v72"
|
2019-10-31 16:56:54 +00:00
|
|
|
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2019-10-31 16:56:54 +00:00
|
|
|
"storj.io/storj/satellite/payments"
|
|
|
|
)
|
|
|
|
|
|
|
|
// invoices is an implementation of payments.Invoices.
|
2019-11-05 13:16:02 +00:00
|
|
|
//
|
2020-01-29 00:57:15 +00:00
|
|
|
// architecture: Service
|
2019-10-31 16:56:54 +00:00
|
|
|
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)
|
|
|
|
|
2019-11-05 13:16:02 +00:00
|
|
|
customerID, err := invoices.service.db.Customers().GetCustomerID(ctx, userID)
|
2019-10-31 16:56:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
params := &stripe.InvoiceListParams{
|
|
|
|
Customer: &customerID,
|
|
|
|
}
|
|
|
|
|
2020-05-15 09:46:41 +01:00
|
|
|
invoicesIterator := invoices.service.stripeClient.Invoices().List(params)
|
2019-10-31 16:56:54 +00:00
|
|
|
for invoicesIterator.Next() {
|
|
|
|
stripeInvoice := invoicesIterator.Invoice()
|
|
|
|
|
2020-05-08 12:14:55 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 16:56:54 +00:00
|
|
|
invoicesList = append(invoicesList, payments.Invoice{
|
|
|
|
ID: stripeInvoice.ID,
|
|
|
|
Description: stripeInvoice.Description,
|
2020-05-08 12:14:55 +01:00
|
|
|
Amount: total,
|
2019-10-31 16:56:54 +00:00
|
|
|
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
|
|
|
|
}
|
2020-07-06 22:31:40 +01:00
|
|
|
|
|
|
|
// CheckPendingItems returns if pending invoice items for a given payment account exist.
|
|
|
|
func (invoices *invoices) CheckPendingItems(ctx context.Context, userID uuid.UUID) (existingItems bool, err error) {
|
|
|
|
defer mon.Task()(&ctx, userID)(&err)
|
|
|
|
|
|
|
|
customerID, err := invoices.service.db.Customers().GetCustomerID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return false, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
params := &stripe.InvoiceItemListParams{
|
|
|
|
Customer: &customerID,
|
|
|
|
Pending: stripe.Bool(true),
|
|
|
|
}
|
|
|
|
|
|
|
|
itemIterator := invoices.service.stripeClient.InvoiceItems().List(params)
|
|
|
|
for itemIterator.Next() {
|
|
|
|
item := itemIterator.InvoiceItem()
|
|
|
|
if item != nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = itemIterator.Err(); err != nil {
|
|
|
|
return false, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|