Example #1
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);
        }