public async Task <string> Request(string apiURL, string cmd, string rsaPublicKey, string rsaPrivateKey, string rsaPublicKeyVTP, DataRequestPayment dataRequestPayment, SoapDataRequestPayment soapDataRequestPayment) { try { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"" + apiURL + ""); webRequest.ContentType = "text/xml;charset=UTF-8;action=\"SOAP:Action\""; webRequest.Method = "POST"; webRequest.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true; var sign = string.Empty; var data = GetSoapDataRequestPayment(dataRequestPayment, soapDataRequestPayment, rsaPrivateKey, rsaPublicKey, out sign); XmlDocument soapEnvelopeXml = CreateSoapEnvelope(cmd, data, sign); var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); logger.Info("ViettelPay Request Payment: Data: " + data); using (Stream stream = webRequest.GetRequestStream()) { soapEnvelopeXml.Save(stream); } using (WebResponse response = webRequest.GetResponse()) { using (StreamReader rd = new StreamReader(response.GetResponseStream())) { string soapResult = await rd.ReadToEndAsync(); var xDoc = XDocument.Parse(soapResult); var resultReturn = xDoc.Descendants("return").Single().Value; dynamic resultReturnJson = JObject.Parse(resultReturn); var soapData = resultReturnJson.data.ToString(); var soapSign = resultReturnJson.signature.ToString(); var rsa = new RSAHelper(RSAType.RSA, Encoding.UTF8, "", rsaPublicKeyVTP); var soapDataReplace = soapData.Replace(" \"", "\"").Replace(" \"", "\"").Replace("\r", "").Replace("\n", ""); var verifySign = rsa.Verify(soapDataReplace, soapSign); dynamic soapDataJson = JObject.Parse(soapData); var dataZip = soapDataJson.data.ToString(); var dataUnzip = StringHelper.UnzipBase64(dataZip); if (!verifySign) { logger.Error("ViettelPay Request Payment Error: Code: " + soapDataJson.errorCode?.ToString() + ", Message: " + soapDataJson.errorMsg?.ToString() + ", Verify Sign: False"); return(string.Empty); } if (soapDataJson.errorCode.ToString() != "00") { logger.Error("ViettelPay Request Payment Error: Code: " + soapDataJson.errorCode?.ToString() + ", Message: " + soapDataJson.errorMsg?.ToString()); } else { logger.Info("ViettelPay Request Payment: Status: Success"); } return(soapDataJson.errorCode.ToString()); } } } catch (Exception ex) { throw new ApplicationException("ViettelPay Request Error: " + ex.Message); } }
public async Task <IActionResult> Withdrawal(WithdrawalViewModel model, string withdrawalAll) { if (!_userService.IsSignedIn(User)) { return(RedirectToAction(nameof(HomeController.Index), "Home")); } var config = await _globalConfigurationService.GetValueConfig(Constants.Configuration.ProgramLocked); if (config.Contains("true")) { return(View("~/Views/Lock.cshtml")); } if (!ModelState.IsValid) { return(View(model)); } if (model.WithdrawalAmount < _configuration.GetValue <decimal>("InvestmentValidation:MinWithdrawalAmount")) { ViewData["Message"] = string.Format(ValidationMessages.WithdrawalInvalid, _configuration.GetValue <decimal>("InvestmentValidation:MinWithdrawalAmount")); ModelState.AddModelError("WithdrawalAmount", string.Format(ValidationMessages.WithdrawalInvalid, _configuration.GetValue <decimal>("InvestmentValidation:MinWithdrawalAmount"))); return(View(model)); } if (!await GoogleRecaptchaHelper.IsReCaptchaPassedAsync(Request.Form["g-recaptcha-response"], _configuration.GetValue <string>("GoogleReCaptcha:SecretKey"))) { ViewData["CaptchaInvalidMessage"] = ValidationMessages.CaptchaInvalidMessage; return(View(model)); } var currentUser = await _userService.GetCurrentUser(); if (WithdrawalProcessingUsers.ContainsKey(currentUser.UserName) && WithdrawalProcessingUsers[currentUser.UserName]) { ViewBag.Error = ValidationMessages.WithdrawalError; return(View()); } try { //Lock user of withdrawal if (WithdrawalProcessingUsers.ContainsKey(currentUser.UserName)) { WithdrawalProcessingUsers[currentUser.UserName] = true; } else { WithdrawalProcessingUsers.Add(currentUser.UserName, true); } var amount = (decimal)model.WithdrawalAmount; if (!string.IsNullOrWhiteSpace(withdrawalAll)) { if (model.WithdrawalType == Common.WithdrawalType.Quick) { amount = currentUser.CurrentAccountAmount * 90 / 100; } else { amount = currentUser.CurrentAccountAmount; } } var withdrawalFee = await _withdrawFeeService.GetFeeAmount(amount, true); var checkWithdrawal = _configuration.GetValue <bool>("ViewClient:Withdrawal") != null?_configuration.GetValue <bool>("ViewClient:Withdrawal") : true; if (model.WithdrawalType == Common.WithdrawalType.Quick && checkWithdrawal) { var quickWithdrawalFee = await _withdrawFeeService.GetQuickWithdrawalFee(); var dateQuickWithdrawal = _configuration.GetValue <int>("Fee:DateQuickWithdrawal"); withdrawalFee += amount * quickWithdrawalFee * dateQuickWithdrawal; } var personalIncomeFee = _configuration.GetValue <decimal>("Fee:PersonalIncomeFee") / 100; withdrawalFee += amount * personalIncomeFee; withdrawalFee = Decimal.Round(withdrawalFee, 0); if ((decimal)model.WithdrawalAmount + withdrawalFee > Decimal.Round(currentUser.CurrentAccountAmount, 0)) { ViewData["Message"] = ValidationMessages.WithdrawalInvalid2; ModelState.AddModelError("WithdrawalAmount", ValidationMessages.WithdrawalInvalid2); return(View(model)); } if (model.WithdrawalType == Common.WithdrawalType.Quick && model.WithdrawalAmount > Decimal.Round(currentUser.CurrentAccountAmount * 90 / 100, 0) && checkWithdrawal) { ViewData["Message"] = ValidationMessages.WithdrawalInvalid3; ModelState.AddModelError("WithdrawalAmount", ValidationMessages.WithdrawalInvalid3); return(View(model)); } if (currentUser.WithdrawProcessing && (DateTime.Now - currentUser.WithdrawProcessingDate).Minutes <= 5) { ViewBag.Error = ValidationMessages.WithdrawalError; return(View()); } TempData["Message"] = Model.Resources.Common.WithdrawalResult; if (model.WithdrawalType == Common.WithdrawalType.Quick && checkWithdrawal) { if (!_configuration.GetValue <bool>("PaymentSecurity:DisableVTP")) { var viettelPayApi = _configuration.GetValue <bool>("RequestPaymentLink:IsLive") ? _configuration.GetValue <string>("RequestPaymentLink:Live") : _configuration.GetValue <string>("RequestPaymentLink:Test"); var cmd = _configuration.GetValue <string>("RequestPaymentParam:cmdRequest"); var rsaPublicKey = _configuration.GetValue <string>("RSAKey:public"); var rsaPrivateKey = _configuration.GetValue <string>("RSAKey:private"); var rsaPublicKeyVTP = _configuration.GetValue <string>("RSAKey:VTPpublic"); var rsa = new RSAHelper(RSAType.RSA, Encoding.UTF8, rsaPrivateKey, rsaPublicKeyVTP); var passwordEncrypt = rsa.Encrypt(_configuration.GetValue <string>("RequestPaymentParam:password")); var dataRequestPayment = new DataRequestPayment() { msisdn = "84" + currentUser.PhoneNumber.Remove(0, 1), customerName = currentUser.FullName, transId = TempData["OrderId"].ToString(), amount = ((decimal)model.WithdrawalAmount).ToString("0"), smsContent = _configuration.GetValue <string>("RequestPaymentParam:smsContent"), note = "Rut tien tu Savenow" }; var soapDataRequestPayment = new SoapDataRequestPayment() { username = _configuration.GetValue <string>("RequestPaymentParam:username"), password = passwordEncrypt, serviceCode = _configuration.GetValue <string>("RequestPaymentParam:serviceCode"), orderId = TempData["OrderId"].ToString(), totalTrans = "1", totalAmount = ((decimal)model.WithdrawalAmount).ToString("0"), transContent = _configuration.GetValue <string>("RequestPaymentParam:smsContent") }; var code = await _viettelPay.Request(viettelPayApi, cmd, rsaPublicKey, rsaPrivateKey, rsaPublicKeyVTP, dataRequestPayment, soapDataRequestPayment); if (!string.IsNullOrWhiteSpace(code) && code == "10") { currentUser.WithdrawProcessing = false; await _userService.UpdateUser(currentUser); ViewBag.Error = ValidationMessages.VTPInvalidAccount; return(View()); } else if (code != "00") { currentUser.WithdrawProcessing = false; await _userService.UpdateUser(currentUser); ViewBag.Error = ValidationMessages.VTPError; return(View()); } } TempData["Message"] = Model.Resources.Common.WithdrawalQuickResult; } await _withdrawFeeService.GetFeeAmount(amount); if (!string.IsNullOrWhiteSpace(withdrawalAll)) { await _fundTransactionHistoryService.Withdrawal(currentUser.UserName, (decimal)model.WithdrawalAmount, withdrawalFee, model.WithdrawalType, true, objectId : TempData["OrderId"]?.ToString()); } else { await _fundTransactionHistoryService.Withdrawal(currentUser.UserName, (decimal)model.WithdrawalAmount, withdrawalFee, model.WithdrawalType, objectId : TempData["OrderId"]?.ToString()); } if (TempData["OrderId"] != null) { var order = await _orderRequestService.GetOrder(orderId : Int32.Parse(TempData["OrderId"].ToString())); order.Amount = (decimal)model.WithdrawalAmount; await _orderRequestService.UpdateOrder(order); } } finally { WithdrawalProcessingUsers.Remove(currentUser.UserName); } return(RedirectToAction(nameof(AccountController.WithdrawalResult), "Account")); }
private string GetSoapDataRequestPayment(DataRequestPayment dataRequestPayment, SoapDataRequestPayment soapDataRequestPayment, string privateKey, string publicKey, out string sign) { var list = new List <DataRequestPayment>(); list.Add(dataRequestPayment); var dataRequestPaymentJson = JsonConvert.SerializeObject(list); var dataRequestPaymentJsonGzip = StringHelper.GzipBase64(dataRequestPaymentJson); soapDataRequestPayment.data = dataRequestPaymentJsonGzip; var soapDataRequestPaymentJson = JsonConvert.SerializeObject(soapDataRequestPayment); var rsa = new RSAHelper(RSAType.RSA, Encoding.UTF8, privateKey, publicKey); sign = rsa.Sign(soapDataRequestPaymentJson); return(soapDataRequestPaymentJson); }
public async Task <IActionResult> UpdateDealCustom(int objectID, string userID, decimal transactionAmount) { var user = await _userService.GetUserById(userID); var objectName = user.FullName; if (!_configuration.GetValue <bool>("PaymentSecurity:DisableVTP")) { var newOrder = new OrderRequestModel() { PhoneNumber = "84" + user.PhoneNumber.Remove(0, 1), FullName = user.FullName, Amount = transactionAmount }; var order = await _orderRequestService.SaveOrder(newOrder); var viettelPayApi = _configuration.GetValue <bool>("RequestPaymentLink:IsLive") ? _configuration.GetValue <string>("RequestPaymentLink:Live") : _configuration.GetValue <string>("RequestPaymentLink:Test"); var cmd = _configuration.GetValue <string>("RequestPaymentParam:cmdRequest"); var cmdCheckAccount = _configuration.GetValue <string>("RequestPaymentParam:cmdCheckAccount"); var rsaPublicKey = _configuration.GetValue <string>("RSAKey:public"); var rsaPrivateKey = _configuration.GetValue <string>("RSAKey:private"); var rsaPublicKeyVTP = _configuration.GetValue <string>("RSAKey:VTPpublic"); var rsa = new RSAHelper(RSAType.RSA, Encoding.UTF8, rsaPrivateKey, rsaPublicKeyVTP); var passwordEncrypt = rsa.Encrypt(_configuration.GetValue <string>("RequestPaymentParam:password")); var dataCheckAccount = new DataCheckAccount() { msisdn = "84" + user.PhoneNumber.Remove(0, 1), customerName = user.FullName }; var soapDataCheckAccount = new SoapDataCheckAccount() { username = _configuration.GetValue <string>("RequestPaymentParam:username"), password = passwordEncrypt, serviceCode = _configuration.GetValue <string>("RequestPaymentParam:serviceCode"), orderId = order.Id.ToString() }; var codeCheckAccount = await _viettelPay.CheckAccount(viettelPayApi, cmdCheckAccount, rsaPublicKey, rsaPrivateKey, rsaPublicKeyVTP, dataCheckAccount, soapDataCheckAccount); if (!string.IsNullOrWhiteSpace(codeCheckAccount) && codeCheckAccount == "10") { return(Json(new { success = false, message = ValidationMessages.VTPInvalidAccount2 })); } else if (codeCheckAccount != "00") { return(Json(new { success = false, message = ValidationMessages.VTPError })); } var dataRequestPayment = new DataRequestPayment() { msisdn = "84" + user.PhoneNumber.Remove(0, 1), customerName = user.FullName, transId = order.Id.ToString(), amount = transactionAmount.ToString("0"), smsContent = _configuration.GetValue <string>("RequestPaymentParam:smsContent"), note = "Rut tien tu Savenow" }; var soapDataRequestPayment = new SoapDataRequestPayment() { username = _configuration.GetValue <string>("RequestPaymentParam:username"), password = passwordEncrypt, serviceCode = _configuration.GetValue <string>("RequestPaymentParam:serviceCode"), orderId = order.Id.ToString(), totalTrans = "1", totalAmount = transactionAmount.ToString("0"), transContent = _configuration.GetValue <string>("RequestPaymentParam:smsContent") }; var code = await _viettelPay.Request(viettelPayApi, cmd, rsaPublicKey, rsaPrivateKey, rsaPublicKeyVTP, dataRequestPayment, soapDataRequestPayment); if (!string.IsNullOrWhiteSpace(code) && code == "10") { return(Json(new { success = false, message = ValidationMessages.VTPInvalidAccount })); } else if (code != "00") { return(Json(new { success = false, message = ValidationMessages.VTPError })); } } await _transactionHistoryService.UpdateStatusTransactionHistory(objectID, TransactionStatus.Success); await _taskCompletedService.SaveTaskCompleted(new TaskCompletedModel() { ObjectID = objectID, ObjectName = objectName, TaskType = TaskTypeAccountant.DealCustomer, TransactionAmount = transactionAmount }); return(Json(new { success = true })); }