Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PM-16512] Enable Automatic Tax for all subscriptions #5210

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
๏ปฟusing Bit.Billing.Constants;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Repositories;
using Bit.Core.Billing.Constants;
using Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces;
using Bit.Core.Repositories;
using Bit.Core.Services;
Expand Down Expand Up @@ -161,18 +160,13 @@ await _mailService.SendInvoiceUpcoming(

private async Task<Subscription> TryEnableAutomaticTaxAsync(Subscription subscription)
{
var customerGetOptions = new CustomerGetOptions { Expand = ["tax"] };
var customer = await _stripeFacade.GetCustomer(subscription.CustomerId, customerGetOptions);

if (subscription.AutomaticTax.Enabled ||
customer.Tax?.AutomaticTax != StripeConstants.AutomaticTaxStatus.Supported)
if (subscription.AutomaticTax.Enabled)
{
return subscription;
}

var subscriptionUpdateOptions = new SubscriptionUpdateOptions
{
DefaultTaxRates = [],
AutomaticTax = new SubscriptionAutomaticTaxOptions { Enabled = true }
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@
{
AutomaticTax = new SubscriptionAutomaticTaxOptions
{
Enabled = customer.Tax?.AutomaticTax == StripeConstants.AutomaticTaxStatus.Supported
Enabled = true

Check warning on line 362 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/Billing/Services/Implementations/OrganizationBillingService.cs#L362

Added line #L362 was not covered by tests
},
CollectionMethod = StripeConstants.CollectionMethod.ChargeAutomatically,
Customer = customer.Id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
{
AutomaticTax = new SubscriptionAutomaticTaxOptions
{
Enabled = customer.Tax?.AutomaticTax == StripeConstants.AutomaticTaxStatus.Supported,
Enabled = true

Check warning on line 238 in src/Core/Billing/Services/Implementations/PremiumUserBillingService.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/Billing/Services/Implementations/PremiumUserBillingService.cs#L238

Added line #L238 was not covered by tests
},
CollectionMethod = StripeConstants.CollectionMethod.ChargeAutomatically,
Customer = customer.Id,
Expand Down
20 changes: 5 additions & 15 deletions src/Core/Billing/Services/Implementations/SubscriberService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -661,21 +661,11 @@ await stripeAdapter.TaxIdCreateAsync(customer.Id,
}
}

if (SubscriberIsEligibleForAutomaticTax(subscriber, customer))
{
await stripeAdapter.SubscriptionUpdateAsync(subscriber.GatewaySubscriptionId,
new SubscriptionUpdateOptions
{
AutomaticTax = new SubscriptionAutomaticTaxOptions { Enabled = true }
});
}

return;

bool SubscriberIsEligibleForAutomaticTax(ISubscriber localSubscriber, Customer localCustomer)
=> !string.IsNullOrEmpty(localSubscriber.GatewaySubscriptionId) &&
(localCustomer.Subscriptions?.Any(sub => sub.Id == localSubscriber.GatewaySubscriptionId && !sub.AutomaticTax.Enabled) ?? false) &&
localCustomer.Tax?.AutomaticTax == StripeConstants.AutomaticTaxStatus.Supported;
await stripeAdapter.SubscriptionUpdateAsync(subscriber.GatewaySubscriptionId,
new SubscriptionUpdateOptions
{
AutomaticTax = new SubscriptionAutomaticTaxOptions { Enabled = true },
});
}

public async Task VerifyBankAccount(
Expand Down
70 changes: 22 additions & 48 deletions src/Core/Services/Implementations/StripePaymentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,7 @@
customer = await _stripeAdapter.CustomerCreateAsync(customerCreateOptions);
subCreateOptions.AddExpand("latest_invoice.payment_intent");
subCreateOptions.Customer = customer.Id;

if (CustomerHasTaxLocationVerified(customer))
{
subCreateOptions.AutomaticTax = new SubscriptionAutomaticTaxOptions { Enabled = true };
}
subCreateOptions.AutomaticTax = new SubscriptionAutomaticTaxOptions { Enabled = true };

subscription = await _stripeAdapter.SubscriptionCreateAsync(subCreateOptions);
if (subscription.Status == "incomplete" && subscription.LatestInvoice?.PaymentIntent != null)
Expand Down Expand Up @@ -366,13 +362,10 @@
customer = await _stripeAdapter.CustomerUpdateAsync(org.GatewayCustomerId, customerUpdateOptions);
}

var subCreateOptions = new OrganizationUpgradeSubscriptionOptions(customer.Id, org, plan, upgrade);

if (CustomerHasTaxLocationVerified(customer))
var subCreateOptions = new OrganizationUpgradeSubscriptionOptions(customer.Id, org, plan, upgrade)
{
subCreateOptions.DefaultTaxRates = [];
subCreateOptions.AutomaticTax = new SubscriptionAutomaticTaxOptions { Enabled = true };
}
AutomaticTax = new SubscriptionAutomaticTaxOptions { Enabled = true }
};

var (stripePaymentMethod, paymentMethodType) = IdentifyPaymentMethod(customer, subCreateOptions);

Expand Down Expand Up @@ -531,6 +524,10 @@

var customerCreateOptions = new CustomerCreateOptions
{
Tax = new CustomerTaxOptions
{
ValidateLocation = StripeConstants.ValidateTaxLocationTiming.Immediately
},

Check warning on line 530 in src/Core/Services/Implementations/StripePaymentService.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/Services/Implementations/StripePaymentService.cs#L527-L530

Added lines #L527 - L530 were not covered by tests
Description = user.Name,
Email = user.Email,
Metadata = stripeCustomerMetadata,
Expand Down Expand Up @@ -568,6 +565,7 @@

var subCreateOptions = new SubscriptionCreateOptions
{
AutomaticTax = new SubscriptionAutomaticTaxOptions { Enabled = true },

Check warning on line 568 in src/Core/Services/Implementations/StripePaymentService.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/Services/Implementations/StripePaymentService.cs#L568

Added line #L568 was not covered by tests
Customer = customer.Id,
Items = [],
Metadata = new Dictionary<string, string>
Expand All @@ -587,16 +585,10 @@
subCreateOptions.Items.Add(new SubscriptionItemOptions
{
Plan = StoragePlanId,
Quantity = additionalStorageGb
Quantity = additionalStorageGb,

Check warning on line 588 in src/Core/Services/Implementations/StripePaymentService.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/Services/Implementations/StripePaymentService.cs#L588

Added line #L588 was not covered by tests
});
}

if (CustomerHasTaxLocationVerified(customer))
{
subCreateOptions.DefaultTaxRates = [];
subCreateOptions.AutomaticTax = new SubscriptionAutomaticTaxOptions { Enabled = true };
}

var subscription = await ChargeForNewSubscriptionAsync(user, customer, createdStripeCustomer,
stripePaymentMethod, paymentMethodType, subCreateOptions, braintreeCustomer);

Expand Down Expand Up @@ -634,10 +626,7 @@
SubscriptionItems = ToInvoiceSubscriptionItemOptions(subCreateOptions.Items)
});

if (CustomerHasTaxLocationVerified(customer))
{
previewInvoice.AutomaticTax = new InvoiceAutomaticTax { Enabled = true };
}
previewInvoice.AutomaticTax = new InvoiceAutomaticTax { Enabled = true };

if (previewInvoice.AmountDue > 0)
{
Expand Down Expand Up @@ -695,14 +684,12 @@
Customer = customer.Id,
SubscriptionItems = ToInvoiceSubscriptionItemOptions(subCreateOptions.Items),
SubscriptionDefaultTaxRates = subCreateOptions.DefaultTaxRates,
AutomaticTax = new InvoiceAutomaticTaxOptions
{
Enabled = true
}

Check warning on line 690 in src/Core/Services/Implementations/StripePaymentService.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/Services/Implementations/StripePaymentService.cs#L687-L690

Added lines #L687 - L690 were not covered by tests
};

if (CustomerHasTaxLocationVerified(customer))
{
upcomingInvoiceOptions.AutomaticTax = new InvoiceAutomaticTaxOptions { Enabled = true };
upcomingInvoiceOptions.SubscriptionDefaultTaxRates = [];
}

var previewInvoice = await _stripeAdapter.InvoiceUpcomingAsync(upcomingInvoiceOptions);

if (previewInvoice.AmountDue > 0)
Expand Down Expand Up @@ -821,21 +808,18 @@
Items = updatedItemOptions,
ProrationBehavior = invoiceNow ? Constants.AlwaysInvoice : Constants.CreateProrations,
DaysUntilDue = daysUntilDue ?? 1,
CollectionMethod = "send_invoice"
CollectionMethod = "send_invoice",
AutomaticTax = new SubscriptionAutomaticTaxOptions
{
Enabled = true
}

Check warning on line 815 in src/Core/Services/Implementations/StripePaymentService.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/Services/Implementations/StripePaymentService.cs#L811-L815

Added lines #L811 - L815 were not covered by tests
};
if (!invoiceNow && isAnnualPlan && sub.Status.Trim() != "trialing")
{
subUpdateOptions.PendingInvoiceItemInterval =
new SubscriptionPendingInvoiceItemIntervalOptions { Interval = "month" };
}

