1
0
mirror of https://github.com/bitwarden/server synced 2026-03-02 19:31:24 +00:00

[PM-21878] update gateway/stripe fields for business units (#6186)

* [PM-21878] also update gateway/stripe fields for business units

* pr feedback: replacing switch with extension method

* [PM-21878] prevent invalid stripe ids from crashing the edit provider page

* pr feedback: adding service methods to validate stripe ids

and added unit tests for the new methods

* pr feedback: move validation to SubscriberService and cleanup

* pr feedback: use subscriber service to remove dependency on stripe adapter
This commit is contained in:
Kyle Denney
2025-08-21 13:54:20 -05:00
committed by GitHub
parent 1c98e59003
commit c519fa43c6
6 changed files with 228 additions and 11 deletions

View File

@@ -36,6 +36,10 @@ public static class BillingExtensions
Status: ProviderStatusType.Billable
};
// Reseller types do not have Stripe entities
public static bool IsStripeSupported(this ProviderType providerType) =>
providerType is ProviderType.Msp or ProviderType.BusinessUnit;
public static bool SupportsConsolidatedBilling(this ProviderType providerType)
=> providerType is ProviderType.Msp or ProviderType.BusinessUnit;

View File

@@ -157,4 +157,22 @@ public interface ISubscriberService
Task VerifyBankAccount(
ISubscriber subscriber,
string descriptorCode);
/// <summary>
/// Validates whether the <paramref name="subscriber"/>'s <see cref="ISubscriber.GatewayCustomerId"/> exists in the gateway.
/// If the <paramref name="subscriber"/>'s <see cref="ISubscriber.GatewayCustomerId"/> is <see langword="null"/> or empty, returns <see langword="true"/>.
/// </summary>
/// <param name="subscriber">The subscriber whose gateway customer ID should be validated.</param>
/// <returns><see langword="true"/> if the gateway customer ID is valid or empty; <see langword="false"/> if the customer doesn't exist in the gateway.</returns>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="subscriber"/> is <see langword="null"/>.</exception>
Task<bool> IsValidGatewayCustomerIdAsync(ISubscriber subscriber);
/// <summary>
/// Validates whether the <paramref name="subscriber"/>'s <see cref="ISubscriber.GatewaySubscriptionId"/> exists in the gateway.
/// If the <paramref name="subscriber"/>'s <see cref="ISubscriber.GatewaySubscriptionId"/> is <see langword="null"/> or empty, returns <see langword="true"/>.
/// </summary>
/// <param name="subscriber">The subscriber whose gateway subscription ID should be validated.</param>
/// <returns><see langword="true"/> if the gateway subscription ID is valid or empty; <see langword="false"/> if the subscription doesn't exist in the gateway.</returns>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="subscriber"/> is <see langword="null"/>.</exception>
Task<bool> IsValidGatewaySubscriptionIdAsync(ISubscriber subscriber);
}

View File

@@ -909,6 +909,44 @@ public class SubscriberService(
}
}
public async Task<bool> IsValidGatewayCustomerIdAsync(ISubscriber subscriber)
{
ArgumentNullException.ThrowIfNull(subscriber);
if (string.IsNullOrEmpty(subscriber.GatewayCustomerId))
{
// subscribers are allowed to have no customer id as a business rule
return true;
}
try
{
await stripeAdapter.CustomerGetAsync(subscriber.GatewayCustomerId);
return true;
}
catch (StripeException e) when (e.StripeError.Code == "resource_missing")
{
return false;
}
}
public async Task<bool> IsValidGatewaySubscriptionIdAsync(ISubscriber subscriber)
{
ArgumentNullException.ThrowIfNull(subscriber);
if (string.IsNullOrEmpty(subscriber.GatewaySubscriptionId))
{
// subscribers are allowed to have no subscription id as a business rule
return true;
}
try
{
await stripeAdapter.SubscriptionGetAsync(subscriber.GatewaySubscriptionId);
return true;
}
catch (StripeException e) when (e.StripeError.Code == "resource_missing")
{
return false;
}
}
#region Shared Utilities
private async Task AddBraintreeCustomerIdAsync(