Пример #1
0
        public async Task <IActionResult> CheckContractorByApi([FromBody] Contractor contractor)
        {
            ApiService apiService = new ApiService();
            var        token      = await apiService.ApiLogin();

            var countries = await apiService.GetCountriesAsync(token);

            var ueCountries = await _context.Countries.ToArrayAsync();

            if (contractor.CountryCode.ToUpper() == "PL")
            {
                var response = await apiService.GetGusDataAsync(contractor.VatId, token);

                if (response.Count > 0)
                {
                    contractor.Name           = response[0].Name;
                    contractor.VatId          = response[0].VatNumber;
                    contractor.Street         = response[0].Street;
                    contractor.EstateNumber   = response[0].EstateNumber;
                    contractor.Firm           = Firm.FirmaPolska;
                    contractor.QuartersNumber = response[0].QuartersNumber;
                    contractor.City           = response[0].City;
                    contractor.Province       = response[0].Province;
                    contractor.Email          = response[0].Email;
                    contractor.PostalCode     = response[0].PostalCode;
                    contractor.Regon          = response[0].Regon;
                    return(new JsonResult(new { Origin = "GUS", Valid = true, Data = contractor }));
                }
            }
            else if (ueCountries.Select(c => c.Symbol).Contains(contractor.CountryCode.ToUpper()))
            {
                var viesRequest = new ViesSimpleRequest
                {
                    ContractorPrefix = contractor.CountryCode.ToUpper(),
                    ContractorSuffix = contractor.VatId.Substring(2, contractor.VatId.Length - 2),
                };
                var response = await apiService.GetViesDataAsync(viesRequest, token);

                var viesContractor = new Contractor {
                    Name = string.IsNullOrEmpty(response.Name) ? "" : response.Name
                };
                return(new JsonResult(new { Origin = "VIES", Valid = response.Status, Data = viesContractor }));
            }
            else if (countries.Select(c => c.Symbol).Contains(contractor.CountryCode.ToUpper()))
            {
                return(new JsonResult(new
                {
                    Origin = "WORLD",
                    Valid = true,
                    Data = contractor
                }));
            }

            return(new JsonResult(new { Origin = "NONE", Valid = false, Data = new Contractor() }));
        }