if (sub.AutomaticTax.Enabled != true &&
CustomerHasTaxLocationVerified(sub.Customer))
{
subUpdateOptions.DefaultTaxRates = [];
subUpdateOptions.AutomaticTax = new SubscriptionAutomaticTaxOptions { Enabled = true };
}

if (!subscriptionUpdate.UpdateNeeded(sub))
{
// No need to update subscription, quantity matches
Expand Down Expand Up @@ -866,7 +850,7 @@
}
else
{
invoice = await _stripeAdapter.InvoiceFinalizeInvoiceAsync(subResponse.LatestInvoiceId,

Check warning on line 853 in src/Core/Services/Implementations/StripePaymentService.cs

View workflow job for this annotation

GitHub Actions / Quality scan

'subResponse' is null on at least one execution path. (https://rules.sonarsource.com/csharp/RSPEC-2259)
new InvoiceFinalizeOptions { AutoAdvance = false, });
await _stripeAdapter.InvoiceSendInvoiceAsync(invoice.Id, new InvoiceSendOptions());
paymentIntentClientSecret = null;
Expand All @@ -891,7 +875,7 @@
else if (!invoice.Paid)
{
// Pay invoice with no charge to customer this completes the invoice immediately without waiting the scheduled 1h
invoice = await _stripeAdapter.InvoicePayAsync(subResponse.LatestInvoiceId);

Check warning on line 878 in src/Core/Services/Implementations/StripePaymentService.cs

View workflow job for this annotation

GitHub Actions / Quality scan

'subResponse' is null on at least one execution path. (https://rules.sonarsource.com/csharp/RSPEC-2259)
paymentIntentClientSecret = null;
}

Expand Down Expand Up @@ -1520,13 +1504,11 @@
if (!string.IsNullOrEmpty(subscriber.GatewaySubscriptionId) &&
customer.Subscriptions.Any(sub =>
sub.Id == subscriber.GatewaySubscriptionId &&
!sub.AutomaticTax.Enabled) &&
CustomerHasTaxLocationVerified(customer))
!sub.AutomaticTax.Enabled))

Check warning on line 1507 in src/Core/Services/Implementations/StripePaymentService.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/Services/Implementations/StripePaymentService.cs#L1507

Added line #L1507 was not covered by tests
{
var subscriptionUpdateOptions = new SubscriptionUpdateOptions
{
AutomaticTax = new SubscriptionAutomaticTaxOptions { Enabled = true },
DefaultTaxRates = []
AutomaticTax = new SubscriptionAutomaticTaxOptions { Enabled = true }

Check warning on line 1511 in src/Core/Services/Implementations/StripePaymentService.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/Services/Implementations/StripePaymentService.cs#L1511

Added line #L1511 was not covered by tests
};

_ = await _stripeAdapter.SubscriptionUpdateAsync(
Expand Down Expand Up @@ -2328,14 +2310,6 @@
}
}

/// <summary>
/// Determines if a Stripe customer supports automatic tax
/// </summary>
/// <param name="customer"></param>
/// <returns></returns>
private static bool CustomerHasTaxLocationVerified(Customer customer) =>
customer?.Tax?.AutomaticTax == StripeConstants.AutomaticTaxStatus.Supported;

// We are taking only first 30 characters of the SubscriberName because stripe provide
// for 30 characters for custom_fields,see the link: https://stripe.com/docs/api/invoices/create
private static string GetFirstThirtyCharacters(string subscriberName)
Expand Down
Loading