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
1 change: 1 addition & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func main() {
AppStripe: app.AppRegistry.Stripe,
AppCustomInvoicing: app.AppRegistry.CustomInvoicing,
Billing: app.Billing,
BillingFeatureSwitches: conf.Billing.FeatureSwitches,
Customer: app.Customer,
DebugConnector: debugConnector,
ErrorHandler: errorsx.NewSlogHandler(logger),
Expand Down
4 changes: 4 additions & 0 deletions openmeter/billing/httpdriver/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log/slog"
"net/http"

"github.com/openmeterio/openmeter/app/config"
appstripe "github.com/openmeterio/openmeter/openmeter/app/stripe"
"github.com/openmeterio/openmeter/openmeter/billing"
"github.com/openmeterio/openmeter/openmeter/namespace/namespacedriver"
Expand Down Expand Up @@ -52,6 +53,7 @@ type CustomerOverrideHandler interface {
type handler struct {
service billing.Service
namespaceDecoder namespacedriver.NamespaceDecoder
featureSwitches config.BillingFeatureSwitchesConfiguration
options []httptransport.HandlerOption
}

Expand All @@ -67,6 +69,7 @@ func (h *handler) resolveNamespace(ctx context.Context) (string, error) {
func New(
logger *slog.Logger,
namespaceDecoder namespacedriver.NamespaceDecoder,
featureSwitches config.BillingFeatureSwitchesConfiguration,
service billing.Service,
stripeAppService appstripe.Service,
options ...httptransport.HandlerOption,
Expand All @@ -75,5 +78,6 @@ func New(
service: service,
namespaceDecoder: namespaceDecoder,
options: options,
featureSwitches: featureSwitches,
}
}
22 changes: 22 additions & 0 deletions openmeter/billing/httpdriver/invoiceline.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ 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"),
}
}
}
}

return CreatePendingLineRequest{
Customer: customer.CustomerID{
Namespace: ns,
Expand Down Expand Up @@ -942,6 +952,12 @@ 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 {
return billing.LineChildren{}, billing.ValidationError{
Err: fmt.Errorf("creating flat fee lines is not supported, please use usage based lines instead"),
}
}

if invoice.Status != billing.InvoiceStatusGathering {
newLine, err = h.service.SnapshotLineQuantity(ctx, billing.SnapshotLineQuantityInput{
Invoice: invoice,
Expand All @@ -962,6 +978,12 @@ 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 {
return billing.LineChildren{}, billing.ValidationError{
Err: fmt.Errorf("updating flat fee lines is not supported, please use usage based lines instead"),
}
}

if changed && invoice.Status != billing.InvoiceStatusGathering {
mergedLine, err = h.service.SnapshotLineQuantity(ctx, billing.SnapshotLineQuantityInput{
Invoice: invoice,
Expand Down
3 changes: 3 additions & 0 deletions openmeter/server/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/getkin/kin-openapi/openapi3filter"

"github.com/openmeterio/openmeter/api"
"github.com/openmeterio/openmeter/app/config"
"github.com/openmeterio/openmeter/openmeter/app"
appcustominvoicing "github.com/openmeterio/openmeter/openmeter/app/custominvoicing"
appcustominvoicinghttpdriver "github.com/openmeterio/openmeter/openmeter/app/custominvoicing/httpdriver"
Expand Down Expand Up @@ -86,6 +87,7 @@ type Config struct {
AppStripe appstripe.Service
AppCustomInvoicing appcustominvoicing.SyncService
Billing billing.Service
BillingFeatureSwitches config.BillingFeatureSwitchesConfiguration
Customer customer.Service
DebugConnector debug.DebugConnector
EntitlementConnector entitlement.Connector
Expand Down Expand Up @@ -344,6 +346,7 @@ func NewRouter(config Config) (*Router, error) {
router.billingHandler = billinghttpdriver.New(
config.Logger,
staticNamespaceDecoder,
config.BillingFeatureSwitches,
config.Billing,
config.AppStripe,
httptransport.WithErrorHandler(config.ErrorHandler),
Expand Down
Loading