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

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

            var order = await _orderRepository.Get(orderId);

            if (order == null)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Server, ErrorDescriptions.TheOrderDoesntExist);
                return(_controllerHelper.BuildResponse(System.Net.HttpStatusCode.NotFound, error));
            }

            if (order.Subject != subject && order.SellerId != subject)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Server, ErrorDescriptions.TheOrderCannotBeAccessedByYou);
                return(_controllerHelper.BuildResponse(System.Net.HttpStatusCode.BadRequest, error));
            }

            _halResponseBuilder.AddLinks(l => l.AddSelf("/" + Constants.RouteNames.Orders + "/" + orderId));
            await _orderEnricher.Enrich(_halResponseBuilder, order);

            return(new OkObjectResult(_halResponseBuilder.Build()));
        }
Exemple #2
0
        public async Task <IActionResult> Execute(string subject, string orderId)
        {
            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new ArgumentNullException(nameof(subject));
            }

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

            var validationResult = await _validator.Validate(subject, orderId);

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

            var label = await _upsClient.GetLabel(new GetLabelParameter
            {
                Credentials = new UpsCredentials
                {
                    LicenseNumber = _settingsProvider.GetUpsLicenseNumber(),
                    Password      = _settingsProvider.GetUpsPassword(),
                    UserName      = _settingsProvider.GetUpsUsername()
                },
                TrackingNumber = _settingsProvider.IsTstMode() ? "1Z12345E8791315509" : validationResult.Order.TrackingNumber
            });

            if (label.LabelResults == null || label.LabelResults.LabelImage == null)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Request, ErrorDescriptions.TheLabelCannotBeRetrieved);
                return(_controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error));
            }

            if (!string.Equals(label.LabelResults.LabelImage.LabelImageFormat.Code, "gif", StringComparison.CurrentCultureIgnoreCase) &&
                !string.Equals(label.LabelResults.LabelImage.LabelImageFormat.Code, "pdf", StringComparison.CurrentCultureIgnoreCase))
            {
                var error = _responseBuilder.GetError(ErrorCodes.Request, ErrorDescriptions.TheLabelFormatIsNotSupported);
                return(_controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error));
            }

            var contentType = "application/pdf";

            if (string.Equals(label.LabelResults.LabelImage.LabelImageFormat.Code, "gif", StringComparison.CurrentCultureIgnoreCase))
            {
                contentType = "application/gif";
            }

            var b64      = label.LabelResults.LabelImage.GraphicImage;
            var response = new FileContentResult(Convert.FromBase64String(b64), contentType)
            {
                FileDownloadName = $"{validationResult.Order.Id}.{label.LabelResults.LabelImage.LabelImageFormat.Code}"
            };

            return(response);
        }
        public async Task<IActionResult> Execute(JObject jObj, string id, string subject, string commonId)
        {
            if (jObj == null)
            {
                throw new ArgumentNullException(nameof(jObj));
            }

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

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

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

            command.Id = id;
            command.CommonId = commonId;
            var validationResult = await _updateOrderValidator.Validate(command, subject);
            if (!validationResult.IsValid)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Request, validationResult.Message);
                return _controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error);
            }

            if (command.TransportMode == OrderTransportModes.Packet)
            {
                try
                {
                    var result = await CreatePayment(validationResult);
                    command.PaymentOrder = new AddPaymentOrder
                    {
                        TransactionId = result.Id,
                        PaymentMethod = OrderPayments.Paypal,
                        ApprovalUrl = result.Links.FirstOrDefault(l => l.Rel == "approval_url").Href
                    };
                }
                catch
                {
                    var error = _responseBuilder.GetError(ErrorCodes.Request, ErrorDescriptions.ThePaypalPaymentDoesntWork);
                    return _controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error);
                }
            }

            _commandSender.Send(command);
            return new OkResult();
        }
