1717using Microsoft . Extensions . Logging . Abstractions ;
1818using NoFrixion . MoneyMoov . Models ;
1919using System . Net ;
20+ using System . Net . Http . Headers ;
2021
2122namespace NoFrixion . MoneyMoov ;
2223
@@ -25,6 +26,10 @@ public interface IAccountClient
2526 Task < RestApiResponse < PaymentAccount > > GetAccountAsync ( string userAccessToken , Guid accountID ) ;
2627
2728 Task < RestApiResponse < PaymentAccount > > CreateAccountAsync ( string userAccessToken , PaymentAccountCreate accountCreate ) ;
29+
30+ Task < RestApiFileResponse > GetStatementAsync ( string accessToken , Guid accountID , Guid statementID ) ;
31+
32+ Task < RestApiResponse > ClearStatementsAsync ( string accessToken ) ;
2833}
2934
3035public class AccountClient : IAccountClient
@@ -81,4 +86,62 @@ public Task<RestApiResponse<PaymentAccount>> CreateAccountAsync(string userAcces
8186 _ => Task . FromResult ( new RestApiResponse < PaymentAccount > ( HttpStatusCode . PreconditionFailed , new Uri ( url ) , prob ) )
8287 } ;
8388 }
89+
90+ /// <summary>
91+ /// Calls the MoneyMoov statements endpoint to get a statement file.
92+ /// </summary>
93+ /// <param name="accessToken">A User or Merchant scoped JWT access token.</param>
94+ /// <param name="accountID">The account ID</param>
95+ /// <param name="statementID">The ID of the report to retrieve the result for.</param>
96+ /// <returns>If successful, the statement result.</returns>
97+ public async Task < RestApiFileResponse > GetStatementAsync ( string accessToken , Guid accountID , Guid statementID )
98+ {
99+ var url = MoneyMoovUrlBuilder . AccountsApi . StatementsUrl ( _apiClient . GetBaseUri ( ) . ToString ( ) , accountID , statementID ) ;
100+ var prob = _apiClient . CheckAccessToken ( accessToken , nameof ( GetStatementAsync ) ) ;
101+
102+ if ( ! prob . IsEmpty )
103+ {
104+ return new RestApiFileResponse ( HttpStatusCode . PreconditionFailed , new Uri ( url ) , prob ) ;
105+ }
106+
107+ using ( var httpClient = new HttpClient ( ) )
108+ {
109+ httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , accessToken ) ;
110+ var response = await httpClient . GetAsync ( url ) ;
111+
112+ if ( response . IsSuccessStatusCode )
113+ {
114+ var content = await response . Content . ReadAsByteArrayAsync ( ) ;
115+ var contentType = response . Content . Headers . ContentType ? . ToString ( ) ?? "application/pdf" ;
116+ var fileName = response . Content . Headers . ContentDisposition ? . FileName ? . Trim ( '\" ' ) ?? "downloaded_file" ;
117+
118+ return new RestApiFileResponse ( HttpStatusCode . OK , new Uri ( url ) , response . Headers , content , contentType , fileName ) ;
119+ }
120+ else
121+ {
122+ return new RestApiFileResponse (
123+ response . StatusCode ,
124+ new Uri ( url ) ,
125+ new NoFrixionProblem ( response . ReasonPhrase ?? "File download failed." , ( int ) response . StatusCode ) ) ;
126+ }
127+ }
128+ }
129+
130+ /// <summary>
131+ /// Calls the MoneyMoov Statements endpoint to clear the user's cached statements. This allows them to be re-generated, for example
132+ /// if the statement was for the current month and the month has not yet completed.
133+ /// </summary>
134+ /// <param name="accessToken">The user token deleting the payout.</param>
135+ public Task < RestApiResponse > ClearStatementsAsync ( string accessToken )
136+ {
137+ var url = MoneyMoovUrlBuilder . AccountsApi . StatementsUrl ( _apiClient . GetBaseUri ( ) . ToString ( ) ) ;
138+
139+ var prob = _apiClient . CheckAccessToken ( accessToken , nameof ( ClearStatementsAsync ) ) ;
140+
141+ return prob switch
142+ {
143+ var p when p . IsEmpty => _apiClient . DeleteAsync ( url , accessToken ) ,
144+ _ => Task . FromResult ( new RestApiResponse ( HttpStatusCode . PreconditionFailed , new Uri ( url ) , prob ) )
145+ } ;
146+ }
84147}
0 commit comments