Esempio n. 1
0
        public async Task CheckOrderRequestPendingTask()
        {
            try
            {
                var config = await _globalConfigurationService.GetValueConfig(Constants.Configuration.ProgramLocked);

                if (config.Contains("true"))
                {
                    return;
                }
                var listorder = await _orderRequestService.GetAllOrderRequestByStatus(OrderRequestStatus.Pending);

                if (listorder != null && listorder.Count > 0)
                {
                    var viettelPayApi   = _configuration.GetValue <bool>("RequestPaymentLink:IsLive") ? _configuration.GetValue <string>("RequestPaymentLink:Live") : _configuration.GetValue <string>("RequestPaymentLink:Test");
                    var cmd             = _configuration.GetValue <string>("RequestPaymentParam:cmdCheckOrderRequest");
                    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, "", rsaPublicKeyVTP);
                    var passwordEncrypt = rsa.Encrypt(_configuration.GetValue <string>("RequestPaymentParam:password"));
                    var logger          = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
                    logger.Info("Check Order Request Job : Start ");
                    foreach (var orderRequest in listorder)
                    {
                        if (orderRequest.Amount > 0)
                        {
                            logger.Info("Check Order Request Job : orderRequestId = " + orderRequest.Id);

                            SoapDataCheckOrderRequest request = new SoapDataCheckOrderRequest()
                            {
                                username    = _configuration.GetValue <string>("RequestPaymentParam:username"),
                                password    = passwordEncrypt,
                                serviceCode = _configuration.GetValue <string>("RequestPaymentParam:serviceCode"),
                                orderId     = orderRequest.Id.ToString()
                            };
                            var response = await _viettelPay.CheckOrderRequest(viettelPayApi, cmd, rsaPublicKey, rsaPrivateKey, rsaPublicKeyVTP, request);

                            if (response != null)
                            {
                                logger.Info("Check Order Request Job : orderRequestId = " + orderRequest.Id + " batchErrorCode : " + response);
                                if (response == "DISB_SUCCESS")
                                {
                                    orderRequest.Status = OrderRequestStatus.Success;
                                    await _orderRequestService.UpdateOrder(orderRequest);
                                }
                                else if (response == "DISB_FAILED" || response == "DISB_TIMEOUT" || response == "CANCEL_DISB")
                                {
                                    orderRequest.Status = OrderRequestStatus.Failure;
                                    await _orderRequestService.UpdateOrder(orderRequest);

                                    await _fundTransactionHistoryService.WithdrawRollback(orderRequest.Amount, orderRequest.PhoneNumber);
                                }
                            }
                            else
                            {
                                logger.Info("Check Order Request Job : orderRequestId = " + orderRequest.Id + " ErrorCode != 0 ");
                                orderRequest.Status = OrderRequestStatus.Failure;
                                await _orderRequestService.UpdateOrder(orderRequest);

                                await _fundTransactionHistoryService.WithdrawRollback(orderRequest.Amount, orderRequest.PhoneNumber);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException("Check Order Request Error: " + e.Message);
            }
        }
Esempio n. 2
0
        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"));
        }