Skip to content

Commit 35ff08c

Browse files
[PM-31040] Add logging to bank account setup process (#6898)
* Add logging to bank account setup process * Missed test file constructor
1 parent afb0871 commit 35ff08c

4 files changed

Lines changed: 28 additions & 5 deletions

File tree

src/Billing/Services/Implementations/StripeEventService.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Bit.Billing.Services.Implementations;
99

1010
public class StripeEventService(
1111
GlobalSettings globalSettings,
12+
ILogger<StripeEventService> logger,
1213
IOrganizationRepository organizationRepository,
1314
IProviderRepository providerRepository,
1415
ISetupIntentCache setupIntentCache,
@@ -148,26 +149,36 @@ await GetCustomerMetadataFromSetupIntentSucceededEvent(stripeEvent),
148149
{
149150
var setupIntent = await GetSetupIntent(localStripeEvent);
150151

152+
logger.LogInformation("Extracted Setup Intent ({SetupIntentId}) from Stripe 'setup_intent.succeeded' event", setupIntent.Id);
153+
151154
var subscriberId = await setupIntentCache.GetSubscriberIdForSetupIntent(setupIntent.Id);
155+
156+
logger.LogInformation("Retrieved subscriber ID ({SubscriberId}) from cache for Setup Intent ({SetupIntentId})", subscriberId, setupIntent.Id);
157+
152158
if (subscriberId == null)
153159
{
160+
logger.LogError("Cached subscriber ID for Setup Intent ({SetupIntentId}) is null", setupIntent.Id);
154161
return null;
155162
}
156163

157164
var organization = await organizationRepository.GetByIdAsync(subscriberId.Value);
165+
logger.LogInformation("Retrieved organization ({OrganizationId}) via subscriber ID for Setup Intent ({SetupIntentId})", organization?.Id, setupIntent.Id);
158166
if (organization is { GatewayCustomerId: not null })
159167
{
160168
var organizationCustomer = await stripeFacade.GetCustomer(organization.GatewayCustomerId);
169+
logger.LogInformation("Retrieved customer ({CustomerId}) via organization ID for Setup Intent ({SetupIntentId})", organization.Id, setupIntent.Id);
161170
return organizationCustomer.Metadata;
162171
}
163172

164173
var provider = await providerRepository.GetByIdAsync(subscriberId.Value);
174+
logger.LogInformation("Retrieved provider ({ProviderId}) via subscriber ID for Setup Intent ({SetupIntentId})", provider?.Id, setupIntent.Id);
165175
if (provider is not { GatewayCustomerId: not null })
166176
{
167177
return null;
168178
}
169179

170180
var providerCustomer = await stripeFacade.GetCustomer(provider.GatewayCustomerId);
181+
logger.LogInformation("Retrieved customer ({CustomerId}) via provider ID for Setup Intent ({SetupIntentId})", provider.Id, setupIntent.Id);
171182
return providerCustomer.Metadata;
172183
}
173184
}

src/Core/Billing/Caches/Implementations/SetupIntentDistributedCache.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using Microsoft.Extensions.Caching.Distributed;
22
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Logging;
34

45
namespace Bit.Core.Billing.Caches.Implementations;
56

67
public class SetupIntentDistributedCache(
78
[FromKeyedServices("persistent")]
8-
IDistributedCache distributedCache) : ISetupIntentCache
9+
IDistributedCache distributedCache,
10+
ILogger<SetupIntentDistributedCache> logger) : ISetupIntentCache
911
{
1012
public async Task<string?> GetSetupIntentIdForSubscriber(Guid subscriberId)
1113
{
@@ -17,11 +19,12 @@ public class SetupIntentDistributedCache(
1719
{
1820
var cacheKey = GetCacheKeyBySetupIntentId(setupIntentId);
1921
var value = await distributedCache.GetStringAsync(cacheKey);
20-
if (string.IsNullOrEmpty(value) || !Guid.TryParse(value, out var subscriberId))
22+
if (!string.IsNullOrEmpty(value) && Guid.TryParse(value, out var subscriberId))
2123
{
22-
return null;
24+
return subscriberId;
2325
}
24-
return subscriberId;
26+
logger.LogError("Subscriber ID value ({Value}) cached for Setup Intent ({SetupIntentId}) is null or not a valid Guid", value, setupIntentId);
27+
return null;
2528
}
2629

2730
public async Task RemoveSetupIntentForSubscriber(Guid subscriberId)

src/Core/Billing/Payment/Commands/UpdatePaymentMethodCommand.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ private async Task<BillingCommandResult<MaskedPaymentMethod>> AddBankAccountAsyn
9494

9595
await setupIntentCache.Set(subscriber.Id, setupIntent.Id);
9696

97+
_logger.LogInformation("{Command}: Successfully cached Setup Intent ({SetupIntentId}) for subscriber ({SubscriberID})", CommandName, setupIntent.Id, subscriber.Id);
98+
9799
await UnlinkBraintreeCustomerAsync(customer);
98100

99101
return MaskedPaymentMethod.From(setupIntent);

test/Billing.Test/Services/StripeEventServiceTests.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Bit.Core.Billing.Caches;
55
using Bit.Core.Repositories;
66
using Bit.Core.Settings;
7+
using Microsoft.Extensions.Logging;
78
using NSubstitute;
89
using Stripe;
910
using Xunit;
@@ -28,7 +29,13 @@ public StripeEventServiceTests()
2829
_providerRepository = Substitute.For<IProviderRepository>();
2930
_setupIntentCache = Substitute.For<ISetupIntentCache>();
3031
_stripeFacade = Substitute.For<IStripeFacade>();
31-
_stripeEventService = new StripeEventService(globalSettings, _organizationRepository, _providerRepository, _setupIntentCache, _stripeFacade);
32+
_stripeEventService = new StripeEventService(
33+
globalSettings,
34+
Substitute.For<ILogger<StripeEventService>>(),
35+
_organizationRepository,
36+
_providerRepository,
37+
_setupIntentCache,
38+
_stripeFacade);
3239
}
3340

3441
#region GetCharge

0 commit comments

Comments
 (0)