You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

143 lines
4.7 KiB

using DealerSelection.Api.CommonUtil;
using DealerSelection.Api.Infrastructure.InfoBip;
using DealerSelection.Api.Interface;
using DealerSelection.Common.Interfaces.HttpClient;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net;
using System.Text;
/// <summary>
/// Summary description for KarixSmsService
/// </summary>
public class InfoBipSmsServiceApi : IInfoBipSmsServiceApi
{
static string userAuth = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("Bajaj_OTP1" + ":" + "BAL@October2022"));
private IRepository Repository { get; }
private readonly ILogger _logger;
private IHttpClientHandler ClientHandler { get; }
public InfoBipSmsServiceApi(IRepository repository, IHttpClientHandler clientHandler, ILogger<InfoBipSmsServiceApi> logger)
{
Repository = repository;
ClientHandler = clientHandler;
_logger = logger;
}
public InfoBipSmsServiceApi()
{
}
public async Task<JObject> sendOTP(int buId, string strMobile)
{
CustomCfg cfg = CustomCfg.GetCustomCfg(buId);
JObject obj = new JObject();
try
{
var reqObject = new SendOTP
{
applicationId = cfg.InfoBipAppilcationId,
messageId = cfg.InfoBipMessageId,
from = cfg.InfoBipFroms,
to = "91" + strMobile
};
var json = JsonConvert.SerializeObject(reqObject);
string apiUrl = "https://266k6.api.infobip.com/2fa/2/pin";
obj = await GetDataFromInfoBip(buId, strMobile, apiUrl, "verify", json);
}
catch (Exception ex)
{
_logger.LogError("Api:- sendOTP:- " + ex.StackTrace.ToString());
}
return obj;
}
public async Task<JObject> resendOTP(int buId, string strMobile, string strOtpPinId)
{
var errorMessage = "";
JObject obj = new JObject();
try
{
string apiUrl = "https://266k6.api.infobip.com/2fa/2/pin/" + strOtpPinId + "/resend";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiUrl);
request.Method = "POST";
request.Headers.Add("Authorization", "Basic " + userAuth);
request.Headers.Add("cache-control", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string content = reader.ReadToEnd();
string apiRequest = strOtpPinId;
string apiResponse = content;
obj = JObject.Parse(content);
await Repository.SaveResponse(buId, strMobile, "resend otp", apiRequest, apiResponse, errorMessage);
}
catch (Exception ex)
{
errorMessage = ex.Message;
_logger.LogError("BUID:- " + buId + "Api:- resendOTP:- " + ex.Message.ToString());
}
return obj;
}
public async Task<JObject> verifyOTP(int buId, string strMobile, string strOtpPinId, string otpPin)
{
JObject obj = new();
try
{
var reqObject = new OTP
{
pin = otpPin
};
var json = JsonConvert.SerializeObject(reqObject);
string apiUrl = "https://266k6.api.infobip.com/2fa/2/pin/" + strOtpPinId + "/verify";
obj = await GetDataFromInfoBip(buId, strMobile, apiUrl, "verify", json);
}
catch (Exception ex)
{
_logger.LogError("BUID:- " + buId + " Api:- verifyOTP:- " + ex.StackTrace.ToString());
}
return obj;
}
private async Task<JObject> GetDataFromInfoBip(int buId, string strMobile, string baseUrl, string otpRequestType, string data)
{
HttpClient client = ClientHandler.GetHttpClient();
Uri requestUri = new Uri(baseUrl);
client.DefaultRequestHeaders.Add("Authorization", "Basic " + userAuth);
client.DefaultRequestHeaders.Add("cache-control", "no-cache");
var response = await client.PostAsync(requestUri, new StringContent(data, UnicodeEncoding.UTF8, "application/json"));
JObject obj = JObject.Parse(await response.Content.ReadAsStringAsync());
_logger.LogError($"BuId: {buId}, MobileNumber: {strMobile}, OTP Type: {otpRequestType}, ApiRequest: {data} , ApiResponse: {response}");
return obj;
}
}
public class OTP
{
public string pin { get; set; }
}
public class SendOTP
{
public string applicationId { get; set; }
public string messageId { get; set; }
public string from { get; set; }
public string to { get; set; }
}