Esempio n. 1
0
        public void ValidateFailedPremiumPaymentGatewayPayment()
        {
            var paymentDetail         = new PaymentDetail();
            var premiumPaymentGateway = new PremiumPaymentGateway();
            var expected = ResponseCodes.ERROR;
            var result   = premiumPaymentGateway.ProcessPayment(paymentDetail, 1);
            var actual   = result.Code;

            Assert.Equal(expected, actual);
        }
Esempio n. 2
0
        public void ValidateSuccessfulPremiumPaymentGatewayPayment()
        {
            var paymentDetail         = new PaymentDetail();
            var premiumPaymentGateway = new PremiumPaymentGateway();
            var expected = ResponseCodes.OK;
            //odd numbers fail, even(2,4,6) numbers succeed. I used 2 below
            var result = premiumPaymentGateway.ProcessPayment(paymentDetail, 2);
            var actual = result.Code;

            Assert.Equal(expected, actual);
        }
Esempio n. 3
0
        public bool ProcessPayment(Card card)
        {
            decimal amount = card.Amount;

            if (amount < 20)
            {
                IExternalService externalService = new CheapPaymentGateway();
                isPaymentProcessed = externalService.ProcessPayment();
            }
            else if (amount > 20 && amount <= 500)
            {
                IExternalService externalService = new ExpensivePaymentGateway();
                if (externalService.IsAvailable)
                {
                    isPaymentProcessed = externalService.ProcessPayment();
                }
                else
                {
                    externalService = new CheapPaymentGateway();
                    while (externalService.RetryCount < 1)
                    {
                        isPaymentProcessed = externalService.ProcessPayment();
                    }
                }
            }
            else if (amount > 500)
            {
                IExternalService externalService = new PremiumPaymentGateway();
                while (externalService.RetryCount < 3)
                {
                    isPaymentProcessed = externalService.ProcessPayment();
                    if (isPaymentProcessed)
                    {
                        break;
                    }
                }
            }

            return(isPaymentProcessed);
        }
Esempio n. 4
0
        public async Task <ActionResult <Payment> > PostPayment(Payment payment)
        {
            if (this.ModelState.IsValid)
            {
                _context.Payment.Add(payment);
                PaymentState paymentState = new PaymentState {
                    Payment = payment, PaymentStatus = PaymentStatus.Pending
                };
                _context.PaymentState.Add(paymentState);
                await _context.SaveChangesAsync();

                if (payment.Amount <= 20)
                {
                    ICheapPaymentGateway cheapPaymentGateway = new CheapPaymentGateway();
                    bool paymentProcessStatus = cheapPaymentGateway.ProcessPayment(payment);
                    if (paymentProcessStatus)
                    {
                        paymentState.PaymentStatus         = PaymentStatus.Processed;
                        _context.Entry(paymentState).State = EntityState.Modified;
                        await _context.SaveChangesAsync();
                    }
                    else
                    {
                        paymentState.PaymentStatus         = PaymentStatus.Failed;
                        _context.Entry(paymentState).State = EntityState.Modified;
                        await _context.SaveChangesAsync();
                    }
                }
                else if (payment.Amount > 20 && payment.Amount <= 500)
                {
                    IExpensivePaymentGateway expensivePaymentGateway = new ExpensivePaymentGateway();
                    if (expensivePaymentGateway.isAvailable)
                    {
                        bool paymentProcessStatus = expensivePaymentGateway.ProcessPayment(payment);
                        if (paymentProcessStatus)
                        {
                            paymentState.PaymentStatus         = PaymentStatus.Processed;
                            _context.Entry(paymentState).State = EntityState.Modified;
                            await _context.SaveChangesAsync();
                        }
                        else
                        {
                            paymentState.PaymentStatus         = PaymentStatus.Failed;
                            _context.Entry(paymentState).State = EntityState.Modified;
                            await _context.SaveChangesAsync();
                        }
                    }
                    else
                    {
                        ICheapPaymentGateway cheapPaymentGateway = new CheapPaymentGateway();
                        bool paymentProcessStatus = cheapPaymentGateway.ProcessPayment(payment);
                        if (paymentProcessStatus)
                        {
                            paymentState.PaymentStatus         = PaymentStatus.Processed;
                            _context.Entry(paymentState).State = EntityState.Modified;
                            await _context.SaveChangesAsync();
                        }
                        else
                        {
                            paymentState.PaymentStatus         = PaymentStatus.Failed;
                            _context.Entry(paymentState).State = EntityState.Modified;
                            await _context.SaveChangesAsync();
                        }
                    }
                }
                else if (payment.Amount > 500)
                {
                    IPremiumPaymentGateway premiumPaymentGateway = new PremiumPaymentGateway();
                    bool paymentProcessStatus = false;
                    for (int i = 0; i < 3; i++)
                    {
                        paymentProcessStatus = premiumPaymentGateway.ProcessPayment(payment);
                        if (paymentProcessStatus)
                        {
                            break;
                        }
                    }
                    if (paymentProcessStatus)
                    {
                        paymentState.PaymentStatus         = PaymentStatus.Processed;
                        _context.Entry(paymentState).State = EntityState.Modified;
                        await _context.SaveChangesAsync();
                    }
                    else
                    {
                        paymentState.PaymentStatus         = PaymentStatus.Failed;
                        _context.Entry(paymentState).State = EntityState.Modified;
                        await _context.SaveChangesAsync();
                    }
                }

                return(CreatedAtAction("GetPayment", new { id = payment.Id }, payment));
            }

            return(BadRequest(this.ModelState));
        }