From fd51797420ef246386e9f3cfacf1de193350c7e5 Mon Sep 17 00:00:00 2001 From: Shane Melton Date: Wed, 28 Jun 2023 17:40:29 -0700 Subject: [PATCH] [AC-1423] Add AddonProduct and BitwardenProduct properties to BillingSubscriptionItem (#3037) * [AC-1423] Add AddonProduct and BitwardenProduct properties to BillingSubscriptionItem - Add a helper method to determine the appropriate addon type based on the subscription items StripeId * [AC-1423] Add helper to StaticStore.cs to find a Plan by StripePlanId * [AC-1423] Use the helper method to set SubscriptionInfo.BitwardenProduct --- .../Response/SubscriptionResponseModel.cs | 5 ++ src/Core/Models/Business/SubscriptionInfo.cs | 11 ++++- src/Core/Utilities/StaticStore.cs | 49 +++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/Api/Models/Response/SubscriptionResponseModel.cs b/src/Api/Models/Response/SubscriptionResponseModel.cs index 3ec1b975e3..4c0ee9338b 100644 --- a/src/Api/Models/Response/SubscriptionResponseModel.cs +++ b/src/Api/Models/Response/SubscriptionResponseModel.cs @@ -1,4 +1,5 @@ using Bit.Core.Entities; +using Bit.Core.Enums; using Bit.Core.Models.Api; using Bit.Core.Models.Business; using Bit.Core.Utilities; @@ -82,6 +83,8 @@ public class BillingSubscription Interval = item.Interval; Quantity = item.Quantity; SponsoredSubscriptionItem = item.SponsoredSubscriptionItem; + AddonSubscriptionItem = item.AddonSubscriptionItem; + BitwardenProduct = item.BitwardenProduct; } public string Name { get; set; } @@ -89,6 +92,8 @@ public class BillingSubscription public int Quantity { get; set; } public string Interval { get; set; } public bool SponsoredSubscriptionItem { get; set; } + public bool AddonSubscriptionItem { get; set; } + public BitwardenProductType BitwardenProduct { get; set; } } } diff --git a/src/Core/Models/Business/SubscriptionInfo.cs b/src/Core/Models/Business/SubscriptionInfo.cs index 61aa060cd4..c72e291de0 100644 --- a/src/Core/Models/Business/SubscriptionInfo.cs +++ b/src/Core/Models/Business/SubscriptionInfo.cs @@ -1,4 +1,5 @@ -using Stripe; +using Bit.Core.Enums; +using Stripe; namespace Bit.Core.Models.Business; @@ -46,12 +47,20 @@ public class SubscriptionInfo Name = item.Plan.Nickname; Amount = item.Plan.Amount.GetValueOrDefault() / 100M; Interval = item.Plan.Interval; + AddonSubscriptionItem = + Utilities.StaticStore.IsAddonSubscriptionItem(item.Plan.Id); + BitwardenProduct = + Utilities.StaticStore.GetPlanByStripeId(item.Plan.Id)?.BitwardenProduct ?? BitwardenProductType.PasswordManager; } Quantity = (int)item.Quantity; SponsoredSubscriptionItem = Utilities.StaticStore.SponsoredPlans.Any(p => p.StripePlanId == item.Plan.Id); } + public BitwardenProductType BitwardenProduct { get; set; } + + public bool AddonSubscriptionItem { get; set; } + public string Name { get; set; } public decimal Amount { get; set; } public int Quantity { get; set; } diff --git a/src/Core/Utilities/StaticStore.cs b/src/Core/Utilities/StaticStore.cs index c5183296a9..ad11b2cfd0 100644 --- a/src/Core/Utilities/StaticStore.cs +++ b/src/Core/Utilities/StaticStore.cs @@ -139,4 +139,53 @@ public class StaticStore public static SponsoredPlan GetSponsoredPlan(PlanSponsorshipType planSponsorshipType) => SponsoredPlans.FirstOrDefault(p => p.PlanSponsorshipType == planSponsorshipType); + + /// + /// Determines if the stripe plan id is an addon item by checking if the provided stripe plan id + /// matches either the or + /// in any . + /// + /// + /// + /// True if the stripePlanId is a addon product, false otherwise + /// + public static bool IsAddonSubscriptionItem(string stripePlanId) + { + if (PasswordManagerPlans.Select(p => p.StripeStoragePlanId).Contains(stripePlanId)) + { + return true; + } + + if (SecretManagerPlans.Select(p => p.StripeServiceAccountPlanId).Contains(stripePlanId)) + { + return true; + } + + return false; + } + + /// + /// Get a by comparing the provided stripeId to the various + /// Stripe plan ids within a . + /// The following properties are checked: + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// The plan if a matching stripeId was found, null otherwise + public static Plan GetPlanByStripeId(string stripeId) + { + return Plans.FirstOrDefault(p => + p.StripePlanId == stripeId || + p.StripeSeatPlanId == stripeId || + p.StripeStoragePlanId == stripeId || + p.StripeServiceAccountPlanId == stripeId || + p.StripePremiumAccessPlanId == stripeId + ); + } }