Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions app/common/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func BillingService(
return nil, err
}

handler, err := NewBillingSubscriptionHandler(logger, subscriptionServices, service, billingAdapter, tracer, fsConfig)
handler, err := NewBillingSubscriptionHandler(logger, subscriptionServices, service, billingAdapter, tracer)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -119,16 +119,11 @@ func NewBillingSubscriptionReconciler(logger *slog.Logger, subsServices Subscrip
})
}

func NewBillingSubscriptionHandler(logger *slog.Logger, subsServices SubscriptionServiceWithWorkflow, billingService billing.Service, billingAdapter billing.Adapter, tracer trace.Tracer, config config.BillingFeatureSwitchesConfiguration) (*billingworkersubscription.Handler, error) {
featureFlags := billingworkersubscription.FeatureFlags{
UseUsageBasedFlatFeeLines: config.UseUsageBasedFlatFeeLines,
}

func NewBillingSubscriptionHandler(logger *slog.Logger, subsServices SubscriptionServiceWithWorkflow, billingService billing.Service, billingAdapter billing.Adapter, tracer trace.Tracer) (*billingworkersubscription.Handler, error) {
return billingworkersubscription.New(billingworkersubscription.Config{
SubscriptionService: subsServices.Service,
BillingService: billingService,
TxCreator: billingAdapter,
FeatureFlags: featureFlags,
Logger: logger,
Tracer: tracer,
})
Expand Down
3 changes: 1 addition & 2 deletions app/config/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ func (c BillingConfiguration) Validate() error {
}

type BillingFeatureSwitchesConfiguration struct {
NamespaceLockdown []string
UseUsageBasedFlatFeeLines bool
NamespaceLockdown []string
}

func (c BillingFeatureSwitchesConfiguration) Validate() error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/billing-worker/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/jobs/internal/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 6 additions & 8 deletions openmeter/billing/httpdriver/invoiceline.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ func (h *handler) CreatePendingLine() CreatePendingLineHandler {
}
}

if h.featureSwitches.UseUsageBasedFlatFeeLines {
for _, line := range lineEntities {
if line.Type == billing.InvoiceLineTypeFee {
return CreatePendingLineRequest{}, billing.ValidationError{
Err: fmt.Errorf("creating flat fee lines is not supported, please use usage based lines instead"),
}
for _, line := range lineEntities {
if line.Type == billing.InvoiceLineTypeFee {
return CreatePendingLineRequest{}, billing.ValidationError{
Err: fmt.Errorf("creating flat fee lines is not supported, please use usage based lines instead"),
}
}
}
Expand Down Expand Up @@ -713,7 +711,7 @@ func (h *handler) mergeInvoiceLinesFromAPI(ctx context.Context, invoice *billing
return billing.LineChildren{}, fmt.Errorf("failed to create new line: %w", err)
}

if h.featureSwitches.UseUsageBasedFlatFeeLines && newLine.Type == billing.InvoiceLineTypeFee {
if newLine.Type == billing.InvoiceLineTypeFee {
return billing.LineChildren{}, billing.ValidationError{
Err: fmt.Errorf("creating flat fee lines is not supported, please use usage based lines instead"),
}
Expand All @@ -739,7 +737,7 @@ func (h *handler) mergeInvoiceLinesFromAPI(ctx context.Context, invoice *billing
return billing.LineChildren{}, fmt.Errorf("failed to merge line: %w", err)
}

if h.featureSwitches.UseUsageBasedFlatFeeLines && changed && mergedLine.Type == billing.InvoiceLineTypeFee {
if changed && mergedLine.Type == billing.InvoiceLineTypeFee {
return billing.LineChildren{}, billing.ValidationError{
Err: fmt.Errorf("updating flat fee lines is not supported, please use usage based lines instead"),
}
Expand Down
75 changes: 28 additions & 47 deletions openmeter/billing/worker/subscription/feehelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ func isFlatFee(line *billing.Line) bool {
return false
}

if line.Type == billing.InvoiceLineTypeFee {
return true
}

if line.Type == billing.InvoiceLineTypeUsageBased &&
line.UsageBased != nil &&
line.UsageBased.Price != nil &&
Expand All @@ -36,58 +32,43 @@ func getFlatFeePerUnitAmount(line *billing.Line) (alpacadecimal.Decimal, error)
return alpacadecimal.Zero, fmt.Errorf("line is nil")
}

switch line.Type {
case billing.InvoiceLineTypeFee:
if line.FlatFee == nil {
return alpacadecimal.Zero, fmt.Errorf("line misses flat fee metadata")
}

return line.FlatFee.PerUnitAmount, nil
case billing.InvoiceLineTypeUsageBased:
if line.UsageBased == nil || line.UsageBased.Price == nil {
return alpacadecimal.Zero, fmt.Errorf("line misses usage based metadata")
}

flatPrice, err := line.UsageBased.Price.AsFlat()
if err != nil {
return alpacadecimal.Zero, err
}

return flatPrice.Amount, nil
default:
return alpacadecimal.Zero, fmt.Errorf("line is not a (flat or usage based) fee line")
if line.Type != billing.InvoiceLineTypeUsageBased {
return alpacadecimal.Zero, fmt.Errorf("line is not a usage based line")
}

if line.UsageBased == nil || line.UsageBased.Price == nil {
return alpacadecimal.Zero, fmt.Errorf("line misses usage based metadata")
}

flatPrice, err := line.UsageBased.Price.AsFlat()
if err != nil {
return alpacadecimal.Zero, err
}

return flatPrice.Amount, nil
}

func setFlatFeePerUnitAmount(line *billing.Line, perUnitAmount alpacadecimal.Decimal) error {
if line == nil {
return fmt.Errorf("line is nil")
}

switch line.Type {
case billing.InvoiceLineTypeFee:
if line.FlatFee == nil {
return fmt.Errorf("line misses flat fee metadata")
}

line.FlatFee.PerUnitAmount = perUnitAmount
return nil
case billing.InvoiceLineTypeUsageBased:
if line.UsageBased == nil || line.UsageBased.Price == nil {
return fmt.Errorf("line misses usage based metadata")
}

flatPrice, err := line.UsageBased.Price.AsFlat()
if err != nil {
return err
}

flatPrice.Amount = perUnitAmount
line.UsageBased.Price = productcatalog.NewPriceFrom(flatPrice)
return nil
default:
return fmt.Errorf("line is not a (flat or usage based) fee line")
if line.Type != billing.InvoiceLineTypeUsageBased {
return fmt.Errorf("line is not a usage based line")
}

if line.UsageBased == nil || line.UsageBased.Price == nil {
return fmt.Errorf("line misses usage based metadata")
}

flatPrice, err := line.UsageBased.Price.AsFlat()
if err != nil {
return err
}

flatPrice.Amount = perUnitAmount
line.UsageBased.Price = productcatalog.NewPriceFrom(flatPrice)
return nil
}

type typeWithEqual[T any] interface {
Expand Down
23 changes: 7 additions & 16 deletions openmeter/billing/worker/subscription/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"
"time"

"github.com/alpacahq/alpacadecimal"
"github.com/samber/lo"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
Expand All @@ -32,7 +31,6 @@ const (
type FeatureFlags struct {
EnableFlatFeeInAdvanceProrating bool
EnableFlatFeeInArrearsProrating bool
UseUsageBasedFlatFeeLines bool
}

type Config struct {
Expand Down Expand Up @@ -617,20 +615,13 @@ func (h *Handler) lineFromSubscritionRateCard(subs subscription.SubscriptionView
return nil, nil
}

if !h.featureFlags.UseUsageBasedFlatFeeLines {
line.Type = billing.InvoiceLineTypeFee
line.FlatFee = &billing.FlatFeeLine{
PerUnitAmount: perUnitAmount,
Quantity: alpacadecimal.NewFromInt(1),
PaymentTerm: price.PaymentTerm,
Category: billing.FlatFeeCategoryRegular,
}
} else {
line.Type = billing.InvoiceLineTypeUsageBased
line.UsageBased = &billing.UsageBasedLine{
Price: item.SubscriptionItem.RateCard.AsMeta().Price,
FeatureKey: lo.FromPtr(item.SubscriptionItem.RateCard.AsMeta().FeatureKey),
}
line.Type = billing.InvoiceLineTypeUsageBased
line.UsageBased = &billing.UsageBasedLine{
Price: productcatalog.NewPriceFrom(productcatalog.FlatPrice{
Amount: perUnitAmount,
PaymentTerm: price.PaymentTerm,
}),
FeatureKey: lo.FromPtr(item.SubscriptionItem.RateCard.AsMeta().FeatureKey),
}

default:
Expand Down
Loading