예제 #1
0
        public async Task <BankingResult <List <Transaction> > > GetTransactionsAsync(string accountId, IPagerContext context = null, string psuIp = null)
        {
            try
            {
                var account     = _userContextLocal.Accounts.FirstOrDefault(x => x.Id == accountId) ?? throw new ApiCallException("Invalide accountId");
                var accessToken = _userContextLocal.Tokens.FirstOrDefault(x => x.AccessToken == account.AccessToken);
                StetPagerContext pagerContext = (context as StetPagerContext) ?? new StetPagerContext();

                var client = GetClient();
                if (accessToken.TokenValidUntil < DateTime.Now)
                {
                    await RefreshToken(accessToken);
                }
                client.DefaultRequestHeaders.Add("Authorization", accessToken.FullToken);

                var url = pagerContext.GetRequestParams(relativeUrlAisPart, $"/accounts/{accountId}/transactions");
                if (!SdkApiSettings.IsSandbox)
                {
                    client.SignRequest(_settings.SigningCertificate, HttpMethod.Get, url, _settings.PemFileUrl);
                }

                var result = await client.GetAsync(url);

                List <Transaction> data    = new List <Transaction>();
                string             rawData = await result.Content.ReadAsStringAsync();

                if (result.StatusCode != System.Net.HttpStatusCode.NoContent)
                {
                    var model = JsonConvert.DeserializeObject <TransactionsDto>(rawData);
                    pagerContext.AddNextPageKey(model._links?.next?.href);

                    // pagerContext.SetTotal((uint)model.pagination.rowCount);
                    // pagerContext.SetPageTotal((uint)model.pagination.pageCount);

                    data = model.transactions?.Where(x => x.status.ToLower() == "book").Select(x => new Transaction
                    {
                        Id            = x.entryReference,
                        Amount        = x.transactionAmount.amount,
                        Currency      = x.transactionAmount.currency,
                        ExecutionDate = x.bookingDate,
                        ValueDate     = x.valueDate,
                        Description   = x.additionalTransactionInformation?.Narrative?.Aggregate((i, j) => i + "\n" + j)
                    }).ToList();
                }

                return(new BankingResult <List <Transaction> >(ResultStatus.DONE, url, data, rawData, pagerContext));
            }
            catch (ApiCallException e) { throw e; }
            catch (ApiUnauthorizedException e) { throw e; }
            catch (PagerException e) { throw e; }
            catch (SdkUnauthorizedException e) { throw e; }
            catch (Exception e)
            {
                await LogAsync(apiUrl, 500, Http.Get, e.ToString());

                throw e;
            }
        }
예제 #2
0
        public async Task <BankingResult <List <Transaction> > > GetTransactionsAsync(string accountId, IPagerContext context = null)
        {
            try
            {
                var account     = _userContextLocal.Accounts.FirstOrDefault(x => x.Id == accountId) ?? throw new ApiCallException("Invalide accountId");
                var accessToken = _userContextLocal.Tokens.FirstOrDefault(x => x.AccessToken == account.AccessToken && x.TokenValidUntil > DateTime.Now) ?? throw new ApiCallException("Consent invalide or expired");
                StetPagerContext pagerContext = (context as StetPagerContext) ?? new StetPagerContext();

                var client = GetClient();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Add("Authorization", accessToken.FullToken);

                // var url = $"/psd2/v1/accounts/{accountId}/transactions{pagerContext.GetRequestParams()}";
                var url = $"/psd2/v1/accounts/{accountId}/transactions";

                client.SignRequest(_settings.SigningCertificate, HttpMethod.Get, url, _settings.PemFileUrl);
                var result = await client.GetAsync(url);

                List <Transaction> data    = new List <Transaction>();
                string             rawData = await result.Content.ReadAsStringAsync();

                if (result.StatusCode != System.Net.HttpStatusCode.NoContent)
                {
                    var model = JsonConvert.DeserializeObject <TransactionsDto>(rawData);
                    // pagerContext.SetTotal((uint)model.pagination.rowCount);
                    // pagerContext.SetPageTotal((uint)model.pagination.pageCount);

                    data = model.transactions?.Select(x => new Transaction
                    {
                        Id            = x.entryReference,
                        Amount        = x.transactionAmount.amount,
                        Currency      = x.transactionAmount.currency,
                        ExecutionDate = x.bookingDate,
                        ValueDate     = x.valueDate
                    }).ToList();
                }

                return(new BankingResult <List <Transaction> >(ResultStatus.DONE, url, data, rawData, pagerContext));
            }
            catch (ApiCallException e) { throw e; }
            catch (ApiUnauthorizedException e) { throw e; }
            catch (PagerException e) { throw e; }
            catch (SdkUnauthorizedException e) { throw e; }
            catch (Exception e)
            {
                await LogAsync(apiUrl, 500, Http.Get, e.ToString());

                throw e;
            }
        }