296 lines
11 KiB
C#
296 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Json;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json;
|
|
using CompanyManagment.App.Contracts.AuthorizedPerson;
|
|
using _0_Framework.Application;
|
|
using _0_Framework.Application.UID;
|
|
using Company.Application.Contracts.AuthorizedBankDetails;
|
|
|
|
namespace CompanyManagment.EFCore.Services;
|
|
|
|
public class UidService : IUidService
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
private readonly IAuthorizedPersonApplication _authorizedPersonApplication;
|
|
private readonly IAuthorizedBankDetailsApplication _authorizedBankDetailsApplication;
|
|
private const string BaseUrl = "https://json-api.uid.ir/api/";
|
|
|
|
public const string BusinessToken = "5e03dd4e-999d-466f-92d8-7c0b1f66a8e9";
|
|
public const string BusinessId = "98ed67ca-d441-4978-a748-e8bebce010eb";
|
|
|
|
|
|
public UidService(IAuthorizedPersonApplication authorizedPersonApplication,
|
|
IAuthorizedBankDetailsApplication authorizedBankDetailsApplication)
|
|
{
|
|
_httpClient = new HttpClient()
|
|
{
|
|
BaseAddress = new Uri(BaseUrl),
|
|
Timeout = new TimeSpan(0, 0, 20)
|
|
};
|
|
_authorizedPersonApplication = authorizedPersonApplication;
|
|
_authorizedBankDetailsApplication = authorizedBankDetailsApplication;
|
|
}
|
|
|
|
public async Task<PersonalInfoResponse> GetPersonalInfo(string nationalCode, string birthDate)
|
|
{
|
|
// First check if person exists in database
|
|
var existingPerson = _authorizedPersonApplication.GetByNationalCode(nationalCode);
|
|
if (existingPerson != null)
|
|
{
|
|
if (birthDate != existingPerson.BirthDate)
|
|
{
|
|
return new PersonalInfoResponse(new UidBasicInformation(),
|
|
new IdentificationInformation(default, default, default, default, default),
|
|
new RegistrationStatus(),
|
|
new ResponseContext(new UidStatus(13, "تاریخ تولد وارد با کد ملی تطابق ندارد")));
|
|
}
|
|
|
|
// Return data from database instead of calling API
|
|
return CreatePersonalInfoResponseFromDatabase(existingPerson);
|
|
}
|
|
|
|
// If not found in database, call UID API
|
|
var request = new PersonalInfoRequest
|
|
{
|
|
BirthDate = birthDate,
|
|
NationalId = nationalCode,
|
|
RequestContext = new UidRequestContext()
|
|
};
|
|
var json = JsonConvert.SerializeObject(request);
|
|
var contentType = new StringContent(json, Encoding.UTF8, "application/json");
|
|
|
|
try
|
|
{
|
|
var requestResult = await _httpClient.PostAsync("inquiry/person/v2", contentType);
|
|
|
|
if (!requestResult.IsSuccessStatusCode)
|
|
return null;
|
|
|
|
var responseResult = await requestResult.Content.ReadFromJsonAsync<PersonalInfoResponse>();
|
|
|
|
if (responseResult.BasicInformation != null)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(responseResult.BasicInformation.FirstName))
|
|
{
|
|
return null;
|
|
}
|
|
responseResult.BasicInformation.FirstName = responseResult.BasicInformation.FirstName?.ToPersian();
|
|
responseResult.BasicInformation.LastName = responseResult.BasicInformation.LastName?.ToPersian();
|
|
responseResult.BasicInformation.FatherName = responseResult.BasicInformation.FatherName?.ToPersian();
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
|
|
// ذخیره اطلاعات در جدول AuthorizedPerson
|
|
await SaveAuthorizedPersonData(responseResult, nationalCode, birthDate);
|
|
|
|
return responseResult;
|
|
}
|
|
catch
|
|
{
|
|
return new PersonalInfoResponse(new UidBasicInformation(),
|
|
new IdentificationInformation(default, default, default, default, default), new RegistrationStatus(),
|
|
new ResponseContext(new UidStatus(14, "")));
|
|
}
|
|
}
|
|
|
|
private PersonalInfoResponse CreatePersonalInfoResponseFromDatabase(AuthorizedPersonViewModel person)
|
|
{
|
|
var basicInfo = new UidBasicInformation
|
|
{
|
|
FirstName = person.FirstName,
|
|
LastName = person.LastName,
|
|
FatherName = person.FatherName,
|
|
Gender = person.Gender
|
|
};
|
|
|
|
var identificationInfo = new IdentificationInformation(
|
|
person.NationalCode,
|
|
person.BirthDate,
|
|
person.ShenasnameSeri,
|
|
person.ShenasnameSerial,
|
|
person.ShenasnamehNumber
|
|
);
|
|
|
|
var registrationStatus = new RegistrationStatus
|
|
{
|
|
DeathStatus = person.DeathStatus
|
|
};
|
|
|
|
var responseContext = new ResponseContext(new UidStatus(0, "Success - از دیتابیس"));
|
|
|
|
return new PersonalInfoResponse(basicInfo, identificationInfo, registrationStatus, responseContext);
|
|
}
|
|
|
|
private async Task SaveAuthorizedPersonData(PersonalInfoResponse response, string nationalCode, string birthDate)
|
|
{
|
|
if (response?.BasicInformation == null || response.ResponseContext?.Status?.Code != 0)
|
|
return;
|
|
|
|
var command = new CreateAuthorizedPerson
|
|
{
|
|
NationalCode = nationalCode,
|
|
FirstName = response.BasicInformation.FirstName ?? "",
|
|
LastName = response.BasicInformation.LastName ?? "",
|
|
FatherName = response.BasicInformation.FatherName ?? "",
|
|
BirthDate = birthDate,
|
|
Gender = response.BasicInformation.Gender ?? "",
|
|
DeathStatus = response.RegistrationStatus?.DeathStatus ?? "",
|
|
ShenasnameSeri = response.IdentificationInformation?.ShenasnameSeri ?? "",
|
|
ShenasnameSerial = response.IdentificationInformation?.ShenasnameSerial ?? "",
|
|
ShenasnamehNumber = response.IdentificationInformation?.ShenasnamehNumber ?? ""
|
|
};
|
|
|
|
_authorizedPersonApplication.CreateFromUidResponse(command);
|
|
}
|
|
|
|
public async Task<MatchMobileWithNationalCodeResponse> IsMachPhoneWithNationalCode(string nationalCode,
|
|
string phoneNumber)
|
|
{
|
|
var request = new PersonalInfoRequest
|
|
{
|
|
MobileNumber = phoneNumber,
|
|
NationalId = nationalCode,
|
|
RequestContext = new UidRequestContext()
|
|
};
|
|
var json = JsonConvert.SerializeObject(request);
|
|
var contentType = new StringContent(json, Encoding.UTF8, "application/json");
|
|
|
|
var requestResult = await _httpClient.PostAsync("inquiry/mobile/owner/v2", contentType);
|
|
if (!requestResult.IsSuccessStatusCode)
|
|
return null;
|
|
|
|
var responseResult = await requestResult.Content.ReadFromJsonAsync<MatchMobileWithNationalCodeResponse>();
|
|
return responseResult;
|
|
}
|
|
|
|
public async Task<IbanInquiryResponse> IbanInquiry(string iban)
|
|
{
|
|
// First check if bank details exist in database
|
|
var existingBankDetails = _authorizedBankDetailsApplication.GetByIban(iban);
|
|
if (existingBankDetails != null)
|
|
{
|
|
// Return data from database instead of calling API
|
|
return CreateIbanInquiryResponseFromDatabase(existingBankDetails);
|
|
}
|
|
|
|
// If not found in database, call UID API
|
|
var request = new
|
|
{
|
|
iban,
|
|
requestContext = new UidRequestContext()
|
|
};
|
|
var json = JsonConvert.SerializeObject(request);
|
|
var contentType = new StringContent(json, Encoding.UTF8, "application/json");
|
|
|
|
try
|
|
{
|
|
var requestResult = await _httpClient.PostAsync("inquiry/iban/v2", contentType);
|
|
requestResult.EnsureSuccessStatusCode();
|
|
var responseResult = await requestResult.Content.ReadFromJsonAsync<IbanInquiryResponse>();
|
|
|
|
if (responseResult.ResponseContext.Status.Code == 0)
|
|
{
|
|
var entity = new CreateAuthorizedBankDetails
|
|
{
|
|
IBan = iban,
|
|
AccountNumber = responseResult?.AccountBasicInformation?.AccountNumber ?? "",
|
|
BankName = responseResult?.AccountBasicInformation?.BankInformation?.BankName ?? "",
|
|
OwnersList = responseResult?.Owners.Select(x => new CreateAuthorizedBankDetailsOwner()
|
|
{
|
|
FName = x.FirstName,
|
|
LName = x.LastName,
|
|
NationalIdentifier = x.NationalIdentifier,
|
|
CustomerType = x.CustomerType
|
|
}).ToList() ?? []
|
|
};
|
|
_authorizedBankDetailsApplication.Create(entity);
|
|
}
|
|
|
|
return responseResult;
|
|
}
|
|
catch
|
|
{
|
|
return new IbanInquiryResponse
|
|
{
|
|
ResponseContext = new ResponseContext(new UidStatus(14, "خطا در دریافت اطلاعات از سرویس"))
|
|
};
|
|
}
|
|
}
|
|
|
|
private IbanInquiryResponse CreateIbanInquiryResponseFromDatabase(AuthorizedBankDetailsViewModel bankDetails)
|
|
{
|
|
var accountBasicInfo = new IbanInquiryAccountBasicInformation
|
|
{
|
|
Iban = bankDetails.IBan,
|
|
AccountNumber = bankDetails.AccountNumber,
|
|
BankInformation = new IbanInquiryBankInformation
|
|
{
|
|
BankName = bankDetails.BankName
|
|
},
|
|
AccountStatus = "Active"
|
|
};
|
|
|
|
var owners = new List<IbanInquiryOwner>();
|
|
|
|
// Add owner information if available
|
|
if (bankDetails.Owners.Any())
|
|
{
|
|
var owner = bankDetails.Owners.First();
|
|
owners.Add(new IbanInquiryOwner
|
|
{
|
|
NationalIdentifier = owner.NationalIdentifier,
|
|
FirstName = owner.FName,
|
|
LastName = owner.LName,
|
|
CustomerType = owner.CustomerType
|
|
});
|
|
}
|
|
|
|
var responseContext = new ResponseContext(new UidStatus(0, "Success - از دیتابیس"));
|
|
|
|
return new IbanInquiryResponse
|
|
{
|
|
AccountBasicInformation = accountBasicInfo,
|
|
Owners = owners,
|
|
ResponseContext = responseContext
|
|
};
|
|
}
|
|
|
|
public async Task<AccountToIbanResponse> AccountToIban(string accountNumber, UidBanks bank)
|
|
{
|
|
var request = new
|
|
{
|
|
accountNumber,
|
|
bank,
|
|
requestContext = new UidRequestContext()
|
|
};
|
|
var json = JsonConvert.SerializeObject(request);
|
|
var contentType = new StringContent(json, Encoding.UTF8, "application/json");
|
|
var requestResult = await _httpClient.PostAsync("account-to-iban", contentType);
|
|
requestResult.EnsureSuccessStatusCode();
|
|
var responseResult = await requestResult.Content.ReadFromJsonAsync<AccountToIbanResponse>();
|
|
return responseResult;
|
|
}
|
|
|
|
public async Task<CardToNumberResponse> CardToIban(string cardNumber)
|
|
{
|
|
var request = new
|
|
{
|
|
cardNumber,
|
|
requestContext = new UidRequestContext()
|
|
};
|
|
var json = JsonConvert.SerializeObject(request);
|
|
var contentType = new StringContent(json, Encoding.UTF8, "application/json");
|
|
var requestResult = await _httpClient.PostAsync("inquiry/card", contentType);
|
|
requestResult.EnsureSuccessStatusCode();
|
|
var responseResult = await requestResult.Content.ReadFromJsonAsync<CardToNumberResponse>();
|
|
return responseResult;
|
|
}
|
|
} |