public ResultModel <string> ProcessPayment(ProcessPaymentViewModel model)
        {
            var resultModel        = new ResultModel <string>();
            var paymentStatusModel = new PaymentStateViewModel
            {
                PaymentStateEnum = Models.Enum.PaymentStateEnum.Pending,
            };

            model.PaymentStateViewModel = paymentStatusModel;


            try
            {
                resultModel = SelectPaymentProcessor(model);
                var response = (ProcessPayment)model;
                response.PaymentState        = (PaymentState)model.PaymentStateViewModel;
                response.PaymentState.Status = response.PaymentState.PaymentStateEnum.GetDescription();

                if (!resultModel.ErrorMessages.Any())
                {
                    _paymentStateRepo.Insert(response.PaymentState);
                    _processPaymentRepo.Insert(response);
                }
                _unitOfWork.SaveChanges();
            }
            catch (Exception ex)
            {
                resultModel.AddError(ex.Message);
                return(resultModel);
            }

            return(resultModel);
        }
Exemplo n.º 2
0
        public ActionResult Edit(ProcessPaymentViewModel model)
        {
            var urlRefer = Request["UrlReferrer"];

            if (ModelState.IsValid)
            {
                if (Request["Submit"] == "Save")
                {
                    var ProcessPayment = ProcessPaymentRepository.GetProcessPaymentById(model.Id);
                    AutoMapper.Mapper.Map(model, ProcessPayment);
                    ProcessPayment.ModifiedUserId = WebSecurity.CurrentUserId;
                    ProcessPayment.ModifiedDate   = DateTime.Now;
                    ProcessPaymentRepository.UpdateProcessPayment(ProcessPayment);

                    TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.UpdateSuccess;
                    if (Request["IsPopup"] == "true")
                    {
                        ViewBag.closePopup = "close and append to page parent";
                        ViewBag.urlRefer   = urlRefer;
                        //  model.Id = InfoPartyA.Id;
                        return(View(model));
                    }
                    return(Redirect(urlRefer));
                }

                return(View(model));
            }

            return(View(model));

            //if (Request.UrlReferrer != null)
            //    return Redirect(Request.UrlReferrer.AbsoluteUri);
            //return RedirectToAction("Index");
        }
Exemplo n.º 3
0
        public UserPaymentsViewModel GetUserPayments(int UserID)
        {
            var payments = _userService.GetUserPaymentDetails(UserID);
            //sort by newest date
            var sortedPayments = payments.OrderByDescending(x => x.Account.Payment.PaymentDate).Select(d => d.Account.Payment);

            var upvm = new UserPaymentsViewModel();
            List <ProcessPaymentViewModel> lstPayments = new List <ProcessPaymentViewModel>();

            upvm.AccountBalance = payments.Select(x => x.Account.Balance).FirstOrDefault();

            foreach (var p in sortedPayments)
            {
                ProcessPaymentViewModel pvm = new ProcessPaymentViewModel();


                pvm.PaymentDate   = p.PaymentDate.Date;
                pvm.Amount        = p.Amount;
                pvm.PaymentStatus = p.PaymentStatus;
                if (p.PaymentStatus == "Closed")
                {
                    pvm.Reason = p.Reason;
                }



                lstPayments.Add(pvm);
            }


            upvm.Payments = lstPayments;


            return(upvm);
        }
