mirror of
https://github.com/bitwarden/server.git
synced 2025-07-17 23:50:58 -05:00
apis for subscription vs billing
This commit is contained in:
@ -3,7 +3,6 @@ using Bit.Core.Models.Table;
|
||||
using Stripe;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Bit.Core.Models.Business
|
||||
{
|
||||
@ -11,8 +10,6 @@ namespace Bit.Core.Models.Business
|
||||
{
|
||||
public decimal CreditAmount { get; set; }
|
||||
public BillingSource PaymentSource { get; set; }
|
||||
public BillingSubscription Subscription { get; set; }
|
||||
public BillingInvoiceInfo UpcomingInvoice { get; set; }
|
||||
public IEnumerable<BillingCharge> Charges { get; set; } = new List<BillingCharge>();
|
||||
public IEnumerable<BillingInvoice> Invoices { get; set; } = new List<BillingInvoice>();
|
||||
public IEnumerable<BillingTransaction> Transactions { get; set; } = new List<BillingTransaction>();
|
||||
@ -88,136 +85,6 @@ namespace Bit.Core.Models.Business
|
||||
public bool NeedsVerification { get; set; }
|
||||
}
|
||||
|
||||
public class BillingSubscription
|
||||
{
|
||||
public BillingSubscription(Subscription sub)
|
||||
{
|
||||
Status = sub.Status;
|
||||
TrialStartDate = sub.TrialStart;
|
||||
TrialEndDate = sub.TrialEnd;
|
||||
PeriodStartDate = sub.CurrentPeriodStart;
|
||||
PeriodEndDate = sub.CurrentPeriodEnd;
|
||||
CancelledDate = sub.CanceledAt;
|
||||
CancelAtEndDate = sub.CancelAtPeriodEnd;
|
||||
Cancelled = sub.Status == "canceled" || sub.Status == "unpaid";
|
||||
if(sub.Items?.Data != null)
|
||||
{
|
||||
Items = sub.Items.Data.Select(i => new BillingSubscriptionItem(i));
|
||||
}
|
||||
}
|
||||
|
||||
public BillingSubscription(Braintree.Subscription sub, Braintree.Plan plan)
|
||||
{
|
||||
Status = sub.Status.ToString();
|
||||
|
||||
if(sub.HasTrialPeriod.GetValueOrDefault() && sub.CreatedAt.HasValue && sub.TrialDuration.HasValue)
|
||||
{
|
||||
TrialStartDate = sub.CreatedAt.Value;
|
||||
if(sub.TrialDurationUnit == Braintree.SubscriptionDurationUnit.DAY)
|
||||
{
|
||||
TrialEndDate = TrialStartDate.Value.AddDays(sub.TrialDuration.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
TrialEndDate = TrialStartDate.Value.AddMonths(sub.TrialDuration.Value);
|
||||
}
|
||||
}
|
||||
|
||||
PeriodStartDate = sub.BillingPeriodStartDate;
|
||||
PeriodEndDate = sub.BillingPeriodEndDate;
|
||||
|
||||
CancelAtEndDate = !sub.NeverExpires.GetValueOrDefault();
|
||||
Cancelled = sub.Status == Braintree.SubscriptionStatus.CANCELED;
|
||||
if(Cancelled)
|
||||
{
|
||||
CancelledDate = sub.UpdatedAt.Value;
|
||||
}
|
||||
|
||||
var items = new List<BillingSubscriptionItem>();
|
||||
items.Add(new BillingSubscriptionItem(plan));
|
||||
if(sub.AddOns != null)
|
||||
{
|
||||
items.AddRange(sub.AddOns.Select(a => new BillingSubscriptionItem(plan, a)));
|
||||
}
|
||||
|
||||
if(items.Count > 0)
|
||||
{
|
||||
Items = items;
|
||||
}
|
||||
}
|
||||
|
||||
public DateTime? TrialStartDate { get; set; }
|
||||
public DateTime? TrialEndDate { get; set; }
|
||||
public DateTime? PeriodStartDate { get; set; }
|
||||
public DateTime? PeriodEndDate { get; set; }
|
||||
public TimeSpan? PeriodDuration => PeriodEndDate - PeriodStartDate;
|
||||
public DateTime? CancelledDate { get; set; }
|
||||
public bool CancelAtEndDate { get; set; }
|
||||
public string Status { get; set; }
|
||||
public bool Cancelled { get; set; }
|
||||
public IEnumerable<BillingSubscriptionItem> Items { get; set; } = new List<BillingSubscriptionItem>();
|
||||
|
||||
public class BillingSubscriptionItem
|
||||
{
|
||||
public BillingSubscriptionItem(SubscriptionItem item)
|
||||
{
|
||||
if(item.Plan != null)
|
||||
{
|
||||
Name = item.Plan.Nickname;
|
||||
Amount = item.Plan.Amount.GetValueOrDefault() / 100M;
|
||||
Interval = item.Plan.Interval;
|
||||
}
|
||||
|
||||
Quantity = (int)item.Quantity;
|
||||
}
|
||||
|
||||
public BillingSubscriptionItem(Braintree.Plan plan)
|
||||
{
|
||||
Name = plan.Name;
|
||||
Amount = plan.Price.GetValueOrDefault();
|
||||
Interval = plan.BillingFrequency.GetValueOrDefault() == 12 ? "year" : "month";
|
||||
Quantity = 1;
|
||||
}
|
||||
|
||||
public BillingSubscriptionItem(Braintree.Plan plan, Braintree.AddOn addon)
|
||||
{
|
||||
Name = addon.Name;
|
||||
Amount = addon.Amount.GetValueOrDefault();
|
||||
Interval = plan.BillingFrequency.GetValueOrDefault() == 12 ? "year" : "month";
|
||||
Quantity = addon.Quantity.GetValueOrDefault();
|
||||
}
|
||||
|
||||
public string Name { get; set; }
|
||||
public decimal Amount { get; set; }
|
||||
public int Quantity { get; set; }
|
||||
public string Interval { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
public class BillingInvoiceInfo
|
||||
{
|
||||
public BillingInvoiceInfo() { }
|
||||
|
||||
public BillingInvoiceInfo(Invoice inv)
|
||||
{
|
||||
Amount = inv.AmountDue / 100M;
|
||||
Date = inv.Date.Value;
|
||||
}
|
||||
|
||||
public BillingInvoiceInfo(Braintree.Subscription sub)
|
||||
{
|
||||
Amount = sub.NextBillAmount.GetValueOrDefault() + sub.Balance.GetValueOrDefault();
|
||||
if(Amount < 0)
|
||||
{
|
||||
Amount = 0;
|
||||
}
|
||||
Date = sub.NextBillingDate;
|
||||
}
|
||||
|
||||
public decimal Amount { get; set; }
|
||||
public DateTime? Date { get; set; }
|
||||
}
|
||||
|
||||
public class BillingCharge
|
||||
{
|
||||
public BillingCharge(Charge charge)
|
||||
@ -309,10 +176,12 @@ namespace Bit.Core.Models.Business
|
||||
public string Details { get; set; }
|
||||
}
|
||||
|
||||
public class BillingInvoice : BillingInvoiceInfo
|
||||
public class BillingInvoice
|
||||
{
|
||||
public BillingInvoice(Invoice inv)
|
||||
{
|
||||
Amount = inv.AmountDue / 100M;
|
||||
Date = inv.Date.Value;
|
||||
Url = inv.HostedInvoiceUrl;
|
||||
PdfUrl = inv.InvoicePdf;
|
||||
Number = inv.Number;
|
||||
@ -321,6 +190,8 @@ namespace Bit.Core.Models.Business
|
||||
Date = inv.Date.Value;
|
||||
}
|
||||
|
||||
public decimal Amount { get; set; }
|
||||
public DateTime? Date { get; set; }
|
||||
public string Url { get; set; }
|
||||
public string PdfUrl { get; set; }
|
||||
public string Number { get; set; }
|
||||
|
@ -16,7 +16,7 @@ namespace Bit.Core.Models.Business
|
||||
public OrganizationLicense()
|
||||
{ }
|
||||
|
||||
public OrganizationLicense(Organization org, BillingInfo billingInfo, Guid installationId,
|
||||
public OrganizationLicense(Organization org, SubscriptionInfo subscriptionInfo, Guid installationId,
|
||||
ILicensingService licenseService)
|
||||
{
|
||||
Version = 4;
|
||||
@ -41,7 +41,7 @@ namespace Bit.Core.Models.Business
|
||||
UsersGetPremium = org.UsersGetPremium;
|
||||
Issued = DateTime.UtcNow;
|
||||
|
||||
if(billingInfo?.Subscription == null)
|
||||
if(subscriptionInfo?.Subscription == null)
|
||||
{
|
||||
if(org.PlanType == PlanType.Custom && org.ExpirationDate.HasValue)
|
||||
{
|
||||
@ -54,10 +54,10 @@ namespace Bit.Core.Models.Business
|
||||
Trial = true;
|
||||
}
|
||||
}
|
||||
else if(billingInfo.Subscription.TrialEndDate.HasValue &&
|
||||
billingInfo.Subscription.TrialEndDate.Value > DateTime.UtcNow)
|
||||
else if(subscriptionInfo.Subscription.TrialEndDate.HasValue &&
|
||||
subscriptionInfo.Subscription.TrialEndDate.Value > DateTime.UtcNow)
|
||||
{
|
||||
Expires = Refresh = billingInfo.Subscription.TrialEndDate.Value;
|
||||
Expires = Refresh = subscriptionInfo.Subscription.TrialEndDate.Value;
|
||||
Trial = true;
|
||||
}
|
||||
else
|
||||
@ -67,11 +67,11 @@ namespace Bit.Core.Models.Business
|
||||
// expired
|
||||
Expires = Refresh = org.ExpirationDate.Value;
|
||||
}
|
||||
else if(billingInfo?.Subscription?.PeriodDuration != null &&
|
||||
billingInfo.Subscription.PeriodDuration > TimeSpan.FromDays(180))
|
||||
else if(subscriptionInfo?.Subscription?.PeriodDuration != null &&
|
||||
subscriptionInfo.Subscription.PeriodDuration > TimeSpan.FromDays(180))
|
||||
{
|
||||
Refresh = DateTime.UtcNow.AddDays(30);
|
||||
Expires = billingInfo?.Subscription.PeriodEndDate.Value.AddDays(60);
|
||||
Expires = subscriptionInfo?.Subscription.PeriodEndDate.Value.AddDays(60);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
143
src/Core/Models/Business/SubscriptionInfo.cs
Normal file
143
src/Core/Models/Business/SubscriptionInfo.cs
Normal file
@ -0,0 +1,143 @@
|
||||
using Stripe;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Bit.Core.Models.Business
|
||||
{
|
||||
public class SubscriptionInfo
|
||||
{
|
||||
public BillingSubscription Subscription { get; set; }
|
||||
public BillingUpcomingInvoice UpcomingInvoice { get; set; }
|
||||
|
||||
public class BillingSubscription
|
||||
{
|
||||
public BillingSubscription(Subscription sub)
|
||||
{
|
||||
Status = sub.Status;
|
||||
TrialStartDate = sub.TrialStart;
|
||||
TrialEndDate = sub.TrialEnd;
|
||||
PeriodStartDate = sub.CurrentPeriodStart;
|
||||
PeriodEndDate = sub.CurrentPeriodEnd;
|
||||
CancelledDate = sub.CanceledAt;
|
||||
CancelAtEndDate = sub.CancelAtPeriodEnd;
|
||||
Cancelled = sub.Status == "canceled" || sub.Status == "unpaid";
|
||||
if(sub.Items?.Data != null)
|
||||
{
|
||||
Items = sub.Items.Data.Select(i => new BillingSubscriptionItem(i));
|
||||
}
|
||||
}
|
||||
|
||||
public BillingSubscription(Braintree.Subscription sub, Braintree.Plan plan)
|
||||
{
|
||||
Status = sub.Status.ToString();
|
||||
|
||||
if(sub.HasTrialPeriod.GetValueOrDefault() && sub.CreatedAt.HasValue && sub.TrialDuration.HasValue)
|
||||
{
|
||||
TrialStartDate = sub.CreatedAt.Value;
|
||||
if(sub.TrialDurationUnit == Braintree.SubscriptionDurationUnit.DAY)
|
||||
{
|
||||
TrialEndDate = TrialStartDate.Value.AddDays(sub.TrialDuration.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
TrialEndDate = TrialStartDate.Value.AddMonths(sub.TrialDuration.Value);
|
||||
}
|
||||
}
|
||||
|
||||
PeriodStartDate = sub.BillingPeriodStartDate;
|
||||
PeriodEndDate = sub.BillingPeriodEndDate;
|
||||
|
||||
CancelAtEndDate = !sub.NeverExpires.GetValueOrDefault();
|
||||
Cancelled = sub.Status == Braintree.SubscriptionStatus.CANCELED;
|
||||
if(Cancelled)
|
||||
{
|
||||
CancelledDate = sub.UpdatedAt.Value;
|
||||
}
|
||||
|
||||
var items = new List<BillingSubscriptionItem>();
|
||||
items.Add(new BillingSubscriptionItem(plan));
|
||||
if(sub.AddOns != null)
|
||||
{
|
||||
items.AddRange(sub.AddOns.Select(a => new BillingSubscriptionItem(plan, a)));
|
||||
}
|
||||
|
||||
if(items.Count > 0)
|
||||
{
|
||||
Items = items;
|
||||
}
|
||||
}
|
||||
|
||||
public DateTime? TrialStartDate { get; set; }
|
||||
public DateTime? TrialEndDate { get; set; }
|
||||
public DateTime? PeriodStartDate { get; set; }
|
||||
public DateTime? PeriodEndDate { get; set; }
|
||||
public TimeSpan? PeriodDuration => PeriodEndDate - PeriodStartDate;
|
||||
public DateTime? CancelledDate { get; set; }
|
||||
public bool CancelAtEndDate { get; set; }
|
||||
public string Status { get; set; }
|
||||
public bool Cancelled { get; set; }
|
||||
public IEnumerable<BillingSubscriptionItem> Items { get; set; } = new List<BillingSubscriptionItem>();
|
||||
|
||||
public class BillingSubscriptionItem
|
||||
{
|
||||
public BillingSubscriptionItem(SubscriptionItem item)
|
||||
{
|
||||
if(item.Plan != null)
|
||||
{
|
||||
Name = item.Plan.Nickname;
|
||||
Amount = item.Plan.Amount.GetValueOrDefault() / 100M;
|
||||
Interval = item.Plan.Interval;
|
||||
}
|
||||
|
||||
Quantity = (int)item.Quantity;
|
||||
}
|
||||
|
||||
public BillingSubscriptionItem(Braintree.Plan plan)
|
||||
{
|
||||
Name = plan.Name;
|
||||
Amount = plan.Price.GetValueOrDefault();
|
||||
Interval = plan.BillingFrequency.GetValueOrDefault() == 12 ? "year" : "month";
|
||||
Quantity = 1;
|
||||
}
|
||||
|
||||
public BillingSubscriptionItem(Braintree.Plan plan, Braintree.AddOn addon)
|
||||
{
|
||||
Name = addon.Name;
|
||||
Amount = addon.Amount.GetValueOrDefault();
|
||||
Interval = plan.BillingFrequency.GetValueOrDefault() == 12 ? "year" : "month";
|
||||
Quantity = addon.Quantity.GetValueOrDefault();
|
||||
}
|
||||
|
||||
public string Name { get; set; }
|
||||
public decimal Amount { get; set; }
|
||||
public int Quantity { get; set; }
|
||||
public string Interval { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
public class BillingUpcomingInvoice
|
||||
{
|
||||
public BillingUpcomingInvoice() { }
|
||||
|
||||
public BillingUpcomingInvoice(Invoice inv)
|
||||
{
|
||||
Amount = inv.AmountDue / 100M;
|
||||
Date = inv.Date.Value;
|
||||
}
|
||||
|
||||
public BillingUpcomingInvoice(Braintree.Subscription sub)
|
||||
{
|
||||
Amount = sub.NextBillAmount.GetValueOrDefault() + sub.Balance.GetValueOrDefault();
|
||||
if(Amount < 0)
|
||||
{
|
||||
Amount = 0;
|
||||
}
|
||||
Date = sub.NextBillingDate;
|
||||
}
|
||||
|
||||
public decimal Amount { get; set; }
|
||||
public DateTime? Date { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
@ -15,7 +15,7 @@ namespace Bit.Core.Models.Business
|
||||
public UserLicense()
|
||||
{ }
|
||||
|
||||
public UserLicense(User user, BillingInfo billingInfo, ILicensingService licenseService)
|
||||
public UserLicense(User user, SubscriptionInfo subscriptionInfo, ILicensingService licenseService)
|
||||
{
|
||||
LicenseKey = user.LicenseKey;
|
||||
Id = user.Id;
|
||||
@ -25,10 +25,10 @@ namespace Bit.Core.Models.Business
|
||||
Premium = user.Premium;
|
||||
MaxStorageGb = user.MaxStorageGb;
|
||||
Issued = DateTime.UtcNow;
|
||||
Expires = billingInfo?.UpcomingInvoice?.Date?.AddDays(7);
|
||||
Refresh = billingInfo?.UpcomingInvoice?.Date;
|
||||
Trial = (billingInfo?.Subscription?.TrialEndDate.HasValue ?? false) &&
|
||||
billingInfo.Subscription.TrialEndDate.Value > DateTime.UtcNow;
|
||||
Expires = subscriptionInfo?.UpcomingInvoice?.Date?.AddDays(7);
|
||||
Refresh = subscriptionInfo?.UpcomingInvoice?.Date;
|
||||
Trial = (subscriptionInfo?.Subscription?.TrialEndDate.HasValue ?? false) &&
|
||||
subscriptionInfo.Subscription.TrialEndDate.Value > DateTime.UtcNow;
|
||||
|
||||
Hash = Convert.ToBase64String(ComputeHash());
|
||||
Signature = Convert.ToBase64String(licenseService.SignLicense(this));
|
||||
|
Reference in New Issue
Block a user