public async Task Handle(AcceptOrderTransactionCommand message)  // 1.2 Accept order transaction.
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var order = await _orderRepository.Get(message.OrderId);

            if (order == null)
            {
                return;
            }

            order.OrderPayment.Status  = OrderPaymentStatus.Approved;
            order.OrderPayment.PayerId = message.PayerId;
            await _orderRepository.Update(order);

            _eventPublisher.Publish(new OrderTransactionApprovedEvent
            {
                OrderId  = message.OrderId,
                Subject  = order.Subject,
                SellerId = order.SellerId
            });
        }
Exemplo n.º 2
0
        public async Task <AcceptOrderTransactionValidationResult> Validate(AcceptOrderTransactionCommand command, string subject)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            var order = await _orderRepository.Get(command.OrderId);

            if (order == null)
            {
                return(new AcceptOrderTransactionValidationResult(ErrorDescriptions.TheOrderDoesntExist));
            }

            if (order.OrderPayment == null)
            {
                return(new AcceptOrderTransactionValidationResult(ErrorDescriptions.TheOrderDoesntHavePayment));
            }


            if (order.OrderPayment.TransactionId != command.TransactionId)
            {
                return(new AcceptOrderTransactionValidationResult(ErrorDescriptions.TheOrderTransactionIsNotCorrect));
            }

            if (order.OrderPayment.Status != OrderPaymentStatus.Created)
            {
                return(new AcceptOrderTransactionValidationResult(ErrorDescriptions.TheOrderTransactionHasBeenApproved));
            }

            if (order.Subject != subject)
            {
                return(new AcceptOrderTransactionValidationResult(ErrorDescriptions.TheOrderCanBeApprovedOnlyByItsCreator));
            }

            return(new AcceptOrderTransactionValidationResult());
        }
        public async Task <IActionResult> Execute(string orderId, string subject, string commonId, JObject jObj)
        {
            if (string.IsNullOrWhiteSpace(orderId))
            {
                throw new ArgumentNullException(nameof(orderId));
            }

            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new ArgumentNullException(nameof(jObj));
            }

            AcceptOrderTransactionCommand command = null;

            try
            {
                command = _requestBuilder.GetAcceptOrderTransactionCommand(jObj);
            }
            catch (ArgumentException ex)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Request, ex.Message);
                return(_controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error));
            }

            command.OrderId  = orderId;
            command.CommonId = commonId;
            var validationResult = await _validator.Validate(command, subject);

            if (!validationResult.IsValid)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Request, validationResult.Message);
                return(_controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error));
            }

            _commandSender.Send(command);
            return(new OkResult());
        }