From 51ffe3cef81e993289c42ed37f4a43ba46a38e45 Mon Sep 17 00:00:00 2001 From: Jonas Hendrickx Date: Fri, 10 Jan 2025 13:44:10 +0100 Subject: [PATCH 1/2] [PM-26932] Improve validation tax information when subscribing to premium --- src/Api/Auth/Controllers/AccountsController.cs | 6 +++++- .../Request/Accounts/PremiumRequestModel.cs | 17 +++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/Api/Auth/Controllers/AccountsController.cs b/src/Api/Auth/Controllers/AccountsController.cs index 7990a5a18a5d..b36e04521781 100644 --- a/src/Api/Auth/Controllers/AccountsController.cs +++ b/src/Api/Auth/Controllers/AccountsController.cs @@ -666,7 +666,11 @@ public async Task PostPremium(PremiumRequestModel model) new TaxInfo { BillingAddressCountry = model.Country, - BillingAddressPostalCode = model.PostalCode + BillingAddressPostalCode = model.PostalCode, + BillingAddressCity = model.City, + BillingAddressLine1 = model.Line1, + BillingAddressLine2 = model.Line2, + BillingAddressState = model.State, }); var userTwoFactorEnabled = await _userService.TwoFactorIsEnabledAsync(user); diff --git a/src/Api/Models/Request/Accounts/PremiumRequestModel.cs b/src/Api/Models/Request/Accounts/PremiumRequestModel.cs index 26d199381f0a..410bb35cc7ec 100644 --- a/src/Api/Models/Request/Accounts/PremiumRequestModel.cs +++ b/src/Api/Models/Request/Accounts/PremiumRequestModel.cs @@ -12,9 +12,19 @@ public class PremiumRequestModel : IValidatableObject [Range(0, 99)] public short? AdditionalStorageGb { get; set; } public IFormFile License { get; set; } - public string Country { get; set; } + + public string Line1 { get; set; } + public string Line2 { get; set; } + + [Required] public string PostalCode { get; set; } + public string City { get; set; } + public string State { get; set; } + + [Required] + public string Country { get; set; } + public bool Validate(GlobalSettings globalSettings) { if (!(License == null && !globalSettings.SelfHosted) || @@ -32,10 +42,5 @@ public IEnumerable Validate(ValidationContext validationContex { yield return new ValidationResult("Payment token or license is required."); } - if (Country == "US" && string.IsNullOrWhiteSpace(PostalCode)) - { - yield return new ValidationResult("Zip / postal code is required.", - new string[] { nameof(PostalCode) }); - } } } From cb0b705ae719d7f615ebaa703c9039875714a499 Mon Sep 17 00:00:00 2001 From: Jonas Hendrickx Date: Tue, 28 Jan 2025 12:30:30 +0100 Subject: [PATCH 2/2] fix --- .../Auth/Controllers/AccountsController.cs | 3 +- .../Implementations/StripePaymentService.cs | 50 +++++++++---------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/Api/Auth/Controllers/AccountsController.cs b/src/Api/Auth/Controllers/AccountsController.cs index b36e04521781..be7498643258 100644 --- a/src/Api/Auth/Controllers/AccountsController.cs +++ b/src/Api/Auth/Controllers/AccountsController.cs @@ -730,8 +730,7 @@ await _userService.ReplacePaymentMethodAsync(user, model.PaymentToken, model.Pay BillingAddressCity = model.City, BillingAddressState = model.State, BillingAddressCountry = model.Country, - BillingAddressPostalCode = model.PostalCode, - TaxIdNumber = model.TaxId + BillingAddressPostalCode = model.PostalCode }); } diff --git a/src/Core/Services/Implementations/StripePaymentService.cs b/src/Core/Services/Implementations/StripePaymentService.cs index ad8c7a599d1d..d6b711f36627 100644 --- a/src/Core/Services/Implementations/StripePaymentService.cs +++ b/src/Core/Services/Implementations/StripePaymentService.cs @@ -1394,15 +1394,9 @@ public async Task UpdatePaymentMethodAsync(ISubscriber subscriber, Payment try { - if (!string.IsNullOrWhiteSpace(taxInfo.TaxIdNumber)) - { - taxInfo.TaxIdType = taxInfo.TaxIdType ?? - _taxService.GetStripeTaxCode(taxInfo.BillingAddressCountry, taxInfo.TaxIdNumber); - } - if (customer == null) { - customer = await _stripeAdapter.CustomerCreateAsync(new CustomerCreateOptions + var customerCreateOptions = new CustomerCreateOptions { Description = subscriber.BillingName(), Email = subscriber.BillingEmailAddress(), @@ -1422,26 +1416,32 @@ public async Task UpdatePaymentMethodAsync(ISubscriber subscriber, Payment ] }, - Address = taxInfo == null ? null : new AddressOptions - { - Country = taxInfo.BillingAddressCountry, - PostalCode = taxInfo.BillingAddressPostalCode, - Line1 = taxInfo.BillingAddressLine1 ?? string.Empty, - Line2 = taxInfo.BillingAddressLine2, - City = taxInfo.BillingAddressCity, - State = taxInfo.BillingAddressState - }, - TaxIdData = string.IsNullOrWhiteSpace(taxInfo.TaxIdNumber) - ? [] - : [ - new CustomerTaxIdDataOptions + Address = taxInfo == null + ? null + : new AddressOptions { - Type = taxInfo.TaxIdType, - Value = taxInfo.TaxIdNumber - } - ], + Country = taxInfo.BillingAddressCountry, + PostalCode = taxInfo.BillingAddressPostalCode, + Line1 = taxInfo.BillingAddressLine1 ?? string.Empty, + Line2 = taxInfo.BillingAddressLine2, + City = taxInfo.BillingAddressCity, + State = taxInfo.BillingAddressState + }, Expand = ["sources", "tax", "subscriptions"], - }); + }; + + if (!string.IsNullOrWhiteSpace(taxInfo.TaxIdNumber)) + { + taxInfo.TaxIdType = taxInfo.TaxIdType ?? + _taxService.GetStripeTaxCode(taxInfo.BillingAddressCountry, taxInfo.TaxIdNumber); + + customerCreateOptions.TaxIdData = + [ + new CustomerTaxIdDataOptions { Type = taxInfo.TaxIdType, Value = taxInfo.TaxIdNumber } + ]; + } + + customer = await _stripeAdapter.CustomerCreateAsync(customerCreateOptions); subscriber.Gateway = GatewayType.Stripe; subscriber.GatewayCustomerId = customer.Id;