satellite/payments: add finalize invoice command

Change-Id: Ib74dc8fd352e9576a302f5923439f5e50f69c9f0
This commit is contained in:
stefanbenten 2020-06-09 17:18:36 +02:00 committed by Stefan Benten
parent fdbb2c3ed6
commit dfeb4dc9c2
5 changed files with 37 additions and 3 deletions

2
go.mod
View File

@ -28,7 +28,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.5.1
github.com/stripe/stripe-go v63.1.1+incompatible
github.com/stripe/stripe-go v70.15.0+incompatible
github.com/vivint/infectious v0.0.0-20190108171102-2455b059135b
github.com/zeebo/errs v1.2.2
go.etcd.io/bbolt v1.3.4

4
go.sum
View File

@ -367,8 +367,8 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stripe/stripe-go v63.1.1+incompatible h1:OpGp55VzHcnu5O/MtVVU42xS2xeAWosx3xELvuoMMMw=
github.com/stripe/stripe-go v63.1.1+incompatible/go.mod h1:A1dQZmO/QypXmsL0T8axYZkSN/uA/T/A64pfKdBAMiY=
github.com/stripe/stripe-go v70.15.0+incompatible h1:hNML7M1zx8RgtepEMlxyu/FpVPrP7KZm1gPFQquJQvM=
github.com/stripe/stripe-go v70.15.0+incompatible/go.mod h1:A1dQZmO/QypXmsL0T8axYZkSN/uA/T/A64pfKdBAMiY=
github.com/tidwall/pretty v0.0.0-20180105212114-65a9db5fad51/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=

View File

@ -42,6 +42,7 @@ type StripePaymentMethods interface {
type StripeInvoices interface {
New(params *stripe.InvoiceParams) (*stripe.Invoice, error)
List(listParams *stripe.InvoiceListParams) *invoice.Iter
FinalizeInvoice(id string, params *stripe.InvoiceFinalizeParams) (*stripe.Invoice, error)
}
// StripeInvoiceItems Stripe InvoiceItems interface.

View File

@ -1003,6 +1003,35 @@ func (service *Service) createInvoice(ctx context.Context, cusID string, period
return nil
}
// FinalizeInvoices sets autoadvance flag on all draft invoices currently available in stripe.
func (service *Service) FinalizeInvoices(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
params := &stripe.InvoiceListParams{
Status: stripe.String("draft"),
}
invoicesIterator := service.stripeClient.Invoices().List(params)
for invoicesIterator.Next() {
stripeInvoice := invoicesIterator.Invoice()
err := service.finalizeInvoice(ctx, stripeInvoice.ID)
if err != nil {
return Error.Wrap(err)
}
}
return Error.Wrap(invoicesIterator.Err())
}
func (service *Service) finalizeInvoice(ctx context.Context, invoiceID string) (err error) {
defer mon.Task()(&ctx)(&err)
params := &stripe.InvoiceFinalizeParams{AutoAdvance: stripe.Bool(true)}
_, err = service.stripeClient.Invoices().FinalizeInvoice(invoiceID, params)
return err
}
// projectUsagePrice represents pricing for project usage.
type projectUsagePrice struct {
Storage decimal.Decimal

View File

@ -157,6 +157,10 @@ func (m *mockInvoices) List(listParams *stripe.InvoiceListParams) *invoice.Iter
return &invoice.Iter{Iter: &stripe.Iter{}}
}
func (m *mockInvoices) FinalizeInvoice(id string, params *stripe.InvoiceFinalizeParams) (*stripe.Invoice, error) {
return nil, nil
}
type mockInvoiceItems struct {
}