Exemple #4
0
        public async Task <IActionResult> Execute(string orderId, string subject)
        {
            if (string.IsNullOrWhiteSpace(orderId))
            {
                throw new ArgumentNullException(nameof(orderId));
            }

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

            var validationResult = await _validator.Validate(orderId, subject);

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

            var isPaymentReceived = false;

            if (validationResult.Order.TransportMode == OrderTransportModes.Packet)
            {
                var token = await _paypalOauthClient.GetAccessToken(_settingsProvider.GetPaypalClientId(), _settingsProvider.GetPaypalClientSecret(),
                                                                    new PaypalOauthClientOptions
                {
                    ApplicationMode = PaypalApplicationModes.sandbox
                });

                var payment = validationResult.Order.OrderPayment;
                var executePaymentResult = await _paypalClient.ExecutePayment(payment.TransactionId, new ExecutePaymentParameter
                {
                    AccessToken = token.AccessToken,
                    PayerId     = payment.PayerId
                });

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

                isPaymentReceived = true;
            }

            _commandSender.Send(new ReceiveOrderCommand
            {
                OrderId           = orderId,
                IsPaymentReceived = isPaymentReceived
            });

            return(new OkResult());
        }
        public async Task <IActionResult> AddShop([FromBody] JObject obj)
        {
            var subject = User.GetSubject();

            if (string.IsNullOrEmpty(subject))
            {
                var error = _responseBuilder.GetError(ErrorCodes.Request, ErrorDescriptions.TheSubjectCannotBeRetrieved);
                return(this.BuildResponse(error, HttpStatusCode.BadRequest));
            }

            return(await _addShopOperation.Execute(obj, this.Request, subject, this.GetCommonId()));
        }
Exemple #6
0
        public async Task <IActionResult> Execute(string id, string subject)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentNullException(nameof(id));
            }

            var order = await _orderRepository.Get(id);

            if (order == null)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Server, ErrorDescriptions.TheOrderDoesntExist);
                return(_controllerHelper.BuildResponse(System.Net.HttpStatusCode.NotFound, error));
            }

            if (order.Subject != subject && order.SellerId != subject)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Server, ErrorDescriptions.TheOrderCannotBeAccessedByYou);
                return(_controllerHelper.BuildResponse(System.Net.HttpStatusCode.BadRequest, error));
            }

            if (order.OrderPayment == null)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Server, ErrorDescriptions.TheOrderDoesntHavePayment);
                return(_controllerHelper.BuildResponse(System.Net.HttpStatusCode.BadRequest, error));
            }

            _halResponseBuilder.AddLinks(l => l.AddSelf("/" + Constants.RouteNames.OrderTransaction.Replace("{id}", id)));
            if (order.OrderPayment.PaymentMethod != OrderPayments.Paypal)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Server, ErrorDescriptions.ThePaymentMethodIsNotSupported);
                return(_controllerHelper.BuildResponse(System.Net.HttpStatusCode.InternalServerError, error));
            }

            var token = await _paypalOauthClient.GetAccessToken(_settingsProvider.GetPaypalClientId(),
                                                                _settingsProvider.GetPaypalClientSecret(),
                                                                new PaypalOauthClientOptions
            {
                ApplicationMode = PaypalApplicationModes.sandbox
            });

            var payment = await _paypalClient.GetPayment(new GetPaymentParameter
            {
                AccessToken = token.AccessToken,
                PaymentId   = order.OrderPayment.TransactionId
            });

            return(new OkObjectResult(_responseBuilder.GetPaypalTransaction(payment)));
        }
        public async Task <IActionResult> Execute(JObject jObj, string subject)
        {
            if (jObj == null)
            {
                throw new ArgumentNullException(nameof(jObj));
            }

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

            var command = _requestBuilder.GetAddServiceComment(jObj);

            command.CreateDateTime = DateTime.UtcNow;
            command.UpdateDateTime = DateTime.UtcNow;
            command.Id             = Guid.NewGuid().ToString();
            command.Subject        = subject;
            var validationResult = await _addCommentValidator.Validate(command);

            if (!validationResult.IsValid)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Request, validationResult.Message);
                return(_controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error));
            }
            _commandSender.Send(command);
            var obj = new { id = command.Id };

            return(new OkObjectResult(obj));
        }
Exemple #8
0
        public async Task <IActionResult> Execute(JObject jObj)
        {
            if (jObj == null)
            {
                throw new ArgumentNullException(nameof(jObj));
            }

            var parameter = _requestBuilder.GetUpsRatingsParameter(jObj);

            parameter.Credentials = new client.Params.UpsCredentials
            {
                LicenseNumber = _settingsProvider.GetUpsLicenseNumber(),
                UserName      = _settingsProvider.GetUpsUsername(),
                Password      = _settingsProvider.GetUpsPassword()
            };
            var result = await _upsClient.GetRatings(parameter);

            if (result.Response != null && result.Response.Error != null)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Request, result.Response.Error.ErrorDescription);
                return(_controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error));
            }

            return(new OkObjectResult(_responseBuilder.GetUpsRatings(result)));
        }
