Ejemplo n.º 1
0
        public PayStateDto ProcessPayment(PayRequestDto payReq)
        {
            PayStateDto pys = new PayStateDto();
            Random      rd  = new Random();
            int         res = rd.Next(0, 10);

            int [] array = new int[5] {
                2, 4, 6, 8, 10
            };
            if (array.Contains(res))
            {
                pys = new PayStateDto()
                {
                    PayState = PayStateEnum.Processed, PaymentStateDate = DateTime.Now
                };
            }
            else
            {
                pys = new PayStateDto()
                {
                    PayState = PayStateEnum.Failed, PaymentStateDate = DateTime.Now
                };
            }
            return(pys);
        }
Ejemplo n.º 2
0
        public void TestpayReqService(PayRequestDto payReqDto, PayStateDto cheapGateResDto, int cheapGateCount, PayStateDto expGateResponseDto, int ExpGateCount, PayStateEnum expPayStateEnum)
        {
            cheapPayGateway.Setup(c => c.ProcessPayment(payReqDto)).Returns(cheapGateResDto);
            expensivePayGateway.Setup(c => c.ProcessPayment(payReqDto)).Returns(expGateResponseDto);
            var payStateDto = PayReqService.ProcessPayment(payReqDto);

            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsNotNull(payStateDto);
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(payStateDto.PayState, expPayStateEnum);
            cheapPayGateway.Verify(c => c.ProcessPayment(payReqDto), Times.Exactly(cheapGateCount));
            expensivePayGateway.Verify(c => c.ProcessPayment(payReqDto), Times.Exactly(ExpGateCount));
        }
        public PayStateDto ProcessPayment(PayRequestDto PayReqDto)
        {
            var payEntity = Mapper.Map <PayRequestDto, Payment>(PayReqDto);
            // payEntity = payRepo.Create(payEntity);
            var payStateEntity = new PaymentState()
            {
                Payment = payEntity, PayId = payEntity.PayId, CreatedDate = DateTime.Now, State = PayStateEnum.Pending.ToString()
            };

            payStateEntity = payStateRepo.Create(payStateEntity);
            if (PayReqDto.Amount <= 20)
            {
                var Paystdto = ProcessPaymentStateDto(cheapPayGateway, PayReqDto, payEntity);
                return(Paystdto);
            }
            else if (PayReqDto.Amount > 20 && PayReqDto.Amount <= 500)
            {
                PayStateDto PayStDto = new PayStateDto()
                {
                    PayState = PayStateEnum.Failed, PaymentStateDate = DateTime.Now
                };
                int counter = 1;
                try {
                    PayStDto = ProcessPaymentStateDto(expensivePayGateway, PayReqDto, payEntity);
                    if (PayStDto != null && PayStDto.PayState == PayStateEnum.Processed)
                    {
                        return(PayStDto);
                    }
                    else
                    {
                        counter++;
                        PayStDto = ProcessPaymentStateDto(cheapPayGateway, PayReqDto, payEntity);
                        return(PayStDto);
                    }
                }
                catch (Exception)
                {
                    if (counter == 0)
                    {
                        PayStDto = ProcessPaymentStateDto(cheapPayGateway, PayReqDto, payEntity);
                        return(PayStDto);
                    }
                }
                return(PayStDto);
            }
            else
            {
                int         counter  = 0;
                PayStateDto payStDto = new PayStateDto()
                {
                    PayState = PayStateEnum.Failed, PaymentStateDate = DateTime.Now
                };
                while (counter < 3)
                {
                    try
                    {
                        payStDto = ProcessPaymentStateDto(expensivePayGateway, PayReqDto, payEntity);
                        if (payStDto != null && payStDto.PayState == PayStateEnum.Processed)
                        {
                            return(payStDto);
                        }
                    }
                    catch (Exception)
                    {
                    }
                    finally {
                        counter++;
                    }
                }
                return(payStDto);
            }
            throw new Exception("Payment could not be processed");
        }