示例#1
0
        public async Task <BankingResult <string> > RequestAccountsAccessAsync(AccountsAccessRequest model)
        {
            try
            {
                var request = new BerlinGroupAccountAccessRequest
                {
                    access = new BerlinGroupAccess
                    {
                        allPsd2  = (model.TransactionAccounts == null && model.BalanceAccounts == null) ? "allAccounts" : null,
                        balances = model.BalanceAccounts?.Select(x => new BerlinGroupAccountIban {
                            iban = x
                        }).ToList(),
                        transactions = model.TransactionAccounts?.Select(x => new BerlinGroupAccountIban {
                            iban = x
                        }).ToList()
                    },
                    combinedServiceIndicator = false,
                    frequencyPerDay          = model.FrequencyPerDay,
                    recurringIndicator       = true,
                    validUntil = DateTime.Today.AddDays(90).ToString("yyyy-MM-dd")
                };

                var payload = JsonConvert.SerializeObject(request);

                var content = new StringContent(payload, Encoding.UTF8, "application/json");
                var client  = GetClient();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Add("PSU-IP-Address", model.PsuIp);
                client.DefaultRequestHeaders.Add("TPP-Redirect-Preferred", "true");
                client.DefaultRequestHeaders.Add("TPP-Redirect-URI", model.RedirectUrl + $"?flowId={model.FlowId}");
                client.DefaultRequestHeaders.Add("TPP-Nok-Redirect-URI", model.RedirectUrl + $"?flowId={model.FlowId}&error");
                var url    = "/sgbt/lu/bg/v1/consents";
                var result = await client.PostAsync(url, content);

                string rawData = await result.Content.ReadAsStringAsync();

                var accountAccessResult = JsonConvert.DeserializeObject <BankingSDK.LU.SocieteGenerale.Models.SocieteGeneraleAccountsAccessResponse>(rawData);

                if (string.IsNullOrEmpty(accountAccessResult._links.scaRedirect))
                {
                    content = new StringContent("", Encoding.UTF8, "application/json");
                    client  = GetClient();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    string url2 = $"/sgbt/lu/bg/v1/consents/{accountAccessResult.consentId}/authorisations";
                    result = await client.PostAsync(url2, content);

                    string rawData2 = await result.Content.ReadAsStringAsync();

                    var consentAthorisation = JsonConvert.DeserializeObject <SocieteGeneraleAuthorization>(rawData2);
                    accountAccessResult._links.scaRedirect = consentAthorisation._links.scaRedirect;
                    rawData += rawData2;
                    url     += url2;
                }

                var flowContext = new FlowContext
                {
                    Id                      = model.FlowId,
                    ConnectorType           = ConnectorType,
                    FlowType                = FlowType.AccountsAccess,
                    AccountAccessProperties = new AccountAccessProperties
                    {
                        ConsentId           = accountAccessResult.consentId,
                        ValidUntil          = DateTime.Today.AddDays(90).Date,
                        BalanceAccounts     = model.BalanceAccounts,
                        TransactionAccounts = model.TransactionAccounts
                    }
                };

                return(new BankingResult <string>(ResultStatus.REDIRECT, url, accountAccessResult._links.scaRedirect, rawData, flowContext: flowContext));
            }
            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;
            }
        }
        public async Task <BankingResult <string> > RequestAccountsAccessAsync(AccountsAccessRequest model)
        {
            try
            {
                var request = new BerlinGroupAccountAccessRequest
                {
                    access = new BerlinGroupAccess
                    {
                        balances = new List <BerlinGroupAccountIban> {
                            new BerlinGroupAccountIban {
                                iban = model.SingleAccount
                            }
                        },
                        transactions = new List <BerlinGroupAccountIban> {
                            new BerlinGroupAccountIban {
                                iban = model.SingleAccount
                            }
                        }
                    },
                    combinedServiceIndicator = false,
                    frequencyPerDay          = model.FrequencyPerDay,
                    recurringIndicator       = true,
                    validUntil = DateTime.Today.AddDays(89).ToString("yyyy-MM-dd")
                };

                var payload = JsonConvert.SerializeObject(request);
                var content = new StringContent(payload, Encoding.UTF8, "application/json");
                content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
                var client = GetClient();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Add("PSU-IP-Address", model.PsuIp);
                client.DefaultRequestHeaders.Add("TPP-Redirect-URI", model.RedirectUrl);
                var url    = "/psd2/v2/consents";
                var result = await client.PostAsync(url, content);

                string rawData = await result.Content.ReadAsStringAsync();

                var accountAccessResult = JsonConvert.DeserializeObject <BerlinGroupAccountsAccessResponse>(rawData);
                //var codeVerifier = "WtTEuIaHve9RS_mMK6P99Z_RWL5cLTOqAJ2ar2BeN9g";
                var    codeVerifier = Guid.NewGuid().ToString();
                string codeChallenge;
                using (SHA256 sha256Hash = SHA256.Create())
                {
                    codeChallenge = Convert.ToBase64String(sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(codeVerifier))).Replace("=", "").Replace("+", "-").Replace("/", "_");//"IObWtymAvqW35KPIr8Gsl8jbKJUoL7Dx_EijWCvkwEM";
                }

                string mainCompany;
                switch (ConnectorType)
                {
                case ConnectorType.BE_KBC:
                    mainCompany = "0001";
                    break;

                case ConnectorType.BE_CBC:
                    mainCompany = "0002";
                    break;

                case ConnectorType.BE_KBC_BRUSSELS:
                    mainCompany = "0001&company=9998";
                    break;

                default:
                    throw new Exception("Unknown connector type");
                }

                // to specify the language add &language=NL
                //var redirect = $"{apiUrl}/ASK/oauth/authorize/1?client_id={_settings.NcaId}&redirect_uri={WebUtility.UrlEncode(model.RedirectUrl)}&response_type=code&scope={WebUtility.UrlEncode($"AIS:{accountAccessResult.consentId}")}&state={model.FlowId}&language=NL&mainCompany={mainCompany}&code_challenge={WebUtility.UrlEncode(codeChallenge)}&code_challenge_method=S256";
                var redirect = $"{RedirectUrl}/ASK/oauth/authorize/1?client_id={_settings.NcaId}&redirect_uri={WebUtility.UrlEncode(model.RedirectUrl)}&response_type=code&scope={WebUtility.UrlEncode($"AIS:{accountAccessResult.consentId}")}&state={model.FlowId}&mainCompany={mainCompany}&code_challenge={WebUtility.UrlEncode(codeChallenge)}&code_challenge_method=S256";

                var flowContext = new FlowContext
                {
                    Id                      = model.FlowId,
                    ConnectorType           = ConnectorType,
                    FlowType                = FlowType.AccountsAccess,
                    CodeVerifier            = codeVerifier,
                    RedirectUrl             = model.RedirectUrl,
                    AccountAccessProperties = new AccountAccessProperties
                    {
                        ConsentId     = accountAccessResult.consentId,
                        ValidUntil    = DateTime.Today.AddDays(89).Date,
                        SingleAccount = model.SingleAccount
                    }
                };

                return(new BankingResult <string>(ResultStatus.REDIRECT, url, redirect, rawData, flowContext: flowContext));
            }
            catch (ApiCallException e) { throw e; }
            catch (SdkUnauthorizedException e) { throw e; }
            catch (Exception e)
            {
                await LogAsync(apiUrl, 500, Http.Get, e.ToString());

                throw e;
            }
        }