Dinesh
2 months ago
5 changed files with 329 additions and 12 deletions
-
6Business/BusinessLogic/TPApiServices/Factory/ApiFactoryServices.cs
-
96TPServices/GuavaPayRemit/Model/GuavaPayModel.cs
-
203TPServices/GuavaPayRemit/Services/GuavaPayAPI.cs
-
2TPServices/TPServices.csproj
-
34ThirdPartyAPIs/Web.config
@ -0,0 +1,96 @@ |
|||||
|
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 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<string> 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; }
|
||||
|
|
||||
|
} |
||||
|
} |
@ -0,0 +1,203 @@ |
|||||
|
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>(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<AuthResponseModel>(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<StatusResponseModel>(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<SendTransactionResponse>(request); |
||||
|
var sendTxnResponse = txnResponseData.Data; |
||||
|
_log.Info($"GUAVAPAYAPI|SENDTRANSACTION|RESPONSE : {txnResponseData.Content}"); |
||||
|
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; |
||||
|
} |
||||
|
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 |
||||
|
{ |
||||
|
RDocumentNumber = model.Receiver.RIdNo |
||||
|
}, |
||||
|
accountInfo = new AccountInfo |
||||
|
{ |
||||
|
AccountNumber = model.Receiver.UnitaryBankAccountNo,// "1312431424141414",
|
||||
|
BankName = model.Agent.PBankName //"MeezanBank"
|
||||
|
} |
||||
|
}, |
||||
|
Country = model.Receiver.RNativeCountry, |
||||
|
AgentName = model.Agent.PBankId, //"MeezanBank",
|
||||
|
Amount = Convert.ToString(model.Transaction.PAmt), |
||||
|
AgentTransactionId = model.Transaction.JMEControlNo, |
||||
|
Currency = model.Transaction.PCurr, |
||||
|
CallBackUrl = "" |
||||
|
|
||||
|
}; |
||||
|
} |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue