private void ProcessAmountGreaterThan500(int paymentId)
        {
            var results = RetryMechanism.RetryWithSame(() =>
                                                       _expensivePaymentGateway.ProcessPayment(paymentId), 3);

            if (!results)
            {
                ProcessFailedPayment(paymentId);
            }
        }
        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);
        }
Esempio n. 3
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");
            }
        }
Esempio n. 4
0
        public bool ProcessPaymentGreaterThan21LessThan501(PaymentDetail paymentDetail, int transactionType = 0)
        {
            var referenceNumberr = Utility.Utility.GenerateReferenceNumber();

            paymentDetail.PaymentReference = referenceNumberr;
            paymentDetail.PaymentLogs      = new List <PaymentLog> {
                new PaymentLog {
                    Amount = paymentDetail.Amount, PaymentDate = DateTime.Now, PaymentState = PaymentState.Pending, PaymentGateway = PaymentGateway.ExpensiveGateway, PaymentReference = referenceNumberr
                }
            };
            detailRepository.Add(paymentDetail);
            if (!(detailRepository.Save() > 0))
            {
                return(false);
            }

            var expGatewayResult = expensivePaymentGateway.ProcessPayment(paymentDetail, transactionType);
            var paymentLog       = paymentLogRepository.GetPaymentLogByFilter(c => c.PaymentReference == referenceNumberr);//.FirstOrDefault();

            if (expGatewayResult.Code == ResponseCodes.OK)
            {
                paymentLog.PaymentState = PaymentState.Processed;
                paymentLogRepository.Update(paymentLog);
                paymentLogRepository.Save();
                return(true);
            }
            else
            {
                paymentLog.PaymentState = PaymentState.Failed;
                paymentLogRepository.Update(paymentLog);
                paymentLogRepository.Save();


                //retry with cheapGateWay
                var logg = paymentLogRepository.Add(new PaymentLog {
                    PaymentDetailId = paymentLog.PaymentDetailId, PaymentReference = paymentLog.PaymentReference, Amount = paymentDetail.Amount, PaymentGateway = PaymentGateway.CheapGateway, PaymentState = PaymentState.Pending
                });
                var cheapGatewayResult = cheapPaymentGateway.ProcessPayment(paymentDetail);
                var paymentLogCheap    = paymentLogRepository.GetPaymentLogByFilter(c => c.PaymentReference == referenceNumberr && c.PaymentGateway == PaymentGateway.CheapGateway);//.FirstOrDefault();
                if (cheapGatewayResult.Code == ResponseCodes.OK)
                {
                    paymentLog.PaymentState = PaymentState.Processed;
                    paymentLogRepository.Update(paymentLog);
                    paymentLogRepository.Save();
                    return(true);
                }
                else
                {
                    paymentLog.PaymentState = PaymentState.Failed;
                    paymentLogRepository.Update(paymentLog);
                    paymentLogRepository.Save();
                    return(false);
                }
            }
        }
Esempio n. 5
0
        public async Task <Payment> ProcessPaymentAsync(ProcessPaymentRequest processPaymentRequest)
        {
            var payment = await AddNewPayment(processPaymentRequest).ConfigureAwait(false);

            bool isPaymentSuceed;

            if (processPaymentRequest.Amount <= 20)
            {
                var paymentTask = _CheapPaymentGateway.ProcessPayment(processPaymentRequest.CreditCardNumber,
                                                                      processPaymentRequest.CardHolder,
                                                                      processPaymentRequest.ExpirationDate,
                                                                      processPaymentRequest.SecurityCode,
                                                                      processPaymentRequest.Amount);
                isPaymentSuceed = await ProcessPaymentAsync(paymentTask, 1);
            }
            else if (processPaymentRequest.Amount <= 500)
            {
                var paymentTask = _ExpensivePaymentGateway.ProcessPayment(processPaymentRequest.CreditCardNumber,
                                                                          processPaymentRequest.CardHolder,
                                                                          processPaymentRequest.ExpirationDate,
                                                                          processPaymentRequest.SecurityCode,
                                                                          processPaymentRequest.Amount);
                isPaymentSuceed = await ProcessPaymentAsync(paymentTask, 1);

                if (!isPaymentSuceed)
                {
                    paymentTask = _CheapPaymentGateway.ProcessPayment(processPaymentRequest.CreditCardNumber,
                                                                      processPaymentRequest.CardHolder,
                                                                      processPaymentRequest.ExpirationDate,
                                                                      processPaymentRequest.SecurityCode,
                                                                      processPaymentRequest.Amount);
                    isPaymentSuceed = await ProcessPaymentAsync(paymentTask, 1);
                }
            }
            else
            {
                var paymentTask = _CheapPaymentGateway.ProcessPayment(processPaymentRequest.CreditCardNumber,
                                                                      processPaymentRequest.CardHolder,
                                                                      processPaymentRequest.ExpirationDate,
                                                                      processPaymentRequest.SecurityCode,
                                                                      processPaymentRequest.Amount);
                isPaymentSuceed = await ProcessPaymentAsync(paymentTask, 3);
            }
            payment.Status = isPaymentSuceed ? "processed" : "failed";
            await UpdatePayment(payment);

            return(payment);
        }
