public IActionResult Post([FromBody] CreditcardTransaction paymentTransaction)
        {
            try
            {
                _logger.LogError("Received a new payment for Credit-Cart: {0} Amount:{1} Receiver: {2}",
                                 new object[] {
                    paymentTransaction.CreditcardNumber,
                    paymentTransaction.Amount,
                    paymentTransaction.ReceiverName
                });

                if (ModelState.IsValid == false)
                {
                    return(BadRequest(ModelState));
                }

                //Do some crazy payment shit, but not as crazy as Hybris does it!

                return(Ok(new { id = System.Guid.NewGuid() }));
            } catch (Exception e)
            {
                _logger.LogError("Error while processing payment", e);
                _logger.LogError(e.Message);
                return(StatusCode(500));
            }
        }
        public async Task <bool> PostCreditcartTransaction(CreditcardTransaction creditCardTransaction)
        {
            if (!BaseUrlsAvailable())
            {
                _logger.LogError("No futher services available - This service is totaly broken");
            }
            else
            {
                try
                {
                    _client.DefaultRequestHeaders.Accept.Clear();
                    _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(creditCardTransaction), Encoding.UTF8);
                    httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                    var baseUrl  = GetRoundRobinBaseUrl();
                    var response = await _client.PostAsync(baseUrl + "/api/CreditcardTransactions", httpContent);

                    return((response.IsSuccessStatusCode) ?  true :  false);
                }
                catch (HttpRequestException ex)
                {
                    _logger.LogError("An error occurred connecting to SimpleCreditCartServiceClient");

                    //Call next Service. This is probably down or has another issue (Some crazy shit is going on with this service...)
                    //Endlos-Loop prevention is done by CircuitBreakerAsync
                    await GetPaymentMethods();
                }
            }

            //Fallback
            return(false);
        }
Beispiel #3
0
        public IActionResult Post([FromBody] Basket basket, [FromServices] SimpleCreditCartServiceClient creditcartServiceClient)
        {
            _logger.LogError("TransactionInfo Creditcard: {0} Product:{1} Amount: {2}", new object[] { basket.CustomerCreditCardnumber, basket.Product, basket.AmountInEuro });

            //Mapping
            CreditcardTransaction creditCardTransaction = new CreditcardTransaction()
            {
                Amount           = basket.AmountInEuro,
                CreditcardNumber = basket.CustomerCreditCardnumber,
                ReceiverName     = basket.Vendor
            };

            //Use the new awesome HttpClient with Resiliance and Failure Handling.
            bool result = creditcartServiceClient.PostCreditcartTransaction(creditCardTransaction).Result;

            if (result == true)
            {
                return(CreatedAtAction("Get", new { id = Guid.NewGuid() }, creditCardTransaction));
            }

            //Fallback
            return(BadRequest());

            /*
             * HttpClient client = new HttpClient();
             * client.BaseAddress = new Uri(creditcardServiceBaseAddress);
             * client.DefaultRequestHeaders.Accept.Clear();
             * client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
             * HttpResponseMessage response =  client.PostAsJsonAsync(creditcardServiceBaseAddress + "/api/CreditcardTransactions", creditCardTransaction).Result;
             * response.EnsureSuccessStatusCode();
             *
             * return CreatedAtAction("Get", new { id = System.Guid.NewGuid() }, creditCardTransaction);
             */
        }
Beispiel #4
0
        public IActionResult Post([FromBody] CreditcardTransaction creditcardTransaction)
        {
            //xml formatter funktioniert leider nicht so....conversion error

            if (creditcardTransaction == null)
            {
                return(NoContent());
            }

            return(Ok(creditcardTransaction));
        }
Beispiel #5
0
        public IActionResult Post([FromBody] Basket basket)
        {
            _logger.LogError("TransactionInfo Creditcard: {0} Product:{1} Amount: {2}", new object[] { basket.CustomerCreditCardnumber, basket.Product, basket.AmountInEuro });
            //Mapping
            CreditcardTransaction creditCardTransaction = new CreditcardTransaction()
            {
                Amount           = basket.AmountInEuro,
                CreditcardNumber = basket.CustomerCreditCardnumber,
                ReceiverName     = basket.Vendor
            };
            var client = httpClientFactory.CreateClient("CreditCardService");
            HttpResponseMessage response = client.PostAsJsonAsync(apiClient.GetLoadBalancedUrl("creditcard-service") + "/api/CreditcardTransactions", creditCardTransaction).Result;

            response.EnsureSuccessStatusCode();
            return(CreatedAtAction("Get", new { id = System.Guid.NewGuid() }));
        }
        public IActionResult Get()
        {
            CreditcardTransaction creditcardTransaction = new CreditcardTransaction();

            creditcardTransaction.Amount           = 13.37;
            creditcardTransaction.CreditcardNumber = "1234 5678 9012";
            creditcardTransaction.CreditcardType   = "Master";
            creditcardTransaction.ReceiverName     = "The rich AG";

            if (Request.Headers["Accept"] == "text/csv")
            {
                return(Content(String.Format("{0};{1};{2};{3}",
                                             creditcardTransaction.CreditcardNumber,
                                             creditcardTransaction.CreditcardType,
                                             creditcardTransaction.Amount,
                                             creditcardTransaction.ReceiverName)));
            }

            return(Ok(creditcardTransaction));
        }
        public IActionResult Post([FromBody] Basket basket)
        {
            _logger.LogError("TransactionInfo Creditcard: {0} Product:{1} Amount: {2}", new object[] { basket.CustomerCreditCardnumber, basket.Product, basket.AmountInEuro });

            //Mapping
            CreditcardTransaction creditCardTransaction = new CreditcardTransaction()
            {
                Amount           = basket.AmountInEuro,
                CreditcardNumber = basket.CustomerCreditCardnumber,
                ReceiverName     = basket.Vendor
            };

            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri(creditcardServiceBaseAddress);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = client.PostAsJsonAsync(creditcardServiceBaseAddress + "/api/CreditcardTransactions", creditCardTransaction).Result;

            response.EnsureSuccessStatusCode();

            return(CreatedAtAction("Get", new { id = System.Guid.NewGuid() }));
        }
        public async Task <IActionResult> Post([FromBody] Basket basket)
        {
            _logger.LogError("TransactionInfo Creditcard: {0} Product:{1} Amount: {2}", new object[] { basket.CustomerCreditCardnumber, basket.Product, basket.AmountInEuro });

            //Mapping
            CreditcardTransaction creditCardTransaction = new CreditcardTransaction()
            {
                Amount           = basket.AmountInEuro,
                CreditcardNumber = basket.CustomerCreditCardnumber,
                ReceiverName     = basket.Vendor
            };

            string     url    = paymentServiceProxy.GetCurrentUrl();
            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri(url);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = await Policy
                                           .HandleResult <HttpResponseMessage>(message => !message.IsSuccessStatusCode)
                                           .WaitAndRetryAsync(3, i => TimeSpan.FromSeconds(2), (result, timeSpan, retryCount, context) =>
            {
                _logger.LogWarning($"Request failed with {result.Result.StatusCode}. Waiting {timeSpan} before next retry. Retry attempt {retryCount}");
            })
                                           .ExecuteAsync(() => client.PostAsJsonAsync(url + "/api/CreditcardTransactions", creditCardTransaction));

            if (response.IsSuccessStatusCode)
            {
                _logger.LogInformation("Response was successful.");
            }
            else
            {
                _logger.LogError($"Response failed. Status code {response.StatusCode}");
            }

            return(CreatedAtAction("Get", new { id = System.Guid.NewGuid() }, creditCardTransaction));
        }
