Пример #1
0
        public async Task <string> ProcessPayment(PaymentRequest paymentRequest)
        {
            try
            {
                if (paymentRequest.Amount <= 20)
                {
                    string response = await _cheapPayment.ProcessPayment(paymentRequest);

                    return(response);
                }

                if (paymentRequest.Amount >= 20 && paymentRequest.Amount <= 500)
                {
                    if (await _expensiveRepo.CheckIfPaymentGetwayExist(paymentRequest))
                    {
                        string response = await _expensiveRepo.ProcessPayment(paymentRequest);
                    }
                    else
                    {
                        string response = await _cheapPayment.ProcessPayment(paymentRequest);

                        return(response);
                    }
                }

                bool result = false;

                if (paymentRequest.Amount >= 500) //  PremiumPaymentService
                {
                    result = await _premiumRepo.ProcessPayment(paymentRequest);

                    if (!result)
                    {
                        for (int i = 1; i < 3; i++)
                        {
                            result = await _premiumRepo.ProcessPayment(paymentRequest);

                            if (result)
                            {
                                break;
                            }
                        }
                    }
                }
                if (result)
                {
                    return("Success");
                }
                else
                {
                    return("Faild");
                }
            }
            catch (Exception)
            {
                return("Exception");
            }
        }
Пример #2
0
        public string GetPaymentProcessingStatus(ProcessPaymentRequest processPaymentRequest, CacheDetails cacheDetails)
        {
            string result = "";

            if (processPaymentRequest.Amount < 20)
            {
                result = _cheapPaymentGateway.ProcessPayment(cacheDetails.ExpensiveGatewayAvailability, cacheDetails.ProcessCount);
                return(result);
            }
            else if (processPaymentRequest.Amount > 20 && processPaymentRequest.Amount < 500)
            {
                if (cacheDetails.ExpensiveGatewayAvailability) // if expensive payment gateway is available use it else switch to cheap gateway
                {
                    result = _expensivePaymentGateway.ProcessPayment(cacheDetails.ExpensiveGatewayAvailability, cacheDetails.ProcessCount);
                    return(result);
                }
                else
                {
                    result = _cheapPaymentGateway.ProcessPayment(cacheDetails.ExpensiveGatewayAvailability, cacheDetails.ProcessCount);
                    return(result);
                }
            }
            int i = 0;

            while (i < Constants.PremiumCount && result != Constants.Processed)   //retry three times if transaction not successful
            {
                result = _premiumPaymentService.ProcessPayment(cacheDetails.ExpensiveGatewayAvailability, cacheDetails.ProcessCount);
            }
            return(result);
        }
Пример #3
0
        public IActionResult ProcessPayment(PaymentDto paymentDto)
        {
            int count = 0;

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (paymentDto.Amount <= 20)
            {
                _cheapPaymentGateway.ProcessPayment(_mapper.Map <PaymentSo>(paymentDto));
            }
            else if (paymentDto.Amount > 20 && paymentDto.Amount < 501)
            {
                if (!_expensivePaymentGateway.isAvailable)
                {
                    do
                    {
                        _cheapPaymentGateway.ProcessPayment(_mapper.Map <PaymentSo>(paymentDto));
                        count++;
                    } while (count < 1);
                }
                else
                {
                    _expensivePaymentGateway.ProcessPayment(_mapper.Map <PaymentSo>(paymentDto));
                }
            }
            else if (paymentDto.Amount > 500)
            {
                do
                {
                    bool res = _premiumPaymentService.ProcessPayment(_mapper.Map <PaymentSo>(paymentDto));
                    if (!res)
                    {
                        count++;
                    }
                    else
                    {
                        break;
                    }
                } while (count < 3);
            }
            return(Ok());
        }
