Skip to content

Commit

Permalink
Merge pull request #411 from nofrixion/feature/MOOV-3606-statements-a…
Browse files Browse the repository at this point in the history
…ccount

Feature/moov 3606 statements account
  • Loading branch information
donalnofrixion authored Sep 10, 2024
2 parents 3b80003 + 4d2badf commit 140890c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/NoFrixion.MoneyMoov/ApiClients/AccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Microsoft.Extensions.Logging.Abstractions;
using NoFrixion.MoneyMoov.Models;
using System.Net;
using System.Net.Http.Headers;

namespace NoFrixion.MoneyMoov;

Expand All @@ -25,6 +26,10 @@ public interface IAccountClient
Task<RestApiResponse<PaymentAccount>> GetAccountAsync(string userAccessToken, Guid accountID);

Task<RestApiResponse<PaymentAccount>> CreateAccountAsync(string userAccessToken, PaymentAccountCreate accountCreate);

Task<RestApiFileResponse> GetStatementAsync(string accessToken, Guid accountID, Guid statementID);

Task<RestApiResponse> ClearStatementsAsync(string accessToken);
}

public class AccountClient : IAccountClient
Expand Down Expand Up @@ -81,4 +86,62 @@ public Task<RestApiResponse<PaymentAccount>> CreateAccountAsync(string userAcces
_ => Task.FromResult(new RestApiResponse<PaymentAccount>(HttpStatusCode.PreconditionFailed, new Uri(url), prob))
};
}

/// <summary>
/// Calls the MoneyMoov statements endpoint to get a statement file.
/// </summary>
/// <param name="accessToken">A User or Merchant scoped JWT access token.</param>
/// <param name="accountID">The account ID</param>
/// <param name="statementID">The ID of the report to retrieve the result for.</param>
/// <returns>If successful, the statement result.</returns>
public async Task<RestApiFileResponse> GetStatementAsync(string accessToken, Guid accountID, Guid statementID)
{
var url = MoneyMoovUrlBuilder.AccountsApi.StatementsUrl(_apiClient.GetBaseUri().ToString(), accountID, statementID);
var prob = _apiClient.CheckAccessToken(accessToken, nameof(GetStatementAsync));

if (!prob.IsEmpty)
{
return new RestApiFileResponse(HttpStatusCode.PreconditionFailed, new Uri(url), prob);
}

using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var response = await httpClient.GetAsync(url);

if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsByteArrayAsync();
var contentType = response.Content.Headers.ContentType?.ToString() ?? "application/pdf";
var fileName = response.Content.Headers.ContentDisposition?.FileName?.Trim('\"') ?? "downloaded_file";

return new RestApiFileResponse(HttpStatusCode.OK, new Uri(url), response.Headers, content, contentType, fileName);
}
else
{
return new RestApiFileResponse(
response.StatusCode,
new Uri(url),
new NoFrixionProblem(response.ReasonPhrase ?? "File download failed.", (int)response.StatusCode));
}
}
}

/// <summary>
/// Calls the MoneyMoov Statements endpoint to clear the user's cached statements. This allows them to be re-generated, for example
/// if the statement was for the current month and the month has not yet completed.
/// </summary>
/// <param name="accessToken">The user token deleting the payout.</param>
public Task<RestApiResponse> ClearStatementsAsync(string accessToken)
{
var url = MoneyMoovUrlBuilder.AccountsApi.StatementsUrl(_apiClient.GetBaseUri().ToString());

var prob = _apiClient.CheckAccessToken(accessToken, nameof(ClearStatementsAsync));

return prob switch
{
var p when p.IsEmpty => _apiClient.DeleteAsync(url, accessToken),
_ => Task.FromResult(new RestApiResponse(HttpStatusCode.PreconditionFailed, new Uri(url), prob))
};
}
}
2 changes: 2 additions & 0 deletions src/NoFrixion.MoneyMoov/ApiClients/StatementClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public StatementClient(IRestApiClient apiClient, ILogger<StatementClient> logger
/// <param name="accessToken">A User or Merchant scoped JWT access token.</param>
/// <param name="statementID">The ID of the report to retrieve the result for.</param>
/// <returns>If successful, the statement result.</returns>
[Obsolete("Use AccountClient.GetStatementAsync instead.")]
public async Task<RestApiFileResponse> GetStatementAsync(string accessToken, Guid statementID)
{
var url = MoneyMoovUrlBuilder.StatementsApi.StatementsUrl(_apiClient.GetBaseUri().ToString(), statementID);
Expand Down Expand Up @@ -88,6 +89,7 @@ public async Task<RestApiFileResponse> GetStatementAsync(string accessToken, Gui
/// if the statement was for the current month and the month has not yet completed.
/// </summary>
/// <param name="accessToken">The user token deleting the payout.</param>
[Obsolete("Use AccountClient.ClearStatementsAsync instead.")]
public Task<RestApiResponse> ClearStatementsAsync(string accessToken)
{
var url = MoneyMoovUrlBuilder.StatementsApi.StatementsUrl(_apiClient.GetBaseUri().ToString());
Expand Down
9 changes: 9 additions & 0 deletions src/NoFrixion.MoneyMoov/MoneyMoovUrlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ public static string AccountsUrl(string moneyMoovBaseUrl)

public static string AccountPayoutsUrl(string moneyMoovBaseUrl, Guid accountID)
=> $"{moneyMoovBaseUrl}/{MoneyMoovResources.accounts}/{accountID}/{MoneyMoovResources.payouts}";

public static string StatementsUrl(string moneyMoovBaseUrl)
=> $"{moneyMoovBaseUrl}/{MoneyMoovResources.accounts}/{MoneyMoovResources.statements}";

public static string StatementsUrlWithAccountPlaceholder(string moneyMoovBaseUrl)
=> $"{moneyMoovBaseUrl}/{MoneyMoovResources.accounts}/##account##/{MoneyMoovResources.statements}";

public static string StatementsUrl(string moneyMoovBaseUrl, Guid accountID, Guid statementID)
=> $"{moneyMoovBaseUrl}/{MoneyMoovResources.accounts}/{accountID}/{MoneyMoovResources.statements}/{statementID}";
}

/// <summary>
Expand Down

0 comments on commit 140890c

Please sign in to comment.