Exemple #9
0
        public async Task <IActionResult> Execute(JObject jObj)
        {
            if (jObj == null)
            {
                throw new ArgumentNullException(nameof(jObj));
            }

            var searchUpsLocations = _requestBuilder.GetLocationsParameter(jObj);

            searchUpsLocations.Credentials = new client.Params.UpsCredentials
            {
                LicenseNumber = Constants.UpsCredentials._accessLicenseNumber,
                UserName      = Constants.UpsCredentials._userName,
                Password      = Constants.UpsCredentials._password
            };
            var result = await _upsClient.GetLocations(searchUpsLocations);

            if (result.Response != null && result.Response.Error != null)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Request, result.Response.Error.ErrorDescription);
                return(_controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error));
            }

            return(new OkObjectResult(_responseBuilder.GetUpsLocations(result)));
        }
Exemple #10
0
        public async Task <IActionResult> Execute(string id, JObject jObj, string subject, string commonId, HttpRequest request)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentNullException(nameof(id));
            }

            if (jObj == null)
            {
                throw new ArgumentNullException(nameof(jObj));
            }

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

            var command = _requestBuilder.GetUpdateService(jObj);

            command.Id = id;
            var validationResult = await _updateServiceValidator.Execute(command, subject);

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

            var images = new List <string>();

            if (command.Images != null)
            {
                foreach (var image in command.Images)
                {
                    Uri uri = null;
                    if (Uri.TryCreate(image, UriKind.Absolute, out uri) && (uri.Scheme == "http" || uri.Scheme == "https"))
                    {
                        images.Add(image);
                        continue;
                    }

                    string path;
                    if (!AddImage(image, request, out path))
                    {
                        continue;
                    }

                    images.Add(path);
                }
            }

            command.CommonId       = commonId;
            command.Images         = images;
            command.UpdateDateTime = DateTime.UtcNow;
            _commandSender.Send(command);
            return(new OkResult());
        }
        public async Task <IActionResult> Execute(JObject obj, string subject, string commonId)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

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

            // 1. Check the request
            AddMessageCommand command = null;

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

            command.From = subject;
            var validationResult = await _addMessageValidator.Validate(command);

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

            command.Id             = Guid.NewGuid().ToString();
            command.CreateDateTime = DateTime.UtcNow;
            command.CommonId       = commonId;

            // 2. Send the command.
            var res = new { id = command.Id };

            _commandSender.Send(command);
            return(new OkObjectResult(res));
        }
        public async Task <IActionResult> Execute(string id, string subject)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentNullException(nameof(id));
            }

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

            var validationResult = await _validator.Validate(id, subject);

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

            if (!string.IsNullOrWhiteSpace(validationResult.Order.TrackingNumber))
            {
                var cancel = await _upsClient.Cancel(validationResult.Order.TrackingNumber,
                                                     new UpsCredentials
                {
                    LicenseNumber = _settingsProvider.GetUpsLicenseNumber(),
                    Password      = _settingsProvider.GetUpsPassword(),
                    UserName      = _settingsProvider.GetUpsUsername()
                });

                if (cancel.Response.ResponseStatusCode == "0")
                {
                    var error = _responseBuilder.GetError(ErrorCodes.Request, cancel.Response.Error.ErrorDescription);
                    return(_controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error));
                }
            }

            _commandSender.Send(new CancelOrderCommand
            {
                OrderId = id
            });
            return(new OkResult());
        }
