1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-01 08:02:49 -05: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
8 changed files with 55 additions and 25 deletions

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;
}