diff --git a/Business/BusinessLogic/TPApiServices/Factory/ApiFactoryServices.cs b/Business/BusinessLogic/TPApiServices/Factory/ApiFactoryServices.cs index cc20494..331ff49 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.AliPayRemit.Services; using TPApiServices.GuavaPayRemit.Services; using TPApiServices.SendMNRemit.Services; using TPService.GCC.Services; @@ -38,6 +39,11 @@ namespace Business.BusinessLogic.TPApiServices.Factory log.Debug("Choose Provider as guavaPay"); apiFactory = _container.Resolve(); } + else if (providerNo == GetStatic.ReadWebConfig("aliPay", "")) + { + log.Debug("Choose Provider as aliPay"); + apiFactory = _container.Resolve(); + } return apiFactory; } } diff --git a/TPServices/AliPayRemit/Model/AliPayModel.cs b/TPServices/AliPayRemit/Model/AliPayModel.cs new file mode 100644 index 0000000..f8aaba6 --- /dev/null +++ b/TPServices/AliPayRemit/Model/AliPayModel.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TPApiServices.AliPay.Model +{ + public class AliPayModel + { + } + public class AliPaySendRequest + { + public string transferRequestId { get; set; } + public BeneficiaryReceiptMethod beneficiaryReceiptMethod { get; set; } + public string instructedAmountType { get; set; } + public string bizSceneType { get; set; } + public TrahsferToAmount trahsferToAmount { get; set; } + public additionalTransferDetails additionalTransferDetails { get; set; } + public string transferReference { get; set; } + } + + public class BeneficiaryReceiptMethod + { + public WalletDetail walletDetail { get; set; } + public string paymentMethodType { get; set; } + } + public class WalletDetail + { + public string walletName { get; set; } + public string customerId { get; set; } + public CustomerName customerName { get; set; } + } + public class CustomerName + { + public string firstName { get; set; } + public string lastName { get; set; } + public string fullName { get; set; } + public string middleName { get; set; } + } + + public class TrahsferToAmount + { + public string currency { get; set; } + public string value { get; set; } + } + + public class additionalTransferDetails + { + public beneficiary beneficiary { get; set; } + public string transferPurpose { get; set; } + public string subTransferPurpose { get; set; } + public string transferFromRegion { get; set; } + public payer payer { get; set; } + public string transferToRegion { get; set; } + + } + public class beneficiary + { + public string nationality { get; set; } + public CustomerName userName { get; set; } + } + + public class payer + { + public userAddress userAddress { get; set; } + public string nationality { get; set; } + public string userPhoneNo { get; set; } + public certificate certificate { get; set; } + public CustomerName userName { get; set; } + public string userId { get; set; } + public string birthDate { get; set; } + } + + public class userAddress + { + public string zipCode { get; set; } + public string city { get; set; } + public string address2 { get; set; } + public string address1 { get; set; } + public string region { get; set; } + } + + + public class certificate + { + public string certificateNo { get; set; } + public string certificateType { get; set; } + } + + + + public class AliPayAuthResponsetModel + { + public class result + { + public string resultCode { get; set; } + public string resultMessage { get; set; } + public string resultStatus { get; set; } + } + public string transferId { get; set; } + } +} diff --git a/TPServices/AliPayRemit/Services/AliPayAPI.cs b/TPServices/AliPayRemit/Services/AliPayAPI.cs new file mode 100644 index 0000000..3d7a7d0 --- /dev/null +++ b/TPServices/AliPayRemit/Services/AliPayAPI.cs @@ -0,0 +1,226 @@ +using Common.Models.RequestResponse; +using Common.Models.Status; +using Common.Models.TxnModel; +using Common.TPService; +using Common.Utility; +using log4net; +using Newtonsoft.Json; +using Org.BouncyCastle.Crypto; +using Org.BouncyCastle.OpenSsl; +using Org.BouncyCastle.Security; +using RestSharp; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using System.Threading.Tasks; +using TPApiServices.AliPay.Model; + +namespace TPApiServices.AliPayRemit.Services +{ + public class AliPayAPI : ITPApiServices + { + private readonly ILog _log = LogManager.GetLogger(typeof(AliPayAPI)); + protected string baseUrl { get; set; } + protected string clientId { get; set; } + protected string private_key { get; set; } + protected string requestTime { get; set; } + + public AliPayAPI() + { + baseUrl = GetStatic.ReadWebConfig("aliPay_base_url", ""); + clientId = GetStatic.ReadWebConfig("aliPay_clint_Id", ""); + private_key = GetStatic.ReadWebConfig("aliPay_private_key", ""); + } + + public TPResponse GetTPResponse(T model, string MethodName) where T : class + { + switch (MethodName) + { + case "send": + return SendTransaction(model as SendTransaction); + case "status": + return GetStatus(model as GetStatus); + default: + throw new NotImplementedException(); + } + } + + private TPResponse GetStatus(GetStatus getStatus) + { + throw new NotImplementedException(); + } + + private TPResponse SendTransaction(SendTransaction model) + { + TPResponse txnResponse = new TPResponse(); + + // Map transaction details to AliPaySendRequest + AliPaySendRequest aliPaySendRequest = MapSendTxnDetails(model); + + // Generate the signature + string signature = GenerateSignature(aliPaySendRequest); + + // Create a new REST client and request + var client = new RestClient(baseUrl); + var request = new RestRequest("/transfer", Method.POST); + + // Set headers + request.AddHeader("Content-Type", "application/json"); + request.AddHeader("clientId", clientId); + request.AddHeader("requestTime", DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss+08:00")); // Adjust to match your request time format + request.AddHeader("signature", $"algorithm=RSA256, keyVersion=0, signature={signature}"); + + // Set the request body + string bodyContent = JsonConvert.SerializeObject(aliPaySendRequest, Formatting.Indented); + request.AddParameter("application/json", bodyContent, ParameterType.RequestBody); + + // Execute the request + IRestResponse response = client.Execute(request); + + // Handle the response + if (response.IsSuccessful) + { + txnResponse = JsonConvert.DeserializeObject(response.Content); + _log.Info($"Transaction successful: {response.Content}"); + } + else + { + _log.Error($"Transaction failed: {response.ErrorMessage}"); + txnResponse.Msg = response.ErrorMessage; + } + + return txnResponse; + } + + public AliPaySendRequest MapSendTxnDetails(SendTransaction model) + { + return new AliPaySendRequest + { + transferRequestId = model.Transaction.JMEControlNo, + beneficiaryReceiptMethod = new BeneficiaryReceiptMethod + { + walletDetail = new WalletDetail + { + walletName = "ALIPAY_CN", + customerId = model.Receiver.REmail, + customerName = new CustomerName + { + firstName = model.Receiver.RFirstName, + lastName = model.Receiver.RLastName, + fullName = model.Receiver.RFullName, + middleName = model.Receiver.RMiddleName + } + }, + paymentMethodType = model.Transaction.PaymentType + }, + instructedAmountType = "TRANSFER_TO", + bizSceneType = "TRANSFER", + trahsferToAmount = new TrahsferToAmount + { + currency = model.Transaction.PCurr, + value = Convert.ToString(model.Transaction.PAmt) + }, + additionalTransferDetails = new additionalTransferDetails + { + beneficiary = new beneficiary + { + nationality = model.Receiver.RNativeCountry, + userName = new CustomerName + { + firstName = model.Receiver.RFirstName, + lastName = model.Receiver.RLastName, + fullName = model.Receiver.RFullName, + middleName = model.Receiver.RMiddleName + } + }, + transferPurpose = model.Transaction.PurposeOfRemittanceName, + subTransferPurpose = "", + transferFromRegion = "GB", + payer = new payer + { + userAddress = new userAddress + { + zipCode = model.Sender.SZipCode, + city = model.Sender.SCity, + address1 = model.Sender.SAddress, + address2 = model.Sender.SCityId, + region = "GB" + }, + nationality = model.Sender.SNativeCountry, + userPhoneNo = model.Sender.SMobile, + certificate = new certificate + { + certificateNo = model.Sender.SIdNo, + certificateType = model.Sender.SIdType + }, + userName = new CustomerName + { + firstName = model.Sender.SFirstName, + lastName = model.Sender.SMiddleName, + fullName = model.Sender.SFullName, + middleName = model.Sender.SMiddleName + }, + userId = model.Sender.SEmail, + birthDate = model.Sender.SBirthDate + }, + transferToRegion = "CN" + }, + transferReference = "" + }; + } + + private string GenerateSignature(AliPaySendRequest aliPaySendRequest) + { + string textToEncrypt = aliPaySendRequest.additionalTransferDetails.payer.userName.firstName + + aliPaySendRequest.additionalTransferDetails.payer.userName.middleName + + aliPaySendRequest.additionalTransferDetails.payer.userName.lastName + + aliPaySendRequest.additionalTransferDetails.payer.userName.fullName + + aliPaySendRequest.additionalTransferDetails.payer.nationality + + aliPaySendRequest.additionalTransferDetails.payer.userAddress.city + + aliPaySendRequest.additionalTransferDetails.payer.userAddress.address1 + + aliPaySendRequest.additionalTransferDetails.payer.certificate.certificateType + + aliPaySendRequest.additionalTransferDetails.payer.certificate.certificateNo + + aliPaySendRequest.additionalTransferDetails.payer.userPhoneNo + + aliPaySendRequest.additionalTransferDetails.payer.userId + + aliPaySendRequest.additionalTransferDetails.payer.birthDate + + aliPaySendRequest.beneficiaryReceiptMethod.walletDetail.walletName + + aliPaySendRequest.beneficiaryReceiptMethod.walletDetail.customerId + + aliPaySendRequest.beneficiaryReceiptMethod.walletDetail.customerName.firstName + + aliPaySendRequest.beneficiaryReceiptMethod.walletDetail.customerName.middleName + + aliPaySendRequest.beneficiaryReceiptMethod.walletDetail.customerName.lastName + + aliPaySendRequest.additionalTransferDetails.beneficiary.nationality + + aliPaySendRequest.additionalTransferDetails.beneficiary.userName.firstName + + aliPaySendRequest.additionalTransferDetails.beneficiary.userName.middleName + + aliPaySendRequest.additionalTransferDetails.beneficiary.userName.lastName + + aliPaySendRequest.trahsferToAmount.currency + + aliPaySendRequest.trahsferToAmount.value + + aliPaySendRequest.additionalTransferDetails.transferPurpose + + aliPaySendRequest.additionalTransferDetails.transferFromRegion + + aliPaySendRequest.additionalTransferDetails.transferToRegion + + aliPaySendRequest.transferReference; + + // Convert the text to encrypt into bytes + byte[] dataBytes = Encoding.UTF8.GetBytes(textToEncrypt); + + // Load private key and sign the data + byte[] signatureBytes; + using (TextReader reader = new StringReader(private_key)) + { + PemReader pemReader = new PemReader(reader); + AsymmetricCipherKeyPair keyPair = (AsymmetricCipherKeyPair)pemReader.ReadObject(); + ISigner signer = SignerUtilities.GetSigner("SHA256withRSA"); + signer.Init(true, keyPair.Private); + signer.BlockUpdate(dataBytes, 0, dataBytes.Length); + signatureBytes = signer.GenerateSignature(); + } + + // Convert the signature to Base64 + string signature = Convert.ToBase64String(signatureBytes); + + return signature; + } + } +} \ No newline at end of file diff --git a/TPServices/App.config b/TPServices/App.config index cd94312..63f2fd6 100644 --- a/TPServices/App.config +++ b/TPServices/App.config @@ -14,9 +14,7 @@ - + diff --git a/TPServices/TPServices.csproj b/TPServices/TPServices.csproj index 4941879..d8281c4 100644 --- a/TPServices/TPServices.csproj +++ b/TPServices/TPServices.csproj @@ -37,6 +37,9 @@ + + ..\packages\Portable.BouncyCastle.1.8.10\lib\net40\BouncyCastle.Crypto.dll + ..\packages\log4net.2.0.8\lib\net45-full\log4net.dll @@ -59,6 +62,8 @@ + + True True @@ -294,5 +299,6 @@ Reference.cs + \ No newline at end of file diff --git a/TPServices/packages.config b/TPServices/packages.config index f245211..9d6959d 100644 --- a/TPServices/packages.config +++ b/TPServices/packages.config @@ -2,5 +2,6 @@ + \ No newline at end of file diff --git a/ThirdPartyAPIs/Web.config b/ThirdPartyAPIs/Web.config index 3d2e66e..f93c263 100644 --- a/ThirdPartyAPIs/Web.config +++ b/ThirdPartyAPIs/Web.config @@ -64,6 +64,7 @@ + @@ -84,7 +85,12 @@ - + + + + + +