Esempio n. 1
0
        public async Task <ActionResult <OrderModel> > PayForOrderAsync(int orderId, UserDetailsModel userDetails)
        {
            try
            {
                _logger.LogInformation("{Username} has started payment for the order with OrderId {OrderId}.",
                                       userDetails.Username, orderId);

                if (userDetails.CardAuthorizationInfo.ToLower() != "authorized" &&
                    userDetails.CardAuthorizationInfo.ToLower() != "unauthorized")
                {
                    throw new ArgumentException("CardAuthorizationInfo is not valid.");
                }


                var orderRequestDto = new OrderRequestDto {
                    OrderId = orderId
                };
                _rpcClient.Open();
                var order = _rpcClient.Call(orderRequestDto);
                _rpcClient.Close();


                if (order.OrderId == 0)
                {
                    throw new ArgumentException("No order with such id.");
                }

                if (userDetails.Username != order.Username)
                {
                    throw new ArgumentException("Usernames in order and user details should be equal.");
                }

                if (order.Status.ToLower() != "collecting")
                {
                    throw new InvalidOperationException("Order status should be \"Collecting\".");
                }

                switch (userDetails.CardAuthorizationInfo.ToLower())
                {
                case "authorized":
                    order.PaymentId = DateTime.Now.Ticks;
                    order.Status    = "Paid";
                    break;

                case "unauthorized":
                    order.PaymentId = DateTime.Now.Ticks;
                    order.Status    = "Failed";
                    break;
                }

                var payment = new Payment
                {
                    PaymentId = order.PaymentId.Value,
                    OrderId   = order.OrderId,
                    Username  = order.Username,
                    TotalCost = order.TotalCost,
                    IsPassed  = order.Status == "Paid" ? true : false
                };

                await _paymentRepository.AddPaymentAsync(payment);

                var payForOrderDto = _mapper.Map <PayForOrderDto>(order);

                _rabbitManager.Publish(payForOrderDto, "PaymentService_OrderExchange", ExchangeType.Direct, "PayForOrder");

                _logger.LogInformation("{Username} has finished payment for the order with OrderId {OrderId} with status {Status}.",
                                       order.Username, order.OrderId, order.Status);

                return(order);
            }
            catch (TimeoutException e)
            {
                _logger.LogInformation("{Username} has failed to perform payment for the order with OrderId {OrderId}.",
                                       userDetails.Username, orderId);

                return(StatusCode(408, e.Message));
            }
            catch (Exception e)
            {
                _logger.LogInformation("{Username} has failed to perform payment for the order with OrderId {OrderId}.",
                                       userDetails.Username, orderId);

                return(BadRequest(e.Message));
            }
        }