1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-04 01:22:50 -05:00

[PM-5766] Enabled Automatic Tax for all customers (#3685)

* Removed TaxRate logic when creating or updating a Stripe subscription and replaced it with AutomaticTax enabled flag

* Updated Stripe webhook to update subscription to automatically calculate tax

* Removed TaxRate unit tests since Stripe now handles tax

* Removed test proration logic

* Including taxInfo when updating payment method

* Adding the address to the upgrade free org flow if it doesn't exist

* Fixed failing tests and added a new test to validate that the customer is updated
This commit is contained in:
Conner Turnbull
2024-01-29 09:48:59 -05:00
committed by GitHub
parent c2b4ee7eac
commit a2e6550b61
6 changed files with 127 additions and 167 deletions

View File

@ -21,7 +21,6 @@ using Customer = Stripe.Customer;
using Event = Stripe.Event;
using PaymentMethod = Stripe.PaymentMethod;
using Subscription = Stripe.Subscription;
using TaxRate = Bit.Core.Entities.TaxRate;
using Transaction = Bit.Core.Entities.Transaction;
using TransactionType = Bit.Core.Enums.TransactionType;
@ -223,9 +222,17 @@ public class StripeController : Controller
$"Received null Subscription from Stripe for ID '{invoice.SubscriptionId}' while processing Event with ID '{parsedEvent.Id}'");
}
var updatedSubscription = await VerifyCorrectTaxRateForCharge(invoice, subscription);
if (!subscription.AutomaticTax.Enabled)
{
subscription = await _stripeFacade.UpdateSubscription(subscription.Id,
new SubscriptionUpdateOptions
{
DefaultTaxRates = new List<string>(),
AutomaticTax = new SubscriptionAutomaticTaxOptions { Enabled = true }
});
}
var (organizationId, userId) = GetIdsFromMetaData(updatedSubscription.Metadata);
var (organizationId, userId) = GetIdsFromMetaData(subscription.Metadata);
var invoiceLineItemDescriptions = invoice.Lines.Select(i => i.Description).ToList();
@ -246,7 +253,7 @@ public class StripeController : Controller
if (organizationId.HasValue)
{
if (IsSponsoredSubscription(updatedSubscription))
if (IsSponsoredSubscription(subscription))
{
await _validateSponsorshipCommand.ValidateSponsorshipAsync(organizationId.Value);
}
@ -828,32 +835,6 @@ public class StripeController : Controller
invoice.BillingReason == "subscription_cycle" && invoice.SubscriptionId != null;
}
private async Task<Subscription> VerifyCorrectTaxRateForCharge(Invoice invoice, Subscription subscription)
{
if (!string.IsNullOrWhiteSpace(invoice?.CustomerAddress?.Country) && !string.IsNullOrWhiteSpace(invoice?.CustomerAddress?.PostalCode))
{
var localBitwardenTaxRates = await _taxRateRepository.GetByLocationAsync(
new TaxRate()
{
Country = invoice.CustomerAddress.Country,
PostalCode = invoice.CustomerAddress.PostalCode
}
);
if (localBitwardenTaxRates.Any())
{
var stripeTaxRate = await new TaxRateService().GetAsync(localBitwardenTaxRates.First().Id);
if (stripeTaxRate != null && !subscription.DefaultTaxRates.Any(x => x == stripeTaxRate))
{
subscription.DefaultTaxRates = new List<Stripe.TaxRate> { stripeTaxRate };
var subscriptionOptions = new SubscriptionUpdateOptions() { DefaultTaxRates = new List<string>() { stripeTaxRate.Id } };
subscription = await new SubscriptionService().UpdateAsync(subscription.Id, subscriptionOptions);
}
}
}
return subscription;
}
private static bool IsSponsoredSubscription(Subscription subscription) =>
StaticStore.SponsoredPlans.Any(p => p.StripePlanId == subscription.Id);