Exemple #13
0
        public async Task <IActionResult> Execute(JObject jObj, string id, string subject, string commonId)
        {
            if (jObj == null)
            {
                throw new ArgumentNullException(nameof(jObj));
            }

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

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

            UpdateNotificationCommand command = null;

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

            command.Id       = id;
            command.CommonId = commonId;
            var validationResult = await _updateNotificationValidator.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());
        }
        public async Task <IActionResult> Execute(string orderId, string subject)
        {
            if (string.IsNullOrWhiteSpace(orderId))
            {
                throw new ArgumentNullException(nameof(orderId));
            }

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

            var validationResult = await _validator.Validate(orderId, subject);

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

            var token = await _paypalOauthClient.GetAccessToken(_settingsProvider.GetPaypalClientId(), _settingsProvider.GetPaypalClientSecret(),
                                                                new PaypalOauthClientOptions
            {
                ApplicationMode = PaypalApplicationModes.sandbox
            });

            var payment = await _paypalClient.GetPayment(new GetPaymentParameter
            {
                AccessToken = token.AccessToken,
                PaymentId   = validationResult.Order.OrderPayment.TransactionId
            });

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

            return(new OkObjectResult(_responseBuilder.GetPaypalPayment(payment)));
        }
        public async Task<IActionResult> Execute(JObject jObj, string subject, string commonId, HttpRequest request)
        {
            if (jObj == null)
            {
                throw new ArgumentNullException(nameof(jObj));
            }

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

            var command = _requestBuilder.GetAddService(jObj);
            var validationResult = await _addServiceValidator.Validate(command, subject);
            if (!validationResult.IsValid)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Request, validationResult.Message);
                return _controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error);
            }

            var images = new List<string>();
            if (command.Images != null)
            {
                foreach (var image in command.Images)
                {
                    string path;
                    if (!AddImage(image, request, out path))
                    {
                        continue;
                    }

                    images.Add(path);
                }
            }

            command.Id = Guid.NewGuid().ToString();
            command.CreateDateTime = DateTime.UtcNow;
            command.UpdateDateTime = DateTime.UtcNow;
            command.CommonId = commonId;
            command.Images = images;

            _commandSender.Send(command);
            var obj = new { id = command.Id };
            return new OkObjectResult(obj);
        }
Exemple #16
0
        public async Task <IActionResult> Execute(RemoveProductCommand removeProductCommand)
        {
            if (removeProductCommand == null)
            {
                throw new ArgumentNullException(nameof(removeProductCommand));
            }

            var validationResult = await _validator.Validate(removeProductCommand);

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

            _commandSender.Send(removeProductCommand);
            return(new OkResult());
        }
        public async Task<IActionResult> Execute(JObject jObj, string subject, string commonId)
        {
            if (jObj == null)
            {
                throw new ArgumentNullException(nameof(jObj));
            }

            var command = _requestBuilder.GetAddOrderLine(jObj);
            command.Subject = subject;
            command.CommonId = commonId;
            command.Quantity = 1;
            var validationResult = await _validator.Execute(command);
            if (!validationResult.IsValid)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Request, validationResult.Message);
                return _controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error);
            }

            command.Id = Guid.NewGuid().ToString();
            _commandSender.Send(command);
            var obj = new { id = command.Id };
            return new OkObjectResult(obj);
        }
        public async Task <IActionResult> Execute(JObject obj, HttpRequest request, string subject, string commonId)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

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

            // 1. Check the request
            AddShopCommand command = null;

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

            var validationResult = await _addShopValidator.Validate(command, subject);

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

            command.Id             = Guid.NewGuid().ToString();
            command.CreateDateTime = DateTime.UtcNow;
            command.UpdateDateTime = DateTime.UtcNow;
            command.Subject        = subject;
            command.CommonId       = commonId;
            if (!string.IsNullOrWhiteSpace(command.BannerImage))
            {
                string bannerImage = null;
                if (AddImage(command.BannerImage, request, "banner", out bannerImage))
                {
                    command.BannerImage = bannerImage;
                }
            }

            if (!string.IsNullOrWhiteSpace(command.ProfileImage))
            {
                string profileImage = null;
                if (AddImage(command.ProfileImage, request, "profile", out profileImage))
                {
                    command.ProfileImage = profileImage;
                }
            }

            var res = new { id = command.Id };

            _commandSender.Send(command);
            return(new OkObjectResult(res));
        }
