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: cancel subscription after trial #843

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions billing/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ type PlanChangeConfig struct {
}

type SubscriptionConfig struct {
// CancelAfterTrial will cancel the subscription after trial end and will not generate invoice.
CancelAfterTrial bool `yaml:"cancel_after_trial" mapstructure:"cancel_after_trial" default:"false"`
rsbh marked this conversation as resolved.
Show resolved Hide resolved
}

type ProductConfig struct {
Expand Down
10 changes: 8 additions & 2 deletions billing/subscription/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ func (s *Service) getPlanFromSchedule(ctx context.Context, stripeSchedule *strip

// CancelUpcomingPhase cancels the scheduled phase of the subscription
func (s *Service) CancelUpcomingPhase(ctx context.Context, sub Subscription) error {
_, stripeSchedule, err := s.createOrGetSchedule(ctx, sub)
stripeSub, stripeSchedule, err := s.createOrGetSchedule(ctx, sub)
if err != nil {
return err
}
Expand All @@ -988,6 +988,12 @@ func (s *Service) CancelUpcomingPhase(ctx context.Context, sub Subscription) err
var currency = string(stripeSchedule.Phases[0].Currency)
var prorationBehavior = s.config.PlanChangeConfig.ProrationBehavior

var endBehavior = stripe.SubscriptionScheduleEndBehaviorRelease

if stripeSub.Status == stripe.SubscriptionStatusTrialing && s.config.SubscriptionConfig.CancelAfterTrial {
endBehavior = stripe.SubscriptionScheduleEndBehaviorCancel
}

// update the phases
_, err = s.stripeClient.SubscriptionSchedules.Update(stripeSchedule.ID, &stripe.SubscriptionScheduleParams{
Params: stripe.Params{
Expand All @@ -1005,7 +1011,7 @@ func (s *Service) CancelUpcomingPhase(ctx context.Context, sub Subscription) err
},
},
},
EndBehavior: stripe.String("release"),
EndBehavior: stripe.String(string(endBehavior)),
ProrationBehavior: stripe.String(prorationBehavior),
DefaultSettings: &stripe.SubscriptionScheduleDefaultSettingsParams{
CollectionMethod: stripe.String(s.config.PlanChangeConfig.CollectionMethod),
Expand Down
2 changes: 2 additions & 0 deletions config/sample.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,5 @@ billing:
subscription: 1m
invoice: 5m
checkout: 1m
subscription:
cancel_after_trial: true
Loading