private async Task <int> ProcessPaymentInternal(PaymentRequest paymentRequest)
        {
            if (paymentRequest.Amount <= 20)
            {
                return(await _cheapPaymentGateway.Process(paymentRequest));
            }

            if (paymentRequest.Amount >= 21 && paymentRequest.Amount <= 500)
            {
                if (_expensivePaymentGateway.IsAvailable())
                {
                    return(await _expensivePaymentGateway.Process(paymentRequest));
                }

                return(await _cheapPaymentGateway.Process(paymentRequest));
            }

            var retryPolicy = Policy
                              .Handle <Exception>()
                              .RetryAsync(RETRY_COUNT);

            return(await retryPolicy.ExecuteAsync(async() =>
            {
                return await _expensivePaymentGateway.Process(paymentRequest);
            }));
        }
Example #2
0
        public bool ProcessRequest(PaymentRequest PaymentRequest)
        {
            unitOfWork.BeginTransaction();

            try
            {
                unitOfWork.PaymentRequest.Add(PaymentRequest);
                unitOfWork.SaveChanges();

                unitOfWork.PaymentRequestState.Add(new PaymentRequestState {
                    PaymentRequestId = PaymentRequest.PaymentRequestId, State = (int)PaymentState.Pending, AdditionDate = DateTime.Now
                });
                unitOfWork.SaveChanges();

                bool paymentProccessed = false;

                if (PaymentRequest.Amount <= 20)
                {
                    paymentProccessed = cheapPaymentGateway.ProccessRequest(PaymentRequest);
                }
                else if (PaymentRequest.Amount >= 21 && PaymentRequest.Amount <= 500)
                {
                    if (expensivePaymentGateway.IsAvailable())
                    {
                        paymentProccessed = expensivePaymentGateway.ProccessRequest(PaymentRequest);
                    }
                    else
                    {
                        paymentProccessed = cheapPaymentGateway.ProccessRequest(PaymentRequest);
                    }
                }
                else
                {
                    paymentProccessed = premiumPaymentGateway.ProccessRequest(PaymentRequest, 1);
                }

                UpdateRequest(PaymentRequest.PaymentRequestId, paymentProccessed);

                unitOfWork.Commit();
                return(paymentProccessed);
            }
            catch
            {
                unitOfWork.RollBack();
                return(false);
            }
        }
Example #3
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);
         });
     }
 }
        public async Task <PaymentResponseModel> ProcessPayment(PaymentDto payemntDto)
        {
            var responseModel = mapper.Map <PaymentResponseModel>(payemntDto);

            if (payemntDto.Amount <= 20)
            {
                Console.WriteLine("ICheapPaymentGateway");
                this.cheapPaymentGateway.ProcessCheapPayment(payemntDto, responseModel);
            }
            if (payemntDto.Amount >= 21 && payemntDto.Amount <= 500)
            {
                if (expensivePaymentGateway.IsAvailable())
                {
                    this.expensivePaymentGateway.ProcessExpensivePayment(payemntDto, responseModel);
                }
                else
                {
                    this.cheapPaymentGateway.ProcessCheapPayment(payemntDto, responseModel);
                }
            }
            if (payemntDto.Amount > 500)
            {
                int  ctr         = 1;
                bool isProcessed = false;
                while (ctr < 3 && !isProcessed)
                {
                    this.premiumPaymentService.ProcessPremiumPayment(payemntDto, responseModel);
                    isProcessed = responseModel.Success;
                    ctr++;
                }
            }
            //Code for Saving Processed data
            await this.repositoryService.Add(mapper.Map <Payment>(responseModel));

            return(responseModel);
        }