Exemple #19
0
        public async Task <IActionResult> Execute(JObject jObj, string subject, string commonId, HttpRequest request)
        {
            if (jObj == null)
            {
                throw new ArgumentNullException(nameof(jObj));
            }

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

            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            // 1. Check the request
            AddProductCommand command = null;

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

            var validationResult = await _validator.Validate(command, subject);

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

            // 2. Add images
            var images = new List <string>();

            if (command.PartialImagesUrl != null)
            {
                foreach (var image in command.PartialImagesUrl)
                {
                    string path;
                    if (!AddImage(image, request, out path))
                    {
                        continue;
                    }

                    images.Add(path);
                }
            }

            command.Id               = Guid.NewGuid().ToString();
            command.CreateDateTime   = DateTime.UtcNow;
            command.UpdateDateTime   = DateTime.UtcNow;
            command.CommonId         = commonId;
            command.PartialImagesUrl = images;
            var res = new { id = command.Id };

            _commandSender.Send(command);
            return(new OkObjectResult(res));
        }
        public async Task <IActionResult> Execute(string orderId, string subject, string commonId, JObject jObj)
        {
            if (jObj == null)
            {
                throw new ArgumentNullException(nameof(jObj));
            }


            ConfirmOrderLabelPurchaseCommand command = null;

            try
            {
                command = _requestBuilder.GetConfirmOrderLabelPurchase(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(subject, command); // Validate the command.

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

            if (validationResult.Order.OrderParcel.Transporter != Transporters.Ups) // Only UPS transport mode is supported.
            {
                var error = _responseBuilder.GetError(ErrorCodes.Request, ErrorDescriptions.TheUpsIsOnlySupported);
                return(_controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error));
            }

            var acceptUpsShipment = await _upsClient.AcceptShip(new AcceptShipParameter // 1. Accept UPS shipment.
            {
                Credentials = new UpsCredentials
                {
                    LicenseNumber = _settingsProvider.GetUpsLicenseNumber(),
                    Password      = _settingsProvider.GetUpsPassword(),
                    UserName      = _settingsProvider.GetUpsUsername()
                },
                ShipmentDigest = validationResult.Order.ShipmentDigest
            });

            if (acceptUpsShipment.Response.ResponseStatusCode == "0")
            {
                var error = _responseBuilder.GetError(ErrorCodes.Request, acceptUpsShipment.Response.Error.ErrorDescription);
                return(_controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error));
            }

            var order = validationResult.Order;
            var token = await _paypalOauthClient.GetAccessToken(_settingsProvider.GetPaypalClientId(), _settingsProvider.GetPaypalClientSecret(),
                                                                new PaypalOauthClientOptions
            {
                ApplicationMode = PaypalApplicationModes.sandbox
            });

            if (order.OrderPayment.PaymentMethod == OrderPayments.Paypal) // Update transaction.
            {
                var transactionId   = order.OrderPayment.TransactionId;
                var newPrice        = new JObject();
                var newPriceDetails = new JObject();
                newPriceDetails.Add("shipping", order.ShippingPrice);
                newPriceDetails.Add("subtotal", order.TotalPrice);
                newPrice.Add("total", order.TotalPrice + order.ShippingPrice);
                newPrice.Add("currency", "EUR");
                newPrice.Add("details", newPriceDetails);
                var updatePayment = await _paypalClient.UpdatePayment(transactionId, new UpdatePaymentParameter
                {
                    AccessToken = token.AccessToken,
                    JsonPatches = new[]
                    {
                        new JsonPatch
                        {
                            Operation = JsonPatchOperations.replace,
                            Path      = "/transactions/0/amount",
                            Value     = newPrice
                        }
                    }
                });

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

            var acceptedPayment = await _paypalClient.ExecutePayment(command.TransactionId, new ExecutePaymentParameter // Accept the payment.
            {
                AccessToken = token.AccessToken,
                PayerId     = command.PayerId
            });

            if (!acceptedPayment.IsValid)
            {
                await _upsClient.Cancel(acceptUpsShipment.ShipmentResults.ShipmentIdentificationNumber, new UpsCredentials
                {
                    LicenseNumber = _settingsProvider.GetUpsLicenseNumber(),
                    Password      = _settingsProvider.GetUpsPassword(),
                    UserName      = _settingsProvider.GetUpsUsername()
                });

                var error = _responseBuilder.GetError(ErrorCodes.Request, acceptedPayment.ErrorResponse.Message);
                return(_controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error));
            }

            command.TrackingNumber = acceptUpsShipment.ShipmentResults.PackageResults.TrackingNumber;
            _commandSender.Send(command);
            return(new OkResult());
        }
