Пример #1
0
        public virtual async Task <IHttpActionResult> UpdateRecurringOrderCartAddress([FromBody] UpdateRecurringOrderCartAddressRequest request)
        {
            if (request == null)
            {
                return(BadRequest("Missing Request Body"));
            }
            if (request.ShippingAddressId == null)
            {
                return(BadRequest("Missing Request Body"));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var param = new UpdateRecurringOrderCartShippingAddressParam()
            {
                CultureInfo                  = ComposerContext.CultureInfo,
                ScopeId                      = ComposerContext.Scope,
                CartName                     = request.cartName,
                CustomerId                   = ComposerContext.CustomerId,
                ShippingAddressId            = request.ShippingAddressId.ToGuid(),
                BaseUrl                      = RequestUtils.GetBaseUrl(Request).ToString(),
                UseSameForShippingAndBilling = request.UseSameForShippingAndBilling
            };

            if (!string.IsNullOrEmpty(request.BillingAddressId))
            {
                param.BillingAddressId = request.BillingAddressId.ToGuid();
            }

            var results = await RecurringOrderCartsService.UpdateRecurringOrderCartShippingAddressAsync(param).ConfigureAwait(false);

            return(Ok(results));
        }
        public virtual async Task <CartViewModel> UpdateRecurringOrderCartShippingAddressAsync(UpdateRecurringOrderCartShippingAddressParam param)
        {
            if (!RecurringOrdersSettings.Enabled)
            {
                return(GetEmptyRecurringOrderCartViewModel());
            }

            if (param == null)
            {
                throw new ArgumentNullException(nameof(param), ArgumentNullMessageFormatter.FormatErrorMessage(nameof(param)));
            }
            if (param.ShippingAddressId == null)
            {
                throw new ArgumentNullException(nameof(param), ArgumentNullMessageFormatter.FormatErrorMessage(nameof(param.ShippingAddressId)));
            }

            var cart = await CartRepository.GetCartAsync(new GetCartParam
            {
                BaseUrl     = param.BaseUrl,
                Scope       = param.ScopeId,
                CultureInfo = param.CultureInfo,
                CustomerId  = param.CustomerId,
                CartName    = param.CartName
            }).ConfigureAwait(false);

            var shipment   = cart.Shipments.First();
            var newAddress = await AddressRepository.GetAddressByIdAsync(param.ShippingAddressId);

            if (newAddress == null)
            {
                throw new InvalidOperationException("Address not found");
            }
            shipment.Address = newAddress;

            var payment = cart.Payments.First();

            if (payment == null)
            {
                throw new InvalidOperationException("No payment");
            }

            if (param.UseSameForShippingAndBilling)
            {
                payment.BillingAddress = newAddress;
            }
            else if (param.BillingAddressId != Guid.Empty)
            {
                var newbillingAddress = await AddressRepository.GetAddressByIdAsync(param.BillingAddressId);

                if (newbillingAddress == null)
                {
                    throw new InvalidOperationException("Address not found");
                }
                payment.BillingAddress   = newbillingAddress;
                payment.BillingAddressId = newbillingAddress.Id;
            }

            var updatedCart = await CartRepository.UpdateCartAsync(UpdateCartParamFactory.Build(cart));

            var vm = await CreateCartViewModelAsync(new CreateRecurringOrderCartViewModelParam
            {
                Cart        = updatedCart,
                CultureInfo = param.CultureInfo,
                IncludeInvalidCouponsMessages = false,
                BaseUrl = param.BaseUrl
            });

            return(vm);
        }