diff --git a/Business/BusinessLogic/TPApiServices/Factory/ApiFactoryServices.cs b/Business/BusinessLogic/TPApiServices/Factory/ApiFactoryServices.cs index 6df37e7..cc20494 100644 --- a/Business/BusinessLogic/TPApiServices/Factory/ApiFactoryServices.cs +++ b/Business/BusinessLogic/TPApiServices/Factory/ApiFactoryServices.cs @@ -2,6 +2,7 @@ using Common.Utility; using GMENepal.GMENepalAPIService; using log4net; +using TPApiServices.GuavaPayRemit.Services; using TPApiServices.SendMNRemit.Services; using TPService.GCC.Services; using Unity; @@ -32,6 +33,11 @@ namespace Business.BusinessLogic.TPApiServices.Factory log.Debug("Choose Provider as SENDMN "); apiFactory = _container.Resolve(); } + else if (providerNo == GetStatic.ReadWebConfig("guavaPay", "")) + { + log.Debug("Choose Provider as guavaPay"); + apiFactory = _container.Resolve(); + } return apiFactory; } } diff --git a/TPServices/GuavaPayRemit/Model/GuavaPayModel.cs b/TPServices/GuavaPayRemit/Model/GuavaPayModel.cs new file mode 100644 index 0000000..c5a9aa7 --- /dev/null +++ b/TPServices/GuavaPayRemit/Model/GuavaPayModel.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TPApiServices.GuavaPayRemit.Model +{ + public class GuavaPayModel + { + public string clientId { get; set; } + public string secretKey { get; set; } + + } + + public class AuthResponseModel + { + public string accessToken { get; set; } + public string refreshToken { get; set; } + public string Message { get; set; } + public string Code { get; set; } + public DateTime ExpiryTime { get; set; } + } + public class GuavaSendTransaction + { + public string Country { get; set; } + public string AgentName { get; set; } + public string Amount { get; set; } + public string Currency { get; set; } + public string AgentTransactionId { get; set; } + public string CallBackUrl { get; set; } + public Sender senderClient { get; set; } + public Receiver receiverClient { get; set; } + } + public class Sender + { + public string FirstName { get; set; } + public string MiddleName { get; set; } + public string LastName { get; set; } + public string Phone { get; set; } + public string Address { get; set; } + public string BirthDate { get; set; } + public string zipCode { get; set; } + public DocumentInfo documentInfo { get; set; } + + } + public class DocumentInfo + { + public string DocumentType { get; set; } + public string DocumentNumber { get; set; } + public string RDocumentNumber { get; set; } + public string DocumentIssuedCountry { get; set; } + public string DocumentIssuedDate { get; set; } + public string DocumentEndDate { get; set; } + } + + public class Receiver + { + public string FirstName { get; set; } + public string MiddleName { get; set; } + public string LastName { get; set; } + public string Phone { get; set; } + public DocumentInfo documentInfo { get; set; } + public AccountInfo accountInfo { get; set; } + + } + public class AccountInfo + { + public string AccountNumber { get; set; } + public string BankName { get; set; } + public string BankBranchCode { get; set; } + public string BankCode { get; set; } + } + + public class SendTransactionResponse + { + public string Status { get; set; } + public string StatusDescription { get; set; } + public string RRN { get; set; } + public string Fee { get; set; } + public string Rate { get; set; } + public string StatusCode { get; set; } + public string Msg { get; set; } + public string Message { get; set; } + public List Errors { get; internal set; } + } + + public class StatusResponseModel + { + public string statusDescription; + + public string referenceCode { get; set; } + + public string Status { get; set; } + //public Detail Detail { get; set; } + + } +} diff --git a/TPServices/GuavaPayRemit/Services/GuavaPayAPI.cs b/TPServices/GuavaPayRemit/Services/GuavaPayAPI.cs new file mode 100644 index 0000000..7f14d2a --- /dev/null +++ b/TPServices/GuavaPayRemit/Services/GuavaPayAPI.cs @@ -0,0 +1,234 @@ +using Common.Models.RequestResponse; +using Common.Models.Status; +using Common.Models.TxnModel; +using Common.TPService; +using Common.Utility; +using log4net; +using Newtonsoft.Json; +using RestSharp; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using TPApiServices.GuavaPayRemit.Model; + +namespace TPApiServices.GuavaPayRemit.Services +{ + public class GuavaPayAPI : ITPApiServices + { + private readonly ILog _log = LogManager.GetLogger(typeof(GuavaPayAPI)); + protected string baseUrl { get; set; } + protected string clientId { get; set; } + protected string secretKey { get; set; } + protected string agentCode { get; set; } + + public GuavaPayAPI() + { + baseUrl = GetStatic.ReadWebConfig("guavaPay_base_url", ""); + clientId = GetStatic.ReadWebConfig("guavaPay_clint_Id", ""); + secretKey = GetStatic.ReadWebConfig("guavaPay_secret_key", ""); + //agentCode = GetStatic.ReadWebConfig("guavaPay_agent_code", ""); + } + public TPResponse GetTPResponse(T model, string MethodName) where T : class + { + switch (MethodName) + { + //case "exRate": + // return GetExRate(model as SendExRateRequestModel); + case "send": + return SendTransaction(model as SendTransaction); + + //case "verify": + // return AccountVerifySendMN(model as AccountValidate); + + case "status": + return GetStatus(model as GetStatus); + + //case "staticData": + // return GetStaticData(model as StaticData); + + default: + throw new NotImplementedException(); + } + } + private AuthResponseModel GetToken(string user) + { + AuthResponseModel tokenResponse = new AuthResponseModel(); + try + { + var requestBody = new GuavaPayModel + { + clientId = clientId, + secretKey = secretKey//"7cf3288cc67d4a2b965a5d41a4005fc1123fa64081" + }; + var client = new RestClient(baseUrl); + var request = new RestRequest("/v2/auth", Method.POST); + request.AddHeader("Content-Type", "application/json"); + request.AddJsonBody(requestBody); + _log.Info($"GUAVAPAYAPI|GETTOKEN|REQUEST URL: {client.BaseUrl}{request.Resource}"); + _log.Info($"GUAVAPAYAPI|GETTOKEN|REQUEST BODY: {JsonConvert.SerializeObject(requestBody)}"); + var response = client.Execute(request); + if (response.StatusCode == HttpStatusCode.OK) + { + tokenResponse = JsonConvert.DeserializeObject(response.Content); + tokenResponse.ExpiryTime = DateTime.UtcNow.AddMinutes(90); + _log.Info($"GUAVAPAYAPI|GETTOKEN|RESPONSE : {JsonConvert.SerializeObject(tokenResponse)}"); + } + else + { + _log.Error($"GUAVAPAYAPI|GETTOKEN|ERROR RESPONSE : {response.Content}"); + } + } + catch (Exception ex) + { + tokenResponse = new AuthResponseModel + { + Code = "999" + }; + _log.Error($"GUAVAPAYAPI|GETTOKEN|EXCEPTION OCCURRED : {ex}"); + } + + return tokenResponse; + } + private TPResponse GetStatus(GetStatus model) + { + TPResponse statusResponse = new TPResponse(); + var tokenResponse = GetToken(model.RequestBy); + var client = new RestClient(baseUrl); + var request = new RestRequest($"/v2/transactions/{model.PartnerPinNo}/status", Method.GET); + request.AddHeader("Content-Type", "application/json"); + request.AddHeader("Authorization", "Bearer " + tokenResponse.accessToken); + request.RequestFormat = DataFormat.Json; + // request.AddJsonBody(requestBody); + var getStatusResponse = client.Execute(request).Data; + //var statusres = getStatusResponse.Data; + _log.Info($"GUAVAPAYAPI|GETSTATUSSENDMN|RESPONSE : {JsonConvert.SerializeObject(getStatusResponse)}"); + statusResponse.ResponseCode = "0"; + statusResponse.Id = getStatusResponse.referenceCode; + statusResponse.Extra2 = getStatusResponse.Status; + statusResponse.Extra4 = getStatusResponse.statusDescription; + statusResponse.Data = getStatusResponse; + return statusResponse; + } + public TPResponse SendTransaction(SendTransaction model) + { + TPResponse txnResponse = new TPResponse(); + try + { + var tokenResponse = GetToken(model.RequestBy); // Method to get the token + var requestBody = MapSendTxnDetails(model); // Method to map transaction details + _log.Info($"GUAVAPAYAPI|SENDTRANSACTION|REQUEST BODY : {JsonConvert.SerializeObject(requestBody)}"); + var client = new RestClient(baseUrl); + if (model?.Transaction == null || string.IsNullOrEmpty(model.Transaction.PaymentType)) + { + throw new ArgumentNullException("Transaction or PaymentType is null or empty."); + } + _log.Info($"GUAVAPAYAPI|SENDTRANSACTION|PAYMENT TYPE : {model.Transaction.PaymentType}"); + string endpoint = model.Transaction.PaymentType == "CASH" ? "/v2/payments/cash" : "/v2/payments/account"; + var request = new RestRequest(endpoint, Method.POST); + // request = new RestRequest(endpoint, Method.POST); + request.AddHeader("Content-Type", "application/json"); + request.AddHeader("Authorization", "Bearer " + tokenResponse.accessToken); + request.AddJsonBody(requestBody); + request.RequestFormat = DataFormat.Json; + var txnResponseData = client.Execute(request); + var sendTxnResponse = txnResponseData.Data; + _log.Info($"GUAVAPAYAPI|SENDTRANSACTION|REQUEST URL: {client.BaseUrl}{request.Resource}"); + _log.Info($"GUAVAPAYAPI|SENDTRANSACTION|RESPONSE : {txnResponseData.Content}"); + + if (sendTxnResponse != null) + { + _log.Info($"GUAVAPAYAPI|SENDTRANSACTION|RESPONSE DATA : {JsonConvert.SerializeObject(sendTxnResponse)}"); + + if (string.IsNullOrEmpty(sendTxnResponse.RRN) || string.IsNullOrEmpty(sendTxnResponse.Rate) || + string.IsNullOrEmpty(sendTxnResponse.Status) || string.IsNullOrEmpty(sendTxnResponse.Fee) || + string.IsNullOrEmpty(sendTxnResponse.StatusDescription)) + { + txnResponse.ResponseCode = "1"; + txnResponse.Msg = "One or more critical response fields are null."; + _log.Warn($"GUAVAPAYAPI|SENDTRANSACTION|MISSING FIELDS: " + + $"RRN: {sendTxnResponse.RRN}, " + + $"Rate: {sendTxnResponse.Rate}, " + + $"Status: {sendTxnResponse.Status}, " + + $"Fee: {sendTxnResponse.Fee}, " + + $"StatusDescription: {sendTxnResponse.StatusDescription}"); + } + else + { + txnResponse.ResponseCode = "0"; + txnResponse.Id = sendTxnResponse.RRN; + txnResponse.Extra = sendTxnResponse.Rate; + txnResponse.Extra2 = sendTxnResponse.Status; + txnResponse.Extra3 = sendTxnResponse.Fee; + txnResponse.Extra4 = sendTxnResponse.StatusDescription; + txnResponse.Data = sendTxnResponse; + } + } + else + { + txnResponse.ResponseCode = "1"; + txnResponse.Msg = "Response data is null"; + _log.Warn($"GUAVAPAYAPI|SENDTRANSACTION|RESPONSE DATA IS NULL"); + } + } + catch (Exception ex) + { + txnResponse.ResponseCode = "999"; + txnResponse.Msg = ex.ToString(); + _log.Error($"GUAVAPAYAPI|SENDTRANSACTION|EXCEPTION OCCURRED|RESPONSE : {JsonConvert.SerializeObject(txnResponse)}"); + } + return txnResponse; + } + public GuavaSendTransaction MapSendTxnDetails(SendTransaction model) + { + return new GuavaSendTransaction + { + senderClient = new Sender + { + FirstName = model.Sender.SFirstName, + MiddleName = model.Sender.SMiddleName, + LastName = model.Sender.SLastName1, + Address = model.Sender.SCityId, + BirthDate = model.Sender.SBirthDate, + Phone = model.Sender.SMobile, + zipCode = model.Sender.SZipCode, + documentInfo = new DocumentInfo + { + DocumentType = model.Sender.SIdType, + DocumentNumber = model.Sender.SIdNo, + DocumentIssuedCountry = model.Sender.SCountryName, + DocumentIssuedDate = model.Sender.SIdIssueDate, + DocumentEndDate = model.Sender.SIdExpiryDate, + } + }, + receiverClient = new Receiver + { + FirstName = model.Receiver.RFirstName, + MiddleName = model.Receiver.RMiddleName, + LastName = model.Receiver.RLastName, + Phone = model.Receiver.RMobile, + documentInfo = new DocumentInfo + { + DocumentNumber = model.Receiver.RIdNo + }, + accountInfo = new AccountInfo + { + AccountNumber = model.Receiver.UnitaryBankAccountNo,// "1312431424141414", + BankName = model.Agent.PBankName, //"MeezanBank" + BankBranchCode = model.Agent.PAgentName, + BankCode = model.Agent.PBranchId + } + }, + Country = model.Receiver.RNativeCountry, + AgentName = model.Agent.PBankId, //"MeezanBank", + Amount = Convert.ToString(model.Transaction.PAmt), + AgentTransactionId = model.Transaction.JMEControlNo, + Currency = model.Transaction.PCurr, + CallBackUrl = "" + + }; + } + } +} diff --git a/TPServices/TPServices.csproj b/TPServices/TPServices.csproj index ae6762d..4941879 100644 --- a/TPServices/TPServices.csproj +++ b/TPServices/TPServices.csproj @@ -71,8 +71,8 @@ - - + + diff --git a/ThirdPartyAPIs/Web.config b/ThirdPartyAPIs/Web.config index e0954bb..3d2e66e 100644 --- a/ThirdPartyAPIs/Web.config +++ b/ThirdPartyAPIs/Web.config @@ -17,8 +17,8 @@ - - + + @@ -62,29 +62,39 @@ - + + + + - - + - --> + - - + + + + + + - - - - + + + + + + +