-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathRestartSubscriptionCommand.cs
More file actions
186 lines (158 loc) · 6.89 KB
/
RestartSubscriptionCommand.cs
File metadata and controls
186 lines (158 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using Bit.Core.AdminConsole.Entities;
using Bit.Core.Billing.Commands;
using Bit.Core.Billing.Constants;
using Bit.Core.Billing.Extensions;
using Bit.Core.Billing.Pricing;
using Bit.Core.Billing.Services;
using Bit.Core.Entities;
using Bit.Core.Exceptions;
using Bit.Core.Repositories;
using Microsoft.Extensions.Logging;
using OneOf.Types;
using Stripe;
namespace Bit.Core.Billing.Subscriptions.Commands;
using static StripeConstants;
public interface IRestartSubscriptionCommand
{
Task<BillingCommandResult<None>> Run(
ISubscriber subscriber);
}
public class RestartSubscriptionCommand(
ILogger<RestartSubscriptionCommand> logger,
IOrganizationRepository organizationRepository,
IPricingClient pricingClient,
IStripeAdapter stripeAdapter,
ISubscriberService subscriberService) : BaseBillingCommand<RestartSubscriptionCommand>(logger), IRestartSubscriptionCommand
{
public Task<BillingCommandResult<None>> Run(
ISubscriber subscriber) => HandleAsync<None>(async () =>
{
var existingSubscription = await subscriberService.GetSubscription(subscriber);
if (existingSubscription is not { Status: SubscriptionStatus.Canceled })
{
return new BadRequest("Cannot restart a subscription that is not canceled.");
}
await RestartSubscriptionAsync(subscriber, existingSubscription);
return new None();
});
private Task RestartSubscriptionAsync(
ISubscriber subscriber,
Subscription canceledSubscription) => subscriber switch
{
Organization organization => RestartOrganizationSubscriptionAsync(organization, canceledSubscription),
_ => throw new NotSupportedException("Only organization subscriptions can be restarted")
};
private async Task RestartOrganizationSubscriptionAsync(
Organization organization,
Subscription canceledSubscription)
{
var plans = await pricingClient.ListPlans();
var oldPlan = plans.FirstOrDefault(plan => plan.Type == organization.PlanType);
if (oldPlan == null)
{
throw new ConflictException("Could not find plan for organization's plan type");
}
var newPlan = oldPlan.Disabled
? plans.FirstOrDefault(plan =>
plan.ProductTier == oldPlan.ProductTier &&
plan.IsAnnual == oldPlan.IsAnnual &&
!plan.Disabled)
: oldPlan;
if (newPlan == null)
{
throw new ConflictException("Could not find the current, enabled plan for organization's tier and cadence");
}
if (newPlan.Type != oldPlan.Type)
{
organization.PlanType = newPlan.Type;
organization.Plan = newPlan.Name;
organization.SelfHost = newPlan.HasSelfHost;
organization.UsePolicies = newPlan.HasPolicies;
organization.UseMyItems = newPlan.HasMyItems;
organization.UseInviteLinks = newPlan.HasInviteLinks;
organization.UseGroups = newPlan.HasGroups;
organization.UseDirectory = newPlan.HasDirectory;
organization.UseEvents = newPlan.HasEvents;
organization.UseTotp = newPlan.HasTotp;
organization.Use2fa = newPlan.Has2fa;
organization.UseApi = newPlan.HasApi;
organization.UseSso = newPlan.HasSso;
organization.UseOrganizationDomains = newPlan.HasOrganizationDomains;
organization.UseKeyConnector = newPlan.HasKeyConnector;
organization.UseScim = newPlan.HasScim;
organization.UseResetPassword = newPlan.HasResetPassword;
organization.UsersGetPremium = newPlan.UsersGetPremium;
organization.UseCustomPermissions = newPlan.HasCustomPermissions;
}
var items = new List<SubscriptionItemOptions>();
// Password Manager
var passwordManagerItem = canceledSubscription.Items.FirstOrDefault(item =>
item.Price.Id == (oldPlan.HasNonSeatBasedPasswordManagerPlan()
? oldPlan.PasswordManager.StripePlanId
: oldPlan.PasswordManager.StripeSeatPlanId));
if (passwordManagerItem == null)
{
throw new ConflictException("Organization's subscription does not have a Password Manager subscription item.");
}
items.Add(new SubscriptionItemOptions
{
Price = newPlan.HasNonSeatBasedPasswordManagerPlan() ? newPlan.PasswordManager.StripePlanId : newPlan.PasswordManager.StripeSeatPlanId,
Quantity = passwordManagerItem.Quantity
});
// Storage
var storageItem = canceledSubscription.Items.FirstOrDefault(
item => item.Price.Id == oldPlan.PasswordManager.StripeStoragePlanId);
if (storageItem != null)
{
items.Add(new SubscriptionItemOptions
{
Price = newPlan.PasswordManager.StripeStoragePlanId,
Quantity = storageItem.Quantity
});
}
// Secrets Manager & Service Accounts
var secretsManagerItem = oldPlan.SecretsManager != null
? canceledSubscription.Items.FirstOrDefault(item =>
item.Price.Id == oldPlan.SecretsManager.StripeSeatPlanId)
: null;
var serviceAccountsItem = oldPlan.SecretsManager != null
? canceledSubscription.Items.FirstOrDefault(item =>
item.Price.Id == oldPlan.SecretsManager.StripeServiceAccountPlanId)
: null;
if (newPlan.SecretsManager != null)
{
if (secretsManagerItem != null)
{
items.Add(new SubscriptionItemOptions
{
Price = newPlan.SecretsManager.StripeSeatPlanId,
Quantity = secretsManagerItem.Quantity
});
}
if (serviceAccountsItem != null)
{
items.Add(new SubscriptionItemOptions
{
Price = newPlan.SecretsManager.StripeServiceAccountPlanId,
Quantity = serviceAccountsItem.Quantity
});
}
}
var options = new SubscriptionCreateOptions
{
AutomaticTax = new SubscriptionAutomaticTaxOptions { Enabled = true },
CollectionMethod = CollectionMethod.ChargeAutomatically,
Customer = canceledSubscription.CustomerId,
Items = items,
Metadata = canceledSubscription.Metadata,
OffSession = true,
TrialPeriodDays = 0
};
var subscription = await stripeAdapter.CreateSubscriptionAsync(options);
organization.GatewaySubscriptionId = subscription.Id;
organization.Enabled = true;
organization.ExpirationDate = subscription.GetCurrentPeriodEnd();
organization.RevisionDate = DateTime.UtcNow;
await organizationRepository.ReplaceAsync(organization);
}
}