diff --git a/src/Billing/Controllers/StripeController.cs b/src/Billing/Controllers/StripeController.cs index c825d55bde..8b9ef9b878 100644 --- a/src/Billing/Controllers/StripeController.cs +++ b/src/Billing/Controllers/StripeController.cs @@ -512,6 +512,10 @@ public class StripeController : Controller break; } case HandledStripeWebhook.UpcomingInvoice: + var eventInvoice = await GetInvoiceAsync(parsedEvent); + var customer = await GetCustomerAsync(eventInvoice.CustomerId); + customerRegion = GetCustomerRegionFromMetadata(customer.Metadata); + break; case HandledStripeWebhook.PaymentSucceeded: case HandledStripeWebhook.PaymentFailed: case HandledStripeWebhook.InvoiceCreated: @@ -864,6 +868,23 @@ public class StripeController : Controller return subscription; } + private async Task GetCustomerAsync(string customerId) + { + if (string.IsNullOrWhiteSpace(customerId)) + { + throw new Exception("Customer ID cannot be empty when attempting to get a customer from Stripe"); + } + + var customerService = new CustomerService(); + var customer = await customerService.GetAsync(customerId); + if (customer == null) + { + throw new Exception($"Customer is null. {customerId}"); + } + + return customer; + } + private async Task VerifyCorrectTaxRateForCharge(Invoice invoice, Subscription subscription) { if (!string.IsNullOrWhiteSpace(invoice?.CustomerAddress?.Country) && !string.IsNullOrWhiteSpace(invoice?.CustomerAddress?.PostalCode))