Skip to content

Commit

Permalink
do not raise billing issues with noop biller (#5919)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjain1 committed Oct 16, 2024
1 parent 8f96265 commit 754c8bf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
32 changes: 22 additions & 10 deletions admin/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (s *Service) InitOrganizationBilling(ctx context.Context, org *database.Org
return nil, fmt.Errorf("failed to update organization: %w", err)
}

err = s.RaiseNewOrgBillingIssues(ctx, org.ID, org.CreatedOn, pc.HasPaymentMethod, pc.HasBillableAddress)
err = s.RaiseNewOrgBillingIssues(ctx, org.ID, org.CreatedOn, pc.HasPaymentMethod, pc.HasBillableAddress, org.BillingCustomerID == "") // noop biller will have customer id as "" so do not raise never subscribed billing error for them
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -135,7 +135,14 @@ func (s *Service) RepairOrganizationBilling(ctx context.Context, org *database.O
return nil, nil, fmt.Errorf("failed to update organization: %w", err)
}

err = s.RaiseNewOrgBillingIssues(ctx, org.ID, org.CreatedOn, pc.HasPaymentMethod, pc.HasBillableAddress)
sub, err := s.Biller.GetActiveSubscription(ctx, org.BillingCustomerID)
if err != nil {
if !errors.Is(err, billing.ErrNotFound) {
return nil, nil, fmt.Errorf("failed to get subscriptions for customer: %w", err)
}
}

err = s.RaiseNewOrgBillingIssues(ctx, org.ID, org.CreatedOn, pc.HasPaymentMethod, pc.HasBillableAddress, sub != nil)
if err != nil {
return nil, nil, err
}
Expand All @@ -144,13 +151,6 @@ func (s *Service) RepairOrganizationBilling(ctx context.Context, org *database.O
return org, nil, nil
}

sub, err := s.Biller.GetActiveSubscription(ctx, org.BillingCustomerID)
if err != nil {
if !errors.Is(err, billing.ErrNotFound) {
return nil, nil, fmt.Errorf("failed to get subscriptions for customer: %w", err)
}
}

var updatedOrg *database.Organization
if sub == nil {
updatedOrg, sub, err = s.StartTrial(ctx, org)
Expand Down Expand Up @@ -269,7 +269,7 @@ func (s *Service) StartTrial(ctx context.Context, org *database.Organization) (*
}

// RaiseNewOrgBillingIssues raises billing issues for a new organization
func (s *Service) RaiseNewOrgBillingIssues(ctx context.Context, orgID string, creationTime time.Time, hasPaymentMethod, hasBillableAddress bool) error {
func (s *Service) RaiseNewOrgBillingIssues(ctx context.Context, orgID string, creationTime time.Time, hasPaymentMethod, hasBillableAddress, hasSubscription bool) error {
if !hasPaymentMethod {
_, err := s.DB.UpsertBillingIssue(ctx, &database.UpsertBillingIssueOptions{
OrgID: orgID,
Expand All @@ -294,6 +294,18 @@ func (s *Service) RaiseNewOrgBillingIssues(ctx context.Context, orgID string, cr
}
}

if !hasSubscription {
_, err := s.DB.UpsertBillingIssue(ctx, &database.UpsertBillingIssueOptions{
OrgID: orgID,
Type: database.BillingIssueTypeNeverSubscribed,
Metadata: database.BillingIssueMetadataNeverSubscribed{},
EventTime: creationTime,
})
if err != nil {
return fmt.Errorf("failed to upsert billing error: %w", err)
}
}

return nil
}

Expand Down
15 changes: 12 additions & 3 deletions admin/billing/payment/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,24 @@ func NewNoop() Provider {
}

func (n noop) CreateCustomer(ctx context.Context, organization *database.Organization) (*Customer, error) {
return &Customer{}, nil
return &Customer{
HasPaymentMethod: true,
HasBillableAddress: true,
}, nil
}

func (n noop) FindCustomer(ctx context.Context, customerID string) (*Customer, error) {
return &Customer{}, nil
return &Customer{
HasPaymentMethod: true,
HasBillableAddress: true,
}, nil
}

func (n noop) FindCustomerForOrg(ctx context.Context, organization *database.Organization) (*Customer, error) {
return &Customer{}, nil
return &Customer{
HasPaymentMethod: true,
HasBillableAddress: true,
}, nil
}

func (n noop) UpdateCustomerEmail(ctx context.Context, customerID, email string) error {
Expand Down
11 changes: 0 additions & 11 deletions admin/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,17 +239,6 @@ func (s *Service) CreateOrganizationForUser(ctx context.Context, userID, email,
return nil, err
}

// raise never subscribed billing issue
_, err = s.DB.UpsertBillingIssue(txCtx, &database.UpsertBillingIssueOptions{
OrgID: org.ID,
Type: database.BillingIssueTypeNeverSubscribed,
Metadata: database.BillingIssueMetadataNeverSubscribed{},
EventTime: org.CreatedOn,
})
if err != nil {
return nil, err
}

err = tx.Commit()
if err != nil {
return nil, err
Expand Down

1 comment on commit 754c8bf

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Published on https://ui.rilldata.com as production
🚀 Deployed on https://670fa15e4d35b04e1052f395--rill-ui.netlify.app

Please sign in to comment.