Esempio n. 1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "plaid/token/create")] HttpRequest req,
            ILogger log,
            ExecutionContext context)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string countryCode = req.Query["countryCode"];

            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddJsonFile("secret.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            var apiConfig = new PlaidApiConfig();

            config.Bind(nameof(PlaidApiConfig), apiConfig);

            var httpClient         = new HttpClient();
            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post,
                                                            $"https://{apiConfig.EnvUrl}/link/token/create");

            countryCode = string.IsNullOrEmpty(countryCode) ? "GB" : countryCode;

            var content = $@"{{
                ""client_id"": ""{apiConfig.ClientId}"",
                ""secret"": ""{apiConfig.Secret}"",
                ""client_name"": ""Insert Client name here"",
                ""country_codes"": [""{countryCode}""],
                ""language"": ""en"",
                ""user"": {{
                    ""client_user_id"": ""unique_user_id""
                }},
                ""products"": [""auth""]
            }}";

            httpRequestMessage.Content = new StringContent(content, Encoding.UTF8, "application/json");

            var result = await httpClient.SendAsync(httpRequestMessage);

            var response = JsonConvert.DeserializeObject <PlaidApiTokenResponse>(await result.Content.ReadAsStringAsync());

            return(new OkObjectResult(response.link_token));
        }
Esempio n. 2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "plaid/transactions")] HttpRequest req,
            ILogger log,
            ExecutionContext context)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            var bodyRaw = await new StreamReader(req.Body).ReadToEndAsync();

            var body = JsonConvert.DeserializeObject <PlaidApiTransactionsRequest>(bodyRaw);

            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddJsonFile("secret.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            var apiConfig = new PlaidApiConfig();

            config.Bind(nameof(PlaidApiConfig), apiConfig);

            var httpClient         = new HttpClient();
            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post,
                                                            $"https://{apiConfig.EnvUrl}/transactions/get");


            var content = $@"{{
                ""client_id"": ""{apiConfig.ClientId}"",
                ""secret"": ""{apiConfig.Secret}"",
                ""access_token"": ""{body.access_token}"",
                ""start_date"": ""2020-09-01"",
                ""end_date"": ""2020-10-28"" 
            }}";

            httpRequestMessage.Content = new StringContent(content, Encoding.UTF8, "application/json");

            var result = await httpClient.SendAsync(httpRequestMessage);

            var response = JsonConvert.DeserializeObject <PlaidApiTokenExchangeResponse>(await result.Content.ReadAsStringAsync());

            return(new OkObjectResult(response.access_token));
        }