1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-26 12:55:17 +01:00

prorate fix

This commit is contained in:
Kyle Spearrin 2017-05-19 18:38:47 -04:00
parent ef945cc7c4
commit 35a31a4496
2 changed files with 31 additions and 14 deletions

View File

@ -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<ValidationResult> Validate(ValidationContext validationContext)
{
if(SeatAdjustment == 0)
{
yield return new ValidationResult("Seat adjustment cannot be 0.", new string[] { nameof(SeatAdjustment) });
}
}
}
}

View File

@ -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) { }
}
}