//public static CheckOutResult GetPayAndShipTypeList(CheckOutContext context, int customerSysNo)
        //{
        //    CheckOutResult result = new CheckOutResult();

        //    CheckOutContext newCheckoutContenxt = new CheckOutContext();
        //    if (context != null)
        //    {
        //        newCheckoutContenxt = context.Clone();
        //    }

        //    MemberInfo memberInfo = CustomerDA.GetCustomerInfo(customerSysNo);

        //    //取得用户选择的收货地址信息
        //    var custShippingAddrResult = GetCustomerShippingAddressList(newCheckoutContenxt, customerSysNo);
        //    result.SelShippingAddress = custShippingAddrResult.SelShippingAddress;

        //    #region 支付类别选择
        //    result.PaymentCategoryList = GetAllPaymentCategoryList();
        //    //优先取context指定的支付类型
        //    //其次取配送地址指定的支付类型
        //    if (!result.PaymentCategoryList.Exists(x =>
        //    {
        //        if (x.Key.ToString() == newCheckoutContenxt.PaymentCategoryID
        //            || x.Key == result.SelShippingAddress.PaymentCategoryID)
        //        {
        //            result.SelPaymentCategoryID = x.Key;
        //            return true;
        //        }
        //        return false;
        //    }))
        //    {
        //        //都没有就取第一条支付类型
        //        result.SelPaymentCategoryID = result.PaymentCategoryList.First().Key;
        //    }
        //    #endregion

        //    #region 配送方式选择
        //    //step1 取得配送地址支持的所有配送方式
        //    var shipTypeList = ShipTypeFacade.GetSupportedShipTypeList(result.SelShippingAddress.ReceiveAreaSysNo, null);
        //    //step2 如果不存在支持货到付款的配送方式, 则移除掉货到付款支付类别
        //    if (shipTypeList.Count(x => x.IsPayWhenRecv) <= 0)
        //    {
        //        result.PaymentCategoryList = result.PaymentCategoryList.FindAll(x => x.Key == (int)PaymentCategory.OnlinePay);
        //        result.SelPaymentCategoryID = (int)PaymentCategory.OnlinePay;
        //    }
        //    //step3 如果选择的是货到付款,则移除掉不支持货到付款的配送方式
        //    if (result.SelPaymentCategoryID == (int)PaymentCategory.PayWhenRecv)
        //    {
        //        result.ShipTypeList = shipTypeList.Where(x => x.IsPayWhenRecv).ToList();
        //        //step4 移除掉不支持货到付款的配送方式后没有可用的配送方式时,系统自动选择在线支付
        //        if (result.ShipTypeList.Count <= 0)
        //        {
        //            result.ShipTypeList = shipTypeList;
        //            result.PaymentCategoryList= result.PaymentCategoryList.FindAll(x =>x.Key==(int)PaymentCategory.OnlinePay);
        //            result.SelPaymentCategoryID=(int)PaymentCategory.OnlinePay;
        //        }
        //    }
        //    else
        //    {
        //        result.ShipTypeList = shipTypeList;
        //    }

        //    //优先取context指定的配送方式
        //    result.SelShipType = result.ShipTypeList.Find(x => x.ShipTypeSysNo.ToString() == newCheckoutContenxt.ShipTypeID);
        //    //其次取配送地址指定的配送方式
        //    if (result.SelShipType == null && result.SelShippingAddress != null)
        //    {
        //        result.SelShipType = result.ShipTypeList.Find(x => x.ShipTypeSysNo == result.SelShippingAddress.ShipTypeSysNo);
        //    }
        //    //都没有就取第一条配送方式
        //    if (result.SelShipType == null && result.ShipTypeList.Count > 0)
        //    {
        //        result.SelShipType = result.ShipTypeList.First();
        //    }
        //    result.ShipTypeList = EnsureNotNullObject(result.ShipTypeList);
        //    result.SelShipType = EnsureNotNullObject(result.SelShipType);
        //    #endregion

        //    #region 支付方式选择
        //    result.PayTypeList = GetAllPayTypeList();
        //    if (result.SelPaymentCategoryID == (int)PaymentCategory.PayWhenRecv)
        //    {
        //        result.PayTypeList = result.PayTypeList.FindAll(x => x.IsPayWhenRecv == 1);
        //    }
        //    //优先取用户上次下单使用的支付方式
        //    result.SelPayType = result.PayTypeList.Find(x => x.PayTypeID == memberInfo.ExtendInfo.LastPayTypeSysNo);
        //    if (result.SelPayType == null && result.PayTypeList.Count > 0)
        //    {
        //        result.SelPayType = result.PayTypeList.First();
        //    }
        //    if (result.SelPayType != null && result.PayTypeList.Count > 0)
        //    {
        //        var cateId = result.SelPayType.IsPayWhenRecv == 1 ? (int)PaymentCategory.PayWhenRecv : (int)PaymentCategory.OnlinePay;
        //        var isPayWhenRecvValue = result.SelPaymentCategoryID == (int)PaymentCategory.PayWhenRecv ? 1 : 0;
        //        //如果上次下单用户使用的支付方式类型跟本次下单选择的支付类型不一致
        //        //则选择符合当前选择的支付类型的第一个支付方式
        //        if (cateId != result.SelPaymentCategoryID)
        //        {
        //            result.SelPayType = result.PayTypeList.Where(x => x.IsPayWhenRecv == isPayWhenRecvValue).First();
        //        }
        //    }
        //    result.PayTypeList = EnsureNotNullObject(result.PayTypeList);
        //    result.SelPayType = EnsureNotNullObject(result.SelPayType);
        //    #endregion

        //    return result;
        //}

        public static CheckOutResult GetCustomerShippingAddressList(CheckOutContext context, int customerSysNo)
        {
            CheckOutResult             result                      = new CheckOutResult();
            ShippingContactInfo        curShippingAddress          = null;
            List <ShippingContactInfo> customerShippingAddressList = CustomerShippingAddresssFacade.GetCustomerShippingAddressList(customerSysNo);

            if (customerShippingAddressList != null && customerShippingAddressList.Count > 0)
            {
                if (context == null)
                {
                    curShippingAddress = customerShippingAddressList.Find(x => x.IsDefault);
                }
                else
                {
                    curShippingAddress = customerShippingAddressList.Find(x => x.SysNo == context.ShippingAddressID);
                }
                if (curShippingAddress == null)
                {
                    curShippingAddress = customerShippingAddressList.First();
                }
            }
            result.ShippingAddressList = EnsureNotNullObject(customerShippingAddressList);
            result.SelShippingAddress  = EnsureNotNullObject(curShippingAddress);

            return(result);
        }
        /// <summary>
        /// 提交checkout
        /// </summary>
        /// <param name="context">checkout数据上下文</param>
        /// <param name="shoppingCart">购物车数据</param>
        /// <param name="customerSysNo">当前登录用户编号</param>
        /// <param name="source">下单来源</param>
        /// <returns>提交checkout结果</returns>
        public static CheckOutResult SubmitCheckout(CheckOutContext context, ShoppingCart shoppingCart, int customerSysNo, SOSource orderSource)
        {
            CheckOutResult result = PreCheckAndBuild(context, shoppingCart, customerSysNo, (int)orderSource, SOPipelineProcessor.CreateSO);

            //订单创建发送邮件
            if (result.OrderProcessResult.HasSucceed)
            {
                SalesOrderMailSuccessful(result.OrderProcessResult.ReturnData);
            }

            return(result);
        }
        private static CheckOutResult BuildFailedCheckOutResult(string errorMsg)
        {
            CheckOutResult result = new CheckOutResult()
            {
                PayTypeList         = new List <PayTypeInfo>(),
                ShippingAddressList = new List <ShippingContactInfo>()
            };

            result.HasSucceed = false;
            result.ErrorMessages.Add(errorMsg);

            return(result);
        }
        public static List <ECommerce.Entity.Member.CustomerCouponInfo> GetCustomerPlatformCouponCode(CheckOutResult model)
        {
            int customerID    = model.Customer.SysNo;
            var item          = model.OrderProcessResult.ReturnData.OrderItemGroupList.FirstOrDefault();
            int merchantSysNo = item == null ? 1 : item.MerchantSysNo;

            return(ECommerce.SOPipeline.Impl.CouponCalculator.GetCustomerPlatformCouponCode(customerID, merchantSysNo));
        }
        public static CheckOutResult GetPayAndShipTypeList(CheckOutContext context, int customerSysNo, ShoppingCart shoppingCart)
        {
            CheckOutResult result = new CheckOutResult();

            CheckOutContext newCheckoutContenxt = new CheckOutContext();

            if (context != null)
            {
                newCheckoutContenxt = context.Clone();
            }

            MemberInfo memberInfo = CustomerDA.GetCustomerInfo(customerSysNo);

            //取得用户选择的收货地址信息
            var custShippingAddrResult = GetCustomerShippingAddressList(newCheckoutContenxt, customerSysNo);

            result.SelShippingAddress = custShippingAddrResult.SelShippingAddress;

            #region 支付类别选择
            result.PaymentCategoryList = GetAllPaymentCategoryList();
            //优先取context指定的支付类型
            //其次取配送地址指定的支付类型
            if (!result.PaymentCategoryList.Exists(x =>
            {
                if (x.Key.ToString() == newCheckoutContenxt.PaymentCategoryID ||
                    x.Key == result.SelShippingAddress.PaymentCategoryID)
                {
                    result.SelPaymentCategoryID = x.Key;
                    return(true);
                }
                return(false);
            }))
            {
                //都没有就取第一条支付类型
                result.SelPaymentCategoryID = result.PaymentCategoryList.First().Key;
            }
            #endregion



            #region 配送方式选择
            //step1 取得配送地址支持的所有配送方式
            List <ShipTypeInfo> ShipTypeInfoList = new List <ShipTypeInfo>();
            List <ShipTypeInfo> shipTypeList     = new List <ShipTypeInfo>();
            foreach (ShoppingItemGroup ShoppingItemGroup in shoppingCart.ShoppingItemGroupList)
            {
                foreach (ShoppingItem ShoppingItem in ShoppingItemGroup.ShoppingItemList)
                {
                    ProductBasicInfo    basicInfo   = ProductFacade.GetProductBasicInfoBySysNo(ShoppingItem.ProductSysNo);
                    List <ShipTypeInfo> ShipTypeNew = ShipTypeFacade.Checkout_GetStockShippingType(basicInfo.VendorSysno);
                    if (ShipTypeNew.Count > 0)
                    {
                        if (ShipTypeInfoList.Count <= 0)
                        {
                            ShipTypeInfoList.AddRange(ShipTypeNew);
                            shipTypeList.AddRange(ShipTypeNew);
                        }
                        else
                        {
                            shipTypeList = new List <ShipTypeInfo>();
                            for (int i = 0; i < ShipTypeInfoList.Count; i++)
                            {
                                for (int j = 0; j < ShipTypeNew.Count; j++)
                                {
                                    if (ShipTypeInfoList[i].ShipTypeName == ShipTypeNew[j].ShipTypeName)
                                    {
                                        shipTypeList.Add(ShipTypeInfoList[i]);
                                    }
                                }
                            }
                            if (shipTypeList.Count <= 0)
                            {
                                result.ErrorMessages.Add("不同商家的商品,没有相同的配送方式,请分开下单!");
                                break;
                            }
                            else
                            {
                                ShipTypeInfoList = shipTypeList;
                            }
                        }
                    }
                    else
                    {
                        string error = string.Format("商品【{0}】没有对应配送方式,暂时无法为您配送!", basicInfo.ProductName);
                        result.ErrorMessages.Add(error);
                        result.ShipTypeList = null;
                        break;
                    }
                }
                if (result.ErrorMessages.Count > 0)
                {
                    break;
                }
            }
            //var shipTypeList = ShipTypeFacade.GetSupportedShipTypeList(result.SelShippingAddress.ReceiveAreaSysNo, null);
            //step2 如果不存在支持货到付款的配送方式, 则移除掉货到付款支付类别
            if (shipTypeList.Count(x => x.IsPayWhenRecv) <= 0)
            {
                result.PaymentCategoryList  = result.PaymentCategoryList.FindAll(x => x.Key == (int)PaymentCategory.OnlinePay);
                result.SelPaymentCategoryID = (int)PaymentCategory.OnlinePay;
            }
            //step3 如果选择的是货到付款,则移除掉不支持货到付款的配送方式
            if (result.SelPaymentCategoryID == (int)PaymentCategory.PayWhenRecv)
            {
                result.ShipTypeList = shipTypeList.Where(x => x.IsPayWhenRecv).ToList();
                //step4 移除掉不支持货到付款的配送方式后没有可用的配送方式时,系统自动选择在线支付
                if (result.ShipTypeList.Count <= 0)
                {
                    result.ShipTypeList         = shipTypeList;
                    result.PaymentCategoryList  = result.PaymentCategoryList.FindAll(x => x.Key == (int)PaymentCategory.OnlinePay);
                    result.SelPaymentCategoryID = (int)PaymentCategory.OnlinePay;
                }
            }
            else
            {
                result.ShipTypeList = shipTypeList;
            }

            //优先取context指定的配送方式
            result.SelShipType = result.ShipTypeList.Find(x => x.ShipTypeSysNo.ToString() == newCheckoutContenxt.ShipTypeID);
            //其次取配送地址指定的配送方式
            if (result.SelShipType == null && result.SelShippingAddress != null)
            {
                result.SelShipType = result.ShipTypeList.Find(x => x.ShipTypeSysNo == result.SelShippingAddress.ShipTypeSysNo);
            }
            //都没有就取第一条配送方式
            if (result.SelShipType == null && result.ShipTypeList.Count > 0)
            {
                result.SelShipType = result.ShipTypeList.First();
            }
            result.ShipTypeList = EnsureNotNullObject(result.ShipTypeList);
            result.SelShipType  = EnsureNotNullObject(result.SelShipType);
            #endregion

            #region 支付方式选择
            result.PayTypeList = GetAllPayTypeList();
            if (result.SelPaymentCategoryID == (int)PaymentCategory.PayWhenRecv)
            {
                result.PayTypeList = result.PayTypeList.FindAll(x => x.IsPayWhenRecv == 1);
            }
            //优先取用户上次下单使用的支付方式
            result.SelPayType = result.PayTypeList.Find(x => x.PayTypeID == memberInfo.ExtendInfo.LastPayTypeSysNo);
            if (result.SelPayType == null && result.PayTypeList.Count > 0)
            {
                result.SelPayType = result.PayTypeList.First();
            }
            if (result.SelPayType != null && result.PayTypeList.Count > 0)
            {
                var cateId             = result.SelPayType.IsPayWhenRecv == 1 ? (int)PaymentCategory.PayWhenRecv : (int)PaymentCategory.OnlinePay;
                var isPayWhenRecvValue = result.SelPaymentCategoryID == (int)PaymentCategory.PayWhenRecv ? 1 : 0;
                //如果上次下单用户使用的支付方式类型跟本次下单选择的支付类型不一致
                //则选择符合当前选择的支付类型的第一个支付方式
                if (cateId != result.SelPaymentCategoryID)
                {
                    result.SelPayType = result.PayTypeList.Where(x => x.IsPayWhenRecv == isPayWhenRecvValue).First();
                }

                if (context != null && context.PayTypeID.HasValue)
                {
                    result.SelPayType = result.PayTypeList.Where(x => x.PayTypeID == context.PayTypeID.Value).First();
                }
            }
            result.PayTypeList = EnsureNotNullObject(result.PayTypeList);
            result.SelPayType  = EnsureNotNullObject(result.SelPayType);
            #endregion

            return(result);
        }
 private static void SetCheckoutResult(OrderPipelineProcessResult orderProcessResult, CheckOutResult checkoutResult, CheckOutContext context)
 {
     checkoutResult.OrderProcessResult = orderProcessResult;
     checkoutResult.HasSucceed         = orderProcessResult.HasSucceed;
     checkoutResult.ShoppingItemParam  = context.ShoppingItemParam;
     if (orderProcessResult.ErrorMessages != null)
     {
         checkoutResult.ErrorMessages = orderProcessResult.ErrorMessages.SelectMany(msg =>
         {
             if (!String.IsNullOrWhiteSpace(msg))
             {
                 return(msg.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList());
             }
             return(new List <String>(0));
         })
                                        .ToList();
         checkoutResult.ErrorMessages.RemoveAll(x => String.IsNullOrWhiteSpace(x));
     }
     if (orderProcessResult != null && orderProcessResult.ReturnData != null)
     {
         //优惠券使用情况
         checkoutResult.ApplyCouponCode   = orderProcessResult.ReturnData.CouponCode;
         checkoutResult.ApplyCouponName   = orderProcessResult.ReturnData.CouponName;
         checkoutResult.ApplyedCouponDesc = orderProcessResult.ReturnData.CouponErrorDesc;
         //积分使用情况
         checkoutResult.UsePointPay     = orderProcessResult.ReturnData.PointPay;
         checkoutResult.MaxPointPay     = orderProcessResult.ReturnData.MaxPointPay;
         checkoutResult.UsePointPayDesc = orderProcessResult.ReturnData.UsePointPayDesc;
         //礼品卡使用情况
         checkoutResult.ApplyedGiftCardDesc = orderProcessResult.ReturnData.GiftCardErrorDesc;
         checkoutResult.BindingGiftCardList = orderProcessResult.ReturnData.BindingGiftCardList;
         if (orderProcessResult.ReturnData.GiftCardList != null)
         {
             checkoutResult.ApplyedGiftCardList = orderProcessResult.ReturnData.GiftCardList.Select(g =>
             {
                 GiftCardInfo giftCardInfo = (GiftCardInfo)g.Clone();
                 giftCardInfo.Password     = ConfuseGiftCardPassword(g.Password, orderProcessResult.ReturnData.Customer.SysNo);
                 return(giftCardInfo);
             }).ToList();
         }
     }
 }
        /// <summary>
        /// 构造checkout对象
        /// </summary>
        /// <param name="context">checkout数据上下文</param>
        /// <param name="shoppingCart">购物车数据</param>
        /// <param name="customerSysNo">当前登录用户编号</param>
        /// <returns>checkout对象</returns>
        public static CheckOutResult BuildCheckOut(CheckOutContext context, ShoppingCart shoppingCart, int customerSysNo)
        {
            CheckOutResult result = PreCheckAndBuild(context, shoppingCart, customerSysNo, -1, SOPipelineProcessor.BuildCheckOut);

            return(result);
        }
        private static CheckOutResult PreCheckAndBuild(CheckOutContext context, ShoppingCart shoppingCart, int customerSysNo, int orderSource
                                                       , Func <OrderInfo, OrderPipelineProcessResult> action)
        {
            CheckOutResult result = new CheckOutResult();

            MemberInfo memberInfo = CustomerDA.GetCustomerInfo(customerSysNo);

            CheckOutContext newCheckoutContenxt = new CheckOutContext();

            if (context != null)
            {
                newCheckoutContenxt = context.Clone();
            }

            CustomerInfo customerInfo = new CustomerInfo()
            {
                AccountBalance   = memberInfo.ValidPrepayAmt,
                AccountPoint     = memberInfo.ValidScore,
                CustomerRank     = (int)memberInfo.CustomerRank,
                ID               = memberInfo.CustomerID,
                SysNo            = memberInfo.SysNo,
                Name             = memberInfo.CustomerName,
                IsEmailConfirmed = memberInfo.IsEmailConfirmed,
                IsPhoneValided   = memberInfo.IsPhoneValided,
                CellPhone        = memberInfo.CellPhone,
                SocietyID        = memberInfo.SocietyID
            };

            result.Customer = customerInfo;
            //用户个人实名认证信息
            result.CustomerAuthenticationInfo = CustomerDA.GetCustomerAuthenticationInfo(customerSysNo);

            //用户购物发票信息
            result.CustomerInvoiceInfo = CustomerDA.GetCustomerInvoiceInfo(customerSysNo);
            if (result.CustomerInvoiceInfo == null)
            {
                result.CustomerInvoiceInfo = new Entity.Member.CustomerInvoiceInfo()
                {
                    CustomerSysNo = customerSysNo, InvoiceTitle = customerInfo.Name
                };
            }

            //收货地址
            var custShippingAddressListResult = GetCustomerShippingAddressList(context, customerSysNo);

            result.ShippingAddressList = custShippingAddressListResult.ShippingAddressList;
            result.SelShippingAddress  = custShippingAddressListResult.SelShippingAddress;

            //支付方式&配送方式
            var payAndShipTypeResult = GetPayAndShipTypeList(context, customerSysNo, shoppingCart);

            result.PaymentCategoryList  = payAndShipTypeResult.PaymentCategoryList;
            result.SelPaymentCategoryID = payAndShipTypeResult.SelPaymentCategoryID;
            result.PayTypeList          = payAndShipTypeResult.PayTypeList;
            result.SelPayType           = payAndShipTypeResult.SelPayType;
            result.ShipTypeList         = payAndShipTypeResult.ShipTypeList;
            result.SelShipType          = payAndShipTypeResult.SelShipType;

            //根据CheckOutContext 进一步构造shoppingCartResult.ReturnData对象
            OrderInfo preOrderInfo = SOPipelineProcessor.Convert2OrderInfo(shoppingCart);

            preOrderInfo.Customer               = customerInfo;
            preOrderInfo.PayTypeID              = result.SelPayType.PayTypeID.ToString();
            preOrderInfo.ShipTypeID             = result.SelShipType.ShipTypeSysNo.ToString();
            preOrderInfo.Memo                   = newCheckoutContenxt.OrderMemo;
            preOrderInfo.CouponCode             = newCheckoutContenxt.PromotionCode;
            preOrderInfo.ChannelID              = shoppingCart.ChannelID;
            preOrderInfo.LanguageCode           = shoppingCart.LanguageCode;
            preOrderInfo.OrderSource            = orderSource;
            preOrderInfo.VirualGroupBuyOrderTel = context != null ? context.VirualGroupBuyOrderTel : "";

            preOrderInfo.Contact = new ContactInfo()
            {
                AddressAreaID = result.SelShippingAddress.ReceiveAreaSysNo,
                //AddressAreaID = result.SelShippingAddress.ReceiveAreaCitySysNo,
                AddressTitle  = result.SelShippingAddress.AddressTitle,
                AddressDetail = result.SelShippingAddress.ReceiveAddress,
                MobilePhone   = result.SelShippingAddress.ReceiveCellPhone,
                Phone         = result.SelShippingAddress.ReceivePhone,
                Name          = result.SelShippingAddress.ReceiveName,
                ZipCode       = result.SelShippingAddress.ReceiveZip,
                ID            = result.SelShippingAddress.SysNo,
            };
            //使用余额进行支付,给订单的余额支付金额赋值,在SOPipline中会对订单的余额支付金额重新进行计算
            if (newCheckoutContenxt.IsUsedPrePay > 0)
            {
                preOrderInfo.BalancePayAmount = customerInfo.AccountBalance;
            }
            //积分
            preOrderInfo.PointPay = newCheckoutContenxt.PointPay;
            //礼品卡
            if (newCheckoutContenxt.GiftCardList != null && newCheckoutContenxt.GiftCardList.Count > 0)
            {
                preOrderInfo.GiftCardList = new List <GiftCardInfo>();
                foreach (var giftCardContext in newCheckoutContenxt.GiftCardList)
                {
                    if (!string.IsNullOrWhiteSpace(giftCardContext.Crypto))
                    {
                        giftCardContext.Password = ExtractGiftCardPassword(giftCardContext.Password, customerSysNo);
                    }
                    GiftCardInfo giftCardInfo = new GiftCardInfo()
                    {
                        Code     = giftCardContext.Code,
                        Password = giftCardContext.Password
                    };
                    giftCardInfo["Crypto"] = giftCardContext.Crypto;
                    preOrderInfo.GiftCardList.Add(giftCardInfo);
                }
            }
            //购物发票,1表示要开发票
            if (newCheckoutContenxt.NeedInvoice == 1)
            {
                preOrderInfo.Receipt = new ReceiptInfo()
                {
                    PersonalInvoiceTitle = result.CustomerInvoiceInfo.InvoiceTitle
                };
            }
            //执行真正的action操作
            OrderPipelineProcessResult checkOutResult = action(preOrderInfo);

            SetCheckoutResult(checkOutResult, result, newCheckoutContenxt);

            return(result);
        }