From 35a31a44964676008a9a6d0d2fe4771c69c3cc7d Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 19 May 2017 18:38:47 -0400 Subject: [PATCH] prorate fix --- .../OrganizationSeatRequestModel.cs | 14 +++++++-- .../Implementations/OrganizationService.cs | 31 ++++++++++++------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/Core/Models/Api/Request/Organizations/OrganizationSeatRequestModel.cs b/src/Core/Models/Api/Request/Organizations/OrganizationSeatRequestModel.cs index 055a331cdc..e2e5e827b1 100644 --- a/src/Core/Models/Api/Request/Organizations/OrganizationSeatRequestModel.cs +++ b/src/Core/Models/Api/Request/Organizations/OrganizationSeatRequestModel.cs @@ -1,10 +1,20 @@ -using System.ComponentModel.DataAnnotations; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; namespace Bit.Core.Models.Api { - public class OrganizationSeatRequestModel + public class OrganizationSeatRequestModel : IValidatableObject { [Required] public int? SeatAdjustment { get; set; } + + public IEnumerable Validate(ValidationContext validationContext) + { + if(SeatAdjustment == 0) + { + yield return new ValidationResult("Seat adjustment cannot be 0.", new string[] { nameof(SeatAdjustment) }); + } + } } } diff --git a/src/Core/Services/Implementations/OrganizationService.cs b/src/Core/Services/Implementations/OrganizationService.cs index 0dd02231b5..3b36986c69 100644 --- a/src/Core/Services/Implementations/OrganizationService.cs +++ b/src/Core/Services/Implementations/OrganizationService.cs @@ -401,7 +401,6 @@ namespace Bit.Core.Services } } - var invoiceService = new StripeInvoiceService(); var subscriptionItemService = new StripeSubscriptionItemService(); var subscriptionService = new StripeSubscriptionService(); var sub = await subscriptionService.GetAsync(organization.StripeSubscriptionId); @@ -421,7 +420,7 @@ namespace Bit.Core.Services SubscriptionId = sub.Id }); - await PreviewUpcomingAndPayAsync(invoiceService, organization, plan); + await PreviewUpcomingAndPayAsync(organization, plan); } else if(additionalSeats > 0) { @@ -432,7 +431,7 @@ namespace Bit.Core.Services Prorate = true }); - await PreviewUpcomingAndPayAsync(invoiceService, organization, plan); + await PreviewUpcomingAndPayAsync(organization, plan); } else if(additionalSeats == 0) { @@ -443,8 +442,9 @@ namespace Bit.Core.Services await _organizationRepository.ReplaceAsync(organization); } - private async Task PreviewUpcomingAndPayAsync(StripeInvoiceService invoiceService, Organization org, Plan plan) + private async Task PreviewUpcomingAndPayAsync(Organization org, Plan plan) { + var invoiceService = new StripeInvoiceService(); var upcomingPreview = await invoiceService.UpcomingAsync(org.StripeCustomerId, new StripeUpcomingInvoiceOptions { @@ -452,17 +452,24 @@ namespace Bit.Core.Services }); var prorationAmount = upcomingPreview.StripeInvoiceLineItems?.Data? - .TakeWhile(i => i.Plan.Id == plan.StripeSeatPlanId).Sum(i => i.Amount); + .TakeWhile(i => i.Plan.Id == plan.StripeSeatPlanId && i.Proration).Sum(i => i.Amount); if(prorationAmount.GetValueOrDefault() >= 500) { - // Owes more than $5.00 on next invoice. Invoice them and pay now instead of waiting until next month. - var invoice = await invoiceService.CreateAsync(org.StripeCustomerId, - new StripeInvoiceCreateOptions - { - SubscriptionId = org.StripeSubscriptionId - }); + try + { + // Owes more than $5.00 on next invoice. Invoice them and pay now instead of waiting until next month. + var invoice = await invoiceService.CreateAsync(org.StripeCustomerId, + new StripeInvoiceCreateOptions + { + SubscriptionId = org.StripeSubscriptionId + }); - await invoiceService.PayAsync(invoice.Id); + if(invoice.AmountDue > 0) + { + await invoiceService.PayAsync(invoice.Id); + } + } + catch(StripeException) { } } }