Exemplo n.º 4
0
        public ActionResult Create(ProcessPaymentViewModel model)
        {
            var urlRefer = Request["UrlReferrer"];

            if (ModelState.IsValid)
            {
                var ProcessPayment = new ProcessPayment();
                AutoMapper.Mapper.Map(model, ProcessPayment);
                ProcessPayment.IsDeleted      = false;
                ProcessPayment.CreatedUserId  = WebSecurity.CurrentUserId;
                ProcessPayment.ModifiedUserId = WebSecurity.CurrentUserId;
                ProcessPayment.AssignedUserId = WebSecurity.CurrentUserId;
                ProcessPayment.CreatedDate    = DateTime.Now;
                ProcessPayment.ModifiedDate   = DateTime.Now;
                ProcessPayment.Status         = "Chưa thanh toán";
                ProcessPaymentRepository.InsertProcessPayment(ProcessPayment);

                TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.InsertSuccess;
                if (Request["IsPopup"] == "true")
                {
                    ViewBag.closePopup = "close and append to page parent";
                    ViewBag.urlRefer   = urlRefer;
                    //  model.Id = InfoPartyA.Id;
                    return(View(model));
                }
                return(Redirect(urlRefer));
            }
            return(View(model));
        }
Exemplo n.º 5
0
        public ActionResult AddProcessPayment(int moneyPayment, DateTime?dayPayment, int orderNo)
        {
            ProcessPaymentViewModel model = new ProcessPaymentViewModel();

            model.DayPayment   = dayPayment;
            model.MoneyPayment = moneyPayment;
            model.OrderNo      = orderNo + 1;
            model.Name         = "Đợt " + model.OrderNo;
            return(View(model));
        }
        public ResultModel <string> PaymentGreaterThan500Pounds(ProcessPaymentViewModel model)
        {
            var resultModel = new ResultModel <string>();

            resultModel.ServiceAvailable = true;

            model.PaymentStateViewModel.PaymentStateEnum = Models.Enum.PaymentStateEnum.Processed;
            _unitOfWork.SaveChanges();
            return(resultModel);
        }
        public ResultModel <string> PaymentFor21To500Pounds(ProcessPaymentViewModel model)
        {
            var resultModel = new ResultModel <string>();

            //For test purpose Service Availability is set to false mocking real payment service availability
            resultModel.ServiceAvailable = false;
            resultModel.AddError("Service unavailable");
            model.PaymentStateViewModel.PaymentStateEnum = Models.Enum.PaymentStateEnum.Failed;
            _unitOfWork.SaveChanges();

            return(resultModel);
        }
