Skip to content

Commit 3d37ed6

Browse files
authored
feat(billing): store payment processing entered at ts on invoice (#3645)
1 parent cbc5845 commit 3d37ed6

18 files changed

+452
-13
lines changed

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,6 +2007,8 @@ github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
20072007
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
20082008
github.com/oklog/ulid/v2 v2.1.1 h1:suPZ4ARWLOJLegGFiZZ1dFAkqzhMjL3J1TzI+5wHz8s=
20092009
github.com/oklog/ulid/v2 v2.1.1/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
2010+
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
2011+
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
20102012
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852 h1:Yl0tPBa8QPjGmesFh1D0rDy+q1Twx6FyU7VWHi8wZbI=
20112013
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852/go.mod h1:eqOVx5Vwu4gd2mmMZvVZsgIqNSaW3xxRThUJ0k/TPk4=
20122014
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=

openmeter/billing/adapter/invoice.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ func (a *adapter) UpdateInvoice(ctx context.Context, in billing.UpdateInvoiceAda
457457
SetOrClearDescription(in.Description).
458458
SetOrClearDueAt(convert.SafeToUTC(in.DueAt)).
459459
SetOrClearCollectionAt(convert.SafeToUTC(in.CollectionAt)).
460+
SetOrClearPaymentProcessingEnteredAt(convert.SafeToUTC(in.PaymentProcessingEnteredAt)).
460461
SetOrClearDraftUntil(convert.SafeToUTC(in.DraftUntil)).
461462
SetOrClearIssuedAt(convert.SafeToUTC(in.IssuedAt)).
462463
SetOrClearDeletedAt(convert.SafeToUTC(in.DeletedAt)).
@@ -679,7 +680,8 @@ func (a *adapter) mapInvoiceBaseFromDB(ctx context.Context, invoice *db.BillingI
679680
UpdatedAt: invoice.UpdatedAt.In(time.UTC),
680681
DeletedAt: convert.TimePtrIn(invoice.DeletedAt, time.UTC),
681682

682-
CollectionAt: lo.ToPtr(invoice.CollectionAt.In(time.UTC)),
683+
CollectionAt: lo.ToPtr(invoice.CollectionAt.In(time.UTC)),
684+
PaymentProcessingEnteredAt: convert.TimePtrIn(invoice.PaymentProcessingEnteredAt, time.UTC),
683685

684686
ExternalIDs: billing.InvoiceExternalIDs{
685687
Invoicing: lo.FromPtr(invoice.InvoicingAppExternalID),

openmeter/billing/invoice.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ type InvoiceBase struct {
282282
QuantitySnapshotedAt *time.Time `json:"quantitySnapshotedAt,omitempty"`
283283

284284
CollectionAt *time.Time `json:"collectionAt,omitempty"`
285+
// PaymentProcessingEnteredAt stores when the invoice first entered payment processing
286+
PaymentProcessingEnteredAt *time.Time `json:"paymentProcessingEnteredAt,omitempty"`
285287

286288
// Customer is either a snapshot of the contact information of the customer at the time of invoice being sent
287289
// or the data from the customer entity (draft state)

openmeter/billing/service/invoicestate.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,16 @@ func allocateStateMachine() *InvoiceStateMachine {
5252
return fmt.Errorf("invalid state type: %v", state)
5353
}
5454

55+
previousStatus := out.Invoice.Status
5556
out.Invoice.Status = invState
5657

58+
if invState == billing.InvoiceStatusPaymentProcessingPending &&
59+
previousStatus != billing.InvoiceStatusPaymentProcessingPending &&
60+
out.Invoice.PaymentProcessingEnteredAt == nil {
61+
now := clock.Now().UTC()
62+
out.Invoice.PaymentProcessingEnteredAt = &now
63+
}
64+
5765
sd, err := out.StatusDetails(ctx)
5866
if err != nil {
5967
return err
@@ -366,6 +374,7 @@ func (m *InvoiceStateMachine) calculateAvailableActionDetails(ctx context.Contex
366374

367375
originalState := m.Invoice.Status
368376
originalValidationErrors := m.Invoice.ValidationIssues
377+
originalPaymentProcessingEnteredAt := m.Invoice.PaymentProcessingEnteredAt
369378
m.Invoice.ValidationIssues = nil
370379

371380
if err := m.StateMachine.FireCtx(ctx, baseTrigger); err != nil {
@@ -389,6 +398,7 @@ func (m *InvoiceStateMachine) calculateAvailableActionDetails(ctx context.Contex
389398

390399
resultingState := m.Invoice.Status
391400
m.Invoice.Status = originalState
401+
m.Invoice.PaymentProcessingEnteredAt = originalPaymentProcessingEnteredAt
392402
m.Invoice.ValidationIssues = originalValidationErrors
393403

394404
return &billing.InvoiceAvailableActionDetails{

openmeter/ent/db/billinginvoice.go

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

openmeter/ent/db/billinginvoice/billinginvoice.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

openmeter/ent/db/billinginvoice/where.go

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

openmeter/ent/db/billinginvoice_create.go

Lines changed: 78 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)