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)); } }
public Task <bool> Pay(decimal Amount, bool Retry) { if (!(Amount > 20 && Amount <= 500)) { throw new ArgumentException("Expensive Payment Gateway can only process amounts between 21-500 pounds"); } if (isExpensivePaymentGatewayAvailable) { var random = new Random().Next(100); return(Task.FromResult(random % 2 == 0)); } return(_cheapPaymentGateWay.Pay(Amount, true)); }
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)); }