Exemplo n.º 8
0
 private bool ValidateCreditCard(ProcessPaymentViewModel paymentViewModel)
 {
     try
     {
         //TO DO: check cc is valid or not
         //TO DO: if cc is not valid using API than set isCardValid = false
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
        private ResultModel <string> SelectPaymentProcessor(ProcessPaymentViewModel model)
        {
            var resultModel = new ResultModel <string>();

            if (model.Amount < 20)
            {
                resultModel = PaymentForLessThan20Pounds(model);
                if (resultModel.ErrorMessages.Any())
                {
                    return(resultModel);
                }
                return(resultModel);
            }
            if (model.Amount >= 21 && model.Amount <= 500)
            {
                resultModel = PaymentFor21To500Pounds(model);
                if (!resultModel.ServiceAvailable)
                {
                    for (int i = 0; i < 1; i++)
                    {
                        resultModel = PaymentForLessThan20Pounds(model);
                        if (!resultModel.ErrorMessages.Any())
                        {
                            return(resultModel);
                        }
                    }
                }
                return(resultModel);
            }
            if (model.Amount > 500)
            {
                resultModel = PaymentGreaterThan500Pounds(model);
                if (resultModel.ErrorMessages.Any())
                {
                    for (int i = 0; i < 3; i++)
                    {
                        resultModel = PaymentGreaterThan500Pounds(model);
                        if (!resultModel.ErrorMessages.Any())
                        {
                            return(resultModel);
                        }
                    }
                }
                return(resultModel);
            }

            resultModel.AddError("Could not identify payment category");

            return(resultModel);
        }
Exemplo n.º 10
0
        public IActionResult MakePayment([FromBody] ProcessPaymentViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(StatusCode(400));
            }
            var result = _paymentService.ProcessPayment(model);

            if (result.ErrorMessages.Any())
            {
                return(StatusCode(500));
            }
            return(Ok());
        }
Exemplo n.º 11
0
        public async void Task_Add_ValidObjectPassed_ReturnsCreatedResponse()
        {
            // Arrange
            ProcessPaymentViewModel testItem = new ProcessPaymentViewModel()
            {
                CreditCardNumber = "0001000000000000",
                CardHolderName   = "Red",
                ExpiryDate       = "01/22",
                CVV    = "000",
                Amount = 20
            };
            // Act
            var createdResponse = await _controller.ProcessPayment(testItem);

            // Assert
            Assert.IsType <ActionResult <ProcessPaymentViewModel> >(createdResponse);
        }
Exemplo n.º 12
0
        /// <summary>
        /// make payment at payment gateway api
        /// </summary>
        /// <param name="processPaymentViewModel"></param>
        /// <returns></returns>
        private async Task <string> MakePayment(ProcessPaymentViewModel paymentViewModel)
        {
            //TO DO: make payment using payment gateway API
            switch (paymentViewModel.PaymentGatewayId)
            {
            case 1:     //CheapPaymentGateway
                break;

            case 2:    //ExpensivePaymentGateway
                break;

            case 3:    //PremiumPaymentGateway
                break;

            default:
                break;
            }

            return(Extentions.GetDescription(EnumClass.PaymentResponse.Processed));
        }
Exemplo n.º 13
0
        public ViewResult Create(int?Id)
        {
            var model = new ProcessPaymentViewModel();

            model.ContractId = Id;
            var q           = ProcessPaymentRepository.GetAllProcessPayment().Where(x => x.ContractId == Id).OrderByDescending(x => x.OrderNo);
            var priceCondos = contractRepository.GetvwLogContractbyId(Id.Value);
            int dem         = 0;

            if (q != null && q.Count() > 0)
            {
                dem             = q.FirstOrDefault().OrderNo.Value;
                ViewBag.Payment = q.Sum(x => x.MoneyPayment.Value);
            }
            ViewBag.Price    = priceCondos.Price;
            model.DayPayment = DateTime.Now;
            model.OrderNo    = dem + 1;
            model.Name       = "Đợt " + model.OrderNo;
            return(View(model));
        }
Exemplo n.º 14
0
        public ActionResult Detail(int?Id)
        {
            var ProcessPayment = ProcessPaymentRepository.GetProcessPaymentById(Id.Value);

            if (ProcessPayment != null && ProcessPayment.IsDeleted != true)
            {
                var model = new ProcessPaymentViewModel();
                AutoMapper.Mapper.Map(ProcessPayment, model);

                if (model.CreatedUserId != Erp.BackOffice.Helpers.Common.CurrentUser.Id && Erp.BackOffice.Helpers.Common.CurrentUser.UserTypeId != 1)
                {
                    TempData["FailedMessage"] = "NotOwner";
                    return(RedirectToAction("Index"));
                }

                return(View(model));
            }
            if (Request.UrlReferrer != null)
            {
                return(Redirect(Request.UrlReferrer.AbsoluteUri));
            }
            return(RedirectToAction("Index"));
        }
Exemplo n.º 15
0
        private async Task <UsersCreditCardDetailModel> SaveUserCCDetail(ProcessPaymentViewModel paymentViewModel)
        {
            UsersCreditCardDetailModel usersCreditCardDetailModel = new UsersCreditCardDetailModel();

            try
            {
                usersCreditCardDetailModel = _unitOfWork.UsersCreditCardDetailModelRepository.FindAllBy(c => c.CreditCardNumber == paymentViewModel.CreditCardNumber).FirstOrDefault();
                if (usersCreditCardDetailModel == null)
                {
                    usersCreditCardDetailModel = new UsersCreditCardDetailModel();
                    usersCreditCardDetailModel.CardHolderName   = paymentViewModel.CardHolderName;
                    usersCreditCardDetailModel.CreditCardNumber = paymentViewModel.CreditCardNumber;
                    usersCreditCardDetailModel.ExpiryDate       = paymentViewModel.ExpiryDate;
                    usersCreditCardDetailModel.UserId           = paymentViewModel.UserId;

                    await _unitOfWork.UsersCreditCardDetailModelRepository.InsertAsync(usersCreditCardDetailModel);
                }
            }
            catch (Exception)
            {
            }
            return(usersCreditCardDetailModel);
        }
        public async Task <ActionResult <ProcessPaymentViewModel> > ProcessPayment(ProcessPaymentViewModel paymentViewModel)
        {
            var result = await _processPaymentService.ProcessPayment(paymentViewModel);

            return(result);
        }
Exemplo n.º 17
0
        public async Task <ProcessPaymentViewModel> ProcessPayment(ProcessPaymentViewModel paymentViewModel)
        {
            paymentViewModel.messageViewModel        = new MessageViewModel();
            paymentViewModel.messageViewModel.Status = true;

            if (paymentViewModel.Amount < 0) //The request is invalid: 400 bad request
            {
                paymentViewModel.messageViewModel.Status  = false;
                paymentViewModel.messageViewModel.Message = Extentions.GetDescription(EnumClass.PaymentResponse.InvalidRequest);
                return(paymentViewModel);
            }

            try
            {
                #region local variables

                string                     paymentGateway             = string.Empty;
                int                        retryPayment               = 0;
                string                     paymentGatewayResponse     = Extentions.GetDescription(EnumClass.PaymentResponse.Processed);
                PaymentDetailModel         paymentDetailModel         = new PaymentDetailModel();
                PaymentDetailModel         _paymentDetailModel        = new PaymentDetailModel();
                UsersCreditCardDetailModel usersCreditCardDetailModel = new UsersCreditCardDetailModel();
                bool                       isCardValid = true;


                #endregion local variables

                #region choose payment gateway

                if (paymentViewModel.Amount <= 20) //If the amount to be paid is less than £20, use ICheapPaymentGateway.
                {
                    paymentViewModel.PaymentGatewayName = Extentions.GetDescription(EnumClass.PaymentGateways.CheapPaymentGateway);
                    paymentViewModel.PaymentGatewayId   = (int)EnumClass.PaymentGateways.CheapPaymentGateway;
                }
                else if (paymentViewModel.Amount > 20 && paymentViewModel.Amount <= 500) //If the amount to be paid is £21 - 500, use IExpensivePaymentGateway
                {
                    paymentViewModel.PaymentGatewayName = Extentions.GetDescription(EnumClass.PaymentGateways.ExpensivePaymentGateway);
                    paymentViewModel.PaymentGatewayId   = (int)EnumClass.PaymentGateways.ExpensivePaymentGateway;

                    //use IExpensivePaymentGateway if available. Otherwise, retry only once with ICheapPaymentGateway.
                    if (!ValidatePaymentGateway(paymentViewModel.PaymentGatewayName))
                    {
                        paymentViewModel.PaymentGatewayName = Extentions.GetDescription(EnumClass.PaymentGateways.CheapPaymentGateway);

                        if (!ValidatePaymentGateway(paymentViewModel.PaymentGatewayName)) //Any error: 500 internal server error
                        {
                            paymentViewModel.messageViewModel.Status  = false;
                            paymentViewModel.messageViewModel.Message = Extentions.GetDescription(EnumClass.PaymentResponse.Failed);
                            return(paymentViewModel);
                        }
                    }
                }
                else //If the amount is > £500, try only PremiumPaymentService
                {
                    paymentViewModel.PaymentGatewayName = Extentions.GetDescription(EnumClass.PaymentGateways.PremiumPaymentGateway);
                    paymentViewModel.PaymentGatewayId   = (int)EnumClass.PaymentGateways.PremiumPaymentGateway;
                }

                #endregion choose payment gateway

                #region Validate & Save new CC detail

                //validate card
                isCardValid = ValidateCreditCard(paymentViewModel);

                //return here if CC detail is not valid
                if (!isCardValid) //400 bad request : Invaild Card Detail
                {
                    paymentViewModel.messageViewModel.Status  = isCardValid;
                    paymentViewModel.messageViewModel.Message = Extentions.GetDescription(EnumClass.PaymentResponse.InvaildCardDetail);
                    return(paymentViewModel);
                }

                //save card detail into database
                usersCreditCardDetailModel = await SaveUserCCDetail(paymentViewModel);

                if (usersCreditCardDetailModel == null)
                {
                    paymentViewModel.messageViewModel.Status  = false;
                    paymentViewModel.messageViewModel.Message = Extentions.GetDescription(EnumClass.PaymentResponse.Failed);
                    return(paymentViewModel);
                }

                #endregion Save/Update CC detail

                #region make payment

                //get payment gateway response
                paymentGatewayResponse = await MakePayment(paymentViewModel);

                #endregion make payment

                #region Save payment detail

                paymentDetailModel.Amount         = paymentViewModel.Amount;
                paymentDetailModel.CCUsed         = usersCreditCardDetailModel;
                paymentDetailModel.PaymentDate    = DateTime.UtcNow;
                paymentDetailModel.CartId         = paymentViewModel.CartId;
                paymentDetailModel.PaymentGateway = paymentViewModel.PaymentGatewayName;
                paymentDetailModel.PaymentStatus  = paymentGatewayResponse;

                await _unitOfWork.PaymentDetailModelRepository.InsertAsync(paymentDetailModel);

                _paymentDetailModel = _mapper.Map <PaymentDetailModel>(paymentDetailModel);

                #endregion Save payment detail

                #region check payment status and update entity as completed

                //If the amount is > £500, try only PremiumPaymentService and retry up to 3 times in case payment does not get processed
                while (paymentViewModel.Amount > 500 &&
                       paymentGatewayResponse != Extentions.GetDescription(EnumClass.PaymentResponse.Processed))
                {
                    if (ValidatePaymentGateway(paymentViewModel.PaymentGatewayName))
                    {
                        paymentGatewayResponse = await MakePayment(paymentViewModel);
                    }

                    if (retryPayment == 3 || paymentGatewayResponse == Extentions.GetDescription(EnumClass.PaymentResponse.Processed))
                    {
                        break;
                    }

                    retryPayment = retryPayment + 1;
                }

                //Store/update the payment and payment state entities created previously once the processing is completed
                if (paymentDetailModel.PaymentStatus == Extentions.GetDescription(EnumClass.PaymentResponse.Processed))
                {
                    //Use of eager loading
                    var paymentDetail = _unitOfWork.PaymentDetailModelRepository.FindAllByQuery(c => c.Id == paymentDetailModel.Id)
                                        .Include(c => c.CCUsed)
                                        .FirstOrDefault();

                    paymentDetail.PaymentStatus = Extentions.GetDescription(EnumClass.PaymentResponse.Completed);

                    await _unitOfWork.PaymentDetailModelRepository.UpdateAsync(paymentDetail);
                }

                #endregion check payment status

                paymentViewModel.messageViewModel.Id      = paymentDetailModel.Id;
                paymentViewModel.messageViewModel.Message = paymentGatewayResponse; //200 Ok
            }
            catch (Exception ex)                                                    //500 internal server error
            {
                paymentViewModel.messageViewModel.Status  = false;
                paymentViewModel.messageViewModel.Message = Extentions.GetDescription(EnumClass.PaymentResponse.Failed);
            }
            return(paymentViewModel);
        }
Exemplo n.º 18
0
 public async Task <ProcessPaymentViewModel> ProcessPayment(ProcessPaymentViewModel paymentViewModel)
 {
     _processPaymentViewModels.Add(paymentViewModel);
     return(paymentViewModel);
 }