Exemple #21
0
        public async Task <IActionResult> Execute(JObject jObj, HttpRequest request, string id, string subject, string commonId)
        {
            if (jObj == null)
            {
                throw new ArgumentNullException(nameof(jObj));
            }

            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

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

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

            // 1. Check the request
            UpdateShopCommand command = null;

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

            // 2. Add image
            command.Id             = id;
            command.UpdateDateTime = DateTime.UtcNow;
            command.CommonId       = commonId;
            if (command.ProductCategories != null)
            {
                foreach (var productCategory in command.ProductCategories)
                {
                    if (string.IsNullOrWhiteSpace(productCategory.Id))
                    {
                        productCategory.Id = Guid.NewGuid().ToString();
                    }
                }
            }

            if (command.ProductFilters != null)
            {
                foreach (var productFilter in command.ProductFilters)
                {
                    if (string.IsNullOrWhiteSpace(productFilter.Id))
                    {
                        productFilter.Id = Guid.NewGuid().ToString();
                    }

                    if (productFilter.Values != null)
                    {
                        foreach (var value in productFilter.Values)
                        {
                            if (string.IsNullOrWhiteSpace(value.Id))
                            {
                                value.Id = Guid.NewGuid().ToString();
                            }
                        }
                    }
                }
            }

            var validationResult = await _updateShopValidator.Validate(command, subject);

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

            if (!string.IsNullOrWhiteSpace(command.BannerImage))
            {
                string bannerImage = null;
                if (AddImage(command.BannerImage, request, "banner", out bannerImage))
                {
                    command.BannerImage = bannerImage;
                }
                else
                {
                    command.BannerImage = validationResult.Shop.BannerImage;
                }
            }
            else
            {
                command.BannerImage = validationResult.Shop.BannerImage;
            }

            if (!string.IsNullOrWhiteSpace(command.ProfileImage))
            {
                string profileImage = null;
                if (AddImage(command.ProfileImage, request, "profile", out profileImage))
                {
                    command.ProfileImage = profileImage;
                }
                else
                {
                    command.ProfileImage = validationResult.Shop.ProfileImage;
                }
            }
            else
            {
                command.ProfileImage = validationResult.Shop.ProfileImage;
            }

            _commandSender.Send(command);
            return(new OkResult());
        }
Exemple #22
0
        public async Task <IActionResult> Execute(string id, string subject, JObject jObj)
        {
            if (jObj == null)
            {
                throw new ArgumentNullException(nameof(jObj));
            }

            var command = _requestBuilder.GetPurchaseOrderLabelCommand(jObj);

            command.OrderId = id;
            var validationResult = await _validator.Validate(subject, command);

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

            var token = await _paypalOauthClient.GetAccessToken(_settingsProvider.GetPaypalClientId(), _settingsProvider.GetPaypalClientSecret(),
                                                                new PaypalOauthClientOptions
            {
                ApplicationMode = PaypalApplicationModes.sandbox
            });

            var payment = await _paypalClient.CreatePayment(new CreatePaymentParameter
            {
                AccessToken = token.AccessToken,
                Intent      = IntentPayments.sale,
                Payer       = new PaypalPayer
                {
                    PaymentMethod = PaypalPaymentMethods.Paypal,
                    Email         = validationResult.PayerPaypalEmail
                },
                Transactions = new[]
                {
                    new PaymentTransaction
                    {
                        Currency = Constants.Currency,
                        Total    = validationResult.ShippingPrice,
                        Shipping = validationResult.ShippingPrice,
                        Payee    = new PaypalPayee
                        {
                            Email = _settingsProvider.GetPaypalEmail()
                        }
                    }
                },
                CancelUrl = $"{_settingsProvider.GetBaseWebsite()}/cancelpurchaselabel",
                ReturnUrl = $"{_settingsProvider.GetBaseWebsite()}/acceptpurchaselabel"
            });

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

            var approvalUrl = payment.Links.FirstOrDefault(l => l.Rel == "approval_url").Href;

            command.TrackingNumber = validationResult.TrackingNumber;
            command.ShippingPrice  = validationResult.ShippingPrice;
            command.ShipmentDigest = validationResult.ShipmentDigest;
            var order = validationResult.Order;

            order.ShippingPrice      = command.ShippingPrice;
            order.ShipmentDigest     = command.ShipmentDigest;
            order.OrderParcel.Height = command.ParcelSize.Height;
            order.OrderParcel.Length = command.ParcelSize.Length;
            order.OrderParcel.Weight = command.ParcelSize.Weight;
            order.OrderParcel.Width  = command.ParcelSize.Width;
            await _orderRepository.Update(order);

            var res = new JObject();

            res.Add("approval_url", approvalUrl);
            res.Add("shipping_price", command.ShippingPrice);
            return(new OkObjectResult(res));
        }
        public async Task <IActionResult> Status()
        {
            var subject = User.GetSubject();

            if (string.IsNullOrEmpty(subject))
            {
                var error = _responseBuilder.GetError(ErrorCodes.Request, ErrorDescriptions.TheSubjectCannotBeRetrieved);
                return(this.BuildResponse(error, HttpStatusCode.BadRequest));
            }

            return(await _getOrderStatusOperation.Execute(subject));
        }