From 6d7bcd98a9d1ae19c480f7d78d06e307fbbb0dba Mon Sep 17 00:00:00 2001 From: cyprain-okeke <108260115+cyprain-okeke@users.noreply.github.com> Date: Fri, 7 Apr 2023 10:45:18 +0100 Subject: [PATCH] [AC 480]Add the organization name to the stripe invoice (#2772) * Adding organization name on subscription and update * Changes after running dotnet format -v diag * Change the OrganizationName to GetName * Change GetName to OwnerName * Change the OwnerName to SubscriberName * Reverting the changes made by dotnet format-v diag * Removing extract space * resolve the lint issue * Fix whitespace formatting issue * Resolving pr comment on changing organization * Fixing the failing test * Using the ISubscriber interface for consistent * fixing the whitescapes * Resolving the PR comments * resolving the whitespace format * Remove unnecessary directive * Resolving the whitespace issue * changes for organization instead of subscriber * resolving the failing test * Resolve lint issue by removing unnecessary directive * Resolve the PR comment * remove these blank lines * Fixing whitespace formatting --- src/Core/Entities/ISubscriber.cs | 2 + src/Core/Entities/Organization.cs | 10 +++++ src/Core/Entities/User.cs | 10 +++++ .../Implementations/StripePaymentService.cs | 41 +++++++++++++++++-- .../Services/StripePaymentServiceTests.cs | 9 ++++ 5 files changed, 68 insertions(+), 4 deletions(-) diff --git a/src/Core/Entities/ISubscriber.cs b/src/Core/Entities/ISubscriber.cs index 6753e648e3..562b78f1d6 100644 --- a/src/Core/Entities/ISubscriber.cs +++ b/src/Core/Entities/ISubscriber.cs @@ -10,8 +10,10 @@ public interface ISubscriber string GatewaySubscriptionId { get; set; } string BillingEmailAddress(); string BillingName(); + string SubscriberName(); string BraintreeCustomerIdPrefix(); string BraintreeIdField(); string GatewayIdField(); bool IsUser(); + string SubscriberType(); } diff --git a/src/Core/Entities/Organization.cs b/src/Core/Entities/Organization.cs index 0f6d75ae0e..d99b1308fc 100644 --- a/src/Core/Entities/Organization.cs +++ b/src/Core/Entities/Organization.cs @@ -88,6 +88,11 @@ public class Organization : ITableObject, ISubscriber, IStorable, IStorabl return BusinessName; } + public string SubscriberName() + { + return Name; + } + public string BraintreeCustomerIdPrefix() { return "o"; @@ -108,6 +113,11 @@ public class Organization : ITableObject, ISubscriber, IStorable, IStorabl return false; } + public string SubscriberType() + { + return "Organization"; + } + public long StorageBytesRemaining() { if (!MaxStorageGb.HasValue) diff --git a/src/Core/Entities/User.cs b/src/Core/Entities/User.cs index d54bf0a308..962d94bd80 100644 --- a/src/Core/Entities/User.cs +++ b/src/Core/Entities/User.cs @@ -84,6 +84,11 @@ public class User : ITableObject, ISubscriber, IStorable, IStorableSubscri return Name; } + public string SubscriberName() + { + return string.IsNullOrWhiteSpace(Name) ? Email : Name; + } + public string BraintreeCustomerIdPrefix() { return "u"; @@ -104,6 +109,11 @@ public class User : ITableObject, ISubscriber, IStorable, IStorableSubscri return true; } + public string SubscriberType() + { + return "Subscriber"; + } + public Dictionary GetTwoFactorProviders() { if (string.IsNullOrWhiteSpace(TwoFactorProviders)) diff --git a/src/Core/Services/Implementations/StripePaymentService.cs b/src/Core/Services/Implementations/StripePaymentService.cs index 25561db4be..1784133c95 100644 --- a/src/Core/Services/Implementations/StripePaymentService.cs +++ b/src/Core/Services/Implementations/StripePaymentService.cs @@ -124,8 +124,17 @@ public class StripePaymentService : IPaymentService Metadata = stripeCustomerMetadata, InvoiceSettings = new Stripe.CustomerInvoiceSettingsOptions { - DefaultPaymentMethod = stipeCustomerPaymentMethodId + DefaultPaymentMethod = stipeCustomerPaymentMethodId, + CustomFields = new List + { + new Stripe.CustomerInvoiceSettingsCustomFieldOptions() + { + Name = org.SubscriberType(), + Value = org.SubscriberName(), + }, + }, }, + Address = new Stripe.AddressOptions { Country = taxInfo.BillingAddressCountry, @@ -427,7 +436,15 @@ public class StripePaymentService : IPaymentService Source = stipeCustomerSourceToken, InvoiceSettings = new Stripe.CustomerInvoiceSettingsOptions { - DefaultPaymentMethod = stipeCustomerPaymentMethodId + DefaultPaymentMethod = stipeCustomerPaymentMethodId, + CustomFields = new List + { + new Stripe.CustomerInvoiceSettingsCustomFieldOptions() + { + Name = user.SubscriberType(), + Value = user.SubscriberName(), + }, + } }, Address = new Stripe.AddressOptions { @@ -1334,7 +1351,15 @@ public class StripePaymentService : IPaymentService PaymentMethod = stipeCustomerPaymentMethodId, InvoiceSettings = new Stripe.CustomerInvoiceSettingsOptions { - DefaultPaymentMethod = stipeCustomerPaymentMethodId + DefaultPaymentMethod = stipeCustomerPaymentMethodId, + CustomFields = new List + { + new Stripe.CustomerInvoiceSettingsCustomFieldOptions() + { + Name = subscriber.SubscriberType(), + Value = subscriber.SubscriberName(), + }, + } }, Address = taxInfo == null ? null : new Stripe.AddressOptions { @@ -1406,7 +1431,15 @@ public class StripePaymentService : IPaymentService DefaultSource = defaultSourceId, InvoiceSettings = new Stripe.CustomerInvoiceSettingsOptions { - DefaultPaymentMethod = defaultPaymentMethodId + DefaultPaymentMethod = defaultPaymentMethodId, + CustomFields = new List + { + new Stripe.CustomerInvoiceSettingsCustomFieldOptions() + { + Name = subscriber.SubscriberType(), + Value = subscriber.SubscriberName(), + }, + } }, Address = taxInfo == null ? null : new Stripe.AddressOptions { diff --git a/test/Core.Test/Services/StripePaymentServiceTests.cs b/test/Core.Test/Services/StripePaymentServiceTests.cs index beb8406dc5..b4e2c81978 100644 --- a/test/Core.Test/Services/StripePaymentServiceTests.cs +++ b/test/Core.Test/Services/StripePaymentServiceTests.cs @@ -10,7 +10,10 @@ using Bit.Test.Common.AutoFixture.Attributes; using Braintree; using NSubstitute; using Xunit; +using Customer = Braintree.Customer; +using PaymentMethod = Braintree.PaymentMethod; using PaymentMethodType = Bit.Core.Enums.PaymentMethodType; +using TaxRate = Bit.Core.Entities.TaxRate; namespace Bit.Core.Test.Services; @@ -65,6 +68,9 @@ public class StripePaymentServiceTests c.PaymentMethod == null && !c.Metadata.Any() && c.InvoiceSettings.DefaultPaymentMethod == null && + c.InvoiceSettings.CustomFields != null && + c.InvoiceSettings.CustomFields[0].Name == "Organization" && + c.InvoiceSettings.CustomFields[0].Value == organization.SubscriberName() && c.Address.Country == taxInfo.BillingAddressCountry && c.Address.PostalCode == taxInfo.BillingAddressPostalCode && c.Address.Line1 == taxInfo.BillingAddressLine1 && @@ -115,6 +121,9 @@ public class StripePaymentServiceTests c.PaymentMethod == paymentToken && !c.Metadata.Any() && c.InvoiceSettings.DefaultPaymentMethod == paymentToken && + c.InvoiceSettings.CustomFields != null && + c.InvoiceSettings.CustomFields[0].Name == "Organization" && + c.InvoiceSettings.CustomFields[0].Value == organization.SubscriberName() && c.Address.Country == taxInfo.BillingAddressCountry && c.Address.PostalCode == taxInfo.BillingAddressPostalCode && c.Address.Line1 == taxInfo.BillingAddressLine1 &&