using FindAndDrive.Domain.Interfaces.Gateways; using FindAndDrive.Domain.Models.DTO.Request; using FindAndDrive.Domain.Models.Gateways.Experian; using Newtonsoft.Json; using System.Net.Http; using System.Text; using FindAndDrive.Domain.Models.Settings; using Microsoft.Extensions.Options; using System.Text.RegularExpressions; using FindAndDrive.Domain.Models.Exceptions; using Newtonsoft.Json.Linq; using Microsoft.Extensions.Logging; namespace FindAndDrive.Gateway.Experian; public class ExperianGateway : IExperianGateway { private readonly HttpClient _httpClient; private readonly ExperianSettings _settings; private readonly ILogger _logger; public ExperianGateway(HttpClient httpClient, IOptions settings, ILogger logger) { _httpClient = httpClient; _settings = settings.Value; _logger = logger; } public async Task getAffordability(ExperianRequest request) { ExperianGatewayResponse? response = null; request.pUsername = _settings.ExperianUsername; request.pPassword = _settings.ExperianPassword; string jsonRequest = JsonConvert.SerializeObject(request); var httpContent = new StringContent(jsonRequest, Encoding.UTF8, "application/json"); var httpResponse = await _httpClient.PostAsync("https://webservices-uat.compuscan.co.za:9443/Afford/DoAffordability", httpContent); if (!httpResponse.IsSuccessStatusCode) { throw new GatewayErrorException(httpResponse.ReasonPhrase ?? "Experian Gateway failed with a status code of:" + httpResponse.StatusCode); } var content = await httpResponse.Content.ReadAsStringAsync(); var experianGatewayFullResponse = JsonConvert.DeserializeObject(content); if (experianGatewayFullResponse.Transaction_Completed == false) { throw new BadRequestException($"Experian gateway failed with a status code of: {experianGatewayFullResponse.Err_Code} and message: {experianGatewayFullResponse.Err_String}"); } if (experianGatewayFullResponse != null && experianGatewayFullResponse.Ret_Data != null) { //JObject jObject = JObject.Parse(experianGatewayFullResponse.ToString()); //jObject["GMIP_Confidence_Level"] = ReplaceWhitespace(jObject["GMIP_Confidence_Level"].ToString(), ""); try { response = JsonConvert.DeserializeObject(experianGatewayFullResponse.Ret_Data.ToString()); } catch(Exception ex) { _logger.LogError(ex.Message); throw new BadRequestException("Response from Experian couldnt be parsed"); } } return response; } public async Task getAffordabilityIndependent(ExperianRequest request) { request.pUsername = _settings.ExperianUsername; request.pPassword = _settings.ExperianPassword; string jsonRequest = JsonConvert.SerializeObject(request); var httpContent = new StringContent(jsonRequest, Encoding.UTF8, "application/json"); var httpResponse = await _httpClient.PostAsync("https://webservices-uat.compuscan.co.za:9443/Afford/DoAffordability", httpContent); if (!httpResponse.IsSuccessStatusCode) { throw new GatewayErrorException(httpResponse.ReasonPhrase ?? "Experian Gateway failed with a status code of:" + httpResponse.StatusCode); } var content = await httpResponse.Content.ReadAsStringAsync(); return content; } private static readonly Regex sWhitespace = new Regex(@"\s+"); public static string ReplaceWhitespace(string input, string replacement) { return sWhitespace.Replace(input, replacement); } }