diff --git a/admin/billing.go b/admin/billing.go index d2c1ba2f852..534882a6c55 100644 --- a/admin/billing.go +++ b/admin/billing.go @@ -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 } @@ -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 } @@ -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) @@ -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, @@ -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 } diff --git a/admin/billing/payment/noop.go b/admin/billing/payment/noop.go index eb7ed12940a..79ef1f0e5b2 100644 --- a/admin/billing/payment/noop.go +++ b/admin/billing/payment/noop.go @@ -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 { diff --git a/admin/user.go b/admin/user.go index 4d8d3d141ee..3b4cb790408 100644 --- a/admin/user.go +++ b/admin/user.go @@ -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