1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-24 12:35:25 +01:00

[AC-1843] Automate PM discount for SM Trial (#3661)

* Added appliesTo to customer discount. Added productId to subscription item

* Added IsFromSecretsManagerTrial flag to add discount for SM trials

* Fixed broken tests

---------

Co-authored-by: Alex Morask <amorask@bitwarden.com>
This commit is contained in:
Conner Turnbull 2024-01-29 11:10:27 -05:00 committed by GitHub
parent 693f0566a6
commit d7de5cbf28
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 55 additions and 25 deletions

View File

@ -46,6 +46,7 @@ public class OrganizationCreateRequestModel : IValidatableObject
public int? AdditionalServiceAccounts { get; set; }
[Required]
public bool UseSecretsManager { get; set; }
public bool IsFromSecretsManagerTrial { get; set; }
public virtual OrganizationSignup ToOrganizationSignup(User user)
{
@ -67,6 +68,7 @@ public class OrganizationCreateRequestModel : IValidatableObject
AdditionalSmSeats = AdditionalSmSeats.GetValueOrDefault(),
AdditionalServiceAccounts = AdditionalServiceAccounts.GetValueOrDefault(),
UseSecretsManager = UseSecretsManager,
IsFromSecretsManagerTrial = IsFromSecretsManagerTrial,
TaxInfo = new TaxInfo
{
TaxIdNumber = TaxIdNumber,

View File

@ -50,11 +50,13 @@ public class BillingCustomerDiscount
Id = discount.Id;
Active = discount.Active;
PercentOff = discount.PercentOff;
AppliesTo = discount.AppliesTo;
}
public string Id { get; }
public bool Active { get; }
public decimal? PercentOff { get; }
public List<string> AppliesTo { get; }
}
public class BillingSubscription
@ -89,6 +91,7 @@ public class BillingSubscription
{
public BillingSubscriptionItem(SubscriptionInfo.BillingSubscription.BillingSubscriptionItem item)
{
ProductId = item.ProductId;
Name = item.Name;
Amount = item.Amount;
Interval = item.Interval;
@ -97,6 +100,7 @@ public class BillingSubscription
AddonSubscriptionItem = item.AddonSubscriptionItem;
}
public string ProductId { get; set; }
public string Name { get; set; }
public decimal Amount { get; set; }
public int Quantity { get; set; }

View File

@ -517,7 +517,7 @@ public class OrganizationService : IOrganizationService
await _paymentService.PurchaseOrganizationAsync(organization, signup.PaymentMethodType.Value,
signup.PaymentToken, plan, signup.AdditionalStorageGb, signup.AdditionalSeats,
signup.PremiumAccessAddon, signup.TaxInfo, provider, signup.AdditionalSmSeats.GetValueOrDefault(),
signup.AdditionalServiceAccounts.GetValueOrDefault());
signup.AdditionalServiceAccounts.GetValueOrDefault(), signup.IsFromSecretsManagerTrial);
}
var ownerId = provider ? default : signup.Owner.Id;

View File

@ -15,4 +15,5 @@ public class OrganizationUpgrade
public int? AdditionalSmSeats { get; set; }
public int? AdditionalServiceAccounts { get; set; }
public bool UseSecretsManager { get; set; }
public bool IsFromSecretsManagerTrial { get; set; }
}

View File

