Esempio n. 1
0
        public PaymentServiceResponse <PaymentResponse> Post([FromBody] PaymentRequest request)
        {
            PaymentServiceResponse <PaymentResponse> response = new PaymentServiceResponse <PaymentResponse>();

            response.Status = HttpStatusCode.OK;

            try
            {
                // throw new ArgumentNullException(nameof(request.Amount), "Amount is null");
                response.DataContent = new PaymentResponse()
                {
                    PaymentId  = Guid.NewGuid(),
                    CustomerId = request.CustomerId,
                    Balance    = 4513.25m
                };
            }
            catch (Exception ex)
            {
                response.Status      = HttpStatusCode.InternalServerError;
                response.DataContent = null;
                response.Error       = new PaymentError()
                {
                    ErrorMessage = ex.Message, ErrorType = ex.GetType().ToString()
                };
            }

            return(response);
        }
Esempio n. 2
0
        // GET api/payment
        public PaymentServiceResponse <List <PaymentResponse> > Get()
        {
            PaymentServiceResponse <List <PaymentResponse> > response = new PaymentServiceResponse <List <PaymentResponse> >();

            response.Status = HttpStatusCode.OK;
            // throw new Exception("DOES NOT WORK!");


            List <PaymentResponse> list = new List <PaymentResponse>();

            list.Add(new PaymentResponse()
            {
                PaymentId = Guid.NewGuid(), CustomerId = 4512, Balance = 4512.45m
            });
            list.Add(new PaymentResponse()
            {
                PaymentId = Guid.NewGuid(), CustomerId = 4514, Balance = 1425
            });
            list.Add(new PaymentResponse()
            {
                PaymentId = Guid.NewGuid(), CustomerId = 4516, Balance = 458.45m
            });
            list.Add(new PaymentResponse()
            {
                PaymentId = Guid.NewGuid(), CustomerId = 4517, Balance = 350.45m
            });

            try
            {
                response.DataContent = list;
            }
            catch (Exception ex)
            {
                response.Status      = HttpStatusCode.InternalServerError;
                response.DataContent = null;
                response.Error       = new PaymentError()
                {
                    ErrorMessage = ex.Message, ErrorType = ex.GetType().ToString()
                };
            }

            return(response);
        }
Esempio n. 3
0
        public async Task <PaymentServiceResponse> MakePayment(PaymentViewModel payment)
        {
            var gatewayResult = new PaymentGatewayResponse();

            if (payment.Amount <= 20)
            {
                gatewayResult = await _cheapPaymentGateway.ProcessPayment(payment.CreditCardNumber, payment.CardHolder, payment.Amount);
            }
            else if (payment.Amount <= 500)
            {
                gatewayResult = await _expensivePaymentGateway.ProcessPayment(payment.CreditCardNumber, payment.CardHolder, payment.Amount);

                if (gatewayResult.StatusCode != 200)
                {
                    gatewayResult = await _cheapPaymentGateway.ProcessPayment(payment.CreditCardNumber, payment.CardHolder, payment.Amount);
                }
            }
            else if (payment.Amount > 500)
            {
                int numberOfRetries = 3;
                int i = 0;
                while (i < numberOfRetries)
                {
                    gatewayResult = await _premiumPaymentGateway.ProcessPayment(payment.CreditCardNumber, payment.CardHolder, payment.Amount);

                    if (gatewayResult.StatusCode == 200)
                    {
                        break;
                    }
                    i++;
                }
            }
            if (gatewayResult.StatusCode == 200)
            {
                SavePayment(payment, gatewayResult.PaymentStatus);
            }
            var result = new PaymentServiceResponse {
                StatusCode = gatewayResult.StatusCode, Message = gatewayResult.Message
            };

            return(result);
        }
Esempio n. 4
0
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            string message    = string.Empty;
            bool   firstError = true;

            if (actionContext.ModelState.IsValid == false)
            {
                foreach (var item in actionContext.ModelState.Values)
                {
                    foreach (var e in item.Errors)
                    {
                        if (!firstError)
                        {
                            message += "; ";
                        }

                        message   += e.ErrorMessage;
                        firstError = false;
                    }
                }


                PaymentError paymentError = new PaymentError();
                paymentError.ErrorMessage = message;
                paymentError.ErrorType    = typeof(PaymentRequiredFieldException).Name;

                var response = new PaymentServiceResponse <Object>()
                {
                    Status      = HttpStatusCode.BadRequest,
                    DataContent = null,
                    Error       = paymentError
                };

                actionContext.Response = actionContext.Request.CreateResponse(response);
            }
        }