using Business.BusinessLogic.Address; using Business.BusinessLogic.NotificationApiService; using Business.BusinessLogic.SMSApiService; using Business.BusinessLogic.TPApiServices.Factory; using Common.Models.Address; using Common.Models.Bank_Account; using Common.Models.DigitalSignature; using Common.Models.ExchangeRate; using Common.Models.Notification; using Common.Models.RequestResponse; using Common.Models.SendSMS; using Common.Models.SendTransactionMobile; using Common.Models.Status; using Common.Models.TxnModel; using Common.TPService; using Common.Utility; using log4net; using Newtonsoft.Json; using Repository.DAO.ThirdParty; using System; using System.Collections.Generic; using System.Data; using System.Linq; namespace Business.BusinessLogic.TPApiServices.TPServices { public class ThirdPartyServices : ITPApiServices { private readonly ILog _log = LogManager.GetLogger(typeof(ThirdPartyServices)); private readonly IThirdPartyRepo _thirdPartyRepo; private readonly IApiFactoryServices _apiFactoryServices; public ThirdPartyServices(IThirdPartyRepo thirdPartyRepo, IApiFactoryServices apiFactoryServices) { _thirdPartyRepo = thirdPartyRepo; _apiFactoryServices = apiFactoryServices; } public TPResponse GetTPResponse(T model, string MethodName) where T : class { switch (MethodName) { case "send": return SendTransaction(model as SendTransaction, MethodName); case "sendV2": return SendTransactionMobileOrOnline(model as SendTransactionMobile, MethodName); case "exRate": return GetExRate(model as ExchangeRate, MethodName); case "status": return GetTxnStatus(model as GetStatus, MethodName); case "SMSApi": return SendMessage(model as SMSRequestModel, MethodName); case "verify": return VerifyAccount(model as AccountValidate, MethodName); case "SendNotificationApi": return SendNotification(model as SendNotificationRequest, MethodName); case "JWTHelper": return JWTHelper(model as JWTRequest, MethodName); case "loqate": return AddressLookup(model as AddressRequest); default: return _apiFactoryServices.GetServices(model.GetType().GetProperty("ProviderId").GetValue(model, null).ToString()).GetTPResponse(model, MethodName); } } private TPResponse SendMessage(SMSRequestModel sMSRequestModel, string methodName) { ISMSService _sms = new SMSService(); return _sms.SMSApiMethod(sMSRequestModel); } private TPResponse GetTxnStatus(GetStatus gS, string MethodName) { TPResponse result = _apiFactoryServices.GetServices(gS.ProviderId).GetTPResponse(gS, MethodName); return result; } private TPResponse GetExRate(ExchangeRate exModel, string MethodName) { TPResponse response = null; _log.Debug("GetExRate Method is calling: "); try { if (exModel.IsExRateCalcByPartner) return _apiFactoryServices.GetServices(exModel.ProviderId).GetTPResponse(exModel, MethodName); else if (exModel.RequestedBy.ToLower().Equals("core")) return _thirdPartyRepo.GetExRateCore(exModel); else if (exModel.RequestedBy.ToLower().Equals("online") || exModel.RequestedBy.ToLower().Equals("mobile")) { return CalculateExRate(exModel); } } catch (Exception ex) { LogicalThreadContext.Properties["exception"] = ex; _log.Error("Error Occured On GetExRate Method " + JsonConvert.SerializeObject(ex.Message)); response = new TPResponse() { Id = null, ResponseCode = "1", Msg = "Error Occured On GetExRate!", Data = null }; } return response; } private TPResponse CalculateExRate(ExchangeRate exModel) { List tPResponses = new List(); DataTable dt = _thirdPartyRepo.GetAPIPartner(exModel); foreach (DataRow dr in dt.Rows) { if (dr["isRealTime"].ToString().ToLower() == "true" | dr["isRealTime"].ToString().ToLower() == "1") { exModel.PaymentType = !string.IsNullOrEmpty(exModel.PaymentType) ? exModel.PaymentType : ""; exModel.ServiceType = !string.IsNullOrEmpty(exModel.ServiceType) ? exModel.ServiceType : ""; if (exModel.PCountry.Equals("151") && (exModel.PaymentType.Equals("wallet") | exModel.ServiceType.Equals("13"))) { exModel.IsExRateCalcByPartner = false; } else if (exModel.PCountry.Equals("36")) { exModel.IsExRateCalcByPartner = false; } else { exModel.pCountryCode = dr["COUNTRYCODE"].ToString(); exModel.SCurrency = (string.IsNullOrEmpty(exModel.SCurrency) ? exModel.CollCurrency : exModel.SCurrency); exModel.IsExRateCalcByPartner = true; exModel.payerName = dr["PayerDetails"].ToString(); //exModel.PayoutPartner = dr["agentid"].ToString(); TPResponse _resp = _apiFactoryServices.GetServices(dr["agentId"].ToString()).GetTPResponse(exModel, "exRate"); ExRateResponse _exrate = (ExRateResponse)_resp.Data; exModel.tPExRate = _exrate.exRate; } } else { exModel.IsExRateCalcByPartner = false; } exModel.SCExcludedCountries = Common.Utility.GetStatic.ReadWebConfig("SCExcludedCountries", ""); var r = (TPResponse)_thirdPartyRepo.GetExRate(exModel); if (r.ResponseCode != null && r.ResponseCode.Equals("0")) { tPResponses.Add(new TPResponsev2() { Data = (ExRateResponse)r.Data, Extra = r.Extra, Extra2 = r.Extra2, ResponseCode = r.ResponseCode }); } else tPResponses.Add(new TPResponsev2() { Data = null, ResponseCode = r.ResponseCode, Msg = r.Msg }); // tPResponses.Add((); } TPResponsev2 final; if (tPResponses.Any(x => x.ResponseCode.Equals("0"))) { final = tPResponses.Where(x => x.ResponseCode.Equals("0") && double.Parse(x.Data.exRate) == tPResponses.Max(y => double.Parse(y.Data.exRate))).FirstOrDefault(); } else { final = tPResponses.FirstOrDefault(); } return new TPResponse() { Data = final.Data, ResponseCode = final.ResponseCode, Msg = final.Msg }; } private TPResponse SendTransactionMobileOrOnline(SendTransactionMobile model, string methodName) { try { TPResponse createResponse = _thirdPartyRepo.CreateTransactionFromMobileOROnline(model); /// create txn on jme system database if (createResponse.ResponseCode == "0" && createResponse.Extra2.ToLower() == "true") { TPResponse detailResponse = new TPResponse() { }; SendTransaction txnDetailsFromDb = _thirdPartyRepo.GetTranactionDetails(model.RequestedBy, createResponse.Id, model.SessionId, out detailResponse); if (detailResponse.ResponseCode == "0") { LogicalThreadContext.Properties["ControlNo"] = txnDetailsFromDb.Transaction.JMEControlNo; TPResponse partnerResponse = SendTxnToPartner(txnDetailsFromDb, "send"); if (partnerResponse.ResponseCode == "0") { _thirdPartyRepo.UpdateTPTxns(partnerResponse.Extra, partnerResponse.Id, "", txnDetailsFromDb.Transaction.JMEControlNo, model.RequestedBy); return createResponse; } else { LogicalThreadContext.Properties["ControlNo"] = txnDetailsFromDb.Transaction.JMEControlNo; LogicalThreadContext.Properties["user_name"] = txnDetailsFromDb.TranId; _log.Info("SendTxnToPartner response : " + JsonConvert.SerializeObject(partnerResponse)); _thirdPartyRepo.RevertTPTxns(txnDetailsFromDb.Transaction.JMEControlNo, model.RequestedBy); return new TPResponse { ResponseCode = "1", Msg = partnerResponse.Msg ?? "Unable to complete transaction, Partner Error - Contact Support team.", Id = null }; } } else return detailResponse; } return createResponse; } catch (Exception ex) { LogicalThreadContext.Properties["exception"] = ex; _log.Error("OnlineOrMobileSendTransaction | Exception occured: " + ex.ToString()); return new TPResponse() { Id = null, ResponseCode = "1", Msg = "Error Occured On SendTransactionMobileOrOnline !", Data = null }; } } private TPResponse SendTransaction(SendTransaction model, string MethodName) { TPResponse response = new TPResponse(); try { if (!model.isTxnAlreadyCreated) { response = _thirdPartyRepo.SendTransaction(model); //if (response.ResponseCode == "0") //{ // model = _thirdPartyRepo.GetTxnDetailsByTempTranId(response.Id, out response); //} } else { response.ResponseCode = "0"; response.Extra = Convert.ToString(model.TranId); response.Id = model.Transaction.JMEControlNo; } if (response.ResponseCode == "0") { model.TranId = Convert.ToInt32(response.Extra); model.Transaction.JMEControlNo = response.Id; if (model.IsRealtime) { response = SendTxnToPartner(model, MethodName); } } else { _log.DebugFormat("Error creating transaction in JME system with Response : " + JsonConvert.SerializeObject(response)); } } catch (Exception ex) { LogicalThreadContext.Properties["exception"] = ex; _log.Error("Error Occured On TransactionSend Method " + JsonConvert.SerializeObject(ex.Message)); response = new TPResponse() { Id = null, ResponseCode = "1", Msg = "Error Occured On Transaction Send!", Data = null }; } return response; } private TPResponse SendTxnToPartner(SendTransaction model, string MethodName) { TPResponse response = _apiFactoryServices.GetServices(model.ProviderId).GetTPResponse(model, MethodName); if (response.ResponseCode == "0") { model.Transaction.TpRefNo = response.Extra; model.Transaction.TpTranId = response.Id; _log.Info("Transaction send to API Partner Success with Response : " + JsonConvert.SerializeObject(response)); return response; //if (model.IsApproveTxn) //{ // TPResponse txnApproveResponse = _thirdPartyRepo.ApproveTransaction(model); // if (txnApproveResponse.ResponseCode == "0") // { // _log.Info("Transaction Approve Response : " + JsonConvert.SerializeObject(txnApproveResponse)); // return response; // } // else // { // _log.Error("Transaction Approve Response : " + JsonConvert.SerializeObject(txnApproveResponse)); // return txnApproveResponse; // } //} //else //{ // return response; //} } else if (response.ResponseCode == "1") { _log.Info("Transaction send to API Partner Failed with Response : " + JsonConvert.SerializeObject(response)); return response; //if (!string.IsNullOrEmpty(model.RequestedBy) && !string.IsNullOrWhiteSpace(model.RequestedBy) && model.RequestedBy.ToLower().Equals("mobile")) //{ // TPResponse txnRejectResponse = _thirdPartyRepo.RejectTransaction(model); // _log.Info("Transaction Reject Response : " + JsonConvert.SerializeObject(txnRejectResponse)); // return response; //} //else //{ // return response; //} } else if (response.ResponseCode == "104") { _log.Info("Model Validation Failed : " + JsonConvert.SerializeObject(response)); return response; } return response; } private TPResponse VerifyAccount(AccountValidate av, string methodName) { TPResponse result = _apiFactoryServices.GetServices(av.ProviderId).GetTPResponse(av, methodName); return result; } private TPResponse SendNotification(SendNotificationRequest requestModel, string methodName) { INotificationService notificationService = new NotificationService(); return notificationService.NotificationApiMethod(requestModel); } private TPResponse AddressLookup(AddressRequest requestModel) { ILocateBusiness service = new LocateBusiness(); return service.QueryAddress(requestModel.TownName); } private TPResponse JWTHelper(JWTRequest requestModel, string methodName) { TPResponse result = new TPResponse(); //TokenRequest tokenRequest = new TokenRequest() { Payload = requestModel.RequestString }; ApiHelper api = new ApiHelper(); if (requestModel.Type == Common.Models.Enums.JWTType.ENCRYPT) result = api.ThirdPartyApi(requestModel, "brac/GetToken", out result); else result = api.ThirdPartyApi(requestModel, "brac/GetToken", out result); return result; } } }