mirror of
https://github.com/bitwarden/server.git
synced 2025-07-03 00:52:49 -05:00
org signup plan details
This commit is contained in:
@ -1,16 +1,28 @@
|
||||
using Bit.Core.Models.Table;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models.Business;
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Bit.Core.Models.Api
|
||||
{
|
||||
public class OrganizationCreateRequestModel
|
||||
public class OrganizationCreateRequestModel : IValidatableObject
|
||||
{
|
||||
[Required]
|
||||
[StringLength(50)]
|
||||
public string Name { get; set; }
|
||||
[StringLength(50)]
|
||||
public string BusinessName { get; set; }
|
||||
[Required]
|
||||
[StringLength(50)]
|
||||
public string BillingEmail { get; set; }
|
||||
public PlanType PlanType { get; set; }
|
||||
[Required]
|
||||
public string Key { get; set; }
|
||||
public string CardToken { get; set; }
|
||||
[Range(0, double.MaxValue)]
|
||||
public short AdditionalUsers { get; set; }
|
||||
public bool Monthly { get; set; }
|
||||
|
||||
public virtual OrganizationSignup ToOrganizationSignup(User user)
|
||||
{
|
||||
@ -20,8 +32,20 @@ namespace Bit.Core.Models.Api
|
||||
OwnerKey = Key,
|
||||
Name = Name,
|
||||
Plan = PlanType,
|
||||
PaymentToken = CardToken
|
||||
PaymentToken = CardToken,
|
||||
AdditionalUsers = AdditionalUsers,
|
||||
BillingEmail = BillingEmail,
|
||||
BusinessName = BusinessName,
|
||||
Monthly = Monthly
|
||||
};
|
||||
}
|
||||
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
{
|
||||
if(PlanType != PlanType.Free && string.IsNullOrWhiteSpace(CardToken))
|
||||
{
|
||||
yield return new ValidationResult("Card required.", new string[] { nameof(CardToken) });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,9 +6,13 @@ namespace Bit.Core.Models.Business
|
||||
public class OrganizationSignup
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string BusinessName { get; set; }
|
||||
public string BillingEmail { get; set; }
|
||||
public User Owner { get; set; }
|
||||
public string OwnerKey { get; set; }
|
||||
public Enums.PlanType Plan { get; set; }
|
||||
public short AdditionalUsers { get; set; }
|
||||
public string PaymentToken { get; set; }
|
||||
public bool Monthly { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -6,10 +6,18 @@ namespace Bit.Core.Models.StaticStore
|
||||
public class Plan
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string StripeId { get; set; }
|
||||
public string StripeAnnualPlanId { get; set; }
|
||||
public string StripeAnnualUserPlanId { get; set; }
|
||||
public string StripeMonthlyPlanId { get; set; }
|
||||
public string StripeMonthlyUserPlanId { get; set; }
|
||||
public PlanType Type { get; set; }
|
||||
public short MaxUsers { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
public short BaseUsers { get; set; }
|
||||
public bool CanBuyAdditionalUsers { get; set; }
|
||||
public bool CanMonthly { get; set; }
|
||||
public decimal BaseMonthlyPrice { get; set; }
|
||||
public decimal UserMonthlyPrice { get; set; }
|
||||
public decimal BaseAnnualPrice { get; set; }
|
||||
public decimal UserAnnualPrice { get; set; }
|
||||
public TimeSpan? Trial { get; set; }
|
||||
public Func<DateTime, TimeSpan> Cycle { get; set; }
|
||||
public bool Disabled { get; set; }
|
||||
|
@ -9,13 +9,19 @@ namespace Bit.Core.Models.Table
|
||||
public Guid Id { get; set; }
|
||||
public Guid UserId { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string BusinessName { get; set; }
|
||||
public string BillingEmail { get; set; }
|
||||
public string Plan { get; set; }
|
||||
public PlanType PlanType { get; set; }
|
||||
public decimal PlanPrice { get; set; }
|
||||
public decimal PlanRenewalPrice { get; set; }
|
||||
public decimal PlanBasePrice { get; set; }
|
||||
public decimal PlanUserPrice { get; set; }
|
||||
public DateTime? PlanRenewalDate { get; set; }
|
||||
public bool PlanTrial { get; set; }
|
||||
public short BaseUsers { get; set; }
|
||||
public short AdditionalUsers { get; set; }
|
||||
public short MaxUsers { get; set; }
|
||||
public string StripeCustomerId { get; set; }
|
||||
public string StripeSubscriptionId { get; set; }
|
||||
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
|
||||
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;
|
||||
|
||||
|
@ -9,6 +9,7 @@ using Bit.Core.Exceptions;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Stripe;
|
||||
using Bit.Core.Models.StaticStore;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
@ -49,41 +50,68 @@ namespace Bit.Core.Services
|
||||
}
|
||||
|
||||
var customerService = new StripeCustomerService();
|
||||
var customer = await customerService.CreateAsync(new StripeCustomerCreateOptions
|
||||
{
|
||||
SourceToken = signup.PaymentToken
|
||||
});
|
||||
|
||||
var subscriptionService = new StripeSubscriptionService();
|
||||
var subscription = await subscriptionService.CreateAsync(customer.Id, plan.StripeId);
|
||||
StripeCustomer customer = null;
|
||||
StripeSubscription subscription = null;
|
||||
|
||||
if(plan.Type != Enums.PlanType.Free)
|
||||
{
|
||||
customer = await customerService.CreateAsync(new StripeCustomerCreateOptions
|
||||
{
|
||||
Description = signup.BusinessName,
|
||||
Email = signup.BillingEmail,
|
||||
SourceToken = signup.PaymentToken
|
||||
});
|
||||
|
||||
var subCreateOptions = new StripeSubscriptionCreateOptions
|
||||
{
|
||||
Items = new List<StripeSubscriptionItemOption>
|
||||
{
|
||||
new StripeSubscriptionItemOption
|
||||
{
|
||||
PlanId = plan.CanMonthly && signup.Monthly ? plan.StripeMonthlyPlanId : plan.StripeAnnualPlanId,
|
||||
Quantity = 1
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if(plan.CanBuyAdditionalUsers && signup.AdditionalUsers > 0)
|
||||
{
|
||||
subCreateOptions.Items.Add(new StripeSubscriptionItemOption
|
||||
{
|
||||
PlanId = plan.CanMonthly && signup.Monthly ? plan.StripeMonthlyUserPlanId : plan.StripeAnnualUserPlanId,
|
||||
Quantity = signup.AdditionalUsers
|
||||
});
|
||||
}
|
||||
|
||||
subscription = await subscriptionService.CreateAsync(customer.Id, subCreateOptions);
|
||||
}
|
||||
|
||||
var organization = new Organization
|
||||
{
|
||||
Name = signup.Name,
|
||||
BillingEmail = signup.BillingEmail,
|
||||
BusinessName = signup.BusinessName,
|
||||
UserId = signup.Owner.Id,
|
||||
PlanType = plan.Type,
|
||||
MaxUsers = plan.MaxUsers,
|
||||
BaseUsers = plan.BaseUsers,
|
||||
AdditionalUsers = (short)(plan.CanBuyAdditionalUsers ? signup.AdditionalUsers : 0),
|
||||
MaxUsers = (short)(plan.BaseUsers + (plan.CanBuyAdditionalUsers ? signup.AdditionalUsers : 0)),
|
||||
PlanTrial = plan.Trial.HasValue,
|
||||
PlanPrice = plan.Trial.HasValue ? 0 : plan.Price,
|
||||
PlanRenewalPrice = plan.Price,
|
||||
PlanBasePrice = plan.CanMonthly && signup.Monthly ? plan.BaseMonthlyPrice : plan.BaseAnnualPrice,
|
||||
PlanUserPrice = plan.CanMonthly && signup.Monthly ? plan.UserMonthlyPrice : plan.UserAnnualPrice,
|
||||
PlanRenewalDate = subscription?.CurrentPeriodEnd,
|
||||
Plan = plan.ToString(),
|
||||
StripeCustomerId = customer?.Id,
|
||||
StripeSubscriptionId = subscription?.Id,
|
||||
CreationDate = DateTime.UtcNow,
|
||||
RevisionDate = DateTime.UtcNow
|
||||
};
|
||||
|
||||
if(plan.Trial.HasValue)
|
||||
{
|
||||
organization.PlanRenewalDate = DateTime.UtcNow.Add(plan.Trial.Value);
|
||||
}
|
||||
else if(plan.Cycle != null)
|
||||
{
|
||||
organization.PlanRenewalDate = DateTime.UtcNow.Add(plan.Cycle(DateTime.UtcNow));
|
||||
}
|
||||
|
||||
await _organizationRepository.CreateAsync(organization);
|
||||
|
||||
try
|
||||
{
|
||||
await _organizationRepository.CreateAsync(organization);
|
||||
|
||||
var orgUser = new OrganizationUser
|
||||
{
|
||||
OrganizationId = organization.Id,
|
||||
@ -102,7 +130,18 @@ namespace Bit.Core.Services
|
||||
}
|
||||
catch
|
||||
{
|
||||
await _organizationRepository.DeleteAsync(organization);
|
||||
if(subscription != null)
|
||||
{
|
||||
await subscriptionService.CancelAsync(subscription.Id);
|
||||
}
|
||||
|
||||
// TODO: reverse payments
|
||||
|
||||
if(organization.Id != default(Guid))
|
||||
{
|
||||
await _organizationRepository.DeleteAsync(organization);
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
@ -94,27 +94,40 @@ namespace Bit.Core.Utilities
|
||||
new Plan
|
||||
{
|
||||
Type = PlanType.Free,
|
||||
MaxUsers = 1,
|
||||
Price = 0
|
||||
BaseUsers = 1,
|
||||
CanBuyAdditionalUsers = false,
|
||||
Name = "Free"
|
||||
},
|
||||
new Plan
|
||||
{
|
||||
Type = PlanType.Personal,
|
||||
MaxUsers = 5,
|
||||
Price = 1,
|
||||
BaseUsers = 5,
|
||||
BaseAnnualPrice = 12,
|
||||
UserAnnualPrice = 12,
|
||||
CanBuyAdditionalUsers = true,
|
||||
Trial = new TimeSpan(14, 0, 0, 0),
|
||||
Cycle = now => now.AddYears(1) - now,
|
||||
Name = "Personal",
|
||||
StripeId = "premium-yearly"
|
||||
|
||||
StripeAnnualPlanId = "premium-yearly",
|
||||
StripeAnnualUserPlanId = "premium-user-yearly"
|
||||
},
|
||||
new Plan
|
||||
{
|
||||
Type = PlanType.Teams,
|
||||
MaxUsers = 5,
|
||||
Price = 10,
|
||||
BaseUsers = 5,
|
||||
BaseAnnualPrice = 60,
|
||||
UserAnnualPrice = 24,
|
||||
BaseMonthlyPrice = 8,
|
||||
UserMonthlyPrice = 2.5M,
|
||||
CanBuyAdditionalUsers = true,
|
||||
CanMonthly = true,
|
||||
Trial = new TimeSpan(14, 0, 0, 0),
|
||||
Cycle = now => now.AddMonths(1) - now
|
||||
Cycle = now => now.AddMonths(1) - now,
|
||||
Name = "Teams",
|
||||
StripeAnnualPlanId = "premium-yearly",
|
||||
StripeAnnualUserPlanId = "premium-user-yearly",
|
||||
StripeMonthlyPlanId = "premium-yearly",
|
||||
StripeMonthlyUserPlanId = "premium-user-yearly"
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user