Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not raise billing issues with noop biller #5919

Merged
merged 1 commit into from
Oct 16, 2024
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
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
Loading