166 lines
5.5 KiB
C#
166 lines
5.5 KiB
C#
|
|
using Microsoft.Extensions.Options;
|
|||
|
|
using System.Net.Http;
|
|||
|
|
using System.Text.Json;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using System;
|
|||
|
|
using System.IO;
|
|||
|
|
using Ease.Core.Model;
|
|||
|
|
using Ease.Core.Utility;
|
|||
|
|
|
|||
|
|
namespace HRM.BO
|
|||
|
|
{
|
|||
|
|
public class SmsService : ISmsService
|
|||
|
|
{
|
|||
|
|
private readonly SmsSettings _smsSettings;
|
|||
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|||
|
|
|
|||
|
|
public SmsService(IHttpClientFactory httpClientFactory, IOptions<SmsSettings> options)
|
|||
|
|
{
|
|||
|
|
_smsSettings = options.Value;
|
|||
|
|
_httpClientFactory = httpClientFactory;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void WriteToFile(string text)
|
|||
|
|
{
|
|||
|
|
string pathDate = DateTime.Today.ToString("dd-MM-yyyy");
|
|||
|
|
string path = "C:\\Log\\SMS log " + pathDate + ".txt";
|
|||
|
|
|
|||
|
|
using (StreamWriter writer = new StreamWriter(path, true))
|
|||
|
|
{
|
|||
|
|
writer.WriteLine($"{DateTime.Now:dd/MM/yyyy hh:mm:ss tt} - {text}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public async Task SendLoginOtp(string mobileNumber, string otpBody)
|
|||
|
|
{
|
|||
|
|
WriteToFile($"Sms started 0");
|
|||
|
|
var referenceId = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
|
|||
|
|
WriteToFile($"Sms started 1");
|
|||
|
|
|
|||
|
|
var body = new
|
|||
|
|
{
|
|||
|
|
accessInfo = new
|
|||
|
|
{
|
|||
|
|
accessKey = _smsSettings.AccessKey,
|
|||
|
|
endUserId = mobileNumber,
|
|||
|
|
accessChannel = "MTSMS",
|
|||
|
|
referenceCode = referenceId,
|
|||
|
|
serviceIdentifier = _smsSettings.ServiceIdentifier,
|
|||
|
|
user = _smsSettings.User,
|
|||
|
|
password = _smsSettings.Password
|
|||
|
|
},
|
|||
|
|
smsInfo = new
|
|||
|
|
{
|
|||
|
|
msgTransactionId = referenceId,
|
|||
|
|
language = "EN",
|
|||
|
|
senderId = _smsSettings.SenderId,
|
|||
|
|
message = otpBody,
|
|||
|
|
msgType = "Text",
|
|||
|
|
validity = "1",
|
|||
|
|
deliveryReport = "1"
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
WriteToFile($"Sms started 2");
|
|||
|
|
|
|||
|
|
var handler = new HttpClientHandler
|
|||
|
|
{
|
|||
|
|
ServerCertificateCustomValidationCallback =
|
|||
|
|
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
//Log SSL validation status
|
|||
|
|
WriteToFile($"SSL Validation Callback Set: {handler.ServerCertificateCustomValidationCallback != null}");
|
|||
|
|
WriteToFile($"SSL Validation Method: {handler.ServerCertificateCustomValidationCallback?.Method.Name}");
|
|||
|
|
|
|||
|
|
var client = _httpClientFactory.CreateClient("SmsClient");
|
|||
|
|
|
|||
|
|
var json = JsonSerializer.Serialize(body);
|
|||
|
|
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|||
|
|
|
|||
|
|
WriteToFile("----- SMS REQUEST BODY (JSON) START -----");
|
|||
|
|
WriteToFile(json);
|
|||
|
|
WriteToFile("----- SMS REQUEST BODY (JSON) END -----");
|
|||
|
|
WriteToFile($"SMS Target URL: {_smsSettings.Url}");
|
|||
|
|
WriteToFile($"Recipient Mobile: {mobileNumber}");
|
|||
|
|
WriteToFile("HTTPS POST request is being sent to SMS server...");
|
|||
|
|
|
|||
|
|
var response = await client.PostAsync(_smsSettings.Url, content);
|
|||
|
|
|
|||
|
|
WriteToFile("HTTP response successfully received from SMS server.");
|
|||
|
|
var responseBody = await response.Content.ReadAsStringAsync();
|
|||
|
|
|
|||
|
|
WriteToFile($"HTTP Status Code: {(int)response.StatusCode}");
|
|||
|
|
WriteToFile($"Response Body: {responseBody}");
|
|||
|
|
WriteToFile("SMS REQUEST COMPLETED");
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
WriteToFile("SMS REQUEST FAILED");
|
|||
|
|
WriteToFile($"Exception Message: {ex.Message}");
|
|||
|
|
if (ex.InnerException != null)
|
|||
|
|
WriteToFile($"Inner Exception: {ex.InnerException.Message}");
|
|||
|
|
//return false;
|
|||
|
|
|
|||
|
|
throw new ServiceException(ex.Message, ex);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public class SmsSettings
|
|||
|
|
{
|
|||
|
|
private string _url;
|
|||
|
|
private string _accessKey;
|
|||
|
|
private string _user;
|
|||
|
|
private string _password;
|
|||
|
|
|
|||
|
|
public string Url
|
|||
|
|
{
|
|||
|
|
get => _url;
|
|||
|
|
set => _url = !string.IsNullOrEmpty(value)
|
|||
|
|
? Ease.Core.Utility.Global.CipherFunctions.Decrypt("CeLiMiTeD.AdMIn", value)
|
|||
|
|
: value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public string AccessKey
|
|||
|
|
{
|
|||
|
|
get => _accessKey;
|
|||
|
|
set => _accessKey = !string.IsNullOrEmpty(value)
|
|||
|
|
? Ease.Core.Utility.Global.CipherFunctions.Decrypt("CeLiMiTeD.AdMIn", value)
|
|||
|
|
: value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public string User
|
|||
|
|
{
|
|||
|
|
get => _user;
|
|||
|
|
set => _user = !string.IsNullOrEmpty(value)
|
|||
|
|
? Ease.Core.Utility.Global.CipherFunctions.Decrypt("CeLiMiTeD.AdMIn", value)
|
|||
|
|
: value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public string Password
|
|||
|
|
{
|
|||
|
|
get => _password;
|
|||
|
|
set => _password = !string.IsNullOrEmpty(value)
|
|||
|
|
? Ease.Core.Utility.Global.CipherFunctions.Decrypt("CeLiMiTeD.AdMIn", value)
|
|||
|
|
: value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public string ServiceIdentifier { get; set; }
|
|||
|
|
public string SenderId { get; set; }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public interface ISmsService
|
|||
|
|
{
|
|||
|
|
Task SendLoginOtp(string mobileNumber, string otp);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|