public IActionResult ProcessPayment(PaymentViewModel payment)
        {
            bool _paymentStauts = false;

            try {
                if (payment.Amount <= 20)
                {
                    _paymentStauts = _cheapPaymentGateway.Pay(_mapper.Map <PaymentViewModel, PaymentDto>(payment));
                }
                else if (payment.Amount > 20 && payment.Amount < 500)
                {
                    _paymentStauts = _expensePaymentGateway.Pay(_mapper.Map <PaymentViewModel, PaymentDto>(payment));
                }
                else if (payment.Amount >= 500)
                {
                    _paymentStauts = _premiumPaymentGateway.Pay(_mapper.Map <PaymentViewModel, PaymentDto>(payment));
                }

                var model = _mapper.Map <PaymentViewModel, PaymentDto>(payment);
                model.PaymentStatus = _paymentStauts == true ? "paid" : "unpaid";
                _paymentEngine.AddPayment(model);

                if (_paymentStauts)
                {
                    return(Ok());
                }
                else
                {
                    return(BadRequest());
                }
            } catch (Exception) {
                return(StatusCode(500));
            }
        }
Exemple #2
0
        public async Task <IActionResult> ProcessPayment(PaymentDto payment)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (payment.ExpirationDate < DateTime.Now)
            {
                return(BadRequest("The Expiration Date Cannot be in the past"));
            }

            // Check if the amount and determines the payment Gateway to use

            if (payment.Amount < 20)
            {
                var result = _cheapPayment.Pay();
                CheckPayment(result, payment);
                await _unitOfWork.processPayment.AddPayment(payment);
            }
            else if (payment.Amount >= 21 && payment.Amount < 500)
            {
                var result = _expensivePayment.Pay();
                CheckPayment(result, payment);

                await _unitOfWork.processPayment.AddPayment(payment);
            }

            _unitOfWork.Complete();

            if (payment.PaymentStateId == 2) // checks if the payment state is Processed
            {
                return(Ok(payment));
            }

            return(StatusCode(500, payment));
        }