From c28106c81b054f87eff2c38acad24933b2a45f97 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 11 Apr 2017 10:00:36 -0400 Subject: [PATCH] prorate until next due date if charge is < $5 --- bitwarden-core.sln | 9 +- src/Core/Core.csproj | 5 +- .../Implementations/OrganizationService.cs | 117 +++++------------- 3 files changed, 35 insertions(+), 96 deletions(-) diff --git a/bitwarden-core.sln b/bitwarden-core.sln index 5bac72065b..085923b743 100644 --- a/bitwarden-core.sln +++ b/bitwarden-core.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26403.0 +VisualStudioVersion = 15.0.26403.3 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D}" EndProject @@ -22,8 +22,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mail", "src\Mail\Mail.cspro EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Billing", "src\Billing\Billing.csproj", "{02BC2982-ED8D-4A6D-A41E-092B3DAEB98A}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stripe.net", "..\stripe.net\src\Stripe.net\Stripe.net.csproj", "{F9D4653F-1E61-44E3-953F-0D67D60195C7}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -52,10 +50,6 @@ Global {02BC2982-ED8D-4A6D-A41E-092B3DAEB98A}.Debug|Any CPU.Build.0 = Debug|Any CPU {02BC2982-ED8D-4A6D-A41E-092B3DAEB98A}.Release|Any CPU.ActiveCfg = Release|Any CPU {02BC2982-ED8D-4A6D-A41E-092B3DAEB98A}.Release|Any CPU.Build.0 = Release|Any CPU - {F9D4653F-1E61-44E3-953F-0D67D60195C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F9D4653F-1E61-44E3-953F-0D67D60195C7}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F9D4653F-1E61-44E3-953F-0D67D60195C7}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F9D4653F-1E61-44E3-953F-0D67D60195C7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -66,6 +60,5 @@ Global {58554E52-FDEC-4832-AFF9-302B01E08DCA} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D} {B78A6C74-1A24-48C6-802A-13BE3E4DAFF1} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D} {02BC2982-ED8D-4A6D-A41E-092B3DAEB98A} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D} - {F9D4653F-1E61-44E3-953F-0D67D60195C7} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D} EndGlobalSection EndGlobal diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 2af3ec39af..85264c47c9 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -22,6 +22,7 @@ + @@ -33,8 +34,4 @@ - - - - diff --git a/src/Core/Services/Implementations/OrganizationService.cs b/src/Core/Services/Implementations/OrganizationService.cs index 79a3f4caab..2a74d524e8 100644 --- a/src/Core/Services/Implementations/OrganizationService.cs +++ b/src/Core/Services/Implementations/OrganizationService.cs @@ -10,6 +10,7 @@ using System.Collections.Generic; using Microsoft.AspNetCore.DataProtection; using Stripe; using Bit.Core.Enums; +using Bit.Core.Models.StaticStore; namespace Bit.Core.Services { @@ -384,10 +385,9 @@ namespace Bit.Core.Services } } - var subscriptionService = new StripeSubscriptionService(); var invoiceService = new StripeInvoiceService(); - var invoiceItemService = new StripeInvoiceItemService(); var subscriptionItemService = new StripeSubscriptionItemService(); + var subscriptionService = new StripeSubscriptionService(); var sub = await subscriptionService.GetAsync(organization.StripeSubscriptionId); if(sub == null) { @@ -397,100 +397,26 @@ namespace Bit.Core.Services var seatItem = sub.Items?.Data?.FirstOrDefault(i => i.Plan.Id == plan.StripeSeatPlanId); if(seatItem == null) { - var upcomingPreview = await invoiceService.UpcomingAsync(organization.StripeCustomerId, - new StripeUpcomingInvoiceOptions - { - SubscriptionId = organization.StripeSubscriptionId, - SubscriptionItems = new List - { - new StripeInvoiceSubscriptionItemOptions - { - PlanId = plan.StripeSeatPlanId, - Quantity = additionalSeats - } - } - }); - - var prorateSub = true; - var prorationAmount = upcomingPreview.StripeInvoiceLineItems?.Data?.Last()?.Amount; - if(prorationAmount.GetValueOrDefault() > 0) - { - var invoiceItem = await invoiceItemService.CreateAsync(new StripeInvoiceItemCreateOptions - { - SubscriptionId = organization.StripeSubscriptionId, - CustomerId = organization.StripeCustomerId, - Amount = prorationAmount.Value, - Description = $"Prorated amount for ${additionalSeats} additional seats.", - Currency = "USD" - }); - - var invoice = await invoiceService.CreateAsync(organization.StripeCustomerId, - new StripeInvoiceCreateOptions - { - SubscriptionId = organization.StripeSubscriptionId - }); - - var paidInvoice = await invoiceService.PayAsync(invoice.Id); - prorateSub = !paidInvoice.Paid; - } - - var subItemCreateOptions = new StripeSubscriptionItemCreateOptions + await subscriptionItemService.CreateAsync(new StripeSubscriptionItemCreateOptions { PlanId = plan.StripeSeatPlanId, Quantity = additionalSeats, - Prorate = prorateSub, + Prorate = true, SubscriptionId = sub.Id - }; + }); - await subscriptionItemService.CreateAsync(subItemCreateOptions); + await PreviewUpcomingAndPayAsync(invoiceService, organization, plan); } else if(additionalSeats > 0) { - var upcomingPreview = await invoiceService.UpcomingAsync(organization.StripeCustomerId, - new StripeUpcomingInvoiceOptions - { - SubscriptionId = organization.StripeSubscriptionId, - SubscriptionItems = new List - { - new StripeInvoiceSubscriptionItemOptions - { - Id = seatItem.Id, - Quantity = additionalSeats - } - } - }); - - var prorateSub = true; - var prorationAmount = upcomingPreview.StripeInvoiceLineItems?.Data?.Take(2).Sum(i => i.Amount); - if(prorationAmount.GetValueOrDefault() > 0) - { - var invoiceItem = await invoiceItemService.CreateAsync(new StripeInvoiceItemCreateOptions - { - SubscriptionId = organization.StripeSubscriptionId, - CustomerId = organization.StripeCustomerId, - Amount = prorationAmount.Value, - Description = $"Prorated amount for ${additionalSeats} additional seats.", - Currency = "USD" - }); - - var invoice = await invoiceService.CreateAsync(organization.StripeCustomerId, - new StripeInvoiceCreateOptions - { - SubscriptionId = organization.StripeSubscriptionId - }); - - var paidInvoice = await invoiceService.PayAsync(invoice.Id); - prorateSub = !paidInvoice.Paid; - } - - var subItemUpdateOptions = new StripeSubscriptionItemUpdateOptions + await subscriptionItemService.UpdateAsync(seatItem.Id, new StripeSubscriptionItemUpdateOptions { PlanId = plan.StripeSeatPlanId, Quantity = additionalSeats, - Prorate = prorateSub - }; + Prorate = true + }); - await subscriptionItemService.UpdateAsync(seatItem.Id, subItemUpdateOptions); + await PreviewUpcomingAndPayAsync(invoiceService, organization, plan); } else if(additionalSeats == 0) { @@ -501,6 +427,29 @@ namespace Bit.Core.Services await _organizationRepository.ReplaceAsync(organization); } + private async Task PreviewUpcomingAndPayAsync(StripeInvoiceService invoiceService, Organization org, Plan plan) + { + var upcomingPreview = await invoiceService.UpcomingAsync(org.StripeCustomerId, + new StripeUpcomingInvoiceOptions + { + SubscriptionId = org.StripeSubscriptionId + }); + + var prorationAmount = upcomingPreview.StripeInvoiceLineItems?.Data? + .TakeWhile(i => i.Plan.Id == plan.StripeSeatPlanId).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 + }); + + await invoiceService.PayAsync(invoice.Id); + } + } + public async Task> SignUpAsync(OrganizationSignup signup) { var plan = StaticStore.Plans.FirstOrDefault(p => p.Type == signup.Plan && !p.Disabled);