예제 #1
0
        public async Task <IActionResult> ProcessPayment([FromBody] PaymentDto paymentDto)
        {
            //return await NoContent();
            paymentDto.CreditCardNumber = Regex.Replace(paymentDto.CreditCardNumber, @"[^\d]", "");
            var validator = new PaymentValidator();
            var validate  = await validator.ValidateAsync(paymentDto);

            if (!validate.IsValid)
            {
                var errors = validator.GetErrorList(validate.Errors);
                return(BadRequest(new { message = "Bad request", errors }));
            }
            AppPayment payment;

            try
            {
                payment = _mapper.Map <AppPayment>(paymentDto);

                if (paymentDto.Amount < 20)
                {
                    await _cheapPaymentGateway.ProcessPayment(payment);
                }
                else if (paymentDto.Amount >= 20 && paymentDto.Amount <= 500)
                {
                    try
                    {
                        await _expensivePaymentGateway.ProcessPayment(payment);
                    }
                    catch (Exception)
                    {
                        await _cheapPaymentGateway.ProcessPayment(payment);
                    }
                }
                else
                {
                    try
                    {
                        await _premiumPaymentService.ProcessPayment(payment);
                    }
                    catch (Exception)
                    {
                        int i = 1;
                        do
                        {
                            var response = await _cheapPaymentGateway.ProcessPayment(payment);

                            if (response)
                            {
                                break;
                            }
                            i++;
                        }while (i < 4);
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                return(StatusCode(500, new { message = "Internal error", errors = "Data access processing error" }));
            }

            try
            {
                var result = await _unitOfWork.appPaymentStatusRepository.GetPaymentStatusById(payment.Id);

                return(Ok(new { message = "Success", data = result }));
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                return(StatusCode(500, new { message = "Internal error", errors = "Data access processing error" }));
            }
        }