public ManagerResponse <SubmitVisitorOrderResult, CommerceOrder> SubmitVisitorOrder(string userId, SubmitOrderInputModel inputModel)
        {
            Assert.ArgumentNotNull(inputModel, nameof(inputModel));

            var errorResult = new SubmitVisitorOrderResult {
                Success = false
            };

            var response = CartManager.GetCart(userId, true);

            if (!response.ServiceProviderResult.Success || response.Result == null)
            {
                response.ServiceProviderResult.SystemMessages.ToList().ForEach(m => errorResult.SystemMessages.Add(m));
                return(new ManagerResponse <SubmitVisitorOrderResult, CommerceOrder>(errorResult, null));
            }

            var cart = (CommerceCart)response.ServiceProviderResult.Cart;

            if (cart.Lines.Count == 0)
            {
                errorResult.SystemMessages.Add(new SystemMessage
                {
                    Message = DictionaryPhraseRepository.Current.Get("/System Messages/Orders/Submit Order Has Empty Cart", "Cannot submit and order with an empty cart.")
                });
                return(new ManagerResponse <SubmitVisitorOrderResult, CommerceOrder>(errorResult, null));
            }

            cart.Email = inputModel.UserEmail;

            var request = new SubmitVisitorOrderRequest(cart);

            RefreshCartOnOrdersRequest(request);
            errorResult = OrderServiceProvider.SubmitVisitorOrder(request);
            if (errorResult.Success && errorResult.Order != null && errorResult.CartWithErrors == null)
            {
                CartCacheHelper.InvalidateCartCache(userId);

                try
                {
                    var wasEmailSent = MailManager.SendMail("PurchaseConfirmation", inputModel.UserEmail, string.Join(" ", cart.Parties.FirstOrDefault()?.FirstName, cart.Parties.FirstOrDefault()?.LastName), errorResult.Order.TrackingNumber, errorResult.Order.OrderDate, string.Join(", ", cart.Lines.Select(x => x.Product.ProductName)), cart.Total.Amount.ToCurrency(cart.Total.CurrencyCode));
                    if (!wasEmailSent)
                    {
                        var message = DictionaryPhraseRepository.Current.Get("/System Messages/Orders/Could Not Send Email Error", "Sorry, the email could not be sent");
                        //errorResult.SystemMessages.Add(new SystemMessage(message));
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("Could not send Purchase Confirmation mail message", ex, this);
                    var message = DictionaryPhraseRepository.Current.Get("/System Messages/Orders/Could Not Send Email Error", "Sorry, the email could not be sent");
                    errorResult.SystemMessages.Add(new SystemMessage(message));
                }
            }

            errorResult.WriteToSitecoreLog();
            return(new ManagerResponse <SubmitVisitorOrderResult, CommerceOrder>(errorResult,
                                                                                 errorResult.Order as CommerceOrder));
        }
Esempio n. 2
0
        public ManagerResponse <GetPaymentMethodsResult, IEnumerable <PaymentMethod> > GetPaymentMethods(string userId, PaymentOption paymentOption)
        {
            Assert.ArgumentNotNull(paymentOption, nameof(paymentOption));

            var result = new GetPaymentMethodsResult {
                Success = false
            };
            var cartResult = CartManager.GetCart(userId);

            if (!cartResult.ServiceProviderResult.Success || cartResult.Result == null)
            {
                result.SystemMessages.ToList().AddRange(cartResult.ServiceProviderResult.SystemMessages);
                return(new ManagerResponse <GetPaymentMethodsResult, IEnumerable <PaymentMethod> >(result, null));
            }

            var request = new GetPaymentMethodsRequest(cartResult.Result, paymentOption);

            result = PaymentServiceProvider.GetPaymentMethods(request);
            result.WriteToSitecoreLog();

            return(new ManagerResponse <GetPaymentMethodsResult, IEnumerable <PaymentMethod> >(result, result.PaymentMethods.ToList()));
        }
Esempio n. 3
0
        public ManagerResponse <GetPaymentOptionsResult, IEnumerable <PaymentOption> > GetPaymentOptions(string userId)
        {
            var result = new GetPaymentOptionsResult {
                Success = false
            };
            var cartResult = CartManager.GetCart(userId);

            if (!cartResult.ServiceProviderResult.Success || cartResult.Result == null)
            {
                result.SystemMessages.ToList().AddRange(cartResult.ServiceProviderResult.SystemMessages);
                return(new ManagerResponse <GetPaymentOptionsResult, IEnumerable <PaymentOption> >(result, null));
            }

            if (StorefrontContext.Current == null)
            {
                throw new InvalidOperationException("Cannot be called without a valid storefront context.");
            }
            var request = new GetPaymentOptionsRequest(StorefrontContext.Current.ShopName, cartResult.Result);

            result = PaymentServiceProvider.GetPaymentOptions(request);
            result.WriteToSitecoreLog();

            return(new ManagerResponse <GetPaymentOptionsResult, IEnumerable <PaymentOption> >(result, result.PaymentOptions.ToList()));
        }