· 5 years ago · Oct 13, 2020, 12:40 PM
1using Newtonsoft.Json.Linq;
2using System;
3using System.Collections.Generic;
4using System.Configuration;
5using System.Linq;
6using System.Net;
7using System.Transactions;
8using System.Web;
9using System.Web.Mvc;
10using VendasPMBLeads.DTO.Customer;
11using VendasPMBLeads.DTO.Documents;
12using VendasPMBLeads.Service.Model;
13using VendasPMBLeads.Web.Utils;
14using VendasPMBLeads.Web.ViewModel;
15
16namespace VendasPMBLeads.Web.Controllers
17{
18 public class HomeController : Controller
19 {
20 public ActionResult Index()
21 {
22 var PDVModel = new PDVTypeModel(ConfigurationHandler.GetVendasPMBLeadsConnectionString());
23 var StateModel = new StateModel(ConfigurationHandler.GetVendasPMBLeadsConnectionString());
24
25 LeadViewModel leadVM = new LeadViewModel();
26
27 leadVM.States = StateModel.GetlstStateType()
28 .Select(s => new SelectListItem()
29 {
30 Text = s.UfCode,
31 Value = Convert.ToString(s.StateID)
32 }).ToList();
33
34 leadVM.PDVTypes = PDVModel.GetlstPDVType()
35 .Select(s => new SelectListItem()
36 {
37 Text = s.Description,
38 Value = Convert.ToString(s.PDVTypeID)
39 }).ToList();
40
41 if (TempData["messageSuccess"] != null)
42 ViewBag.messageSuccess = TempData["messageSuccess"].ToString();
43
44 if (TempData["Status"] != null)
45 ViewBag.Status = TempData["Status"].ToString();
46
47 ViewBag.SiteKey = ConfigurationManager.AppSettings["SiteKey"];
48
49 return View(leadVM);
50 }
51
52 public ActionResult Save(Customer customer)
53 {
54 string message = "success";
55
56 try
57 {
58 var response = Request["g-recaptcha-response"];
59 string secretKey = ConfigurationManager.AppSettings["SecretKey"];
60 var cliente = new WebClient();
61 var resultado = cliente.DownloadString(
62 string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}",
63 secretKey, response));
64 var obj = JObject.Parse(resultado);
65
66 var status = (bool)obj.SelectToken("success");
67
68 if (status)
69 {
70 var customerModel = new CustomerModel(ConfigurationHandler.GetVendasPMBLeadsConnectionString());
71 var documentModel = new DocumentModel(ConfigurationHandler.GetVendasPMBLeadsConnectionString());
72
73 using (var tran = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { Timeout = new TimeSpan(0, 2, 0), IsolationLevel = IsolationLevel.ReadCommitted }))
74 {
75 if (!string.IsNullOrWhiteSpace(customer.CPF)) customer.CPF = customer.CPF.Replace(".", "").Replace("-", "").Replace("/", "");
76 if (!string.IsNullOrWhiteSpace(customer.CNPJ)) customer.CNPJ = customer.CNPJ.Replace(".", "").Replace("-", "").Replace("/", "");
77 if (!string.IsNullOrWhiteSpace(customer.CEP)) customer.CEP = customer.CEP.Replace("-", "");
78
79 message = customerModel.SaveCustomer(customer);
80 var customerID = customerModel.GetCustomerID();
81
82 tran.Complete();
83 }
84 }
85 else
86 {
87 message = "Tente Novamente: Google reCaptcha validação FALHOU !!!";
88 TempData["Status"] = "True";
89 }
90 }
91 catch (Exception ex)
92 {
93 message = "Erro ao salvar: " + ex.Message;
94 }
95
96 TempData["messageSuccess"] = message;
97
98 return RedirectToAction("Index");
99 }
100 }
101}