mirror of
https://github.com/bitwarden/server.git
synced 2025-02-13 01:21:29 +01:00
prorate until next due date if charge is < $5
This commit is contained in:
parent
340f6f03c5
commit
c28106c81b
@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
# Visual Studio 15
|
# Visual Studio 15
|
||||||
VisualStudioVersion = 15.0.26403.0
|
VisualStudioVersion = 15.0.26403.3
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D}"
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D}"
|
||||||
EndProject
|
EndProject
|
||||||
@ -22,8 +22,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mail", "src\Mail\Mail.cspro
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Billing", "src\Billing\Billing.csproj", "{02BC2982-ED8D-4A6D-A41E-092B3DAEB98A}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Billing", "src\Billing\Billing.csproj", "{02BC2982-ED8D-4A6D-A41E-092B3DAEB98A}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stripe.net", "..\stripe.net\src\Stripe.net\Stripe.net.csproj", "{F9D4653F-1E61-44E3-953F-0D67D60195C7}"
|
|
||||||
EndProject
|
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
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}.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.ActiveCfg = Release|Any CPU
|
||||||
{02BC2982-ED8D-4A6D-A41E-092B3DAEB98A}.Release|Any CPU.Build.0 = 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
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
@ -66,6 +60,5 @@ Global
|
|||||||
{58554E52-FDEC-4832-AFF9-302B01E08DCA} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D}
|
{58554E52-FDEC-4832-AFF9-302B01E08DCA} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D}
|
||||||
{B78A6C74-1A24-48C6-802A-13BE3E4DAFF1} = {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}
|
{02BC2982-ED8D-4A6D-A41E-092B3DAEB98A} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D}
|
||||||
{F9D4653F-1E61-44E3-953F-0D67D60195C7} = {DD5BD056-4AAE-43EF-BBD2-0B569B8DA84D}
|
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
<PackageReference Include="Newtonsoft.Json" Version="10.0.1" />
|
<PackageReference Include="Newtonsoft.Json" Version="10.0.1" />
|
||||||
<PackageReference Include="Sendgrid" Version="9.0.12" />
|
<PackageReference Include="Sendgrid" Version="9.0.12" />
|
||||||
<PackageReference Include="PushSharp" Version="4.0.10" />
|
<PackageReference Include="PushSharp" Version="4.0.10" />
|
||||||
|
<PackageReference Include="Stripe.net" Version="7.8.0" />
|
||||||
<PackageReference Include="WindowsAzure.Storage" Version="8.1.1" />
|
<PackageReference Include="WindowsAzure.Storage" Version="8.1.1" />
|
||||||
<PackageReference Include="Otp.NET" Version="1.0.1" />
|
<PackageReference Include="Otp.NET" Version="1.0.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
@ -33,8 +34,4 @@
|
|||||||
<Reference Include="Microsoft.CSharp" />
|
<Reference Include="Microsoft.CSharp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\..\..\stripe.net\src\Stripe.net\Stripe.net.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -10,6 +10,7 @@ using System.Collections.Generic;
|
|||||||
using Microsoft.AspNetCore.DataProtection;
|
using Microsoft.AspNetCore.DataProtection;
|
||||||
using Stripe;
|
using Stripe;
|
||||||
using Bit.Core.Enums;
|
using Bit.Core.Enums;
|
||||||
|
using Bit.Core.Models.StaticStore;
|
||||||
|
|
||||||
namespace Bit.Core.Services
|
namespace Bit.Core.Services
|
||||||
{
|
{
|
||||||
@ -384,10 +385,9 @@ namespace Bit.Core.Services
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var subscriptionService = new StripeSubscriptionService();
|
|
||||||
var invoiceService = new StripeInvoiceService();
|
var invoiceService = new StripeInvoiceService();
|
||||||
var invoiceItemService = new StripeInvoiceItemService();
|
|
||||||
var subscriptionItemService = new StripeSubscriptionItemService();
|
var subscriptionItemService = new StripeSubscriptionItemService();
|
||||||
|
var subscriptionService = new StripeSubscriptionService();
|
||||||
var sub = await subscriptionService.GetAsync(organization.StripeSubscriptionId);
|
var sub = await subscriptionService.GetAsync(organization.StripeSubscriptionId);
|
||||||
if(sub == null)
|
if(sub == null)
|
||||||
{
|
{
|
||||||
@ -397,100 +397,26 @@ namespace Bit.Core.Services
|
|||||||
var seatItem = sub.Items?.Data?.FirstOrDefault(i => i.Plan.Id == plan.StripeSeatPlanId);
|
var seatItem = sub.Items?.Data?.FirstOrDefault(i => i.Plan.Id == plan.StripeSeatPlanId);
|
||||||
if(seatItem == null)
|
if(seatItem == null)
|
||||||
{
|
{
|
||||||
var upcomingPreview = await invoiceService.UpcomingAsync(organization.StripeCustomerId,
|
await subscriptionItemService.CreateAsync(new StripeSubscriptionItemCreateOptions
|
||||||
new StripeUpcomingInvoiceOptions
|
|
||||||
{
|
|
||||||
SubscriptionId = organization.StripeSubscriptionId,
|
|
||||||
SubscriptionItems = new List<StripeInvoiceSubscriptionItemOptions>
|
|
||||||
{
|
|
||||||
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
|
|
||||||
{
|
{
|
||||||
PlanId = plan.StripeSeatPlanId,
|
PlanId = plan.StripeSeatPlanId,
|
||||||
Quantity = additionalSeats,
|
Quantity = additionalSeats,
|
||||||
Prorate = prorateSub,
|
Prorate = true,
|
||||||
SubscriptionId = sub.Id
|
SubscriptionId = sub.Id
|
||||||
};
|
});
|
||||||
|
|
||||||
await subscriptionItemService.CreateAsync(subItemCreateOptions);
|
await PreviewUpcomingAndPayAsync(invoiceService, organization, plan);
|
||||||
}
|
}
|
||||||
else if(additionalSeats > 0)
|
else if(additionalSeats > 0)
|
||||||
{
|
{
|
||||||
var upcomingPreview = await invoiceService.UpcomingAsync(organization.StripeCustomerId,
|
await subscriptionItemService.UpdateAsync(seatItem.Id, new StripeSubscriptionItemUpdateOptions
|
||||||
new StripeUpcomingInvoiceOptions
|
|
||||||
{
|
|
||||||
SubscriptionId = organization.StripeSubscriptionId,
|
|
||||||
SubscriptionItems = new List<StripeInvoiceSubscriptionItemOptions>
|
|
||||||
{
|
|
||||||
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
|
|
||||||
{
|
{
|
||||||
PlanId = plan.StripeSeatPlanId,
|
PlanId = plan.StripeSeatPlanId,
|
||||||
Quantity = additionalSeats,
|
Quantity = additionalSeats,
|
||||||
Prorate = prorateSub
|
Prorate = true
|
||||||
};
|
});
|
||||||
|
|
||||||
await subscriptionItemService.UpdateAsync(seatItem.Id, subItemUpdateOptions);
|
await PreviewUpcomingAndPayAsync(invoiceService, organization, plan);
|
||||||
}
|
}
|
||||||
else if(additionalSeats == 0)
|
else if(additionalSeats == 0)
|
||||||
{
|
{
|
||||||
@ -501,6 +427,29 @@ namespace Bit.Core.Services
|
|||||||
await _organizationRepository.ReplaceAsync(organization);
|
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<Tuple<Organization, OrganizationUser>> SignUpAsync(OrganizationSignup signup)
|
public async Task<Tuple<Organization, OrganizationUser>> SignUpAsync(OrganizationSignup signup)
|
||||||
{
|
{
|
||||||
var plan = StaticStore.Plans.FirstOrDefault(p => p.Type == signup.Plan && !p.Disabled);
|
var plan = StaticStore.Plans.FirstOrDefault(p => p.Type == signup.Plan && !p.Disabled);
|
||||||
|
Loading…
Reference in New Issue
Block a user