Beispiel #9
0
 public IActionResult Post([FromBody] CreditcardTransaction creditcardTransaction)
 {
     _logger.LogError("TransactionInfo Number: {0} Amount:{1} Receiver: {2}", new object[] { creditcardTransaction.CreditcardNumber, creditcardTransaction.Amount, creditcardTransaction.ReceiverName });
     return(CreatedAtAction("Get", new { id = System.Guid.NewGuid() }));
 }
        public IActionResult Post([FromBody] Basket basket)
        {
            _logger.LogError("TransactionInfo Creditcard: {0} Product:{1} Amount: {2}", new object[] { basket.CustomerCreditCardnumber, basket.Product, basket.AmountInEuro });

            bool postSuccessful = false;
            int  currentRepeats = 0;
            HttpResponseMessage response;

            List <string> creditCardServices = new List <string>();

            creditCardServices.Add("http://iegeasycreditcardservice.azurewebsites.net/");
            creditCardServices.Add("http://iwi17-easycreditcardservice.azurewebsites.net");
            creditCardServices.Add("http://iwi17-easycreditcardservice2.azurewebsites.net/");
            creditCardServices.Add("http://iwi17-easycreditcardservice3.azurewebsites.net/");

            int  availableServices = creditCardServices.Count;
            int  triedServices     = 0;
            bool exhaustedRetries  = false;

            //Mapping
            CreditcardTransaction creditCardTransaction = new CreditcardTransaction()
            {
                Amount           = basket.AmountInEuro,
                CreditcardNumber = basket.CustomerCreditCardnumber,
                ReceiverName     = basket.Vendor
            };

            do
            {
                HttpClient client = new HttpClient();

                //client.BaseAddress = new Uri(creditcardServiceBaseAddress);
                client.BaseAddress = new Uri(creditCardServices[_usedService]);

                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                try
                {
                    response = client.PostAsJsonAsync(creditCardServices[_usedService] + "/api/CreditcardTransactions", creditCardTransaction).Result;

                    if (response.IsSuccessStatusCode)
                    {
                        _logger.LogError("TransactionInfo Creditcard: Successful. Retry: {0}, Server: {1} (#{2})", new object[] { currentRepeats + 1, creditCardServices[_usedService], _usedService + 1 });
                        postSuccessful = true;
                        // rotate round robin
                        _usedService = (_usedService + 1) % availableServices;
                    }
                    else
                    {
                        throw (new HttpRequestException("No Success Status Code Received."));
                    }


                    //response.EnsureSuccessStatusCode();
                }
                catch (Exception errorHTTP)
                {
                    _logger.LogError("TransactionInfo Creditcard: failed to Connect to Service via HTTP. Retry: {0}, Error: {1}", new object[] { currentRepeats, errorHTTP.Message });

                    if (currentRepeats < _repeats)
                    {
                        currentRepeats = currentRepeats + 1;
                    }

                    else
                    {
                        if (triedServices < availableServices)
                        {
                            // choose another Service or quit
                            _logger.LogError("TransactionInfo Creditcard: failed to Connect to Service via HTTP. Retry count reached, trying another service.");
                            _usedService  = (_usedService + 1) % availableServices;
                            triedServices = triedServices + 1;
                        }
                        else
                        {
                            exhaustedRetries = true;
                            _logger.LogError("TransactionInfo Creditcard: No more Services to try. Aborting Request.");
                            return(StatusCode(503)); // Internal Server Error - Service Unavailable.;
                        }
                    }
                }
            } while (!postSuccessful && !exhaustedRetries);

            ISell sell = new Sell
            {
                Amount  = basket.AmountInEuro,
                Date    = DateTimeOffset.Now,
                Product = basket.Product,
                Vendor  = basket.Vendor
            };
            int id = _statisticsDal.AddSell(sell);

            return(CreatedAtAction("Get", new { id = System.Guid.NewGuid() }));
        }