From ee9ec680a90c4ab68e36032455ff3ddfb0e203c0 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 15 Aug 2017 15:31:42 -0400 Subject: [PATCH] license dates --- .../Controllers/OrganizationsController.cs | 5 ++- .../Api/Response/BillingResponseModel.cs | 6 ++- src/Core/Models/Business/BillingInfo.cs | 10 +++-- .../Models/Business/OrganizationLicense.cs | 41 +++++++++++++++++-- src/Core/Models/Business/UserLicense.cs | 3 +- 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/src/Api/Controllers/OrganizationsController.cs b/src/Api/Controllers/OrganizationsController.cs index 72e9e9dad1..3ca4044995 100644 --- a/src/Api/Controllers/OrganizationsController.cs +++ b/src/Api/Controllers/OrganizationsController.cs @@ -10,7 +10,6 @@ using Bit.Core.Services; using Bit.Core; using Microsoft.AspNetCore.Identity; using Bit.Core.Models.Table; -using Bit.Core.Utilities; using Bit.Api.Utilities; using Bit.Core.Models.Business; @@ -114,7 +113,9 @@ namespace Bit.Api.Controllers throw new NotFoundException(); } - return new OrganizationLicense(organization, installationId, _licensingService); + var paymentService = new StripePaymentService(); + var billingInfo = await paymentService.GetBillingAsync(organization); + return new OrganizationLicense(organization, billingInfo, installationId, _licensingService); } [HttpGet("")] diff --git a/src/Core/Models/Api/Response/BillingResponseModel.cs b/src/Core/Models/Api/Response/BillingResponseModel.cs index 2f6a17ef44..ae4aa4c150 100644 --- a/src/Core/Models/Api/Response/BillingResponseModel.cs +++ b/src/Core/Models/Api/Response/BillingResponseModel.cs @@ -67,7 +67,8 @@ namespace Bit.Core.Models.Api Status = sub.Status; TrialStartDate = sub.TrialStartDate; TrialEndDate = sub.TrialEndDate; - EndDate = sub.EndDate; + PeriodStartDate = sub.PeriodStartDate; + PeriodEndDate = sub.PeriodEndDate; CancelledDate = sub.CancelledDate; CancelAtEndDate = sub.CancelAtEndDate; Cancelled = sub.Cancelled; @@ -79,7 +80,8 @@ namespace Bit.Core.Models.Api public DateTime? TrialStartDate { get; set; } public DateTime? TrialEndDate { get; set; } - public DateTime? EndDate { 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; } diff --git a/src/Core/Models/Business/BillingInfo.cs b/src/Core/Models/Business/BillingInfo.cs index d39c98814a..df3db64bda 100644 --- a/src/Core/Models/Business/BillingInfo.cs +++ b/src/Core/Models/Business/BillingInfo.cs @@ -95,7 +95,8 @@ namespace Bit.Core.Models.Business Status = sub.Status; TrialStartDate = sub.TrialStart; TrialEndDate = sub.TrialEnd; - EndDate = sub.CurrentPeriodEnd; + PeriodStartDate = sub.CurrentPeriodStart; + PeriodEndDate = sub.CurrentPeriodEnd; CancelledDate = sub.CanceledAt; CancelAtEndDate = sub.CancelAtPeriodEnd; Cancelled = sub.Status == "cancelled"; @@ -122,7 +123,8 @@ namespace Bit.Core.Models.Business } } - EndDate = sub.BillingPeriodEndDate; + PeriodStartDate = sub.BillingPeriodStartDate; + PeriodEndDate = sub.BillingPeriodEndDate; CancelAtEndDate = !sub.NeverExpires.GetValueOrDefault(); Cancelled = sub.Status == SubscriptionStatus.CANCELED; @@ -146,7 +148,9 @@ namespace Bit.Core.Models.Business public DateTime? TrialStartDate { get; set; } public DateTime? TrialEndDate { get; set; } - public DateTime? EndDate { 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; } diff --git a/src/Core/Models/Business/OrganizationLicense.cs b/src/Core/Models/Business/OrganizationLicense.cs index 6b5c61d92c..48054f6b73 100644 --- a/src/Core/Models/Business/OrganizationLicense.cs +++ b/src/Core/Models/Business/OrganizationLicense.cs @@ -13,8 +13,10 @@ namespace Bit.Core.Models.Business public OrganizationLicense() { } - public OrganizationLicense(Organization org, Guid installationId, ILicensingService licenseService) + public OrganizationLicense(Organization org, BillingInfo billingInfo, Guid installationId, + ILicensingService licenseService) { + Version = 1; LicenseKey = org.LicenseKey; InstallationId = installationId; Id = org.Id; @@ -29,10 +31,41 @@ namespace Bit.Core.Models.Business UseTotp = org.UseTotp; MaxStorageGb = org.MaxStorageGb; SelfHost = org.SelfHost; - Version = 1; Issued = DateTime.UtcNow; - Expires = Issued.AddYears(1); - Trial = false; + + if(billingInfo?.Subscription == null) + { + Expires = Refresh = Issued.AddDays(7); + Trial = true; + } + else if(billingInfo.Subscription.TrialEndDate.HasValue && + billingInfo.Subscription.TrialEndDate.Value < DateTime.UtcNow) + { + Expires = Refresh = billingInfo.Subscription.TrialEndDate.Value; + Trial = true; + } + else + { + if(org.ExpirationDate.HasValue && org.ExpirationDate.Value < DateTime.UtcNow) + { + // expired + Expires = Refresh = org.ExpirationDate.Value; + } + else if(billingInfo?.Subscription?.PeriodDuration != null && + billingInfo.Subscription.PeriodDuration > TimeSpan.FromDays(180)) + { + Refresh = DateTime.UtcNow.AddDays(30); + Expires = billingInfo?.Subscription.PeriodEndDate.Value.AddDays(60); + } + else + { + Expires = org.ExpirationDate.HasValue ? org.ExpirationDate.Value.AddMonths(11) : Issued.AddYears(1); + Refresh = DateTime.UtcNow - Expires > TimeSpan.FromDays(30) ? DateTime.UtcNow.AddDays(30) : Expires; + } + + Trial = false; + } + Signature = Convert.ToBase64String(licenseService.SignLicense(this)); } diff --git a/src/Core/Models/Business/UserLicense.cs b/src/Core/Models/Business/UserLicense.cs index 926ff91a69..ea33693f07 100644 --- a/src/Core/Models/Business/UserLicense.cs +++ b/src/Core/Models/Business/UserLicense.cs @@ -22,7 +22,8 @@ namespace Bit.Core.Models.Business Premium = user.Premium; MaxStorageGb = user.MaxStorageGb; Issued = DateTime.UtcNow; - Expires = billingInfo?.UpcomingInvoice?.Date; + Expires = billingInfo?.UpcomingInvoice?.Date?.AddDays(7); + Refresh = billingInfo?.UpcomingInvoice?.Date; Trial = (billingInfo?.Subscription?.TrialEndDate.HasValue ?? false) && billingInfo.Subscription.TrialEndDate.Value > DateTime.UtcNow; Signature = Convert.ToBase64String(licenseService.SignLicense(this));