satellite/payments/stripecoinpayments/service.go: add date to pay invoices command
Add an extra parameter to the pay-invoices command that can be used to restrict which invoices will have a payment attempted in stripe. The parameter should be of the form MM/DD/YYY and any invoices created on or after the date will have token balances applied and be processed for payment according to stripe subscriptions settings. Change-Id: I5da5070d3ac97f45c05c02f2849254bdc44413c3
This commit is contained in:
parent
3d6c3e31f1
commit
85f9dad225
@ -793,12 +793,17 @@ func cmdFinalizeCustomerInvoices(cmd *cobra.Command, args []string) (err error)
|
||||
func cmdPayCustomerInvoices(cmd *cobra.Command, args []string) (err error) {
|
||||
ctx, _ := process.Ctx(cmd)
|
||||
|
||||
t, err := time.Parse("02/01/2006", args[0])
|
||||
if err != nil {
|
||||
return errs.New("invalid date specified specified: %v", err)
|
||||
}
|
||||
|
||||
return runBillingCmd(ctx, func(ctx context.Context, payments *stripecoinpayments.Service, _ satellite.DB) error {
|
||||
err := payments.InvoiceApplyTokenBalance(ctx)
|
||||
err := payments.InvoiceApplyTokenBalance(ctx, t)
|
||||
if err != nil {
|
||||
return errs.New("error applying native token payments: %v", err)
|
||||
}
|
||||
return payments.PayInvoices(ctx)
|
||||
return payments.PayInvoices(ctx, t)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -542,9 +542,9 @@ func (service *Service) InvoiceApplyProjectRecords(ctx context.Context, period t
|
||||
return nil
|
||||
}
|
||||
|
||||
// InvoiceApplyTokenBalance iterates through customer storjscan wallets and creates invoice line items
|
||||
// for stripe customer.
|
||||
func (service *Service) InvoiceApplyTokenBalance(ctx context.Context) (err error) {
|
||||
// InvoiceApplyTokenBalance iterates through customer storjscan wallets and creates invoice credit notes
|
||||
// for stripe customers with invoices on or after the given date.
|
||||
func (service *Service) InvoiceApplyTokenBalance(ctx context.Context, createdOnAfter time.Time) (err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
// get all wallet entries
|
||||
@ -574,7 +574,7 @@ func (service *Service) InvoiceApplyTokenBalance(ctx context.Context) (err error
|
||||
errGrp.Add(Error.New("unable to get stripe customer ID for user ID %s", wallet.UserID.String()))
|
||||
continue
|
||||
}
|
||||
invoices, err := service.getInvoices(ctx, cusID)
|
||||
invoices, err := service.getInvoices(ctx, cusID, createdOnAfter)
|
||||
if err != nil {
|
||||
errGrp.Add(Error.New("unable to get invoice balance for stripe customer ID %s", cusID))
|
||||
continue
|
||||
@ -629,14 +629,15 @@ func (service *Service) InvoiceApplyTokenBalance(ctx context.Context) (err error
|
||||
return errGrp.Err()
|
||||
}
|
||||
|
||||
// getInvoices returns the stripe customer's open finalized invoices.
|
||||
func (service *Service) getInvoices(ctx context.Context, cusID string) (_ []stripe.Invoice, err error) {
|
||||
// getInvoices returns the stripe customer's open finalized invoices created on or after the given date.
|
||||
func (service *Service) getInvoices(ctx context.Context, cusID string, createdOnAfter time.Time) (_ []stripe.Invoice, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
params := &stripe.InvoiceListParams{
|
||||
Customer: stripe.String(cusID),
|
||||
Status: stripe.String(string(stripe.InvoiceStatusOpen)),
|
||||
}
|
||||
params.Filters.AddFilter("created", "gte", strconv.FormatInt(createdOnAfter.Unix(), 10))
|
||||
invoicesIterator := service.stripeClient.Invoices().List(params)
|
||||
var stripeInvoices []stripe.Invoice
|
||||
for invoicesIterator.Next() {
|
||||
@ -988,13 +989,15 @@ func (service *Service) finalizeInvoice(ctx context.Context, invoiceID string) (
|
||||
return err
|
||||
}
|
||||
|
||||
// PayInvoices attempts to transition all open finalized invoices to "paid" by charging the customer according to subscriptions settings.
|
||||
func (service *Service) PayInvoices(ctx context.Context) (err error) {
|
||||
// PayInvoices attempts to transition all open finalized invoices created on or after a certain time to "paid"
|
||||
// by charging the customer according to subscriptions settings.
|
||||
func (service *Service) PayInvoices(ctx context.Context, createdOnAfter time.Time) (err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
params := &stripe.InvoiceListParams{
|
||||
Status: stripe.String("open"),
|
||||
}
|
||||
params.Filters.AddFilter("created", "gte", strconv.FormatInt(createdOnAfter.Unix(), 10))
|
||||
|
||||
var errGrp errs.Group
|
||||
|
||||
|
@ -339,7 +339,7 @@ func TestService_InvoiceItemsFromZeroTokenBalance(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// run apply token balance to see if there are no unexpected errors
|
||||
err = payments.StripeService.InvoiceApplyTokenBalance(ctx)
|
||||
err = payments.StripeService.InvoiceApplyTokenBalance(ctx, time.Time{})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user