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 async Task <Response <Payment> > ProcessPayment(Payment payment)
        {
            var paymentDetails = new Payment();

            if (payment.Amount < 20)
            {
                await _cheapPaymentGateway.Process(payment);

                paymentDetails = await _cheapPaymentGateway.Update(payment.Id);

                var response = new Response <Payment>
                {
                    Data   = paymentDetails,
                    Status = paymentDetails.PaymentState.State
                };

                if (response.Status == PaymentStatus.processed.ToString())
                {
                    return(response);
                }
            }

            else if (payment.Amount > 21 && payment.Amount <= 500)
            {
                if (_expensivePaymentGateway is null)
                {
                    await _cheapPaymentGateway.Process(payment);

                    paymentDetails = await _cheapPaymentGateway.Update(payment.Id);

                    var response = new Response <Payment>
                    {
                        Data   = paymentDetails,
                        Status = paymentDetails.PaymentState.State
                    };

                    if (response.Status == PaymentStatus.processed.ToString())
                    {
                        return(response);
                    }
                }
                else
                {
                    await _expensivePaymentGateway.Process(payment);

                    paymentDetails = await _expensivePaymentGateway.Update(payment.Id);

                    var response = new Response <Payment>
                    {
                        Data   = paymentDetails,
                        Status = paymentDetails.PaymentState.State
                    };

                    if (response.Status == PaymentStatus.processed.ToString())
                    {
                        return(response);
                    }
                }
            }

            else if (payment.Amount > 500)
            {
                var i = 0;

                while (i < 3)
                {
                    await _premiumPaymentGateway.Process(payment);

                    paymentDetails = await _premiumPaymentGateway.Update(payment.Id);

                    var response = new Response <Payment>
                    {
                        Data   = paymentDetails,
                        Status = paymentDetails.PaymentState.State
                    };

                    if (response.Status == PaymentStatus.processed.ToString())
                    {
                        return(response);
                    }

                    i++;
                }
            }

            return(new Response <Payment>
            {
                Data = paymentDetails,
                Status = PaymentStatus.failed.ToString()
            });
        }