feat(infra): implement plaid service

This commit is contained in:
Stevan Freeborn
2026-02-13 04:37:47 -06:00
parent dd08eee5a3
commit c4a8aada6b
8 changed files with 120 additions and 3 deletions
@@ -0,0 +1,32 @@
namespace FiscalOS.Infra.Accounts.Plaid;
public sealed record PlaidClientOptions
{
public string ClientId { get; init; } = string.Empty;
public string Secret { get; init; } = string.Empty;
public IOptions<PlaidOptions> ToPlaidOptions()
{
return Options.Create<PlaidOptions>(new()
{
ClientId = ClientId,
Secret = Secret
});
}
}
public sealed record PlaidClientOptionsSetup : IConfigureOptions<PlaidClientOptions>
{
private const string SectionName = nameof(PlaidClientOptions);
private readonly IConfiguration _configuration;
public PlaidClientOptionsSetup(IConfiguration configuration)
{
_configuration = configuration;
}
public void Configure(PlaidClientOptions options)
{
_configuration.GetSection(SectionName).Bind(options);
}
}
@@ -0,0 +1,16 @@
namespace FiscalOS.Infra.Accounts.Plaid;
public class PlaidException : Exception
{
public PlaidException()
{
}
public PlaidException(string message) : base(message)
{
}
public PlaidException(string message, Exception innerException) : base(message, innerException)
{
}
}
@@ -12,11 +12,15 @@ public sealed class PlaidMetadata : InstitutionMetadata
}
public static PlaidMetadata From(
string plaidId,
string plaidName,
string? plaidId,
string? plaidName,
string encryptedAccessToken
)
{
ArgumentNullException.ThrowIfNull(plaidId, nameof(plaidId));
ArgumentNullException.ThrowIfNull(plaidName, nameof(plaidName));
ArgumentNullException.ThrowIfNull(encryptedAccessToken, nameof(encryptedAccessToken));
return new PlaidMetadata
{
PlaidId = plaidId,
@@ -0,0 +1,47 @@
namespace FiscalOS.Infra.Accounts.Plaid;
public sealed class PlaidService
{
private readonly PlaidClient _client;
private PlaidService(PlaidClient client)
{
_client = client;
}
public static PlaidService From(IServiceProvider sp)
{
var client = sp.GetRequiredService<PlaidClient>();
return new(client);
}
public async Task<(string ItemId, string AccessToken)> ExchangeTokenAsync(string publicToken)
{
var ptr = await _client.ItemPublicTokenExchangeAsync(new()
{
PublicToken = publicToken,
}).ConfigureAwait(false);
if (ptr.IsSuccessStatusCode is false)
{
throw new PlaidException("Unable to exchange public token for access token");
}
return (ptr.ItemId, ptr.AccessToken);
}
public async Task<ItemWithConsentFields> GetItemAsync(string accessToken)
{
var ar = await _client.ItemGetAsync(new()
{
AccessToken = accessToken,
}).ConfigureAwait(false);
if (ar.IsSuccessStatusCode is false)
{
throw new PlaidException("Unable to retrieve item");
}
return ar.Item;
}
}