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

feat: allow trial on default plan of billing account #739

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions billing/checkout/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,14 @@ func (s *Service) Apply(ctx context.Context, ch Checkout) (*subscription.Subscri
if err != nil {
return nil, nil, fmt.Errorf("failed to create subscription: %w", err)
}

// if set to cancel after trial, schedule a phase to cancel the subscription
if ch.CancelAfterTrial && stripeSubscription.TrialEnd > 0 {
_, err := s.subscriptionService.Cancel(ctx, subs.ID, false)
if err != nil {
return nil, nil, fmt.Errorf("failed to schedule cancel of subscription after trial: %w", err)
}
}
return &subs, nil, nil
} else if ch.ProductID != "" {
chProduct, err := s.productService.GetByID(ctx, ch.ProductID)
Expand Down
9 changes: 5 additions & 4 deletions billing/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ type RefreshInterval struct {
}

type AccountConfig struct {
AutoCreateWithOrg bool `yaml:"auto_create_with_org" mapstructure:"auto_create_with_org"`
DefaultPlan string `yaml:"default_plan" mapstructure:"default_plan"`
DefaultOffline bool `yaml:"default_offline" mapstructure:"default_offline"`
OnboardCreditsWithOrg int64 `yaml:"onboard_credits_with_org" mapstructure:"onboard_credits_with_org"`
AutoCreateWithOrg bool `yaml:"auto_create_with_org" mapstructure:"auto_create_with_org"`
DefaultPlan string `yaml:"default_plan" mapstructure:"default_plan"`
DefaultOffline bool `yaml:"default_offline" mapstructure:"default_offline"`
OnboardCreditsWithOrg int64 `yaml:"onboard_credits_with_org" mapstructure:"onboard_credits_with_org"`
DefaultPlanCancelAfterTrial bool `yaml:"default_plan_cancel_after_trial" mapstructure:"default_plan_cancel_after_trial"`
}

type PlanChangeConfig struct {
Expand Down
15 changes: 9 additions & 6 deletions core/event/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,20 @@ func (p *Service) EnsureDefaultPlan(ctx context.Context, orgID string) error {
return fmt.Errorf("failed to get default plan: %w", err)
}

var totalPrice int64
for _, prod := range defaultPlan.Products {
for _, price := range prod.Prices {
if price.Amount > 0 {
return fmt.Errorf("default plan is not free")
}
totalPrice += price.Amount
}
}
if totalPrice > 0 && defaultPlan.TrialDays == 0 {
return fmt.Errorf("default plan is not free to start")
}

_, _, err = p.checkoutService.Apply(ctx, checkout.Checkout{
CustomerID: customr.ID,
PlanID: defaultPlan.ID,
SkipTrial: true,
CustomerID: customr.ID,
PlanID: defaultPlan.ID,
CancelAfterTrial: p.billingConf.AccountConfig.DefaultPlanCancelAfterTrial,
})
if err != nil {
return fmt.Errorf("failed to apply default plan: %w", err)
Expand Down
Loading