@ -17,11 +17,13 @@ public class SubscriptionInfo
Id = discount.Id;
Active = discount.Start != null && discount.End == null;
PercentOff = discount.Coupon?.PercentOff;
AppliesTo = discount.Coupon?.AppliesTo?.Products ?? new List<string>();
}
public string Id { get; }
public bool Active { get; }
public decimal? PercentOff { get; }
public List<string> AppliesTo { get; }
}
public class BillingSubscription
@ -59,6 +61,7 @@ public class SubscriptionInfo
{
if (item.Plan != null)
{
ProductId = item.Plan.ProductId;
Name = item.Plan.Nickname;
Amount = item.Plan.Amount.GetValueOrDefault() / 100M;
Interval = item.Plan.Interval;
@ -72,6 +75,7 @@ public class SubscriptionInfo
public bool AddonSubscriptionItem { get; set; }
public string ProductId { get; set; }
public string Name { get; set; }
public decimal Amount { get; set; }
public int Quantity { get; set; }

View File

@ -12,7 +12,7 @@ public interface IPaymentService
Task<string> PurchaseOrganizationAsync(Organization org, PaymentMethodType paymentMethodType,
string paymentToken, Plan plan, short additionalStorageGb, int additionalSeats,
bool premiumAccessAddon, TaxInfo taxInfo, bool provider = false, int additionalSmSeats = 0,
int additionalServiceAccount = 0);
int additionalServiceAccount = 0, bool signupIsFromSecretsManagerTrial = false);
Task SponsorOrganizationAsync(Organization org, OrganizationSponsorship sponsorship);
Task RemoveOrganizationSponsorshipAsync(Organization org, OrganizationSponsorship sponsorship);
Task<string> UpgradeFreeOrganizationAsync(Organization org, Plan plan, OrganizationUpgrade upgrade);

View File

@ -7,6 +7,7 @@ using Bit.Core.Models.Business;
using Bit.Core.Repositories;
using Bit.Core.Settings;
using Microsoft.Extensions.Logging;
using Stripe;
using StaticStore = Bit.Core.Models.StaticStore;
using TaxRate = Bit.Core.Entities.TaxRate;
@ -17,6 +18,7 @@ public class StripePaymentService : IPaymentService
private const string PremiumPlanId = "premium-annually";
private const string StoragePlanId = "storage-gb-annually";
private const string ProviderDiscountId = "msp-discount-35";
private const string SecretsManagerStandaloneDiscountId = "sm-standalone";
private readonly ITransactionRepository _transactionRepository;
private readonly IUserRepository _userRepository;
@ -47,7 +49,7 @@ public class StripePaymentService : IPaymentService
public async Task<string> PurchaseOrganizationAsync(Organization org, PaymentMethodType paymentMethodType,
string paymentToken, StaticStore.Plan plan, short additionalStorageGb,
int additionalSeats, bool premiumAccessAddon, TaxInfo taxInfo, bool provider = false,
int additionalSmSeats = 0, int additionalServiceAccount = 0)
int additionalSmSeats = 0, int additionalServiceAccount = 0, bool signupIsFromSecretsManagerTrial = false)
{
Braintree.Customer braintreeCustomer = null;
string stipeCustomerSourceToken = null;
@ -124,7 +126,11 @@ public class StripePaymentService : IPaymentService
},
},
},
Coupon = provider ? ProviderDiscountId : null,
Coupon = signupIsFromSecretsManagerTrial
? SecretsManagerStandaloneDiscountId
: provider
? ProviderDiscountId
: null,
Address = new Stripe.AddressOptions
{
Country = taxInfo.BillingAddressCountry,
@ -1410,7 +1416,9 @@ public class StripePaymentService : IPaymentService
if (!string.IsNullOrWhiteSpace(subscriber.GatewayCustomerId))
{
var customer = await _stripeAdapter.CustomerGetAsync(subscriber.GatewayCustomerId);
var customerGetOptions = new CustomerGetOptions();
customerGetOptions.AddExpand("discount.coupon.applies_to");
var customer = await _stripeAdapter.CustomerGetAsync(subscriber.GatewayCustomerId, customerGetOptions);
if (customer.Discount != null)
{
@ -1418,29 +1426,36 @@ public class StripePaymentService : IPaymentService
}
}
if (!string.IsNullOrWhiteSpace(subscriber.GatewaySubscriptionId))
if (string.IsNullOrWhiteSpace(subscriber.GatewaySubscriptionId))
{
var sub = await _stripeAdapter.SubscriptionGetAsync(subscriber.GatewaySubscriptionId);
if (sub != null)
{
subscriptionInfo.Subscription = new SubscriptionInfo.BillingSubscription(sub);
}
return subscriptionInfo;
}
if (!sub.CanceledAt.HasValue && !string.IsNullOrWhiteSpace(subscriber.GatewayCustomerId))
var sub = await _stripeAdapter.SubscriptionGetAsync(subscriber.GatewaySubscriptionId);
if (sub != null)
{
subscriptionInfo.Subscription = new SubscriptionInfo.BillingSubscription(sub);
}
if (sub is { CanceledAt: not null } || string.IsNullOrWhiteSpace(subscriber.GatewayCustomerId))
{
return subscriptionInfo;
}
try
{
var upcomingInvoiceOptions = new UpcomingInvoiceOptions { Customer = subscriber.GatewayCustomerId };
var upcomingInvoice = await _stripeAdapter.InvoiceUpcomingAsync(upcomingInvoiceOptions);
if (upcomingInvoice != null)
{
try
{
var upcomingInvoice = await _stripeAdapter.InvoiceUpcomingAsync(
new Stripe.UpcomingInvoiceOptions { Customer = subscriber.GatewayCustomerId });
if (upcomingInvoice != null)
{
subscriptionInfo.UpcomingInvoice =
new SubscriptionInfo.BillingUpcomingInvoice(upcomingInvoice);
}
}
catch (Stripe.StripeException) { }
subscriptionInfo.UpcomingInvoice = new SubscriptionInfo.BillingUpcomingInvoice(upcomingInvoice);
}
}
catch (StripeException ex)
{
_logger.LogWarning(ex, "Encountered an unexpected Stripe error");
}
return subscriptionInfo;
}

View File

@ -209,6 +209,7 @@ public class OrganizationServiceTests
signup.PaymentMethodType = PaymentMethodType.Card;
signup.PremiumAccessAddon = false;
signup.UseSecretsManager = false;
signup.IsFromSecretsManagerTrial = false;
var purchaseOrganizationPlan = StaticStore.GetPlan(signup.Plan);
@ -247,7 +248,8 @@ public class OrganizationServiceTests
signup.TaxInfo,
false,
signup.AdditionalSmSeats.GetValueOrDefault(),
signup.AdditionalServiceAccounts.GetValueOrDefault()
signup.AdditionalServiceAccounts.GetValueOrDefault(),
signup.UseSecretsManager
);
}
@ -326,6 +328,7 @@ public class OrganizationServiceTests
signup.AdditionalServiceAccounts = 20;
signup.PaymentMethodType = PaymentMethodType.Card;
signup.PremiumAccessAddon = false;
signup.IsFromSecretsManagerTrial = false;
var result = await sutProvider.Sut.SignUpAsync(signup);
@ -362,7 +365,8 @@ public class OrganizationServiceTests
signup.TaxInfo,
false,
signup.AdditionalSmSeats.GetValueOrDefault(),
signup.AdditionalServiceAccounts.GetValueOrDefault()
signup.AdditionalServiceAccounts.GetValueOrDefault(),
signup.IsFromSecretsManagerTrial
);
}