Esempio n. 6
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());
        }
Esempio n. 7
0
        public async Task <PaymentServiceResponse> MakePayment(PaymentViewModel payment)
        {
            var gatewayResult = new PaymentGatewayResponse();

            if (payment.Amount <= 20)
            {
                gatewayResult = await _cheapPaymentGateway.ProcessPayment(payment.CreditCardNumber, payment.CardHolder, payment.Amount);
            }
            else if (payment.Amount <= 500)
            {
                gatewayResult = await _expensivePaymentGateway.ProcessPayment(payment.CreditCardNumber, payment.CardHolder, payment.Amount);

                if (gatewayResult.StatusCode != 200)
                {
                    gatewayResult = await _cheapPaymentGateway.ProcessPayment(payment.CreditCardNumber, payment.CardHolder, payment.Amount);
                }
            }
            else if (payment.Amount > 500)
            {
                int numberOfRetries = 3;
                int i = 0;
                while (i < numberOfRetries)
                {
                    gatewayResult = await _premiumPaymentGateway.ProcessPayment(payment.CreditCardNumber, payment.CardHolder, payment.Amount);

                    if (gatewayResult.StatusCode == 200)
                    {
                        break;
                    }
                    i++;
                }
            }
            if (gatewayResult.StatusCode == 200)
            {
                SavePayment(payment, gatewayResult.PaymentStatus);
            }
            var result = new PaymentServiceResponse {
                StatusCode = gatewayResult.StatusCode, Message = gatewayResult.Message
            };

            return(result);
        }
Esempio n. 8
0
 public void ProcessPayment(Payment p)
 {
     //If upto 20 euros then use Cheap Payment Gateway
     if (p.Amount <= 20)
     {
         var result = _cheapPaymentGateway.ProcessPayment();
         SavePaymentDetails(result, p);
     }
     else if (p.Amount > 20 && p.Amount <= 500)
     {
         //Check if Expensive Gateway is Available
         if (_expensivePaymentGateway.IsAvailable())
         {
             var result = _expensivePaymentGateway.ProcessPayment();
             SavePaymentDetails(result, p);
         }
         else
         {
             var result = _cheapPaymentGateway.ProcessPayment();
             SavePaymentDetails(result, p);
         }
     }
     else if (p.Amount > 500)
     {
         //This will be retried 3 times
         _retryPolicy.Execute(() =>
         {
             var result = _premiumPaymentGateway.ProcessPayment();
             if (result == 0)
             {
                 //Save Failed Status
                 SavePaymentDetails(result, p);
                 //Throw Payment Failed Exception for Polly Retry
                 throw new Exception("Premium Payment Failed");
             }
             SavePaymentDetails(result, p);
             return(null);
         });
     }
 }
Esempio n. 9
0
        public PaymentResponse ProcessPayment(Payment payment)
        {
            PaymentResponse response = null;

            IPaymentGateway gateway;

            if (payment.Amount < 20)
            {
                gateway  = new ICheapPaymentGateway();
                response = gateway.ProcessPayment(payment);
            }
            else if (payment.Amount > 20 && payment.Amount <= 500)
            {
                gateway  = new IExpensivePaymentGateway();
                response = gateway.ProcessPayment(payment);
                if (!response.Success)
                {
                    gateway  = new ICheapPaymentGateway();
                    response = gateway.ProcessPayment(payment);
                }
            }
            else
            {
                gateway = new IPremiumPaymentGateway();
                var retries = 3;
                while (retries > 0)
                {
                    response = gateway.ProcessPayment(payment);
                    if (response.Success)
                    {
                        return(response);
                    }

                    retries--;
                }
                response.ErrorMessage += " Retried: 3 times.";
            }
            return(response);
        }
Esempio n. 10
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);
        }
