Пример #1
0
        public async Task <CustomerProfile> GetCustomerProfile(string accessToken, Guid userId)
        {
            CustomerProfile    result  = new CustomerProfile();
            BankServiceRequest request = new BankServiceRequest()
            {
                HeaderParameters = new Dictionary <string, string>()
            };

            request.HeaderParameters.Add("access_token", accessToken);
            request.HeaderParameters.Add("account_id", "");

            BankServiceResponse accountResponse = await _bankService.GetAccounts(request);

            if (accountResponse.StatusCode == HttpStatusCode.OK)
            {
                JsonDocument accountResponseJson = JsonDocument.Parse(await accountResponse.Content.ReadAsStringAsync());
                result.Accounts = new List <Account>();

                foreach (JsonElement accountElement in accountResponseJson.RootElement.GetProperty("Data").GetProperty("Account").EnumerateArray())
                {
                    // Account bilgisi doldurma kısmı
                    Account account = new Account
                    {
                        ProviderAccountId      = accountElement.GetProperty("AccountId").GetString(),
                        AccountTypeId          = AccountType.GetInstanceByName(accountElement.GetProperty("AccountType").GetString()).Value,
                        CurrencyTypeId         = CurrencyType.GetInstanceByName(accountElement.GetProperty("Currency").GetString()).Value,
                        AccountIdentifications = new List <AccountIdentification>()
                    };
                    foreach (JsonElement accountDetailElement in accountElement.GetProperty("Account").EnumerateArray())
                    {
                        account.Name = accountDetailElement.GetProperty("Name").GetString();
                        if (accountDetailElement.GetProperty("SchemeName").GetString() == "UK.OBIE.IBAN")
                        {
                            account.AccountIdentifications.Add(new AccountIdentification()
                            {
                                AccountIdentificationTypeId = AccountIdentificationType.IBAN.Value,
                                Identification = accountDetailElement.GetProperty("Identification").GetString()
                            });
                        }
                        else if (accountDetailElement.GetProperty("SchemeName").GetString() == "UK.OBIE.SortCodeAccountNumber")
                        {
                            account.AccountIdentifications.Add(new AccountIdentification()
                            {
                                AccountIdentificationTypeId = AccountIdentificationType.SortCode.Value,
                                Identification = accountDetailElement.GetProperty("Identification").GetString()
                            });
                        }
                    }

                    // Account için Transaction bilgisi doldurma kısmı
                    request.HeaderParameters["account_id"] = account.ProviderAccountId;
                    BankServiceResponse transactionResponse = await _bankService.GetTransactions(request);

                    if (transactionResponse.StatusCode == HttpStatusCode.OK)
                    {
                        JsonDocument transactionResponseJson = JsonDocument.Parse(await transactionResponse.Content.ReadAsStringAsync());
                        account.Transactions = new List <Transaction>();
                        foreach (JsonElement transactionElement in transactionResponseJson.RootElement.GetProperty("Data").GetProperty("Transaction").EnumerateArray())
                        {
                            Transaction transaction = new Transaction
                            {
                                ProviderTransactionId = transactionElement.GetProperty("TransactionId").GetString(),
                                Description           = transactionElement.GetProperty("TransactionInformation").GetString(),
                                Date              = transactionElement.GetProperty("BookingDateTime").GetDateTime().ToString(),
                                Amount            = transactionElement.GetProperty("Amount").GetProperty("Amount").GetString(),
                                TransactionType   = TransactionType.GetInstanceByName(transactionElement.GetProperty("CreditDebitIndicator").GetString()).Value,
                                TransactionStatus = TransactionStatus.GetInstanceByName(transactionElement.GetProperty("Status").GetString()).Value
                            };

                            JsonElement merchant;
                            if (transactionElement.TryGetProperty("MerchantDetails", out merchant))
                            {
                                transaction.MerchantCategory = MerchantCategory.GetInstanceByCode(int.Parse(merchant.GetProperty("MerchantCategoryCode").GetString())).Value;
                            }

                            account.Transactions.Add(transaction);
                        }
                    }

                    // Account için Balance bilgisi doldurma kısmı
                    BankServiceResponse balanceResponse = await _bankService.GetBalances(request);

                    if (balanceResponse.StatusCode == HttpStatusCode.OK)
                    {
                        JsonDocument balanceResponseJson = JsonDocument.Parse(await balanceResponse.Content.ReadAsStringAsync());
                        account.Balances = new List <Balance>();
                        foreach (JsonElement balanceElement in balanceResponseJson.RootElement.GetProperty("Data").GetProperty("Balance").EnumerateArray())
                        {
                            Balance balance = new Balance
                            {
                                Amount       = balanceElement.GetProperty("Amount").GetProperty("Amount").GetString(),
                                BalanceType  = BalanceType.GetInstanceByName(balanceElement.GetProperty("Type").GetString()).Value,
                                CurrencyType = CurrencyType.GetInstanceByName(balanceElement.GetProperty("Amount").GetProperty("Currency").GetString()).Value
                            };
                            account.Balances.Add(balance);
                        }
                    }
                    result.Accounts.Add(account);
                }
            }
            result.ProviderId      = Provider.RBS.Value;
            result.UserId          = userId;
            result.UpdateTimestamp = DateTime.Now;
            return(result);
        }