mirror of
https://github.com/bitwarden/server.git
synced 2025-03-02 04:11:04 +01:00
apis for subscription vs billing
This commit is contained in:
parent
5945c39b32
commit
b036657d78
@ -469,6 +469,7 @@ namespace Bit.Api.Controllers
|
||||
}
|
||||
|
||||
[HttpGet("billing")]
|
||||
[SelfHosted(NotSelfHostedOnly = true)]
|
||||
public async Task<BillingResponseModel> GetBilling()
|
||||
{
|
||||
var user = await _userService.GetUserByPrincipalAsync(User);
|
||||
@ -477,20 +478,33 @@ namespace Bit.Api.Controllers
|
||||
throw new UnauthorizedAccessException();
|
||||
}
|
||||
|
||||
var billingInfo = await _paymentService.GetBillingAsync(user);
|
||||
return new BillingResponseModel(billingInfo);
|
||||
}
|
||||
|
||||
[HttpGet("subscription")]
|
||||
public async Task<SubscriptionResponseModel> GetSubscription()
|
||||
{
|
||||
var user = await _userService.GetUserByPrincipalAsync(User);
|
||||
if(user == null)
|
||||
{
|
||||
throw new UnauthorizedAccessException();
|
||||
}
|
||||
|
||||
if(!_globalSettings.SelfHosted && user.Gateway != null)
|
||||
{
|
||||
var billingInfo = await _paymentService.GetBillingAsync(user);
|
||||
var license = await _userService.GenerateLicenseAsync(user, billingInfo);
|
||||
return new BillingResponseModel(user, billingInfo, license);
|
||||
var subscriptionInfo = await _paymentService.GetSubscriptionAsync(user);
|
||||
var license = await _userService.GenerateLicenseAsync(user, subscriptionInfo);
|
||||
return new SubscriptionResponseModel(user, subscriptionInfo, license);
|
||||
}
|
||||
else if(!_globalSettings.SelfHosted)
|
||||
{
|
||||
var license = await _userService.GenerateLicenseAsync(user);
|
||||
return new BillingResponseModel(user, license);
|
||||
return new SubscriptionResponseModel(user, license);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new BillingResponseModel(user);
|
||||
return new SubscriptionResponseModel(user);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,8 +10,6 @@ using Bit.Core.Services;
|
||||
using Bit.Core;
|
||||
using Bit.Api.Utilities;
|
||||
using Bit.Core.Models.Business;
|
||||
using Stripe;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Api.Controllers
|
||||
@ -65,7 +63,27 @@ namespace Bit.Api.Controllers
|
||||
}
|
||||
|
||||
[HttpGet("{id}/billing")]
|
||||
public async Task<OrganizationBillingResponseModel> GetBilling(string id)
|
||||
[SelfHosted(NotSelfHostedOnly = true)]
|
||||
public async Task<BillingResponseModel> GetBilling(string id)
|
||||
{
|
||||
var orgIdGuid = new Guid(id);
|
||||
if(!_currentContext.OrganizationOwner(orgIdGuid))
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
var organization = await _organizationRepository.GetByIdAsync(orgIdGuid);
|
||||
if(organization == null)
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
var billingInfo = await _paymentService.GetBillingAsync(organization);
|
||||
return new BillingResponseModel(billingInfo);
|
||||
}
|
||||
|
||||
[HttpGet("{id}/subscription")]
|
||||
public async Task<OrganizationSubscriptionResponseModel> GetSubscription(string id)
|
||||
{
|
||||
var orgIdGuid = new Guid(id);
|
||||
if(!_currentContext.OrganizationOwner(orgIdGuid))
|
||||
@ -81,49 +99,19 @@ namespace Bit.Api.Controllers
|
||||
|
||||
if(!_globalSettings.SelfHosted && organization.Gateway != null)
|
||||
{
|
||||
var billingInfo = await _paymentService.GetBillingAsync(organization);
|
||||
if(billingInfo == null)
|
||||
var subscriptionInfo = await _paymentService.GetSubscriptionAsync(organization);
|
||||
if(subscriptionInfo == null)
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
return new OrganizationBillingResponseModel(organization, billingInfo);
|
||||
return new OrganizationSubscriptionResponseModel(organization, subscriptionInfo);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new OrganizationBillingResponseModel(organization);
|
||||
return new OrganizationSubscriptionResponseModel(organization);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("{id}/billing-invoice/{invoiceId}")]
|
||||
[SelfHosted(NotSelfHostedOnly = true)]
|
||||
public async Task<IActionResult> GetBillingInvoice(string id, string invoiceId)
|
||||
{
|
||||
var orgIdGuid = new Guid(id);
|
||||
if(!_currentContext.OrganizationOwner(orgIdGuid))
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
var organization = await _organizationRepository.GetByIdAsync(orgIdGuid);
|
||||
if(organization == null)
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var invoice = await new InvoiceService().GetAsync(invoiceId);
|
||||
if(invoice != null && invoice.CustomerId == organization.GatewayCustomerId &&
|
||||
!string.IsNullOrWhiteSpace(invoice.HostedInvoiceUrl))
|
||||
{
|
||||
return new RedirectResult(invoice.HostedInvoiceUrl);
|
||||
}
|
||||
}
|
||||
catch(StripeException) { }
|
||||
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
[HttpGet("{id}/license")]
|
||||
[SelfHosted(NotSelfHostedOnly = true)]
|
||||
public async Task<OrganizationLicense> GetLicense(string id, [FromQuery]Guid installationId)
|
||||
|
@ -2,54 +2,25 @@
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using Bit.Core.Models.Business;
|
||||
using Bit.Core.Models.Table;
|
||||
using Bit.Core.Enums;
|
||||
|
||||
namespace Bit.Core.Models.Api
|
||||
{
|
||||
public class BillingResponseModel : ResponseModel
|
||||
{
|
||||
public BillingResponseModel(User user, BillingInfo billing, UserLicense license)
|
||||
public BillingResponseModel(BillingInfo billing)
|
||||
: base("billing")
|
||||
{
|
||||
CreditAmount = billing.CreditAmount;
|
||||
PaymentSource = billing.PaymentSource != null ? new BillingSource(billing.PaymentSource) : null;
|
||||
Subscription = billing.Subscription != null ? new BillingSubscription(billing.Subscription) : null;
|
||||
Transactions = billing.Transactions?.Select(t => new BillingTransaction(t));
|
||||
Invoices = billing.Invoices?.Select(i => new BillingInvoice(i));
|
||||
UpcomingInvoice = billing.UpcomingInvoice != null ? new BillingInvoiceInfo(billing.UpcomingInvoice) : null;
|
||||
StorageName = user.Storage.HasValue ? Utilities.CoreHelpers.ReadableBytesSize(user.Storage.Value) : null;
|
||||
StorageGb = user.Storage.HasValue ? Math.Round(user.Storage.Value / 1073741824D, 2) : 0; // 1 GB
|
||||
MaxStorageGb = user.MaxStorageGb;
|
||||
License = license;
|
||||
Expiration = License.Expires;
|
||||
}
|
||||
|
||||
public BillingResponseModel(User user, UserLicense license = null)
|
||||
: base("billing")
|
||||
{
|
||||
StorageName = user.Storage.HasValue ? Utilities.CoreHelpers.ReadableBytesSize(user.Storage.Value) : null;
|
||||
StorageGb = user.Storage.HasValue ? Math.Round(user.Storage.Value / 1073741824D, 2) : 0; // 1 GB
|
||||
MaxStorageGb = user.MaxStorageGb;
|
||||
Expiration = user.PremiumExpirationDate;
|
||||
|
||||
if(license != null)
|
||||
{
|
||||
License = license;
|
||||
}
|
||||
}
|
||||
|
||||
public decimal CreditAmount { get; set; }
|
||||
public string StorageName { get; set; }
|
||||
public double? StorageGb { get; set; }
|
||||
public short? MaxStorageGb { get; set; }
|
||||
public BillingSource PaymentSource { get; set; }
|
||||
public BillingSubscription Subscription { get; set; }
|
||||
public BillingInvoiceInfo UpcomingInvoice { get; set; }
|
||||
public IEnumerable<BillingInvoice> Invoices { get; set; }
|
||||
public IEnumerable<BillingTransaction> Transactions { get; set; }
|
||||
public UserLicense License { get; set; }
|
||||
public DateTime? Expiration { get; set; }
|
||||
}
|
||||
|
||||
public class BillingSource
|
||||
@ -68,74 +39,20 @@ namespace Bit.Core.Models.Api
|
||||
public bool NeedsVerification { get; set; }
|
||||
}
|
||||
|
||||
public class BillingSubscription
|
||||
public class BillingInvoice
|
||||
{
|
||||
public BillingSubscription(BillingInfo.BillingSubscription sub)
|
||||
{
|
||||
Status = sub.Status;
|
||||
TrialStartDate = sub.TrialStartDate;
|
||||
TrialEndDate = sub.TrialEndDate;
|
||||
PeriodStartDate = sub.PeriodStartDate;
|
||||
PeriodEndDate = sub.PeriodEndDate;
|
||||
CancelledDate = sub.CancelledDate;
|
||||
CancelAtEndDate = sub.CancelAtEndDate;
|
||||
Cancelled = sub.Cancelled;
|
||||
if(sub.Items != null)
|
||||
{
|
||||
Items = sub.Items.Select(i => new BillingSubscriptionItem(i));
|
||||
}
|
||||
}
|
||||
|
||||
public DateTime? TrialStartDate { get; set; }
|
||||
public DateTime? TrialEndDate { get; set; }
|
||||
public DateTime? PeriodStartDate { get; set; }
|
||||
public DateTime? PeriodEndDate { get; set; }
|
||||
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(BillingInfo.BillingSubscription.BillingSubscriptionItem item)
|
||||
{
|
||||
Name = item.Name;
|
||||
Amount = item.Amount;
|
||||
Interval = item.Interval;
|
||||
Quantity = item.Quantity;
|
||||
}
|
||||
|
||||
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(BillingInfo.BillingInvoiceInfo inv)
|
||||
public BillingInvoice(BillingInfo.BillingInvoice inv)
|
||||
{
|
||||
Amount = inv.Amount;
|
||||
Date = inv.Date;
|
||||
}
|
||||
|
||||
public decimal Amount { get; set; }
|
||||
public DateTime? Date { get; set; }
|
||||
}
|
||||
|
||||
public class BillingInvoice : BillingInvoiceInfo
|
||||
{
|
||||
public BillingInvoice(BillingInfo.BillingInvoice inv)
|
||||
: base(inv)
|
||||
{
|
||||
Url = inv.Url;
|
||||
PdfUrl = inv.PdfUrl;
|
||||
Number = inv.Number;
|
||||
Paid = inv.Paid;
|
||||
}
|
||||
|
||||
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; }
|
||||
|
@ -60,38 +60,34 @@ namespace Bit.Core.Models.Api
|
||||
public bool UsersGetPremium { get; set; }
|
||||
}
|
||||
|
||||
public class OrganizationBillingResponseModel : OrganizationResponseModel
|
||||
public class OrganizationSubscriptionResponseModel : OrganizationResponseModel
|
||||
{
|
||||
public OrganizationBillingResponseModel(Organization organization, BillingInfo billing)
|
||||
: base(organization, "organizationBilling")
|
||||
public OrganizationSubscriptionResponseModel(Organization organization, SubscriptionInfo subscription = null)
|
||||
: base(organization, "organizationSubscription")
|
||||
{
|
||||
PaymentSource = billing.PaymentSource != null ? new BillingSource(billing.PaymentSource) : null;
|
||||
Subscription = billing.Subscription != null ? new BillingSubscription(billing.Subscription) : null;
|
||||
Transactions = billing.Transactions?.Select(t => new BillingTransaction(t));
|
||||
Invoices = billing.Invoices?.Select(i => new BillingInvoice(i));
|
||||
UpcomingInvoice = billing.UpcomingInvoice != null ? new BillingInvoiceInfo(billing.UpcomingInvoice) : null;
|
||||
StorageName = organization.Storage.HasValue ?
|
||||
Utilities.CoreHelpers.ReadableBytesSize(organization.Storage.Value) : null;
|
||||
StorageGb = organization.Storage.HasValue ? Math.Round(organization.Storage.Value / 1073741824D) : 0; // 1 GB
|
||||
Expiration = DateTime.UtcNow.AddYears(1);
|
||||
}
|
||||
if(subscription != null)
|
||||
{
|
||||
Subscription = subscription.Subscription != null ?
|
||||
new BillingSubscription(subscription.Subscription) : null;
|
||||
UpcomingInvoice = subscription.UpcomingInvoice != null ?
|
||||
new BillingSubscriptionUpcomingInvoice(subscription.UpcomingInvoice) : null;
|
||||
Expiration = DateTime.UtcNow.AddYears(1); // TODO?
|
||||
}
|
||||
else
|
||||
{
|
||||
Expiration = organization.ExpirationDate;
|
||||
}
|
||||
|
||||
public OrganizationBillingResponseModel(Organization organization)
|
||||
: base(organization, "organizationBilling")
|
||||
{
|
||||
StorageName = organization.Storage.HasValue ?
|
||||
Utilities.CoreHelpers.ReadableBytesSize(organization.Storage.Value) : null;
|
||||
StorageGb = organization.Storage.HasValue ? Math.Round(organization.Storage.Value / 1073741824D, 2) : 0; // 1 GB
|
||||
Expiration = organization.ExpirationDate;
|
||||
StorageGb = organization.Storage.HasValue ?
|
||||
Math.Round(organization.Storage.Value / 1073741824D, 2) : 0; // 1 GB
|
||||
}
|
||||
|
||||
public string StorageName { get; set; }
|
||||
public double? StorageGb { get; set; }
|
||||
public BillingSource PaymentSource { get; set; }
|
||||
public BillingSubscription Subscription { get; set; }
|
||||
public BillingInvoiceInfo UpcomingInvoice { get; set; }
|
||||
public IEnumerable<BillingInvoice> Invoices { get; set; }
|
||||
public IEnumerable<BillingTransaction> Transactions { get; set; }
|
||||
public BillingSubscriptionUpcomingInvoice UpcomingInvoice { get; set; }
|
||||
public DateTime? Expiration { get; set; }
|
||||
}
|
||||
}
|
||||
|
103
src/Core/Models/Api/Response/SubscriptionResponseModel.cs
Normal file
103
src/Core/Models/Api/Response/SubscriptionResponseModel.cs
Normal file
@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using Bit.Core.Models.Business;
|
||||
using Bit.Core.Models.Table;
|
||||
|
||||
namespace Bit.Core.Models.Api
|
||||
{
|
||||
public class SubscriptionResponseModel : ResponseModel
|
||||
{
|
||||
public SubscriptionResponseModel(User user, SubscriptionInfo subscription, UserLicense license)
|
||||
: base("subscription")
|
||||
{
|
||||
Subscription = subscription.Subscription != null ? new BillingSubscription(subscription.Subscription) : null;
|
||||
UpcomingInvoice = subscription.UpcomingInvoice != null ?
|
||||
new BillingSubscriptionUpcomingInvoice(subscription.UpcomingInvoice) : null;
|
||||
StorageName = user.Storage.HasValue ? Utilities.CoreHelpers.ReadableBytesSize(user.Storage.Value) : null;
|
||||
StorageGb = user.Storage.HasValue ? Math.Round(user.Storage.Value / 1073741824D, 2) : 0; // 1 GB
|
||||
MaxStorageGb = user.MaxStorageGb;
|
||||
License = license;
|
||||
Expiration = License.Expires;
|
||||
}
|
||||
|
||||
public SubscriptionResponseModel(User user, UserLicense license = null)
|
||||
: base("subscription")
|
||||
{
|
||||
StorageName = user.Storage.HasValue ? Utilities.CoreHelpers.ReadableBytesSize(user.Storage.Value) : null;
|
||||
StorageGb = user.Storage.HasValue ? Math.Round(user.Storage.Value / 1073741824D, 2) : 0; // 1 GB
|
||||
MaxStorageGb = user.MaxStorageGb;
|
||||
Expiration = user.PremiumExpirationDate;
|
||||
|
||||
if(license != null)
|
||||
{
|
||||
License = license;
|
||||
}
|
||||
}
|
||||
|
||||
public string StorageName { get; set; }
|
||||
public double? StorageGb { get; set; }
|
||||
public short? MaxStorageGb { get; set; }
|
||||
public BillingSubscriptionUpcomingInvoice UpcomingInvoice { get; set; }
|
||||
public BillingSubscription Subscription { get; set; }
|
||||
public UserLicense License { get; set; }
|
||||
public DateTime? Expiration { get; set; }
|
||||
}
|
||||
|
||||
public class BillingSubscription
|
||||
{
|
||||
public BillingSubscription(SubscriptionInfo.BillingSubscription sub)
|
||||
{
|
||||
Status = sub.Status;
|
||||
TrialStartDate = sub.TrialStartDate;
|
||||
TrialEndDate = sub.TrialEndDate;
|
||||
PeriodStartDate = sub.PeriodStartDate;
|
||||
PeriodEndDate = sub.PeriodEndDate;
|
||||
CancelledDate = sub.CancelledDate;
|
||||
CancelAtEndDate = sub.CancelAtEndDate;
|
||||
Cancelled = sub.Cancelled;
|
||||
if(sub.Items != null)
|
||||
{
|
||||
Items = sub.Items.Select(i => new BillingSubscriptionItem(i));
|
||||
}
|
||||
}
|
||||
|
||||
public DateTime? TrialStartDate { get; set; }
|
||||
public DateTime? TrialEndDate { get; set; }
|
||||
public DateTime? PeriodStartDate { get; set; }
|
||||
public DateTime? PeriodEndDate { get; set; }
|
||||
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(SubscriptionInfo.BillingSubscription.BillingSubscriptionItem item)
|
||||
{
|
||||
Name = item.Name;
|
||||
Amount = item.Amount;
|
||||
Interval = item.Interval;
|
||||
Quantity = item.Quantity;
|
||||
}
|
||||
|
||||
public string Name { get; set; }
|
||||
public decimal Amount { get; set; }
|
||||
public int Quantity { get; set; }
|
||||
public string Interval { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
public class BillingSubscriptionUpcomingInvoice
|
||||
{
|
||||
public BillingSubscriptionUpcomingInvoice(SubscriptionInfo.BillingUpcomingInvoice inv)
|
||||
{
|
||||
Amount = inv.Amount;
|
||||
Date = inv.Date;
|
||||
}
|
||||
|
||||
public decimal Amount { get; set; }
|
||||
public DateTime? Date { get; set; }
|
||||
}
|
||||
}
|
@ -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));
|
||||
|
@ -17,7 +17,7 @@ namespace Bit.Core.Services
|
||||
Task ReinstateSubscriptionAsync(ISubscriber subscriber);
|
||||
Task<bool> UpdatePaymentMethodAsync(ISubscriber subscriber, PaymentMethodType paymentMethodType,
|
||||
string paymentToken);
|
||||
Task<BillingInfo.BillingInvoiceInfo> GetUpcomingInvoiceAsync(ISubscriber subscriber);
|
||||
Task<BillingInfo> GetBillingAsync(ISubscriber subscriber);
|
||||
Task<SubscriptionInfo> GetSubscriptionAsync(ISubscriber subscriber);
|
||||
}
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ namespace Bit.Core.Services
|
||||
Task DisablePremiumAsync(Guid userId, DateTime? expirationDate);
|
||||
Task DisablePremiumAsync(User user, DateTime? expirationDate);
|
||||
Task UpdatePremiumExpirationAsync(Guid userId, DateTime? expirationDate);
|
||||
Task<UserLicense> GenerateLicenseAsync(User user, BillingInfo billingInfo = null);
|
||||
Task<UserLicense> GenerateLicenseAsync(User user, SubscriptionInfo subscriptionInfo = null);
|
||||
Task<bool> CheckPasswordAsync(User user, string password);
|
||||
Task<bool> CanAccessPremium(ITwoFactorProvidersUser user);
|
||||
Task<bool> TwoFactorIsEnabledAsync(ITwoFactorProvidersUser user);
|
||||
|
@ -1207,8 +1207,8 @@ namespace Bit.Core.Services
|
||||
throw new BadRequestException("Invalid installation id");
|
||||
}
|
||||
|
||||
var billingInfo = await _paymentService.GetBillingAsync(organization);
|
||||
return new OrganizationLicense(organization, billingInfo, installationId, _licensingService);
|
||||
var subInfo = await _paymentService.GetSubscriptionAsync(organization);
|
||||
return new OrganizationLicense(organization, subInfo, installationId, _licensingService);
|
||||
}
|
||||
|
||||
public async Task ImportAsync(Guid organizationId,
|
||||
|
@ -885,35 +885,6 @@ namespace Bit.Core.Services
|
||||
return createdCustomer;
|
||||
}
|
||||
|
||||
public async Task<BillingInfo.BillingInvoiceInfo> GetUpcomingInvoiceAsync(ISubscriber subscriber)
|
||||
{
|
||||
if(!string.IsNullOrWhiteSpace(subscriber.GatewaySubscriptionId))
|
||||
{
|
||||
var subscriptionService = new SubscriptionService();
|
||||
var invoiceService = new InvoiceService();
|
||||
var sub = await subscriptionService.GetAsync(subscriber.GatewaySubscriptionId);
|
||||
if(sub != null)
|
||||
{
|
||||
if(!sub.CanceledAt.HasValue && !string.IsNullOrWhiteSpace(subscriber.GatewayCustomerId))
|
||||
{
|
||||
try
|
||||
{
|
||||
var upcomingInvoice = await invoiceService.UpcomingAsync(new UpcomingInvoiceOptions
|
||||
{
|
||||
CustomerId = subscriber.GatewayCustomerId
|
||||
});
|
||||
if(upcomingInvoice != null)
|
||||
{
|
||||
return new BillingInfo.BillingInvoiceInfo(upcomingInvoice);
|
||||
}
|
||||
}
|
||||
catch(StripeException) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<BillingInfo> GetBillingAsync(ISubscriber subscriber)
|
||||
{
|
||||
var billingInfo = new BillingInfo();
|
||||
@ -990,12 +961,21 @@ namespace Bit.Core.Services
|
||||
}
|
||||
}
|
||||
|
||||
return billingInfo;
|
||||
}
|
||||
|
||||
public async Task<SubscriptionInfo> GetSubscriptionAsync(ISubscriber subscriber)
|
||||
{
|
||||
var subscriptionInfo = new SubscriptionInfo();
|
||||
var subscriptionService = new SubscriptionService();
|
||||
var invoiceService = new InvoiceService();
|
||||
|
||||
if(!string.IsNullOrWhiteSpace(subscriber.GatewaySubscriptionId))
|
||||
{
|
||||
var sub = await subscriptionService.GetAsync(subscriber.GatewaySubscriptionId);
|
||||
if(sub != null)
|
||||
{
|
||||
billingInfo.Subscription = new BillingInfo.BillingSubscription(sub);
|
||||
subscriptionInfo.Subscription = new SubscriptionInfo.BillingSubscription(sub);
|
||||
}
|
||||
|
||||
if(!sub.CanceledAt.HasValue && !string.IsNullOrWhiteSpace(subscriber.GatewayCustomerId))
|
||||
@ -1006,14 +986,15 @@ namespace Bit.Core.Services
|
||||
new UpcomingInvoiceOptions { CustomerId = subscriber.GatewayCustomerId });
|
||||
if(upcomingInvoice != null)
|
||||
{
|
||||
billingInfo.UpcomingInvoice = new BillingInfo.BillingInvoiceInfo(upcomingInvoice);
|
||||
subscriptionInfo.UpcomingInvoice =
|
||||
new SubscriptionInfo.BillingUpcomingInvoice(upcomingInvoice);
|
||||
}
|
||||
}
|
||||
catch(StripeException) { }
|
||||
}
|
||||
}
|
||||
|
||||
return billingInfo;
|
||||
return subscriptionInfo;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -868,20 +868,20 @@ namespace Bit.Core.Services
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<UserLicense> GenerateLicenseAsync(User user, BillingInfo billingInfo = null)
|
||||
public async Task<UserLicense> GenerateLicenseAsync(User user, SubscriptionInfo subscriptionInfo = null)
|
||||
{
|
||||
if(user == null)
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
if(billingInfo == null && user.Gateway != null)
|
||||
if(subscriptionInfo == null && user.Gateway != null)
|
||||
{
|
||||
billingInfo = await _paymentService.GetBillingAsync(user);
|
||||
subscriptionInfo = await _paymentService.GetSubscriptionAsync(user);
|
||||
}
|
||||
|
||||
return billingInfo == null ? new UserLicense(user, _licenseService) :
|
||||
new UserLicense(user, billingInfo, _licenseService);
|
||||
return subscriptionInfo == null ? new UserLicense(user, _licenseService) :
|
||||
new UserLicense(user, subscriptionInfo, _licenseService);
|
||||
}
|
||||
|
||||
public override async Task<bool> CheckPasswordAsync(User user, string password)
|
||||
|
Loading…
Reference in New Issue
Block a user