public async Task <IActionResult> Recipient1099Create([Bind("Email,Password,ConfirmPassword,BusinessName,FirstName,LastName,Phone,TaxIDNumber,StreetAddress,City,State,PostalCode,box1,box2,box3,box4,box5,box6,box7,box8,box9,box10,box12,box13,box14,box15,box16,box17")] Recipient1099InputModel recipient1099InputModel) { if (ModelState.IsValid) { // Getting payer info to submit in 1099MISC var intuitPayerId = (from u in _context.Users where u.UserName == User.Identity.Name select u).SingleOrDefault(); IntuitAuth auth; string username = "******"; string password = "******"; string IntuiAuthURL = "https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer"; string accessToken; string intuitRecipientId = ""; var values = new List <KeyValuePair <string, string> >(); values.Add(new KeyValuePair <string, string>("grant_type", "client_credentials")); var content = new FormUrlEncodedContent(values); var authenticationString = $"{username}:{password}"; var base64EncodedAuthenticationString = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(authenticationString)); var request = new HttpRequestMessage(HttpMethod.Post, IntuiAuthURL); request.Headers.Authorization = new AuthenticationHeaderValue("Basic", base64EncodedAuthenticationString); request.Content = content; var client = _clientFactory.CreateClient(); HttpResponseMessage response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { auth = await response.Content.ReadFromJsonAsync <IntuitAuth>(); accessToken = auth.Access_token; string CreateContactURL = @"https://formfly.api.intuit.com/v1/contacts"; var CreateContactBody = new IntuitCreateContactBody { //metadata = (new Metadata { id = "payer1" }), businessName = recipient1099InputModel.BusinessName, firstName = recipient1099InputModel.FirstName, lastName = recipient1099InputModel.LastName, streetAddress = recipient1099InputModel.StreetAddress, city = recipient1099InputModel.City, state = recipient1099InputModel.State, postalCode = recipient1099InputModel.PostalCode, phone = recipient1099InputModel.Phone, email = recipient1099InputModel.Email, tin = recipient1099InputModel.TaxIDNumber }; var CreateContactRequest = new HttpRequestMessage(HttpMethod.Post, CreateContactURL); CreateContactRequest.Content = new StringContent( JsonSerializer.Serialize(CreateContactBody), Encoding.UTF8, "application/json" ); var ContactClient = _clientFactory.CreateClient(); ContactClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); HttpResponseMessage ContactResponse = await ContactClient.SendAsync(CreateContactRequest); if (ContactResponse.IsSuccessStatusCode) { IntuitCreateContact newContact = await ContactResponse.Content.ReadFromJsonAsync <IntuitCreateContact>(); intuitRecipientId = newContact.id; } var New1099 = new Recipient1099 { //actions = new SmallPortal.Models.Actions { submit = true }, deliveryOptions = (new DeliveryOptions { mail = true }), payer = (new Payer { id = intuitPayerId.IntuitId, firstName = intuitPayerId.FirstName, lastName = intuitPayerId.LastName, streetAddress = intuitPayerId.StreetAddress, city = intuitPayerId.City, state = intuitPayerId.State, postalCode = intuitPayerId.PostalCode, phone = intuitPayerId.Phone, email = intuitPayerId.Email, tin = intuitPayerId.TaxIDNumber, validationStatus = "valid" }), recipient = (new Recipient { businessName = recipient1099InputModel.BusinessName, streetAddress = recipient1099InputModel.StreetAddress, city = recipient1099InputModel.City, state = recipient1099InputModel.State, postalCode = recipient1099InputModel.PostalCode, phone = recipient1099InputModel.Phone, email = recipient1099InputModel.Email, tin = recipient1099InputModel.TaxIDNumber }), boxValues = (new Boxvalues { box1 = recipient1099InputModel.box1, box2 = recipient1099InputModel.box2, box3 = recipient1099InputModel.box3, box4 = recipient1099InputModel.box4, box5 = recipient1099InputModel.box5, box6 = recipient1099InputModel.box6, box7 = recipient1099InputModel.box7, box8 = recipient1099InputModel.box8, box9 = recipient1099InputModel.box9, box10 = recipient1099InputModel.box10, box12 = recipient1099InputModel.box12, box13 = recipient1099InputModel.box13, box14 = recipient1099InputModel.box14, box15 = recipient1099InputModel.box15, box16 = recipient1099InputModel.box16, box17 = recipient1099InputModel.box17 }) }; string CreateRecipient1099URL = "https://formfly.api.intuit.com/v2/forms/2020/1099-misc"; var CreateRecipient1099 = new HttpRequestMessage(HttpMethod.Post, CreateRecipient1099URL); CreateRecipient1099.Content = new StringContent( JsonSerializer.Serialize(New1099), Encoding.UTF8, "application/json" ); var Recipient1099Client = _clientFactory.CreateClient(); Recipient1099Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); HttpResponseMessage Recipient1099Response = await Recipient1099Client.SendAsync(CreateRecipient1099); if (Recipient1099Response.IsSuccessStatusCode) { IntuitCreate1099 new1099 = await Recipient1099Response.Content.ReadFromJsonAsync <IntuitCreate1099>(); string formId = new1099.id; } else { IntuitError newError = await Recipient1099Response.Content.ReadFromJsonAsync <IntuitError>(); Console.WriteLine(newError); } } // _context.Add(recipient1099InputModel); //await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(recipient1099InputModel)); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { /* Add code to create contact at intuit * Authenticate first * we'll make these secret later */ IntuitAuth auth; string username = "******"; string password = "******"; string IntuitAuthURL = "https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer"; string accessToken; string IntuitTaxID = ""; var values = new List <KeyValuePair <string, string> >(); values.Add(new KeyValuePair <string, string>("grant_type", "client_credentials")); var content = new FormUrlEncodedContent(values); var authenticationString = $"{username}:{password}"; var base64EncodedAuthenticationString = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(authenticationString)); var request = new HttpRequestMessage(HttpMethod.Post, IntuitAuthURL); request.Headers.Authorization = new AuthenticationHeaderValue("Basic", base64EncodedAuthenticationString); request.Content = content; var client = _clientFactory.CreateClient(); HttpResponseMessage response = await client.SendAsync(request); Debug.WriteLine(response.Headers.ToString()); if (response.IsSuccessStatusCode) { auth = await response.Content.ReadFromJsonAsync <IntuitAuth>(); accessToken = auth.Access_token; string CreateContactURL = @"https://formfly.api.intuit.com/v1/contacts"; var CreateContactBody = new IntuitCreateContactBody { metadata = (new Metadata { id = "payer1" }), businessName = Input.BusinessName, firstName = Input.FirstName, lastName = Input.LastName, phone = Input.Phone, email = Input.Email, streetAddress = Input.StreetAddress, city = Input.City, state = Input.State, postalCode = Input.PostalCode, tin = Input.TaxIDNumber }; var CreateContactRequest = new HttpRequestMessage(HttpMethod.Post, CreateContactURL); CreateContactRequest.Content = new StringContent( JsonSerializer.Serialize(CreateContactBody), Encoding.UTF8, "application/json" ); //var CreateContactRequest = new HttpRequestMessage(HttpMethod.Post, CreateContactURL) //{ // Content = JsonContent.Create<IntuitCreateContactBody>(CreateContactBody) //}; var ContactClient = _clientFactory.CreateClient(); ContactClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); HttpResponseMessage ContactResponse = await ContactClient.SendAsync(CreateContactRequest); if (ContactResponse.IsSuccessStatusCode) { IntuitCreateContact newContact = await ContactResponse.Content.ReadFromJsonAsync <IntuitCreateContact>(); IntuitTaxID = newContact.id; } } var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email, BusinessName = Input.BusinessName, FirstName = Input.FirstName, LastName = Input.LastName, Phone = Input.Phone, StreetAddress = Input.StreetAddress, City = Input.City, State = Input.State, PostalCode = Input.PostalCode, TaxIDNumber = Input.TaxIDNumber, IntuitId = IntuitTaxID }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); if (_userManager.Options.SignIn.RequireConfirmedAccount) { return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl })); } else { await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }