From 2f19636eb3840aa795f6a4469cf987a20c760c0a Mon Sep 17 00:00:00 2001 From: Jeremy Wharton Date: Thu, 28 Sep 2023 13:21:42 -0500 Subject: [PATCH] go.mod,satellite/payments/stripe: bump stripe-go from v72 to v73 This change updates the version of the stripe-go dependency from v72 to v73. This is part of a process to reach v75, which contains performance improvements. References #6287 Change-Id: I95f132378fe05e506f0388a0fe039cb1c2db58e2 --- go.mod | 2 +- go.sum | 4 +- satellite/console/service.go | 2 +- .../payments/accountfreeze/chore_test.go | 99 ++--- satellite/payments/billing/chore_test.go | 23 +- satellite/payments/stripe/accounts.go | 2 +- satellite/payments/stripe/balances.go | 2 +- satellite/payments/stripe/client.go | 24 +- satellite/payments/stripe/client_test.go | 4 +- satellite/payments/stripe/coupons.go | 2 +- satellite/payments/stripe/creditcards.go | 2 +- satellite/payments/stripe/invoices.go | 2 +- satellite/payments/stripe/invoices_test.go | 82 +--- satellite/payments/stripe/service.go | 38 +- satellite/payments/stripe/service_test.go | 375 ++++++------------ satellite/payments/stripe/stripemock.go | 245 ++++++++---- satellite/payments/stripe/tokens.go | 2 +- satellite/payments/stripe/tokens_test.go | 2 +- testsuite/storjscan/go.mod | 2 +- testsuite/storjscan/go.sum | 4 +- testsuite/ui/go.mod | 2 +- testsuite/ui/go.sum | 4 +- 22 files changed, 413 insertions(+), 511 deletions(-) diff --git a/go.mod b/go.mod index 3ebf2908d..79a39f1c7 100644 --- a/go.mod +++ b/go.mod @@ -38,7 +38,7 @@ require ( github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.7.1 github.com/stretchr/testify v1.8.4 - github.com/stripe/stripe-go/v72 v72.90.0 + github.com/stripe/stripe-go/v73 v73.16.0 github.com/vbauerster/mpb/v8 v8.4.0 github.com/vivint/infectious v0.0.0-20200605153912-25a574ae18a3 github.com/zeebo/assert v1.3.1 diff --git a/go.sum b/go.sum index 86372f0e7..d711483fd 100644 --- a/go.sum +++ b/go.sum @@ -567,8 +567,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/stripe/stripe-go/v72 v72.90.0 h1:fvJ/aL1rHHWRj5buuayb/2ufJued1UR1HEVavsoZoFs= -github.com/stripe/stripe-go/v72 v72.90.0/go.mod h1:QwqJQtduHubZht9mek5sds9CtQcKFdsykV9ZepRWwo0= +github.com/stripe/stripe-go/v73 v73.16.0 h1:X3uTpl3zwY7tSPjcltQJ9t/7TYOgfqT6QQK7Qml995I= +github.com/stripe/stripe-go/v73 v73.16.0/go.mod h1:Uk0oBh96JHdlxRsu0/t8XfuJ3xOUQTUgpKAFZuDcFnQ= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= diff --git a/satellite/console/service.go b/satellite/console/service.go index 2191be6d3..64d11e60f 100644 --- a/satellite/console/service.go +++ b/satellite/console/service.go @@ -18,7 +18,7 @@ import ( "github.com/spacemonkeygo/monkit/v3" "github.com/spf13/pflag" - "github.com/stripe/stripe-go/v72" + "github.com/stripe/stripe-go/v73" "github.com/zeebo/errs" "go.uber.org/zap" "golang.org/x/crypto/bcrypt" diff --git a/satellite/payments/accountfreeze/chore_test.go b/satellite/payments/accountfreeze/chore_test.go index 6157eaf78..72e4d09f1 100644 --- a/satellite/payments/accountfreeze/chore_test.go +++ b/satellite/payments/accountfreeze/chore_test.go @@ -8,7 +8,7 @@ import ( "time" "github.com/stretchr/testify/require" - "github.com/stripe/stripe-go/v72" + "github.com/stripe/stripe-go/v73" "go.uber.org/zap" "storj.io/common/currency" @@ -59,7 +59,7 @@ func TestAutoFreezeChore(t *testing.T) { t.Run("No freeze event for paid invoice", func(t *testing.T) { // AnalyticsMock tests that events are sent once. service.TestChangeFreezeTracker(newFreezeTrackerMock(t)) - item, err := stripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + _, err := stripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ Params: stripe.Params{Context: ctx}, Amount: &amount, Currency: &curr, @@ -67,16 +67,9 @@ func TestAutoFreezeChore(t *testing.T) { }) require.NoError(t, err) - items := make([]*stripe.InvoiceUpcomingInvoiceItemParams, 0, 1) - items = append(items, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &item.ID, - Amount: &amount, - Currency: &curr, - }) inv, err := stripeClient.Invoices().New(&stripe.InvoiceParams{ - Params: stripe.Params{Context: ctx}, - Customer: &cus1, - InvoiceItems: items, + Params: stripe.Params{Context: ctx}, + Customer: &cus1, }) require.NoError(t, err) @@ -119,24 +112,18 @@ func TestAutoFreezeChore(t *testing.T) { // reset chore clock chore.TestSetNow(time.Now) - item, err := stripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + inv, err := stripeClient.Invoices().New(&stripe.InvoiceParams{ Params: stripe.Params{Context: ctx}, - Amount: &amount, - Currency: &curr, Customer: &cus1, }) require.NoError(t, err) - items := make([]*stripe.InvoiceUpcomingInvoiceItemParams, 0, 1) - items = append(items, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &item.ID, - Amount: &amount, - Currency: &curr, - }) - inv, err := stripeClient.Invoices().New(&stripe.InvoiceParams{ - Params: stripe.Params{Context: ctx}, - Customer: &cus1, - InvoiceItems: items, + _, err = stripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + Params: stripe.Params{Context: ctx}, + Amount: &amount, + Currency: &curr, + Customer: &cus1, + Invoice: &inv.ID, }) require.NoError(t, err) @@ -191,25 +178,19 @@ func TestAutoFreezeChore(t *testing.T) { // reset chore clock chore.TestSetNow(time.Now) - item, err := stripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + inv, err := stripeClient.Invoices().New(&stripe.InvoiceParams{ + Params: stripe.Params{Context: ctx}, + Customer: &cus1, + DefaultPaymentMethod: stripe.String(stripe1.MockInvoicesPaySuccess), + }) + require.NoError(t, err) + + _, err = stripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ Params: stripe.Params{Context: ctx}, Amount: &amount, Currency: &curr, Customer: &cus1, - }) - require.NoError(t, err) - - items := make([]*stripe.InvoiceUpcomingInvoiceItemParams, 0, 1) - items = append(items, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &item.ID, - Amount: &amount, - Currency: &curr, - }) - inv, err := stripeClient.Invoices().New(&stripe.InvoiceParams{ - Params: stripe.Params{Context: ctx}, - Customer: &cus1, - InvoiceItems: items, - DefaultPaymentMethod: stripe.String(stripe1.MockInvoicesPaySuccess), + Invoice: &inv.ID, }) require.NoError(t, err) @@ -250,24 +231,18 @@ func TestAutoFreezeChore(t *testing.T) { cus2, err := customerDB.GetCustomerID(ctx, user2.ID) require.NoError(t, err) - item, err := stripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + inv, err := stripeClient.Invoices().New(&stripe.InvoiceParams{ Params: stripe.Params{Context: ctx}, - Amount: &amount, - Currency: &curr, Customer: &cus2, }) require.NoError(t, err) - items := make([]*stripe.InvoiceUpcomingInvoiceItemParams, 0, 1) - items = append(items, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &item.ID, - Amount: &amount, - Currency: &curr, - }) - inv, err := stripeClient.Invoices().New(&stripe.InvoiceParams{ - Params: stripe.Params{Context: ctx}, - Customer: &cus2, - InvoiceItems: items, + _, err = stripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + Params: stripe.Params{Context: ctx}, + Amount: &amount, + Currency: &curr, + Customer: &cus2, + Invoice: &inv.ID, }) require.NoError(t, err) @@ -381,24 +356,18 @@ func TestAutoFreezeChore_StorjscanExclusion(t *testing.T) { storjscanCus, err := customerDB.GetCustomerID(ctx, storjscanUser.ID) require.NoError(t, err) - item, err := stripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + inv, err := stripeClient.Invoices().New(&stripe.InvoiceParams{ Params: stripe.Params{Context: ctx}, - Amount: &amount, - Currency: &curr, Customer: &storjscanCus, }) require.NoError(t, err) - items := make([]*stripe.InvoiceUpcomingInvoiceItemParams, 0, 1) - items = append(items, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &item.ID, - Amount: &amount, - Currency: &curr, - }) - inv, err := stripeClient.Invoices().New(&stripe.InvoiceParams{ - Params: stripe.Params{Context: ctx}, - Customer: &storjscanCus, - InvoiceItems: items, + _, err = stripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + Params: stripe.Params{Context: ctx}, + Amount: &amount, + Currency: &curr, + Customer: &storjscanCus, + Invoice: &inv.ID, }) require.NoError(t, err) diff --git a/satellite/payments/billing/chore_test.go b/satellite/payments/billing/chore_test.go index efa1cd801..c53ccedcc 100644 --- a/satellite/payments/billing/chore_test.go +++ b/satellite/payments/billing/chore_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/stripe/stripe-go/v72" + "github.com/stripe/stripe-go/v73" "github.com/zeebo/errs" "go.uber.org/zap/zaptest" @@ -298,25 +298,18 @@ func TestChore_PayInvoiceObserver(t *testing.T) { defer ctx.Check(chore.Close) // create invoice - item, err := stripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + inv, err := stripeClient.Invoices().New(&stripe.InvoiceParams{ Params: stripe.Params{Context: ctx}, - Amount: &amount, - Currency: stripe.String(string(stripe.CurrencyUSD)), Customer: &cus, }) require.NoError(t, err) - fullAmount := amount + amount2 - items := make([]*stripe.InvoiceUpcomingInvoiceItemParams, 0, 1) - items = append(items, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &item.ID, - Amount: &fullAmount, - Currency: stripe.String(string(stripe.CurrencyUSD)), - }) - inv, err := stripeClient.Invoices().New(&stripe.InvoiceParams{ - Params: stripe.Params{Context: ctx}, - Customer: &cus, - InvoiceItems: items, + _, err = stripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + Params: stripe.Params{Context: ctx}, + Amount: stripe.Int64(amount + amount2), + Currency: stripe.String(string(stripe.CurrencyUSD)), + Customer: &cus, + Invoice: &inv.ID, }) require.NoError(t, err) diff --git a/satellite/payments/stripe/accounts.go b/satellite/payments/stripe/accounts.go index e2f431317..b1ab86df0 100644 --- a/satellite/payments/stripe/accounts.go +++ b/satellite/payments/stripe/accounts.go @@ -7,7 +7,7 @@ import ( "context" "time" - "github.com/stripe/stripe-go/v72" + "github.com/stripe/stripe-go/v73" "github.com/zeebo/errs" "storj.io/common/uuid" diff --git a/satellite/payments/stripe/balances.go b/satellite/payments/stripe/balances.go index cbf190fa1..a92c205b6 100644 --- a/satellite/payments/stripe/balances.go +++ b/satellite/payments/stripe/balances.go @@ -7,7 +7,7 @@ import ( "context" "github.com/shopspring/decimal" - "github.com/stripe/stripe-go/v72" + "github.com/stripe/stripe-go/v73" "storj.io/common/uuid" "storj.io/storj/satellite/payments" diff --git a/satellite/payments/stripe/client.go b/satellite/payments/stripe/client.go index 3c461f930..2c47192c6 100644 --- a/satellite/payments/stripe/client.go +++ b/satellite/payments/stripe/client.go @@ -12,16 +12,16 @@ import ( "net/http" "time" - "github.com/stripe/stripe-go/v72" - "github.com/stripe/stripe-go/v72/charge" - "github.com/stripe/stripe-go/v72/client" - "github.com/stripe/stripe-go/v72/customer" - "github.com/stripe/stripe-go/v72/customerbalancetransaction" - "github.com/stripe/stripe-go/v72/form" - "github.com/stripe/stripe-go/v72/invoice" - "github.com/stripe/stripe-go/v72/invoiceitem" - "github.com/stripe/stripe-go/v72/paymentmethod" - "github.com/stripe/stripe-go/v72/promotioncode" + "github.com/stripe/stripe-go/v73" + "github.com/stripe/stripe-go/v73/charge" + "github.com/stripe/stripe-go/v73/client" + "github.com/stripe/stripe-go/v73/customer" + "github.com/stripe/stripe-go/v73/customerbalancetransaction" + "github.com/stripe/stripe-go/v73/form" + "github.com/stripe/stripe-go/v73/invoice" + "github.com/stripe/stripe-go/v73/invoiceitem" + "github.com/stripe/stripe-go/v73/paymentmethod" + "github.com/stripe/stripe-go/v73/promotioncode" "go.uber.org/zap" "storj.io/common/time2" @@ -60,12 +60,12 @@ type Invoices interface { New(params *stripe.InvoiceParams) (*stripe.Invoice, error) List(listParams *stripe.InvoiceListParams) *invoice.Iter Update(id string, params *stripe.InvoiceParams) (*stripe.Invoice, error) - FinalizeInvoice(id string, params *stripe.InvoiceFinalizeParams) (*stripe.Invoice, error) + FinalizeInvoice(id string, params *stripe.InvoiceFinalizeInvoiceParams) (*stripe.Invoice, error) Pay(id string, params *stripe.InvoicePayParams) (*stripe.Invoice, error) Del(id string, params *stripe.InvoiceParams) (*stripe.Invoice, error) Get(id string, params *stripe.InvoiceParams) (*stripe.Invoice, error) MarkUncollectible(id string, params *stripe.InvoiceMarkUncollectibleParams) (*stripe.Invoice, error) - VoidInvoice(id string, params *stripe.InvoiceVoidParams) (*stripe.Invoice, error) + VoidInvoice(id string, params *stripe.InvoiceVoidInvoiceParams) (*stripe.Invoice, error) } // InvoiceItems Stripe InvoiceItems interface. diff --git a/satellite/payments/stripe/client_test.go b/satellite/payments/stripe/client_test.go index 854b554c2..b38ff3d4c 100644 --- a/satellite/payments/stripe/client_test.go +++ b/satellite/payments/stripe/client_test.go @@ -11,8 +11,8 @@ import ( "time" "github.com/stretchr/testify/require" - "github.com/stripe/stripe-go/v72" - "github.com/stripe/stripe-go/v72/form" + "github.com/stripe/stripe-go/v73" + "github.com/stripe/stripe-go/v73/form" "go.uber.org/zap/zaptest" "storj.io/common/testcontext" diff --git a/satellite/payments/stripe/coupons.go b/satellite/payments/stripe/coupons.go index b662ec9bb..eb81c90b2 100644 --- a/satellite/payments/stripe/coupons.go +++ b/satellite/payments/stripe/coupons.go @@ -7,7 +7,7 @@ import ( "context" "time" - "github.com/stripe/stripe-go/v72" + "github.com/stripe/stripe-go/v73" "storj.io/common/uuid" "storj.io/storj/satellite/payments" diff --git a/satellite/payments/stripe/creditcards.go b/satellite/payments/stripe/creditcards.go index d2b614e9d..766498516 100644 --- a/satellite/payments/stripe/creditcards.go +++ b/satellite/payments/stripe/creditcards.go @@ -7,7 +7,7 @@ import ( "context" "strings" - "github.com/stripe/stripe-go/v72" + "github.com/stripe/stripe-go/v73" "github.com/zeebo/errs" "storj.io/common/uuid" diff --git a/satellite/payments/stripe/invoices.go b/satellite/payments/stripe/invoices.go index 22004cdc3..d39024a77 100644 --- a/satellite/payments/stripe/invoices.go +++ b/satellite/payments/stripe/invoices.go @@ -7,7 +7,7 @@ import ( "context" "time" - "github.com/stripe/stripe-go/v72" + "github.com/stripe/stripe-go/v73" "github.com/zeebo/errs" "go.uber.org/zap" diff --git a/satellite/payments/stripe/invoices_test.go b/satellite/payments/stripe/invoices_test.go index 3c2b9da66..7b62a97b8 100644 --- a/satellite/payments/stripe/invoices_test.go +++ b/satellite/payments/stripe/invoices_test.go @@ -8,7 +8,7 @@ import ( "time" "github.com/stretchr/testify/require" - "github.com/stripe/stripe-go/v72" + "github.com/stripe/stripe-go/v73" "storj.io/common/currency" "storj.io/common/testcontext" @@ -92,69 +92,10 @@ func TestPayOverdueInvoices(t *testing.T) { customer, err := satellite.DB.StripeCoinPayments().Customers().GetCustomerID(ctx, user.ID) require.NoError(t, err) - amount1 := int64(75) - amount2 := int64(100) - curr := string(stripe.CurrencyUSD) - - // create invoice items for first invoice - inv1Item1, err := satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ - Params: stripe.Params{Context: ctx}, - Amount: &amount1, - Currency: &curr, - Customer: &customer, - }) - require.NoError(t, err) - inv1Item2, err := satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ - Params: stripe.Params{Context: ctx}, - Amount: &amount1, - Currency: &curr, - Customer: &customer, - }) - require.NoError(t, err) - Inv1Items := make([]*stripe.InvoiceUpcomingInvoiceItemParams, 0, 2) - Inv1Items = append(Inv1Items, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &inv1Item1.ID, - Amount: &amount1, - Currency: &curr, - }) - Inv1Items = append(Inv1Items, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &inv1Item2.ID, - Amount: &amount1, - Currency: &curr, - }) - - // invoice items for second invoice - inv2Item1, err := satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ - Params: stripe.Params{Context: ctx}, - Amount: &amount2, - Currency: &curr, - Customer: &customer, - }) - require.NoError(t, err) - inv2Item2, err := satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ - Params: stripe.Params{Context: ctx}, - Amount: &amount2, - Currency: &curr, - Customer: &customer, - }) - require.NoError(t, err) - Inv2Items := make([]*stripe.InvoiceUpcomingInvoiceItemParams, 0, 2) - Inv2Items = append(Inv2Items, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &inv2Item1.ID, - Amount: &amount2, - Currency: &curr, - }) - Inv2Items = append(Inv2Items, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &inv2Item2.ID, - Amount: &amount2, - Currency: &curr, - }) - // create invoice one inv1, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ Params: stripe.Params{Context: ctx}, Customer: &customer, - InvoiceItems: Inv1Items, DefaultPaymentMethod: stripe.String(stripe1.MockInvoicesPaySuccess), }) require.NoError(t, err) @@ -163,12 +104,29 @@ func TestPayOverdueInvoices(t *testing.T) { inv2, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ Params: stripe.Params{Context: ctx}, Customer: &customer, - InvoiceItems: Inv2Items, DefaultPaymentMethod: stripe.String(stripe1.MockInvoicesPaySuccess), }) require.NoError(t, err) - finalizeParams := &stripe.InvoiceFinalizeParams{Params: stripe.Params{Context: ctx}} + for _, info := range []struct { + invID string + amount int64 + }{ + {inv1.ID, 75}, {inv2.ID, 100}, + } { + for i := 0; i < 2; i++ { + _, err = satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + Params: stripe.Params{Context: ctx}, + Amount: stripe.Int64(info.amount), + Currency: stripe.String(string(stripe.CurrencyUSD)), + Customer: &customer, + Invoice: stripe.String(info.invID), + }) + require.NoError(t, err) + } + } + + finalizeParams := &stripe.InvoiceFinalizeInvoiceParams{Params: stripe.Params{Context: ctx}} // finalize invoice one inv1, err = satellite.API.Payments.StripeClient.Invoices().FinalizeInvoice(inv1.ID, finalizeParams) diff --git a/satellite/payments/stripe/service.go b/satellite/payments/stripe/service.go index 37ef2f44f..1be0a203e 100644 --- a/satellite/payments/stripe/service.go +++ b/satellite/payments/stripe/service.go @@ -17,7 +17,7 @@ import ( "github.com/shopspring/decimal" "github.com/spacemonkeygo/monkit/v3" - "github.com/stripe/stripe-go/v72" + "github.com/stripe/stripe-go/v73" "github.com/zeebo/errs" "go.uber.org/zap" @@ -781,23 +781,35 @@ func (service *Service) CreateInvoices(ctx context.Context, period time.Time) (e func (service *Service) createInvoice(ctx context.Context, cusID string, period time.Time) (stripeInvoice *stripe.Invoice, err error) { defer mon.Task()(&ctx)(&err) + itemsIter := service.stripeClient.InvoiceItems().List(&stripe.InvoiceItemListParams{ + Customer: &cusID, + Pending: stripe.Bool(true), + ListParams: stripe.ListParams{ + Context: ctx, + Limit: stripe.Int64(1), + }, + }) + + hasItems := itemsIter.Next() + if err = itemsIter.Err(); err != nil { + return nil, err + } + if !hasItems { + return nil, nil + } + description := fmt.Sprintf("Storj DCS Cloud Storage for %s %d", period.Month(), period.Year()) stripeInvoice, err = service.stripeClient.Invoices().New( &stripe.InvoiceParams{ - Params: stripe.Params{Context: ctx}, - Customer: stripe.String(cusID), - AutoAdvance: stripe.Bool(service.AutoAdvance), - Description: stripe.String(description), + Params: stripe.Params{Context: ctx}, + Customer: stripe.String(cusID), + AutoAdvance: stripe.Bool(service.AutoAdvance), + Description: stripe.String(description), + PendingInvoiceItemsBehavior: stripe.String("include"), }, ) if err != nil { - var stripErr *stripe.Error - if errors.As(err, &stripErr) { - if stripErr.Code == stripe.ErrorCodeInvoiceNoCustomerLineItems { - return stripeInvoice, nil - } - } return nil, err } @@ -880,7 +892,7 @@ func (service *Service) SetInvoiceStatus(ctx context.Context, startPeriod, endPe err = service.iterateInvoicesInTimeRange(ctx, startPeriod, endPeriod, func(invoiceId string) error { service.log.Info("updating invoice status to void", zap.String("invoiceId", invoiceId)) if !dryRun { - _, err = service.stripeClient.Invoices().VoidInvoice(invoiceId, &stripe.InvoiceVoidParams{}) + _, err = service.stripeClient.Invoices().VoidInvoice(invoiceId, &stripe.InvoiceVoidInvoiceParams{}) if err != nil { return Error.Wrap(err) } @@ -1071,7 +1083,7 @@ func (service *Service) FinalizeInvoices(ctx context.Context) (err error) { func (service *Service) finalizeInvoice(ctx context.Context, invoiceID string) (err error) { defer mon.Task()(&ctx)(&err) - params := &stripe.InvoiceFinalizeParams{ + params := &stripe.InvoiceFinalizeInvoiceParams{ Params: stripe.Params{Context: ctx}, AutoAdvance: stripe.Bool(false), } diff --git a/satellite/payments/stripe/service_test.go b/satellite/payments/stripe/service_test.go index 264c5d572..de343a791 100644 --- a/satellite/payments/stripe/service_test.go +++ b/satellite/payments/stripe/service_test.go @@ -14,7 +14,7 @@ import ( "github.com/shopspring/decimal" "github.com/stretchr/testify/require" - "github.com/stripe/stripe-go/v72" + "github.com/stripe/stripe-go/v73" "go.uber.org/zap" "storj.io/common/currency" @@ -59,31 +59,24 @@ func TestService_SetInvoiceStatusUncollectible(t *testing.T) { customer, err := satellite.DB.StripeCoinPayments().Customers().GetCustomerID(ctx, user.ID) require.NoError(t, err) - // create invoice item - invItem, err := satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + // create invoice + inv, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ Params: stripe.Params{Context: ctx}, - Amount: stripe.Int64(invoiceBalance.BaseUnits()), - Currency: stripe.String(usdCurrency), Customer: &customer, }) require.NoError(t, err) - InvItems := make([]*stripe.InvoiceUpcomingInvoiceItemParams, 0, 1) - InvItems = append(InvItems, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &invItem.ID, - Amount: &invItem.Amount, - Currency: stripe.String(usdCurrency), - }) - - // create invoice - inv, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ - Params: stripe.Params{Context: ctx}, - Customer: &customer, - InvoiceItems: InvItems, + // create invoice item + _, err = satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + Params: stripe.Params{Context: ctx}, + Amount: stripe.Int64(invoiceBalance.BaseUnits()), + Currency: stripe.String(usdCurrency), + Customer: &customer, + Invoice: &inv.ID, }) require.NoError(t, err) - finalizeParams := &stripe.InvoiceFinalizeParams{Params: stripe.Params{Context: ctx}} + finalizeParams := &stripe.InvoiceFinalizeInvoiceParams{Params: stripe.Params{Context: ctx}} // finalize invoice inv, err = satellite.API.Payments.StripeClient.Invoices().FinalizeInvoice(inv.ID, finalizeParams) @@ -132,31 +125,24 @@ func TestService_SetInvoiceStatusVoid(t *testing.T) { customer, err := satellite.DB.StripeCoinPayments().Customers().GetCustomerID(ctx, user.ID) require.NoError(t, err) - // create invoice item - invItem, err := satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + // create invoice + inv, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ Params: stripe.Params{Context: ctx}, - Amount: stripe.Int64(invoiceBalance.BaseUnits()), - Currency: stripe.String(usdCurrency), Customer: &customer, }) require.NoError(t, err) - InvItems := make([]*stripe.InvoiceUpcomingInvoiceItemParams, 0, 1) - InvItems = append(InvItems, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &invItem.ID, - Amount: &invItem.Amount, - Currency: stripe.String(usdCurrency), - }) - - // create invoice - inv, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ - Params: stripe.Params{Context: ctx}, - Customer: &customer, - InvoiceItems: InvItems, + // create invoice item + _, err = satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + Params: stripe.Params{Context: ctx}, + Amount: stripe.Int64(invoiceBalance.BaseUnits()), + Currency: stripe.String(usdCurrency), + Customer: &customer, + Invoice: &inv.ID, }) require.NoError(t, err) - finalizeParams := &stripe.InvoiceFinalizeParams{Params: stripe.Params{Context: ctx}} + finalizeParams := &stripe.InvoiceFinalizeInvoiceParams{Params: stripe.Params{Context: ctx}} // finalize invoice inv, err = satellite.API.Payments.StripeClient.Invoices().FinalizeInvoice(inv.ID, finalizeParams) @@ -205,31 +191,24 @@ func TestService_SetInvoiceStatusPaid(t *testing.T) { customer, err := satellite.DB.StripeCoinPayments().Customers().GetCustomerID(ctx, user.ID) require.NoError(t, err) - // create invoice item - invItem, err := satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + // create invoice + inv, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ Params: stripe.Params{Context: ctx}, - Amount: stripe.Int64(invoiceBalance.BaseUnits()), - Currency: stripe.String(usdCurrency), Customer: &customer, }) require.NoError(t, err) - InvItems := make([]*stripe.InvoiceUpcomingInvoiceItemParams, 0, 1) - InvItems = append(InvItems, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &invItem.ID, - Amount: &invItem.Amount, - Currency: stripe.String(usdCurrency), - }) - - // create invoice - inv, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ - Params: stripe.Params{Context: ctx}, - Customer: &customer, - InvoiceItems: InvItems, + // create invoice item + _, err = satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + Params: stripe.Params{Context: ctx}, + Amount: stripe.Int64(invoiceBalance.BaseUnits()), + Currency: stripe.String(usdCurrency), + Customer: &customer, + Invoice: &inv.ID, }) require.NoError(t, err) - finalizeParams := &stripe.InvoiceFinalizeParams{Params: stripe.Params{Context: ctx}} + finalizeParams := &stripe.InvoiceFinalizeInvoiceParams{Params: stripe.Params{Context: ctx}} // finalize invoice inv, err = satellite.API.Payments.StripeClient.Invoices().FinalizeInvoice(inv.ID, finalizeParams) @@ -278,31 +257,24 @@ func TestService_SetInvoiceStatusInvalid(t *testing.T) { customer, err := satellite.DB.StripeCoinPayments().Customers().GetCustomerID(ctx, user.ID) require.NoError(t, err) - // create invoice item - invItem, err := satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + // create invoice + inv, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ Params: stripe.Params{Context: ctx}, - Amount: stripe.Int64(invoiceBalance.BaseUnits()), - Currency: stripe.String(usdCurrency), Customer: &customer, }) require.NoError(t, err) - InvItems := make([]*stripe.InvoiceUpcomingInvoiceItemParams, 0, 1) - InvItems = append(InvItems, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &invItem.ID, - Amount: &invItem.Amount, - Currency: stripe.String(usdCurrency), - }) - - // create invoice - inv, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ - Params: stripe.Params{Context: ctx}, - Customer: &customer, - InvoiceItems: InvItems, + // create invoice item + _, err = satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + Params: stripe.Params{Context: ctx}, + Amount: stripe.Int64(invoiceBalance.BaseUnits()), + Currency: stripe.String(usdCurrency), + Customer: &customer, + Invoice: &inv.ID, }) require.NoError(t, err) - finalizeParams := &stripe.InvoiceFinalizeParams{Params: stripe.Params{Context: ctx}} + finalizeParams := &stripe.InvoiceFinalizeInvoiceParams{Params: stripe.Params{Context: ctx}} // finalize invoice inv, err = satellite.API.Payments.StripeClient.Invoices().FinalizeInvoice(inv.ID, finalizeParams) @@ -617,27 +589,20 @@ func TestService_FinalizeInvoices(t *testing.T) { customer, err := satellite.DB.StripeCoinPayments().Customers().GetCustomerID(ctx, user.ID) require.NoError(t, err) - // create invoice item - invItem, err := satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + // create invoice + inv, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ Params: stripe.Params{Context: ctx}, - Amount: stripe.Int64(1000), - Currency: stripe.String(string(stripe.CurrencyUSD)), Customer: &customer, }) require.NoError(t, err) - InvItems := make([]*stripe.InvoiceUpcomingInvoiceItemParams, 0, 1) - InvItems = append(InvItems, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &invItem.ID, - Amount: &invItem.Amount, - Currency: stripe.String(string(stripe.CurrencyUSD)), - }) - - // create invoice - _, err = satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ - Params: stripe.Params{Context: ctx}, - Customer: &customer, - InvoiceItems: InvItems, + // create invoice item + _, err = satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + Params: stripe.Params{Context: ctx}, + Amount: stripe.Int64(1000), + Currency: stripe.String(string(stripe.CurrencyUSD)), + Customer: &customer, + Invoice: &inv.ID, }) require.NoError(t, err) @@ -874,31 +839,24 @@ func TestService_PayInvoiceFromTokenBalance(t *testing.T) { customer, err := satellite.DB.StripeCoinPayments().Customers().GetCustomerID(ctx, user.ID) require.NoError(t, err) - // create invoice item - invItem, err := satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + // create invoice + inv, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ Params: stripe.Params{Context: ctx}, - Amount: stripe.Int64(invoiceBalance.BaseUnits()), - Currency: stripe.String(usdCurrency), Customer: &customer, }) require.NoError(t, err) - InvItems := make([]*stripe.InvoiceUpcomingInvoiceItemParams, 0, 1) - InvItems = append(InvItems, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &invItem.ID, - Amount: &invItem.Amount, - Currency: stripe.String(usdCurrency), - }) - - // create invoice - inv, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ - Params: stripe.Params{Context: ctx}, - Customer: &customer, - InvoiceItems: InvItems, + // create invoice item + _, err = satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + Params: stripe.Params{Context: ctx}, + Amount: stripe.Int64(invoiceBalance.BaseUnits()), + Currency: stripe.String(usdCurrency), + Customer: &customer, + Invoice: &inv.ID, }) require.NoError(t, err) - finalizeParams := &stripe.InvoiceFinalizeParams{Params: stripe.Params{Context: ctx}} + finalizeParams := &stripe.InvoiceFinalizeInvoiceParams{Params: stripe.Params{Context: ctx}} // finalize invoice inv, err = satellite.API.Payments.StripeClient.Invoices().FinalizeInvoice(inv.ID, finalizeParams) @@ -961,81 +919,40 @@ func TestService_PayMultipleInvoiceFromTokenBalance(t *testing.T) { customer, err := satellite.DB.StripeCoinPayments().Customers().GetCustomerID(ctx, user.ID) require.NoError(t, err) - amount1 := int64(75) - amount2 := int64(100) - curr := string(stripe.CurrencyUSD) - - // create invoice items for first invoice - inv1Item1, err := satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ - Params: stripe.Params{Context: ctx}, - Amount: &amount1, - Currency: &curr, - Customer: &customer, - }) - require.NoError(t, err) - inv1Item2, err := satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ - Params: stripe.Params{Context: ctx}, - Amount: &amount1, - Currency: &curr, - Customer: &customer, - }) - require.NoError(t, err) - Inv1Items := make([]*stripe.InvoiceUpcomingInvoiceItemParams, 0, 2) - Inv1Items = append(Inv1Items, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &inv1Item1.ID, - Amount: &amount1, - Currency: &curr, - }) - Inv1Items = append(Inv1Items, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &inv1Item2.ID, - Amount: &amount1, - Currency: &curr, - }) - - // invoice items for second invoice - inv2Item1, err := satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ - Params: stripe.Params{Context: ctx}, - Amount: &amount2, - Currency: &curr, - Customer: &customer, - }) - require.NoError(t, err) - inv2Item2, err := satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ - Params: stripe.Params{Context: ctx}, - Amount: &amount2, - Currency: &curr, - Customer: &customer, - }) - require.NoError(t, err) - Inv2Items := make([]*stripe.InvoiceUpcomingInvoiceItemParams, 0, 2) - Inv2Items = append(Inv2Items, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &inv2Item1.ID, - Amount: &amount2, - Currency: &curr, - }) - Inv2Items = append(Inv2Items, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &inv2Item2.ID, - Amount: &amount2, - Currency: &curr, - }) - // create invoice one inv1, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ - Params: stripe.Params{Context: ctx}, - Customer: &customer, - InvoiceItems: Inv1Items, + Params: stripe.Params{Context: ctx}, + Customer: &customer, }) require.NoError(t, err) // create invoice two inv2, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ - Params: stripe.Params{Context: ctx}, - Customer: &customer, - InvoiceItems: Inv2Items, + Params: stripe.Params{Context: ctx}, + Customer: &customer, }) require.NoError(t, err) - finalizeParams := &stripe.InvoiceFinalizeParams{Params: stripe.Params{Context: ctx}} + // create invoice items + for _, info := range []struct { + invID string + amount int64 + }{ + {inv1.ID, 75}, {inv2.ID, 100}, + } { + for i := 0; i < 2; i++ { + _, err = satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + Params: stripe.Params{Context: ctx}, + Amount: stripe.Int64(info.amount), + Currency: stripe.String(string(stripe.CurrencyUSD)), + Customer: &customer, + Invoice: stripe.String(info.invID), + }) + require.NoError(t, err) + } + } + + finalizeParams := &stripe.InvoiceFinalizeInvoiceParams{Params: stripe.Params{Context: ctx}} // finalize invoice one inv1, err = satellite.API.Payments.StripeClient.Invoices().FinalizeInvoice(inv1.ID, finalizeParams) @@ -1107,69 +1024,10 @@ func TestService_PayMultipleInvoiceForCustomer(t *testing.T) { customer, err := satellite.DB.StripeCoinPayments().Customers().GetCustomerID(ctx, user.ID) require.NoError(t, err) - amount1 := int64(75) - amount2 := int64(100) - curr := string(stripe.CurrencyUSD) - - // create invoice items for first invoice - inv1Item1, err := satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ - Params: stripe.Params{Context: ctx}, - Amount: &amount1, - Currency: &curr, - Customer: &customer, - }) - require.NoError(t, err) - inv1Item2, err := satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ - Params: stripe.Params{Context: ctx}, - Amount: &amount1, - Currency: &curr, - Customer: &customer, - }) - require.NoError(t, err) - Inv1Items := make([]*stripe.InvoiceUpcomingInvoiceItemParams, 0, 2) - Inv1Items = append(Inv1Items, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &inv1Item1.ID, - Amount: &amount1, - Currency: &curr, - }) - Inv1Items = append(Inv1Items, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &inv1Item2.ID, - Amount: &amount1, - Currency: &curr, - }) - - // invoice items for second invoice - inv2Item1, err := satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ - Params: stripe.Params{Context: ctx}, - Amount: &amount2, - Currency: &curr, - Customer: &customer, - }) - require.NoError(t, err) - inv2Item2, err := satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ - Params: stripe.Params{Context: ctx}, - Amount: &amount2, - Currency: &curr, - Customer: &customer, - }) - require.NoError(t, err) - Inv2Items := make([]*stripe.InvoiceUpcomingInvoiceItemParams, 0, 2) - Inv2Items = append(Inv2Items, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &inv2Item1.ID, - Amount: &amount2, - Currency: &curr, - }) - Inv2Items = append(Inv2Items, &stripe.InvoiceUpcomingInvoiceItemParams{ - InvoiceItem: &inv2Item2.ID, - Amount: &amount2, - Currency: &curr, - }) - // create invoice one inv1, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ Params: stripe.Params{Context: ctx}, Customer: &customer, - InvoiceItems: Inv1Items, DefaultPaymentMethod: stripe.String(stripe1.MockInvoicesPaySuccess), }) require.NoError(t, err) @@ -1178,12 +1036,30 @@ func TestService_PayMultipleInvoiceForCustomer(t *testing.T) { inv2, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ Params: stripe.Params{Context: ctx}, Customer: &customer, - InvoiceItems: Inv2Items, DefaultPaymentMethod: stripe.String(stripe1.MockInvoicesPaySuccess), }) require.NoError(t, err) - finalizeParams := &stripe.InvoiceFinalizeParams{Params: stripe.Params{Context: ctx}} + // create invoice items + for _, info := range []struct { + invID string + amount int64 + }{ + {inv1.ID, 75}, {inv2.ID, 100}, + } { + for i := 0; i < 2; i++ { + _, err = satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + Params: stripe.Params{Context: ctx}, + Amount: stripe.Int64(info.amount), + Currency: stripe.String(string(stripe.CurrencyUSD)), + Customer: &customer, + Invoice: stripe.String(info.invID), + }) + require.NoError(t, err) + } + } + + finalizeParams := &stripe.InvoiceFinalizeInvoiceParams{Params: stripe.Params{Context: ctx}} // finalize invoice one inv1, err = satellite.API.Payments.StripeClient.Invoices().FinalizeInvoice(inv1.ID, finalizeParams) @@ -1239,14 +1115,22 @@ func TestService_PayMultipleInvoiceForCustomer(t *testing.T) { require.Zero(t, balance.BaseUnits()) // create another invoice - _, err = satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ + inv3, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ Params: stripe.Params{Context: ctx}, Customer: &customer, - InvoiceItems: Inv2Items, DefaultPaymentMethod: stripe.String(stripe1.MockInvoicesPaySuccess), }) require.NoError(t, err) + _, err = satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + Params: stripe.Params{Context: ctx}, + Amount: stripe.Int64(100), + Currency: stripe.String(string(stripe.CurrencyUSD)), + Customer: &customer, + Invoice: stripe.String(inv3.ID), + }) + require.NoError(t, err) + err = satellite.API.Payments.StripeService.FinalizeInvoices(ctx) require.NoError(t, err) @@ -1358,6 +1242,7 @@ func TestService_GenerateInvoice(t *testing.T) { // expect an invoice or invoice items. if !testCase.expectInvoice { require.False(t, hasInvoice, "expected no invoice but got one") + require.Nil(t, invoice, "expected no invoice but got one") require.Empty(t, invoiceItems, "not expecting any invoice items") return } @@ -1365,11 +1250,12 @@ func TestService_GenerateInvoice(t *testing.T) { // Otherwise, we expect one or more line items that have been // associated with the newly created invoice. require.True(t, hasInvoice, "expected invoice but did not get one") + require.NotNil(t, invoice, "expected invoice but did not get one") require.NotZero(t, len(invoiceItems), "expecting one or more invoice items") for _, item := range invoiceItems { require.Contains(t, item.Metadata, "projectID") require.Equal(t, item.Metadata["projectID"], proj.ID.String()) - require.NotNil(t, invoice, item.Invoice) + require.NotNil(t, item.Invoice) require.Equal(t, invoice.ID, item.Invoice.ID) } }) @@ -1528,24 +1414,17 @@ func TestPayInvoicesSkipDue(t *testing.T) { cus1 := "cus_1" cus2 := "cus_2" - amount := int64(100) - curr := string(stripe.CurrencyUSD) due := time.Now().Add(14 * 24 * time.Hour).Unix() - _, err := satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ - Params: stripe.Params{Context: ctx}, - Amount: &amount, - Currency: &curr, - Customer: &cus1, - }) - require.NoError(t, err) - _, err = satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ - Params: stripe.Params{Context: ctx}, - Amount: &amount, - Currency: &curr, - Customer: &cus2, - }) - require.NoError(t, err) + for _, cusID := range []string{cus1, cus2} { + _, err := satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{ + Params: stripe.Params{Context: ctx}, + Amount: stripe.Int64(100), + Currency: stripe.String(string(stripe.CurrencyUSD)), + Customer: stripe.String(cusID), + }) + require.NoError(t, err) + } inv, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{ Params: stripe.Params{Context: ctx}, @@ -1553,7 +1432,7 @@ func TestPayInvoicesSkipDue(t *testing.T) { }) require.NoError(t, err) - finalizeParams := &stripe.InvoiceFinalizeParams{Params: stripe.Params{Context: ctx}} + finalizeParams := &stripe.InvoiceFinalizeInvoiceParams{Params: stripe.Params{Context: ctx}} inv, err = satellite.API.Payments.StripeClient.Invoices().FinalizeInvoice(inv.ID, finalizeParams) require.NoError(t, err) diff --git a/satellite/payments/stripe/stripemock.go b/satellite/payments/stripe/stripemock.go index a01e184ac..d6a923a0e 100644 --- a/satellite/payments/stripe/stripemock.go +++ b/satellite/payments/stripe/stripemock.go @@ -10,15 +10,15 @@ import ( "sync" "time" - "github.com/stripe/stripe-go/v72" - "github.com/stripe/stripe-go/v72/charge" - "github.com/stripe/stripe-go/v72/customer" - "github.com/stripe/stripe-go/v72/customerbalancetransaction" - "github.com/stripe/stripe-go/v72/form" - "github.com/stripe/stripe-go/v72/invoice" - "github.com/stripe/stripe-go/v72/invoiceitem" - "github.com/stripe/stripe-go/v72/paymentmethod" - "github.com/stripe/stripe-go/v72/promotioncode" + "github.com/stripe/stripe-go/v73" + "github.com/stripe/stripe-go/v73/charge" + "github.com/stripe/stripe-go/v73/customer" + "github.com/stripe/stripe-go/v73/customerbalancetransaction" + "github.com/stripe/stripe-go/v73/form" + "github.com/stripe/stripe-go/v73/invoice" + "github.com/stripe/stripe-go/v73/invoiceitem" + "github.com/stripe/stripe-go/v73/paymentmethod" + "github.com/stripe/stripe-go/v73/promotioncode" "storj.io/common/testrand" "storj.io/common/uuid" @@ -520,7 +520,7 @@ func (m *mockInvoices) MarkUncollectible(id string, params *stripe.InvoiceMarkUn return nil, errors.New("invoice not found") } -func (m *mockInvoices) VoidInvoice(id string, params *stripe.InvoiceVoidParams) (*stripe.Invoice, error) { +func (m *mockInvoices) VoidInvoice(id string, params *stripe.InvoiceVoidInvoiceParams) (*stripe.Invoice, error) { for _, invoices := range m.invoices { for _, invoice := range invoices { if invoice.ID == id { @@ -545,10 +545,19 @@ func (m *mockInvoices) New(params *stripe.InvoiceParams) (*stripe.Invoice, error m.root.mu.Lock() defer m.root.mu.Unlock() - items, ok := m.invoiceItems.items[*params.Customer] - if !ok || len(items) == 0 { - if params.PendingInvoiceItemsBehavior == nil || *params.PendingInvoiceItemsBehavior != "exclude" { - return nil, &stripe.Error{Code: stripe.ErrorCodeInvoiceNoCustomerLineItems} + var invoiceItems []*stripe.InvoiceItem + if params.PendingInvoiceItemsBehavior != nil { + switch *params.PendingInvoiceItemsBehavior { + case "include": + for _, item := range m.invoiceItems.items[*params.Customer] { + if item.Invoice == nil { + invoiceItems = append(invoiceItems, item) + } + } + case "exclude": + break + default: + return nil, &stripe.Error{} } } @@ -558,13 +567,13 @@ func (m *mockInvoices) New(params *stripe.InvoiceParams) (*stripe.Invoice, error } amountDue := int64(0) - lineData := make([]*stripe.InvoiceLine, 0, len(params.InvoiceItems)) - for _, item := range params.InvoiceItems { - lineData = append(lineData, &stripe.InvoiceLine{ - InvoiceItem: *item.InvoiceItem, - Amount: *item.Amount, + lineData := make([]*stripe.InvoiceLineItem, 0, len(invoiceItems)) + for _, item := range invoiceItems { + lineData = append(lineData, &stripe.InvoiceLineItem{ + InvoiceItem: item.ID, + Amount: item.Amount, }) - amountDue += *item.Amount + amountDue += item.Amount } var desc string @@ -581,7 +590,7 @@ func (m *mockInvoices) New(params *stripe.InvoiceParams) (*stripe.Invoice, error DueDate: due, Status: stripe.InvoiceStatusDraft, Description: desc, - Lines: &stripe.InvoiceLineList{ + Lines: &stripe.InvoiceLineItemList{ Data: lineData, }, AmountDue: amountDue, @@ -593,10 +602,8 @@ func (m *mockInvoices) New(params *stripe.InvoiceParams) (*stripe.Invoice, error } m.invoices[*params.Customer] = append(m.invoices[*params.Customer], invoice) - for _, item := range items { - if item.Invoice == nil { - item.Invoice = invoice - } + for _, item := range invoiceItems { + item.Invoice = invoice } return invoice, nil @@ -661,7 +668,7 @@ func (m *mockInvoices) Update(id string, params *stripe.InvoiceParams) (invoice } // FinalizeInvoice forwards the invoice's status from draft to open. -func (m *mockInvoices) FinalizeInvoice(id string, params *stripe.InvoiceFinalizeParams) (*stripe.Invoice, error) { +func (m *mockInvoices) FinalizeInvoice(id string, params *stripe.InvoiceFinalizeInvoiceParams) (*stripe.Invoice, error) { for _, invoices := range m.invoices { for _, invoice := range invoices { if invoice.ID == id && invoice.Status == stripe.InvoiceStatusDraft { @@ -724,23 +731,6 @@ func (m *mockInvoices) Get(id string, params *stripe.InvoiceParams) (*stripe.Inv for _, invoices := range m.invoices { for _, inv := range invoices { if inv.ID == id { - items, ok := m.invoiceItems.items[inv.Customer.ID] - if ok { - amountDue := int64(0) - lineData := make([]*stripe.InvoiceLine, 0, len(params.InvoiceItems)) - for _, item := range items { - if item.Invoice != inv { - continue - } - lineData = append(lineData, &stripe.InvoiceLine{ - InvoiceItem: item.ID, - Amount: item.Amount, - }) - amountDue += item.Amount - } - inv.Lines.Data = lineData - inv.Total = amountDue - } return inv, nil } } @@ -772,20 +762,69 @@ func (m *mockInvoiceItems) New(params *stripe.InvoiceItemParams) (*stripe.Invoic m.root.mu.Lock() defer m.root.mu.Unlock() + if params.Customer == nil { + return nil, &stripe.Error{Code: stripe.ErrorCodeParameterMissing} + } + item := &stripe.InvoiceItem{ + ID: "ii_" + string(testrand.RandAlphaNumeric(25)), Metadata: params.Metadata, } + if params.Description != nil { item.Description = *params.Description } - if params.UnitAmountDecimal != nil { - item.UnitAmountDecimal = *params.UnitAmountDecimal - } - if params.UnitAmount != nil { - item.UnitAmount = *params.UnitAmount - } - if params.Amount != nil { + + if params.Quantity != nil { + item.Quantity = *params.Quantity + if params.UnitAmount != nil { + if params.UnitAmountDecimal != nil { + return nil, &stripe.Error{} + } + item.UnitAmount = *params.UnitAmount + item.Amount = item.UnitAmount * item.Quantity + } else if params.UnitAmountDecimal != nil { + item.UnitAmountDecimal = *params.UnitAmountDecimal + item.Amount = int64(*params.UnitAmountDecimal * float64(item.Quantity)) + } else { + return nil, &stripe.Error{Code: stripe.ErrorCodeParameterMissing} + } + } else if params.Amount != nil { + if params.UnitAmount != nil || params.UnitAmountDecimal != nil { + return nil, &stripe.Error{} + } item.Amount = *params.Amount + } else { + return nil, &stripe.Error{Code: stripe.ErrorCodeParameterMissing} + } + + if params.Invoice != nil { + for _, invoices := range m.root.invoices.invoices { + if item.Invoice != nil { + break + } + for _, inv := range invoices { + if inv.ID == *params.Invoice { + if inv.Status != stripe.InvoiceStatusDraft { + return nil, &stripe.Error{Code: stripe.ErrorCodeInvoiceNotEditable} + } + item.Invoice = inv + + inv.AmountDue += item.Amount + inv.AmountRemaining += item.Amount + inv.Total += item.Amount + inv.Lines.Data = append(inv.Lines.Data, &stripe.InvoiceLineItem{ + InvoiceItem: item.ID, + Amount: item.Amount, + }) + + break + } + } + } + if item.Invoice == nil { + return nil, &stripe.Error{Code: stripe.ErrorCodeResourceMissing} + } } m.items[*params.Customer] = append(m.items[*params.Customer], item) @@ -796,23 +835,20 @@ func (m *mockInvoiceItems) List(listParams *stripe.InvoiceItemListParams) *invoi m.root.mu.Lock() defer m.root.mu.Unlock() + var ret []interface{} + for _, item := range m.items[*listParams.Customer] { + if listParams.Pending == nil || (*listParams.Pending && item.Invoice == nil) || (!*listParams.Pending && item.Invoice != nil) { + ret = append(ret, item) + } + } + listMeta := &stripe.ListMeta{ HasMore: false, - TotalCount: uint32(len(m.items)), + TotalCount: uint32(len(ret)), } lc := newListContainer(listMeta) query := stripe.Query(func(*stripe.Params, *form.Values) ([]interface{}, stripe.ListContainer, error) { - list, ok := m.items[*listParams.Customer] - if !ok { - list = []*stripe.InvoiceItem{} - } - ret := make([]interface{}, len(list)) - - for i, v := range list { - ret[i] = v - } - return ret, lc, nil }) return &invoiceitem.Iter{Iter: stripe.GetIter(nil, query)} @@ -929,8 +965,6 @@ func (m *mockPromoCodes) List(params *stripe.PromotionCodeListParams) *promotion type mockCreditNotes struct { root *mockStripeState - - CreditNotes map[string]*stripe.CreditNote } func newMockCreditNotes(root *mockStripeState) *mockCreditNotes { @@ -943,25 +977,82 @@ func (m mockCreditNotes) New(params *stripe.CreditNoteParams) (*stripe.CreditNot m.root.mu.Lock() defer m.root.mu.Unlock() - item := &stripe.CreditNote{} + if params.Invoice == nil || len(params.Lines) == 0 { + return nil, &stripe.Error{Code: stripe.ErrorCodeParameterMissing} + } - if params.Invoice != nil { - item.ID = *params.Invoice - } - if params.Memo != nil { - item.Memo = *params.Memo - } + var invoice *stripe.Invoice for _, invoices := range m.root.invoices.invoices { - for _, invoice := range invoices { - if invoice.ID == *params.Invoice { - invoice.AmountRemaining -= *params.Lines[0].UnitAmount - invoice.AmountDue -= *params.Lines[0].UnitAmount - invoice.Lines.Data[0].Amount -= *params.Lines[0].UnitAmount - if invoice.AmountRemaining <= 0 { - invoice.Status = stripe.InvoiceStatusPaid + if invoice != nil { + break + } + for _, inv := range invoices { + if inv.ID == *params.Invoice { + if inv.Status != stripe.InvoiceStatusOpen { + // The Stripe API supports adding credit notes to paid invoices, + // but we don't need to support that in the mock right now + return nil, &stripe.Error{} } + invoice = inv + break } } } + if invoice == nil { + return nil, &stripe.Error{Code: stripe.ErrorCodeResourceMissing} + } + + totalAmount := invoice.AmountDue + totalDiscount := int64(0) + lines := make([]*stripe.CreditNoteLineItem, 0, len(params.Lines)) + for _, paramLine := range params.Lines { + if paramLine.Type == nil || paramLine.Quantity == nil || paramLine.UnitAmount == nil || paramLine.Description == nil { + return nil, &stripe.Error{Code: stripe.ErrorCodeParameterMissing} + } + if *paramLine.Type != string(stripe.CreditNoteLineItemTypeCustomLineItem) { + // Other types are unsupported in the mock at this time + return nil, &stripe.Error{} + } + if *paramLine.Quantity < 1 || *paramLine.UnitAmount < 1 { + return nil, &stripe.Error{Code: stripe.ErrorCodeParameterInvalidInteger} + } + + discount := *paramLine.Quantity * *paramLine.UnitAmount + totalDiscount += discount + if totalDiscount > totalAmount { + return nil, &stripe.Error{} + } + + line := &stripe.CreditNoteLineItem{ + ID: "crli_" + string(testrand.RandAlphaNumeric(25)), + Type: stripe.CreditNoteLineItemTypeCustomLineItem, + Quantity: *paramLine.Quantity, + UnitAmount: *paramLine.UnitAmount, + Description: *paramLine.Description, + Amount: discount, + } + + lines = append(lines, line) + } + + invoice.AmountDue -= totalDiscount + invoice.AmountRemaining = invoice.AmountDue + if invoice.AmountRemaining == 0 { + invoice.Status = stripe.InvoiceStatusPaid + } + + item := &stripe.CreditNote{ + ID: "cr_" + string(testrand.RandAlphaNumeric(25)), + Invoice: invoice, + Amount: totalDiscount, + Lines: &stripe.CreditNoteLineItemList{ + Data: lines, + }, + } + + if params.Memo != nil { + item.Memo = *params.Memo + } + return item, nil } diff --git a/satellite/payments/stripe/tokens.go b/satellite/payments/stripe/tokens.go index 977411e53..5a1d3be91 100644 --- a/satellite/payments/stripe/tokens.go +++ b/satellite/payments/stripe/tokens.go @@ -10,7 +10,7 @@ import ( "strings" "time" - "github.com/stripe/stripe-go/v72" + "github.com/stripe/stripe-go/v73" "go.uber.org/zap" "storj.io/common/uuid" diff --git a/satellite/payments/stripe/tokens_test.go b/satellite/payments/stripe/tokens_test.go index 4a0d3b7ff..06da2be80 100644 --- a/satellite/payments/stripe/tokens_test.go +++ b/satellite/payments/stripe/tokens_test.go @@ -10,7 +10,7 @@ import ( "time" "github.com/stretchr/testify/require" - "github.com/stripe/stripe-go/v72" + "github.com/stripe/stripe-go/v73" "storj.io/common/memory" "storj.io/common/testcontext" diff --git a/testsuite/storjscan/go.mod b/testsuite/storjscan/go.mod index d30c9f9d9..5cb0e7e7e 100644 --- a/testsuite/storjscan/go.mod +++ b/testsuite/storjscan/go.mod @@ -122,7 +122,7 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/viper v1.7.1 // indirect github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4 // indirect - github.com/stripe/stripe-go/v72 v72.90.0 // indirect + github.com/stripe/stripe-go/v73 v73.16.0 // indirect github.com/subosito/gotenv v1.2.0 // indirect github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect github.com/tklauser/go-sysconf v0.3.5 // indirect diff --git a/testsuite/storjscan/go.sum b/testsuite/storjscan/go.sum index daafa6f49..e89e2ad1b 100644 --- a/testsuite/storjscan/go.sum +++ b/testsuite/storjscan/go.sum @@ -759,8 +759,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/stripe/stripe-go/v72 v72.90.0 h1:fvJ/aL1rHHWRj5buuayb/2ufJued1UR1HEVavsoZoFs= -github.com/stripe/stripe-go/v72 v72.90.0/go.mod h1:QwqJQtduHubZht9mek5sds9CtQcKFdsykV9ZepRWwo0= +github.com/stripe/stripe-go/v73 v73.16.0 h1:X3uTpl3zwY7tSPjcltQJ9t/7TYOgfqT6QQK7Qml995I= +github.com/stripe/stripe-go/v73 v73.16.0/go.mod h1:Uk0oBh96JHdlxRsu0/t8XfuJ3xOUQTUgpKAFZuDcFnQ= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/syndtr/goleveldb v1.0.1-0.20210305035536-64b5b1c73954/go.mod h1:u2MKkTVTVJWe5D1rCvame8WqhBd88EuIwODJZ1VHCPM= diff --git a/testsuite/ui/go.mod b/testsuite/ui/go.mod index 9e6e3fc2b..a0155d6ee 100644 --- a/testsuite/ui/go.mod +++ b/testsuite/ui/go.mod @@ -173,7 +173,7 @@ require ( github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/viper v1.7.1 // indirect github.com/streadway/amqp v1.0.0 // indirect - github.com/stripe/stripe-go/v72 v72.90.0 // indirect + github.com/stripe/stripe-go/v73 v73.16.0 // indirect github.com/subosito/gotenv v1.2.0 // indirect github.com/tidwall/gjson v1.9.3 // indirect github.com/tidwall/match v1.1.1 // indirect diff --git a/testsuite/ui/go.sum b/testsuite/ui/go.sum index 87f9d22ed..1270798cb 100644 --- a/testsuite/ui/go.sum +++ b/testsuite/ui/go.sum @@ -1112,8 +1112,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/stripe/stripe-go/v72 v72.90.0 h1:fvJ/aL1rHHWRj5buuayb/2ufJued1UR1HEVavsoZoFs= -github.com/stripe/stripe-go/v72 v72.90.0/go.mod h1:QwqJQtduHubZht9mek5sds9CtQcKFdsykV9ZepRWwo0= +github.com/stripe/stripe-go/v73 v73.16.0 h1:X3uTpl3zwY7tSPjcltQJ9t/7TYOgfqT6QQK7Qml995I= +github.com/stripe/stripe-go/v73 v73.16.0/go.mod h1:Uk0oBh96JHdlxRsu0/t8XfuJ3xOUQTUgpKAFZuDcFnQ= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=