satellite/payments/stripecoinpayments: skip paying invoices with due date

When running pay invoices command skip invoices that has due date set, so
customers can still pay it themselves until due date.

https://github.com/storj/storj/issues/5453

Change-Id: I8e557062491ab0c8246b28bc5ca57e845eb32e29
This commit is contained in:
Yaroslav Vorobiov 2023-01-20 21:56:12 +01:00
parent 95960572b3
commit 3639c5ee1e
3 changed files with 77 additions and 1 deletions

View File

@ -745,6 +745,13 @@ func (service *Service) PayInvoices(ctx context.Context, createdOnAfter time.Tim
invoicesIterator := service.stripeClient.Invoices().List(params)
for invoicesIterator.Next() {
stripeInvoice := invoicesIterator.Invoice()
if stripeInvoice.DueDate > 0 {
service.log.Info("Skipping invoice marked for manual payment",
zap.String("id", stripeInvoice.ID),
zap.String("number", stripeInvoice.Number),
zap.String("customer", stripeInvoice.Customer.ID))
continue
}
params := &stripe.InvoicePayParams{}
_, err = service.stripeClient.Invoices().Pay(stripeInvoice.ID, params)

View File

@ -579,3 +579,55 @@ func TestProjectUsagePrice(t *testing.T) {
}
})
}
func TestPayInvoicesSkipDue(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 0,
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
satellite := planet.Satellites[0]
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{
Amount: &amount,
Currency: &curr,
Customer: &cus1,
})
require.NoError(t, err)
_, err = satellite.API.Payments.StripeClient.InvoiceItems().New(&stripe.InvoiceItemParams{
Amount: &amount,
Currency: &curr,
Customer: &cus2,
})
require.NoError(t, err)
inv, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{
Customer: &cus1,
})
require.NoError(t, err)
invWithDue, err := satellite.API.Payments.StripeClient.Invoices().New(&stripe.InvoiceParams{
Customer: &cus2,
DueDate: &due,
})
require.NoError(t, err)
err = satellite.API.Payments.StripeService.PayInvoices(ctx, time.Time{})
require.NoError(t, err)
iter := satellite.API.Payments.StripeClient.Invoices().List(&stripe.InvoiceListParams{})
for iter.Next() {
i := iter.Invoice()
if i.ID == inv.ID {
require.Equal(t, stripe.InvoiceStatusPaid, i.Status)
}
// when due date is set invoice should not be paid
if i.ID == invWithDue.ID {
require.Equal(t, stripe.InvoiceStatus(""), i.Status)
}
}
})
}

View File

@ -444,7 +444,16 @@ func (m *mockInvoices) New(params *stripe.InvoiceParams) (*stripe.Invoice, error
return nil, &stripe.Error{Code: stripe.ErrorCodeInvoiceNoCustomerLineItems}
}
invoice := &stripe.Invoice{ID: "in_" + string(testrand.RandAlphaNumeric(25))}
due := int64(0)
if params.DueDate != nil {
due = *params.DueDate
}
invoice := &stripe.Invoice{
ID: "in_" + string(testrand.RandAlphaNumeric(25)),
Customer: &stripe.Customer{ID: *params.Customer},
DueDate: due,
}
m.invoices[*params.Customer] = append(m.invoices[*params.Customer], invoice)
for _, item := range items {
if item.Invoice == nil {
@ -500,6 +509,14 @@ func (m *mockInvoices) FinalizeInvoice(id string, params *stripe.InvoiceFinalize
}
func (m *mockInvoices) Pay(id string, params *stripe.InvoicePayParams) (*stripe.Invoice, error) {
for _, invoices := range m.invoices {
for i, invoice := range invoices {
if invoice.ID == id {
m.invoices[invoice.Customer.ID][i].Status = stripe.InvoiceStatusPaid
return invoice, nil
}
}
}
return nil, nil
}