Esempio n. 11
0
        public async Task <IActionResult> ProcessPayment(PaymentDto payment)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new { message = "Bad Request", errors = "Invalid credentials provided" }));
            }

            //Strip any non-numeric values
            payment.CreditCardNumber = Regex.Replace(payment.CreditCardNumber, @"[^\d]", "");
            if (Utilities.IsModelValid(payment))
            {
                var model = new Payment();
                try
                {
                    model = _mapper.Map <Payment>(payment);

                    if (model.Amount < 21)
                    {
                        await _cheapPayment.ProcessPayment(model);
                    }
                    else if (model.Amount < 501)
                    {
                        try
                        {
                            await _expensivePayment.ProcessPayment(model);
                        }
                        catch (Exception)
                        {
                            await _cheapPayment.ProcessPayment(model);
                        }
                    }
                    else
                    {
                        try
                        {
                            await _premiumPayment.ProcessPayment(model);
                        }
                        catch (Exception)
                        {
                            int i = 1;
                            do
                            {
                                var response = await _cheapPayment.ProcessPayment(model);

                                if (response)
                                {
                                    break;
                                }
                                i++;
                            }while (i < 4);
                        }
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError(e.Message);
                }

                try
                {
                    var result = await _transactionRepository.GetById(model.Id);

                    return(Ok(result));
                }
                catch (Exception e)
                {
                    _logger.LogError(e.Message);
                }
            }
            return(StatusCode(500, "internal error"));
        }
Esempio n. 12
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);
        }
        public async Task <bool> ProcessPayment(PaymentRequest paymentRequest)
        {
            var result = await _expensivePaymentGateway.ProcessPayment(paymentRequest);

            return(result);
        }
Esempio n. 14
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" }));
            }
        }
        public async Task <IActionResult> ProcessPayment([FromBody] PaymentToCreateDTO model)
        {
            model.CreditCardNumber = Regex.Replace(model.CreditCardNumber, @"[^\d]", "");
            var validator = new PaymentValidator();
            var validate  = await validator.ValidateAsync(model);

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

            Payment payment;

            try
            {
                payment = _mapper.Map <Payment>(model);

                if (model.Amount < 21)
                {
                    await _cheapPayment.ProcessPayment(payment);
                }
                else if (model.Amount < 501)
                {
                    try
                    {
                        await _expensivePayment.ProcessPayment(payment);
                    }
                    catch (Exception)
                    {
                        await _cheapPayment.ProcessPayment(payment);
                    }
                }
                else
                {
                    try
                    {
                        await _premiumPayment.ProcessPayment(payment);
                    }
                    catch (Exception)
                    {
                        int i = 1;
                        do
                        {
                            var response = await _cheapPayment.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 _transactionRepository.GetByPaymentId(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" }));
            }
        }
        public int PaymentProcessBusinessLogic(CardInformationDTO cardModel)
        {
            try
            {
                PaymentStatusDTO payment = new PaymentStatusDTO();
                var cardTable            = _mapper.Map <CardInformation>(cardModel);
                var paymentTable         = _mapper.Map <PaymentStatus>(payment);
                if (Common.isValid((long.Parse(cardTable.CreditCardNumber))))
                {
                    if (cardTable.ExpirationDate.Year >= DateTime.Now.Year && cardTable.ExpirationDate.Month >= DateTime.Now.Month)
                    {
                        Task <int> finishedTask = null;

                        if (cardModel.Amount <= 20)
                        {
                            Task <int> finishedTask1 = Task.Run(() => _cheapPaymentGateway.ProcessPayment(cardTable));

                            if (!finishedTask1.IsCompleted)
                            {
                                int result = finishedTask1.Result;
                                paymentTable.Status = "Processed";
                                Save(cardTable, paymentTable);
                                return(200);
                            }
                        }
                        else if (cardModel.Amount >= 21 && cardModel.Amount <= 500)
                        {
                            finishedTask = Task.Run(() => _expensivePaymentGateway.ProcessPayment(cardTable));

                            if (!finishedTask.IsCompleted)
                            {
                                Task <int> finishedTask2 = Task.Run(() => _cheapPaymentGateway.ProcessPayment(cardTable));
                                int        result        = finishedTask2.Result;
                                paymentTable.Status = "Pending";
                                Save(cardTable, paymentTable);
                                return(400);
                            }
                            else
                            {
                                paymentTable.Status = "Processed";
                                Save(cardTable, paymentTable);
                                return(200);
                            }
                        }
                        else if (cardModel.Amount > 500)
                        {
                            var attempts = 3;
                            do
                            {
                                try
                                {
                                    attempts++;
                                    finishedTask = Task.Run(() => _preminumServiceGateway.ProcessPayment(cardTable));
                                    if (finishedTask.IsCompleted)
                                    {
                                        paymentTable.Status = "Processed";
                                        Save(cardTable, paymentTable);
                                        break;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    paymentTable.Status = "Failed";
                                    Save(cardTable, paymentTable);
                                    if (attempts == 3)
                                    {
                                        throw;
                                    }
                                    return(500);
                                }
                            } while (true);
                            return(200);
                        }
                    }
                    else
                    {
                        paymentTable.Status = "Pending";
                        Save(cardTable, paymentTable);
                        return(400);
                    }
                }
                else
                {
                    paymentTable.Status = "Pending";
                    Save(cardTable, paymentTable);
                    return(400);
                }

                return(200);
            }
            catch (Exception)
            {
                return(500);
            }
        }