Пример #4
0
        public bool ProcessPayment(PaymentRequest model)
        {
            bool paymentStatus = false;

            if (model.Amount >= 0 && model.Amount <= 20)
            {
                paymentStatus = _cheapPaymentManager.ProcessPayment(model);
            }

            if (model.Amount >= 21 && model.Amount <= 500)
            {
                if (IsIExpensivePaymentGatewayAvailable())
                {
                    paymentStatus = _expensivePaymentManager.ProcessPayment(model);
                }
                else
                {
                    paymentStatus = _cheapPaymentManager.ProcessPayment(model);
                }
            }

            if (model.Amount > 500)
            {
                int i = 0;
                do
                {
                    if (IsPaymentProcessed())
                    {
                        paymentStatus = _premiumPaymentManager.ProcessPayment(model);
                        break;
                    }
                    i++;
                }while (i < 3);
            }
            return(paymentStatus);
        }
Пример #5
0
        public async Task <dynamic> ProcessPayment(RequestDto request)
        {
            if (request == null)
            {
                return(BadRequest(ModelState));
            }

            if (!ValidateCreditCard(request.CreditCardNumber))
            {
                ModelState.AddModelError("", "Credit Card Number is invalid!");
                return(StatusCode(404, ModelState));
            }

            if (!ValidateExpirationDate(request.ExpirationDate))
            {
                ModelState.AddModelError("", "Card expired!");
                return(StatusCode(404, ModelState));
            }

            if (request.SecurityCode.Length > 0 && !ValidateSecurityCode(request.SecurityCode))
            {
                ModelState.AddModelError("", "Security code is invalid!");
                return(StatusCode(404, ModelState));
            }

            if (!ValidateAmount(request.Amount))
            {
                ModelState.AddModelError("", "Amount can't be negative!");
                return(StatusCode(404, ModelState));
            }

            var requestObj = _mapper.Map <Request>(request);

            var addRequest = _expensivePaymentGateway.AddRequest(requestObj);

            dynamic process;

            if (request.Amount <= 20)
            {
                process = await _cheapPaymentGateway.ProcessPayment(requestObj);
            }
            else if (request.Amount > 20 && request.Amount <= 500)
            {
                process = await _expensivePaymentGateway.ProcessPayment(requestObj);
            }
            else
            {
                process = await _premiumPaymentService.ProcessPayment(requestObj);
            }


            var state = new PaymentStateDto
            {
                RequestId = addRequest.Id,
                Status    = process
            };

            var stateObj = _mapper.Map <PaymentState>(state);

            _expensivePaymentGateway.AddPaymentState(stateObj);

            return(process);
        }
Пример #6
0
        public async Task <IActionResult> ProcessPayment([FromBody] PaymentDto paymentDto)
        {
            //return await NoContent();
            paymentDto.CreditCardNumber = Regex.Replace(paymentDto.CreditCardNumber, @"[^\d]", "");
            var validator = new PaymentValidator();
            var validate  = await validator.ValidateAsync(paymentDto);

            if (!validate.IsValid)
            {
                var errors = validator.GetErrorList(validate.Errors);
                return(BadRequest(new { message = "Bad request", errors }));
            }
            AppPayment payment;

            try
            {
                payment = _mapper.Map <AppPayment>(paymentDto);

                if (paymentDto.Amount < 20)
                {
                    await _cheapPaymentGateway.ProcessPayment(payment);
                }
                else if (paymentDto.Amount >= 20 && paymentDto.Amount <= 500)
                {
                    try
                    {
                        await _expensivePaymentGateway.ProcessPayment(payment);
                    }
                    catch (Exception)
                    {
                        await _cheapPaymentGateway.ProcessPayment(payment);
                    }
                }
                else
                {
                    try
                    {
                        await _premiumPaymentService.ProcessPayment(payment);
                    }
                    catch (Exception)
                    {
                        int i = 1;
                        do
                        {
                            var response = await _cheapPaymentGateway.ProcessPayment(payment);

                            if (response)
                            {
                                break;
                            }
                            i++;
                        }while (i < 4);
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                return(StatusCode(500, new { message = "Internal error", errors = "Data access processing error" }));
            }

            try
            {
                var result = await _unitOfWork.appPaymentStatusRepository.GetPaymentStatusById(payment.Id);

                return(Ok(new { message = "Success", data = result }));
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                return(StatusCode(500, new { message = "Internal error", errors = "Data access processing error" }));
            }
        }