1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-30 23:52:50 -05:00

[EC-144] Fix stripe revert logic (#2014)

* Revert scaling by previous value

* Throw is Stripe subscription revert fails

* Remove unused property

* Add null check to accommodate for not existing storage-gb-xxx subscription item

* Use long? instead of Nullable<long>

* Remove redundant try/catch

* Ensure collectionMethod is changed back, even when revertSub fails

Co-authored-by: Matt Gibson <mgibson@bitwarden.com>
This commit is contained in:
Daniel James Smith
2022-05-31 22:55:09 +02:00
committed by GitHub
parent 39ba68e66b
commit 610be2cdcc
2 changed files with 68 additions and 50 deletions

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using Bit.Core.Entities;
using Stripe;
@ -34,16 +35,17 @@ namespace Bit.Core.Models.Business
public class SeatSubscriptionUpdate : SubscriptionUpdate
{
private readonly Organization _organization;
private readonly int _previousSeats;
private readonly StaticStore.Plan _plan;
private readonly long? _additionalSeats;
protected override List<string> PlanIds => new() { _plan.StripeSeatPlanId };
public SeatSubscriptionUpdate(Organization organization, StaticStore.Plan plan, long? additionalSeats)
{
_organization = organization;
_plan = plan;
_additionalSeats = additionalSeats;
_previousSeats = organization.Seats ?? 0;
}
public override List<SubscriptionItemOptions> UpgradeItemsOptions(Subscription subscription)
@ -63,6 +65,7 @@ namespace Bit.Core.Models.Business
public override List<SubscriptionItemOptions> RevertItemsOptions(Subscription subscription)
{
var item = SubscriptionItem(subscription, PlanIds.Single());
return new()
{
@ -70,8 +73,8 @@ namespace Bit.Core.Models.Business
{
Id = item?.Id,
Plan = PlanIds.Single(),
Quantity = _organization.Seats,
Deleted = item?.Id != null ? true : (bool?)null,
Quantity = _previousSeats,
Deleted = _previousSeats == 0 ? true : (bool?)null,
}
};
}
@ -79,6 +82,7 @@ namespace Bit.Core.Models.Business
public class StorageSubscriptionUpdate : SubscriptionUpdate
{
private long? _prevStorage;
private readonly string _plan;
private readonly long? _additionalStorage;
protected override List<string> PlanIds => new() { _plan };
@ -92,6 +96,7 @@ namespace Bit.Core.Models.Business
public override List<SubscriptionItemOptions> UpgradeItemsOptions(Subscription subscription)
{
var item = SubscriptionItem(subscription, PlanIds.Single());
_prevStorage = item?.Quantity ?? 0;
return new()
{
new SubscriptionItemOptions
@ -106,6 +111,11 @@ namespace Bit.Core.Models.Business
public override List<SubscriptionItemOptions> RevertItemsOptions(Subscription subscription)
{
if (!_prevStorage.HasValue)
{
throw new Exception("Unknown previous value, must first call UpgradeItemsOptions");
}
var item = SubscriptionItem(subscription, PlanIds.Single());
return new()
{
@ -113,8 +123,8 @@ namespace Bit.Core.Models.Business
{
Id = item?.Id,
Plan = _plan,
Quantity = item?.Quantity ?? 0,
Deleted = item?.Id != null ? true : (bool?)null,
Quantity = _prevStorage.Value,
Deleted = _prevStorage.Value == 0 ? true : (bool?)null,
}
};
}